Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add option to clone manually when adding a submodule
  • Loading branch information
JasonGoemaat committed Nov 21, 2015
commit 7b64825087274769ebb06a5a0cfc5e11a7e57981
62 changes: 62 additions & 0 deletions LibGit2Sharp.Tests/SubmoduleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,68 @@ public void CanAddSubmodule()
}
}


[Fact]
public void CanAddSubmoduleWithManualClone()
{
string configPath = CreateConfigurationWithDummyUser(Constants.Identity);
var options = new RepositoryOptions { GlobalConfigurationLocation = configPath };

//var path = SandboxSubmoduleTestRepo();
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, options))
{
// check on adding config entry
var configEntryBeforeAdd = repo.Config.Get<string>(string.Format("submodule.{0}.url", submoduleSubPath));
Assert.Null(configEntryBeforeAdd);

// add submodule
Submodule submodule = repo.Submodules.Add(pathSubRepoOrigin, submoduleSubPath, x => Repository.Clone(pathSubRepoOrigin, x, new CloneOptions() { } ));
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 UpdatingUninitializedSubmoduleThrows()
{
Expand Down
26 changes: 11 additions & 15 deletions LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public virtual Submodule this[string name]
/// </summary>
/// <param name="url">The url of the remote repository</param>
/// <param name="relativePath">The path of the submodule inside of the parent repository, which will also become the submodule name.</param>
/// <param name="cloneMethod">A method that takes the full path to where we expect the repository to be cloned to so the caller
/// can clone it themselves. If not specified or if null, Add() will perform the clone using Repository.Clone(url, subPath, new CloneOptions() { } ).</param>
/// <returns></returns>
public virtual Submodule Add(string url, string relativePath)
public virtual Submodule Add(string url, string relativePath, Action<string> cloneMethod = null)
{
Ensure.ArgumentNotNullOrEmptyString(relativePath, "relativePath");

Expand All @@ -70,20 +72,14 @@ public virtual Submodule Add(string url, string relativePath)
// branches and checkout whatever the remote HEAD is, which seems hard to find.
System.IO.Directory.Delete(subPath, true);

// now clone
//Proxy.git_submodule_init(handle, true);
//GitSubmoduleOptions options = new GitSubmoduleOptions();
//Proxy.git_submodule_update(handle, true, ref options);
string result = Repository.Clone(url, subPath, new CloneOptions() { } );

//using (Repository subRep = new Repository(subPath))
//{
// subRep.Fetch("origin");
// var refs = subRep.Network.ListReferences(subRep.Network.Remotes["origin"]);
// //Branch b = subRep.Checkout(dr.CanonicalName);
// var fhs = subRep.Network.FetchHeads.Select(_ => new { CN = _.CanonicalName, RCN = _.RemoteCanonicalName }).ToArray();
// //string defbranch = subRep.Network.Remotes["origin"].DefaultBranch;
//}
// now clone the repository, or let the caller do it if an action was specified
if (cloneMethod != null)
{
cloneMethod(subPath);
} else
{
string result = Repository.Clone(url, subPath, new CloneOptions() { });
}

Proxy.git_submodule_add_finalize(handle);
}
Expand Down