diff options
author | Edward Thomson <ethomson@microsoft.com> | 2013-01-09 19:51:59 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-01-29 02:08:26 +0400 |
commit | 9cfc8f36d4ef3ee7a66d4d03fb1ac46cb16f4f3f (patch) | |
tree | 512f01063d47bb18b168eee2feafc1071bac7162 | |
parent | efcd62a32935bee486c876742829ce33d10e0b23 (diff) |
Introduce Repository.MergeHeads
-rw-r--r-- | LibGit2Sharp.Tests/MergeFixture.cs | 29 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 10 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Proxy.cs | 11 | ||||
-rw-r--r-- | LibGit2Sharp/IRepository.cs | 5 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/MergeHead.cs | 34 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 13 |
7 files changed, 102 insertions, 1 deletions
diff --git a/LibGit2Sharp.Tests/MergeFixture.cs b/LibGit2Sharp.Tests/MergeFixture.cs index f7ecd1cb..0c7285b7 100644 --- a/LibGit2Sharp.Tests/MergeFixture.cs +++ b/LibGit2Sharp.Tests/MergeFixture.cs @@ -1,4 +1,6 @@ -using System.Linq; +using System; +using System.IO; +using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; @@ -13,6 +15,7 @@ namespace LibGit2Sharp.Tests using (var repo = Repository.Init(scd.DirectoryPath)) { Assert.Equal(true, repo.Index.IsFullyMerged); + Assert.Empty(repo.MergeHeads); } } @@ -22,6 +25,7 @@ namespace LibGit2Sharp.Tests using (var repo = new Repository(StandardTestRepoWorkingDirPath)) { Assert.Equal(true, repo.Index.IsFullyMerged); + Assert.Empty(repo.MergeHeads); foreach (var entry in repo.Index) { @@ -56,5 +60,28 @@ namespace LibGit2Sharp.Tests () => repo.Commit("Try commit unmerged entries", author, author)); } } + + [Fact] + public void CanRetrieveTheBranchBeingMerged() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath); + using (var repo = new Repository(path.RepositoryPath)) + { + const string firstBranch = "9fd738e8f7967c078dceed8190330fc8648ee56a"; + const string secondBranch = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; + + string mergeHeadPath = Path.Combine(repo.Info.Path, "MERGE_HEAD"); + File.WriteAllText(mergeHeadPath, + string.Format("{0}{1}{2}{1}", firstBranch, "\n", secondBranch)); + + Assert.Equal(CurrentOperation.Merge, repo.Info.CurrentOperation); + + MergeHead[] mergedHeads = repo.MergeHeads.ToArray(); + Assert.Equal("MERGE_HEAD[0]", mergedHeads[0].Name); + Assert.Equal(firstBranch, mergedHeads[0].Tip.Id.Sha); + Assert.Equal("MERGE_HEAD[1]", mergedHeads[1].Name); + Assert.Null(mergedHeads[1].Tip); + } + } } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index be258557..c1aaa72b 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -742,6 +742,16 @@ namespace LibGit2Sharp.Core [DllImport(libgit2)] internal static extern int git_repository_is_empty(RepositorySafeHandle repo); + internal delegate int git_repository_mergehead_foreach_cb( + ref GitOid oid, + IntPtr payload); + + [DllImport(libgit2)] + internal static extern int git_repository_mergehead_foreach( + RepositorySafeHandle repo, + git_repository_mergehead_foreach_cb cb, + IntPtr payload); + [DllImport(libgit2)] internal static extern int git_repository_open( out RepositorySafeHandle repository, diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 8b7d1dbc..4e0ff57d 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -1384,6 +1384,17 @@ namespace LibGit2Sharp.Core return RepositoryStateChecker(repo, NativeMethods.git_repository_is_empty); } + public static ICollection<TResult> git_repository_mergehead_foreach<TResult>( + RepositorySafeHandle repo, + Func<GitOid, TResult> resultSelector) + { + return git_foreach( + resultSelector, + c => NativeMethods.git_repository_mergehead_foreach( + repo, (ref GitOid x, IntPtr p) => c(x, p), IntPtr.Zero), + GitErrorCode.NotFound); + } + public static ObjectDatabaseSafeHandle git_repository_odb(RepositorySafeHandle repo) { using (ThreadAffinity()) diff --git a/LibGit2Sharp/IRepository.cs b/LibGit2Sharp/IRepository.cs index 7c108e7a..b21820e9 100644 --- a/LibGit2Sharp/IRepository.cs +++ b/LibGit2Sharp/IRepository.cs @@ -136,5 +136,10 @@ namespace LibGit2Sharp /// Clean the working tree by removing files that are not under version control. /// </summary> void RemoveUntrackedFiles(); + + /// <summary> + /// Gets the references to the tips that are currently being merged. + /// </summary> + IEnumerable<MergeHead> MergeHeads { get; } } } diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index d9723b04..38be381e 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -92,6 +92,7 @@ <Compile Include="Handlers.cs" /> <Compile Include="Ignore.cs" /> <Compile Include="MergeConflictException.cs" /> + <Compile Include="MergeHead.cs" /> <Compile Include="NameConflictException.cs" /> <Compile Include="ReferenceCollectionExtensions.cs" /> <Compile Include="Core\GitRemoteCallbacks.cs" /> diff --git a/LibGit2Sharp/MergeHead.cs b/LibGit2Sharp/MergeHead.cs new file mode 100644 index 00000000..38b19622 --- /dev/null +++ b/LibGit2Sharp/MergeHead.cs @@ -0,0 +1,34 @@ +using System.Globalization; + +namespace LibGit2Sharp +{ + /// <summary> + /// A merge head is a parent for the next commit. + /// </summary> + public class MergeHead : ReferenceWrapper<Commit> + { + /// <summary> + /// Needed for mocking purposes. + /// </summary> + protected MergeHead() + { } + + internal MergeHead(Repository repo, ObjectId targetId, int index) + : base(repo, new DirectReference(string.Format(CultureInfo.InvariantCulture, "MERGE_HEAD[{0}]", index), repo, targetId), r => r.CanonicalName) + { + } + + /// <summary> + /// Gets the <see cref="Commit"/> that this merge head points to. + /// </summary> + public virtual Commit Tip + { + get { return TargetObject; } + } + + protected override string Shorten() + { + return CanonicalName; + } + } +} diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 323e2a63..1faaa9a3 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -709,6 +709,19 @@ namespace LibGit2Sharp } } + /// <summary> + /// Gets the references to the tips that are currently being merged. + /// </summary> + public virtual IEnumerable<MergeHead> MergeHeads + { + get + { + int i = 0; + return Proxy.git_repository_mergehead_foreach(Handle, + commitId => new MergeHead(this, new ObjectId(commitId), i++)); + } + } + private string DebuggerDisplay { get |