Skip to content

Commit 5869fb0

Browse files
committed
Add CompareOptions
* ContextLines * InterhunkLines Closes #423
1 parent dc208c9 commit 5869fb0

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7+
using Xunit.Extensions;
78

89
namespace LibGit2Sharp.Tests
910
{
@@ -222,23 +223,31 @@ public void CanDetectTheRenamingOfAModifiedFile()
222223
* $ git diff --shortstat f8d44d7..ec9e401
223224
* 1 file changed, 2 insertions(+), 1 deletion(-)
224225
*/
225-
[Fact]
226-
public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion()
226+
[Theory]
227+
[InlineData(0, 175)]
228+
[InlineData(1, 191)]
229+
[InlineData(2, 184)]
230+
[InlineData(3, 187)]
231+
[InlineData(4, 193)]
232+
public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion(int contextLines, int expectedPatchLength)
227233
{
228234
using (var repo = new Repository(StandardTestRepoPath))
229235
{
230236
Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree;
231237
Tree commitTreeWithUpdatedFile = repo.Lookup<Commit>("ec9e401").Tree;
232238

233-
TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithUpdatedFile);
239+
TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithUpdatedFile,
240+
compareOptions: new CompareOptions { ContextLines = (short)contextLines });
234241

235242
Assert.Equal(1, changes.Count());
236243
Assert.Equal(1, changes.Modified.Count());
244+
Assert.Equal(expectedPatchLength, changes.Patch.Length);
237245

238246
TreeEntryChanges treeEntryChanges = changes.Modified.Single();
239247

240248
Assert.Equal(2, treeEntryChanges.LinesAdded);
241249
Assert.Equal(1, treeEntryChanges.LinesDeleted);
250+
Assert.Equal(expectedPatchLength, treeEntryChanges.Patch.Length);
242251
}
243252
}
244253

@@ -479,7 +488,7 @@ public void ComparingTwoNullTreesReturnsAnEmptyTreeChanges()
479488
{
480489
using (var repo = new Repository(StandardTestRepoPath))
481490
{
482-
TreeChanges changes = repo.Diff.Compare(null, null, null);
491+
TreeChanges changes = repo.Diff.Compare(default(Tree), default(Tree));
483492

484493
Assert.Equal(0, changes.Count());
485494
}

LibGit2Sharp/CompareOptions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// Options to define file comparison behavior.
5+
/// </summary>
6+
public class CompareOptions
7+
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref = "CompareOptions" /> class.
10+
/// </summary>
11+
public CompareOptions()
12+
{
13+
ContextLines = 3;
14+
InterhunkLines = 0;
15+
}
16+
17+
/// <summary>
18+
/// The number of unchanged lines that define the boundary of a hunk (and to display before and after)
19+
/// </summary>
20+
public short ContextLines { get; set; }
21+
22+
/// <summary>
23+
/// The maximum number of unchanged lines between hunk boundaries before the hunks will be merged into a one. (Default = 0)
24+
/// </summary>
25+
public short InterhunkLines { get; set; }
26+
}
27+
}

LibGit2Sharp/Diff.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ public class Diff
1919
{
2020
private readonly Repository repo;
2121

22-
private static GitDiffOptions BuildOptions(DiffOptions diffOptions, FilePath[] filePaths = null, MatchedPathsAggregator matchedPathsAggregator = null)
22+
private static GitDiffOptions BuildOptions(DiffOptions diffOptions, FilePath[] filePaths = null, MatchedPathsAggregator matchedPathsAggregator = null, CompareOptions compareOptions = null)
2323
{
2424
var options = new GitDiffOptions();
2525

2626
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_TYPECHANGE;
27-
options.ContextLines = 3;
27+
28+
compareOptions = compareOptions ?? new CompareOptions();
29+
options.ContextLines = (ushort)compareOptions.ContextLines;
30+
options.InterhunkLines = (ushort)compareOptions.InterhunkLines;
2831

2932
if (diffOptions.HasFlag(DiffOptions.IncludeUntracked))
3033
{
@@ -110,8 +113,9 @@ internal Diff(Repository repo)
110113
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
111114
/// Use these options to determine how unmatched explicit paths should be handled.
112115
/// </param>
116+
/// <param name = "compareOptions">Additional options to define comparison behavior.</param>
113117
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <paramref name = "oldTree"/> and the <paramref name = "newTree"/>.</returns>
114-
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
118+
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null)
115119
{
116120
var comparer = TreeToTree(repo);
117121
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
@@ -129,18 +133,19 @@ public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<strin
129133
}
130134
}
131135

132-
return BuildTreeChangesFromComparer(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions);
136+
return BuildTreeChangesFromComparer(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
133137
}
134138

135139
/// <summary>
136140
/// Show changes between two <see cref = "Blob"/>s.
137141
/// </summary>
138142
/// <param name = "oldBlob">The <see cref = "Blob"/> you want to compare from.</param>
139143
/// <param name = "newBlob">The <see cref = "Blob"/> you want to compare to.</param>
144+
/// <param name = "compareOptions">Additional options to define comparison behavior.</param>
140145
/// <returns>A <see cref = "ContentChanges"/> containing the changes between the <paramref name = "oldBlob"/> and the <paramref name = "newBlob"/>.</returns>
141-
public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob)
146+
public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions compareOptions = null)
142147
{
143-
using (GitDiffOptions options = BuildOptions(DiffOptions.None))
148+
using (GitDiffOptions options = BuildOptions(DiffOptions.None, compareOptions: compareOptions))
144149
{
145150
return new ContentChanges(repo, oldBlob, newBlob, options);
146151
}
@@ -168,8 +173,9 @@ private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRet
168173
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
169174
/// Use these options to determine how unmatched explicit paths should be handled.
170175
/// </param>
176+
/// <param name = "compareOptions">Additional options to define comparison behavior.</param>
171177
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
172-
public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
178+
public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null)
173179
{
174180
var comparer = handleRetrieverDispatcher[diffTargets](repo);
175181
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
@@ -188,7 +194,7 @@ public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnume
188194
}
189195
}
190196

