From 758f42815886b2361a5f2636b35f57262433e14a Mon Sep 17 00:00:00 2001 From: Jeff Saunders Date: Thu, 17 Sep 2015 15:31:07 +0800 Subject: [PATCH] Add DescribeOptions.OnlyFollowFirstParent property The GitDescribeOptions struct used by git_describe_commit has an OnlyFollowFirstParent field, which corresponds to the --first-parent option in command-line Git. This commit adds the same option as a new property on the DescribeOptions class, with XML documentation from Git docs. --- LibGit2Sharp.Tests/DescribeFixture.cs | 31 +++++++++++++++++++++++++++ LibGit2Sharp/Core/Proxy.cs | 2 +- LibGit2Sharp/DescribeOptions.cs | 10 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/DescribeFixture.cs b/LibGit2Sharp.Tests/DescribeFixture.cs index 52e0ac8fc..cf4f033da 100644 --- a/LibGit2Sharp.Tests/DescribeFixture.cs +++ b/LibGit2Sharp.Tests/DescribeFixture.cs @@ -1,6 +1,7 @@ using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; +using System; namespace LibGit2Sharp.Tests { @@ -49,5 +50,35 @@ public void CanDescribeACommit() new DescribeOptions{ AlwaysRenderLongFormat = true })); } } + + [Fact] + public void CanFollowFirstParent() + { + string path = SandboxStandardTestRepo(); + using (var repo = new Repository(path)) + { + var branch = repo.CreateBranch("branch"); + + // Make an earlier tag on master + repo.Commit("A", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true }); + repo.ApplyTag("firstParentTag"); + + // Make a later tag on branch + repo.Checkout(branch); + repo.Commit("B", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true }); + repo.ApplyTag("mostRecentTag"); + + repo.Checkout("master"); + repo.Commit("C", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true }); + repo.Merge(branch, Constants.Signature, new MergeOptions() { FastForwardStrategy = FastForwardStrategy.NoFastForward }); + + // With OnlyFollowFirstParent = false, the most recent tag reachable should be returned + Assert.Equal("mostRecentTag-3-gf17be71", repo.Describe(repo.Head.Tip, new DescribeOptions { OnlyFollowFirstParent = false, Strategy = DescribeStrategy.Tags })); + + // With OnlyFollowFirstParent = true, the most recent tag on the current branch should be returned + Assert.Equal("firstParentTag-2-gf17be71", repo.Describe(repo.Head.Tip, new DescribeOptions { OnlyFollowFirstParent = true, Strategy = DescribeStrategy.Tags })); + + } + } } } diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index b31e34bdb..2ae5d0800 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -644,7 +644,7 @@ public static string git_describe_commit( Version = 1, DescribeStrategy = options.Strategy, MaxCandidatesTags = 10, - OnlyFollowFirstParent = false, + OnlyFollowFirstParent = options.OnlyFollowFirstParent, ShowCommitOidAsFallback = options.UseCommitIdAsFallback, }; diff --git a/LibGit2Sharp/DescribeOptions.cs b/LibGit2Sharp/DescribeOptions.cs index db8f04655..3ca6f31eb 100644 --- a/LibGit2Sharp/DescribeOptions.cs +++ b/LibGit2Sharp/DescribeOptions.cs @@ -21,6 +21,7 @@ public DescribeOptions() { Strategy = DescribeStrategy.Default; MinimumCommitIdAbbreviatedSize = 7; + OnlyFollowFirstParent = false; } /// @@ -54,5 +55,14 @@ public DescribeOptions() /// /// public bool AlwaysRenderLongFormat { get; set; } + + /// + /// Follow only the first parent commit upon seeing a merge commit. + /// + /// This is useful when you wish to not match tags on branches merged in + /// the history of the target commit. + /// + /// + public bool OnlyFollowFirstParent { get; set; } } }