Skip to content

Commit bd4e4c9

Browse files
committed
Merge pull request #1190 from ninjeff/ninjeff/describe-offp
Support only_follow_first_parent
2 parents 8688f10 + 758f428 commit bd4e4c9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

LibGit2Sharp.Tests/DescribeFixture.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using LibGit2Sharp.Tests.TestHelpers;
33
using Xunit;
4+
using System;
45

56
namespace LibGit2Sharp.Tests
67
{
@@ -49,5 +50,35 @@ public void CanDescribeACommit()
4950
new DescribeOptions{ AlwaysRenderLongFormat = true }));
5051
}
5152
}
53+
54+
[Fact]
55+
public void CanFollowFirstParent()
56+
{
57+
string path = SandboxStandardTestRepo();
58+
using (var repo = new Repository(path))
59+
{
60+
var branch = repo.CreateBranch("branch");
61+
62+
// Make an earlier tag on master
63+
repo.Commit("A", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true });
64+
repo.ApplyTag("firstParentTag");
65+
66+
// Make a later tag on branch
67+
repo.Checkout(branch);
68+
repo.Commit("B", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true });
69+
repo.ApplyTag("mostRecentTag");
70+
71+
repo.Checkout("master");
72+
repo.Commit("C", Constants.Signature, Constants.Signature, new CommitOptions { AllowEmptyCommit = true });
73+
repo.Merge(branch, Constants.Signature, new MergeOptions() { FastForwardStrategy = FastForwardStrategy.NoFastForward });
74+
75+
// With OnlyFollowFirstParent = false, the most recent tag reachable should be returned
76+
Assert.Equal("mostRecentTag-3-gf17be71", repo.Describe(repo.Head.Tip, new DescribeOptions { OnlyFollowFirstParent = false, Strategy = DescribeStrategy.Tags }));
77+
78+
// With OnlyFollowFirstParent = true, the most recent tag on the current branch should be returned
79+
Assert.Equal("firstParentTag-2-gf17be71", repo.Describe(repo.Head.Tip, new DescribeOptions { OnlyFollowFirstParent = true, Strategy = DescribeStrategy.Tags }));
80+
81+
}
82+
}
5283
}
5384
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ public static string git_describe_commit(
644644
Version = 1,
645645
DescribeStrategy = options.Strategy,
646646
MaxCandidatesTags = 10,
647-
OnlyFollowFirstParent = false,
647+
OnlyFollowFirstParent = options.OnlyFollowFirstParent,
648648
ShowCommitOidAsFallback = options.UseCommitIdAsFallback,
649649
};
650650

LibGit2Sharp/DescribeOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public DescribeOptions()
2121
{
2222
Strategy = DescribeStrategy.Default;
2323
MinimumCommitIdAbbreviatedSize = 7;
24+
OnlyFollowFirstParent = false;
2425
}
2526

2627
/// <summary>
@@ -54,5 +55,14 @@ public DescribeOptions()
5455
/// </para>
5556
/// </summary>
5657
public bool AlwaysRenderLongFormat { get; set; }
58+
59+
/// <summary>
60+
/// Follow only the first parent commit upon seeing a merge commit.
61+
/// <para>
62+
/// This is useful when you wish to not match tags on branches merged in
63+
/// the history of the target commit.
64+
/// </para>
65+
/// </summary>
66+
public bool OnlyFollowFirstParent { get; set; }
5767
}
5868
}

0 commit comments

Comments
 (0)