Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add SubmoduleCollection.Add #1501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions LibGit2Sharp.Tests/SubmoduleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,70 @@ public void CanUpdateSubmoduleAfterCheckout()
}
}

[Fact]
public void CanAddSubmodule()
{
var path = SandboxStandardTestRepo();
var pathSubRepoOrigin = SandboxStandardTestRepo();

string submoduleSubPath = "submodule_target_wd";
string expectedSubmodulePath = Path.GetFullPath(Path.Combine(path, submoduleSubPath));
string expectedSubmoduleUrl = pathSubRepoOrigin.Replace('\\', '/');
ObjectId expectedCommitId = (ObjectId)"32eab9cb1f450b5fe7ab663462b77d7f4b703344";

using (var repo = new Repository(path))
{
// setup config with dummy user so we can commit
CreateConfigurationWithDummyUser(repo, Constants.Identity);

// check on adding config entry
var configEntryBeforeAdd = repo.Config.Get<string>(string.Format("submodule.{0}.url", submoduleSubPath));
Assert.Null(configEntryBeforeAdd);

// first step is cloning the repository to where it goes
Repository.Clone(pathSubRepoOrigin, expectedSubmodulePath);

// add submodule
repo.Submodules.Add(pathSubRepoOrigin, submoduleSubPath, 1);
Submodule submodule = repo.Submodules[submoduleSubPath];
Assert.NotNull(submodule);

// check that the expected commit is checked out, but not set in parent repo until committed
Assert.Equal(expectedCommitId, repo.Submodules[submoduleSubPath].WorkDirCommitId);
Assert.Null(repo.Submodules[submoduleSubPath].HeadCommitId);

// check status
var submoduleStatus = submodule.RetrieveStatus();
Assert.True((submoduleStatus & SubmoduleStatus.InIndex) == SubmoduleStatus.InIndex);
Assert.True((submoduleStatus & SubmoduleStatus.InConfig) == SubmoduleStatus.InConfig);
Assert.True((submoduleStatus & SubmoduleStatus.InWorkDir) == SubmoduleStatus.InWorkDir);
Assert.True((submoduleStatus & SubmoduleStatus.IndexAdded) == SubmoduleStatus.IndexAdded);

// check that config entry was added with the correct url
var configEntryAfterAdd = repo.Config.Get<string>(string.Format("submodule.{0}.url", submoduleSubPath));
Assert.NotNull(configEntryAfterAdd);
Assert.Equal(expectedSubmoduleUrl, configEntryAfterAdd.Value);

// check on directory being added and repository directory
Assert.True(Directory.Exists(expectedSubmodulePath));
Assert.True(Directory.Exists(Path.Combine(expectedSubmodulePath, ".git")));

// manually check commit by opening submodule as a repository
using (var repo2 = new Repository(expectedSubmodulePath))
{
Assert.False(repo2.Info.IsHeadDetached);
Assert.False(repo2.Info.IsHeadUnborn);
Commit headCommit = repo2.Head.Tip;
Assert.Equal(headCommit.Id, expectedCommitId);
}

// commit parent repository, then verify it reports the correct CommitId for the submodule
Signature signature = repo.Config.BuildSignature(DateTimeOffset.Now);
repo.Commit("Added submodule " + submoduleSubPath, signature, signature);
Assert.Equal(expectedCommitId, repo.Submodules[submoduleSubPath].HeadCommitId);
}
}

[Fact]
public void CanReadSubmoduleProperties()
{
Expand All @@ -322,6 +386,9 @@ public void CanReadSubmoduleProperties()

using (var repo = new Repository(path))
{
// setup identity for commit we will do
CreateConfigurationWithDummyUser(repo, Constants.Identity);

var submodule = repo.Submodules[submoduleName];

Assert.Equal(SubmoduleUpdate.Checkout, submodule.UpdateRule);
Expand Down
25 changes: 25 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,31 @@ internal static extern unsafe void git_status_list_free(
internal static extern void git_strarray_free(
ref GitStrArray array);

/// <summary>
/// To fully emulate "git submodule add", call this function, then open
/// the submodule repo and perform the clone step as needed. Lastly, call
/// 'git_submodule_add_finalize()' to wrap up adding the new submodule and
/// .gitmodules to the index to be ready to commit.
///
/// In our case, go ahead and do the clone first. The setup can be done
/// with the directory contents already in place.
/// </summary>
[DllImport(libgit2)]
private static extern unsafe int git_submodule_add_setup(
out git_submodule* reference,
git_repository* repo,
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* url,
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path,
int use_gitlink);

[DllImport(libgit2)]
internal static extern unsafe int git_submodule_add_finalize(
git_submodule* submodule);

[DllImport(libgit2)]
internal static extern unsafe string git_submodule_name(
git_submodule* submodule);

[DllImport(libgit2)]
private static extern unsafe int git_submodule_lookup(
out git_submodule* reference,
Expand Down
26 changes: 26 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,32 @@ public static unsafe int git_status_list_entrycount(StatusListHandle list)

#region git_submodule_

/// <summary>
/// This does "git submodule add" up to the fetch and checkout of the
/// submodule contents. It preps a new submodule, creates an entry in
/// .gitmodules and creates an empty initialized repository either at the
/// given path in the working directory or in .git/modules with a gitlink
/// from the working directory to the new repo.
/// </summary>s
public static unsafe SubmoduleHandle git_submodule_add_setup(RepositoryHandle repo, string url, string path, int useGitLink)
{
git_submodule* submodule;
var res = NativeMethods.git_submodule_add_setup(out submodule, repo, url, path, useGitLink);
Ensure.ZeroResult(res);
return new SubmoduleHandle(submodule, true);
}

public static unsafe void git_submodule_add_finalize(SubmoduleHandle submodule)
{
var res = NativeMethods.git_submodule_add_finalize(submodule);
Ensure.ZeroResult(res);
}

public static unsafe string git_submodule_name(SubmoduleHandle submodule)
{
return NativeMethods.git_submodule_name(submodule);
}

/// <summary>
/// Returns a handle to the corresponding submodule,
/// or an invalid handle if a submodule is not found.
Expand Down
16 changes: 16 additions & 0 deletions LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public virtual Submodule this[string name]
}
}

/// <summary>
/// Add a new submodule from the given url to the given path. The
/// repository should have already been cloned into the destination
/// path. After this call the submodule and updated .gitmodules
/// file will be added to the index.
/// </summary>
/// <param name="url">Url to use for the submodule</param>
/// <param name="path">Path of the submodule</param>
/// <param name="useGitLink">Non-zero to use git link</param>
public virtual void Add(string url, string path, int useGitLink)
{
SubmoduleHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, path, useGitLink);
Proxy.git_submodule_add_finalize(handle);
handle.Free();
}

/// <summary>
/// Initialize specified submodule.
/// <para>
Expand Down