Skip to content

Commit 01d105c

Browse files
carlosmnnulltoken
authored andcommitted
CommitFilter: add simplify-by-first-parent option
Now that this is doable directly inside the revision walker, let's expose it as a possible filter option. This fixes #258.
1 parent 1374884 commit 01d105c

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ public void CanEnumerateCommitsWithReverseTopoSorting()
176176
}
177177
}
178178

179+
[Fact]
180+
public void CanSimplifyByFirstParent()
181+
{
182+
AssertEnumerationOfCommits(
183+
repo => new CommitFilter { Since = repo.Head, FirstParent = true },
184+
new[]
185+
{
186+
"4c062a6", "be3563a", "9fd738e",
187+
"4a202b3", "5b5b025", "8496071",
188+
});
189+
}
190+
179191
[Fact]
180192
public void CanGetParentsCount()
181193
{

LibGit2Sharp/CommitFilter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public CommitFilter()
1616
{
1717
SortBy = CommitSortStrategies.Time;
1818
Since = "HEAD";
19+
FirstParent = false;
1920
}
2021

2122
/// <summary>
@@ -57,6 +58,11 @@ internal IList<object> UntilList
5758
get { return ToList(Until); }
5859
}
5960

61+
/// <summary>
62+
/// Whether to limit the walk to each commit's first parent, instead of all of them
63+
/// </summary>
64+
public bool FirstParent { get; set; }
65+
6066
private static IList<object> ToList(object obj)
6167
{
6268
var list = new List<object>();

LibGit2Sharp/CommitLog.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public CommitEnumerator(Repository repo, CommitFilter filter)
154154
Sort(filter.SortBy);
155155
Push(filter.SinceList);
156156
Hide(filter.UntilList);
157+
FirstParent(filter.FirstParent);
157158
}
158159

159160
#region IEnumerator<Commit> Members
@@ -232,6 +233,13 @@ private void Sort(CommitSortStrategies options)
232233
Proxy.git_revwalk_sorting(handle, options);
233234
}
234235

236+
private void FirstParent(bool firstParent)
237+
{
238+
if (firstParent)
239+
{
240+
Proxy.git_revwalk_simplify_first_parent(handle);
241+
}
242+
}
235243
}
236244
}
237245
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,9 @@ internal static extern int git_revparse_ext(
10961096
[DllImport(libgit2)]
10971097
internal static extern void git_revwalk_sorting(RevWalkerSafeHandle walk, CommitSortStrategies sort);
10981098

1099+
[DllImport(libgit2)]
1100+
internal static extern void git_revwalk_simplify_first_parent(RevWalkerSafeHandle walk);
1101+
10991102
[DllImport(libgit2)]
11001103
internal static extern void git_signature_free(IntPtr signature);
11011104

LibGit2Sharp/Core/Proxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,11 @@ public static void git_revwalk_sorting(RevWalkerSafeHandle walker, CommitSortStr
21012101
NativeMethods.git_revwalk_sorting(walker, options);
21022102
}
21032103

2104+
public static void git_revwalk_simplify_first_parent(RevWalkerSafeHandle walker)
2105+
{
2106+
NativeMethods.git_revwalk_simplify_first_parent(walker);
2107+
}
2108+
21042109
#endregion
21052110

21062111
#region git_signature_

0 commit comments

Comments
 (0)