191-
return BuildTreeChangesFromComparer(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions);
197+
return BuildTreeChangesFromComparer(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
192198
}
193199

194200
/// <summary>
@@ -200,14 +206,15 @@ public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnume
200206
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
201207
/// Use these options to determine how unmatched explicit paths should be handled.
202208
/// </param>
209+
/// <param name = "compareOptions">Additional options to define comparison behavior.</param>
203210
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the working directory and the index.</returns>
204-
public virtual TreeChanges Compare(IEnumerable<string> paths = null, bool includeUntracked = false, ExplicitPathsOptions explicitPathsOptions = null)
211+
public virtual TreeChanges Compare(IEnumerable<string> paths = null, bool includeUntracked = false, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null)
205212
{
206-
return Compare(includeUntracked ? DiffOptions.IncludeUntracked : DiffOptions.None, paths, explicitPathsOptions);
213+
return Compare(includeUntracked ? DiffOptions.IncludeUntracked : DiffOptions.None, paths, explicitPathsOptions, compareOptions);
207214
}
208215

209216
internal virtual TreeChanges Compare(DiffOptions diffOptions, IEnumerable<string> paths = null,
210-
ExplicitPathsOptions explicitPathsOptions = null)
217+
ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null)
211218
{
212219
var comparer = WorkdirToIndex(repo);
213220

@@ -222,7 +229,7 @@ internal virtual TreeChanges Compare(DiffOptions diffOptions, IEnumerable<string
222229
}
223230
}
224231

225-
return BuildTreeChangesFromComparer(null, null, comparer, diffOptions, paths, explicitPathsOptions);
232+
return BuildTreeChangesFromComparer(null, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions);
226233
}
227234

228235
private delegate DiffListSafeHandle TreeComparisonHandleRetriever(ObjectId oldTreeId, ObjectId newTreeId, GitDiffOptions options);
@@ -275,14 +282,12 @@ private static TreeComparisonHandleRetriever IndexToTree(Repository repo)
275282
return (oh, nh, o) => Proxy.git_diff_tree_to_index(repo.Handle, repo.Index.Handle, oh, o);
276283
}
277284

278-
private TreeChanges BuildTreeChangesFromComparer(
279-
ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever,
280-
DiffOptions diffOptions, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
285+
private TreeChanges BuildTreeChangesFromComparer(ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever, DiffOptions diffOptions, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null)
281286
{
282287
var matchedPaths = new MatchedPathsAggregator();
283288
var filePaths = ToFilePaths(repo, paths);
284289

285-
using (GitDiffOptions options = BuildOptions(diffOptions, filePaths, matchedPaths))
290+
using (GitDiffOptions options = BuildOptions(diffOptions, filePaths, matchedPaths, compareOptions))
286291
using (DiffListSafeHandle diffList = comparisonHandleRetriever(oldTreeId, newTreeId, options))
287292
{
288293
if (explicitPathsOptions != null)

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="Changes.cs" />
6868
<Compile Include="CheckoutCallbacks.cs" />
6969
<Compile Include="CheckoutOptions.cs" />
70+
<Compile Include="CompareOptions.cs" />
7071
<Compile Include="ObjectType.cs" />
7172
<Compile Include="ReferenceExtensions.cs" />
7273
<Compile Include="Conflict.cs" />

0 commit comments

Comments
 (0)