Skip to content

Commit 96553df

Browse files
dahlbyknulltoken
authored andcommitted
Add CompareOptions
* ContextLines * InterhunkLines Closes #423
1 parent e210b9b commit 96553df

26 files changed

+690
-93
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 36 additions & 78 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 = 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

@@ -293,15 +302,31 @@ public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion()
293302
* super-file.txt | 5 +++++
294303
* 3 files changed, 8 insertions(+), 5 deletions(-)
295304
*/
296-
[Fact]
297-
public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()
305+
[Theory]
306+
[InlineData(0, 3)]
307+
[InlineData(0, 4)]
308+
[InlineData(1, 1)]
309+
[InlineData(1, 2)]
310+
[InlineData(2, 4)]
311+
[InlineData(2, 5)]
312+
[InlineData(3, 2)]
313+
[InlineData(3, 3)]
314+
[InlineData(4, 0)]
315+
[InlineData(4, 1)]
316+
public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, int interhunkLines)
298317
{
318+
var compareOptions = new CompareOptions
319+
{
320+
ContextLines = contextLines,
321+
InterhunkLines = interhunkLines,
322+
};
323+
299324
using (var repo = new Repository(StandardTestRepoPath))
300325
{
301326
Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree;
302327
Tree mergedCommitTree = repo.Lookup<Commit>("7252fe2").Tree;
303328

304-
TreeChanges changes = repo.Diff.Compare(rootCommitTree, mergedCommitTree);
329+
TreeChanges changes = repo.Diff.Compare(rootCommitTree, mergedCommitTree, compareOptions: compareOptions);
305330

306331
Assert.Equal(3, changes.Count());
307332
Assert.Equal(1, changes.Modified.Count());
@@ -314,77 +339,10 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()
314339
Assert.Equal(1, treeEntryChanges.LinesDeleted);
315340

316341
Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Mode);
317-
318-
var expected = new StringBuilder()
319-
.Append("diff --git a/numbers.txt b/numbers.txt\n")
320-
.Append("index 7909961..4e935b7 100644\n")
321-
.Append("--- a/numbers.txt\n")
322-
.Append("+++ b/numbers.txt\n")
323-
.Append("@@ -1,4 +1,5 @@\n")
324-
.Append(" 1\n")
325-
.Append("+2\n")
326-
.Append(" 3\n")
327-
.Append(" 4\n")
328-
.Append(" 5\n")
329-
.Append("@@ -8,8 +9,9 @@\n")
330-
.Append(" 8\n")
331-
.Append(" 9\n")
332-
.Append(" 10\n")
333-
.Append("-12\n")
334-
.Append("+11\n")
335-
.Append(" 12\n")
336-
.Append(" 13\n")
337-
.Append(" 14\n")
338-
.Append(" 15\n")
339-
.Append("+16\n");
340-
341-
Assert.Equal(expected.ToString(), treeEntryChanges.Patch);
342-
343-
expected = new StringBuilder()
344-
.Append("diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt\n")
345-
.Append("deleted file mode 100644\n")
346-
.Append("index e8953ab..0000000\n")
347-
.Append("--- a/my-name-does-not-feel-right.txt\n")
348-
.Append("+++ /dev/null\n")
349-
.Append("@@ -1,4 +0,0 @@\n")
350-
.Append("-That's a terrible name!\n")
351-
.Append("-I don't like it.\n")
352-
.Append("-People look down at me and laugh. :-(\n")
353-
.Append("-Really!!!!\n")
354-
.Append("diff --git a/numbers.txt b/numbers.txt\n")
355-
.Append("index 7909961..4e935b7 100644\n")
356-
.Append("--- a/numbers.txt\n")
357-
.Append("+++ b/numbers.txt\n")
358-
.Append("@@ -1,4 +1,5 @@\n")
359-
.Append(" 1\n")
360-
.Append("+2\n")
361-
.Append(" 3\n")
362-
.Append(" 4\n")
363-
.Append(" 5\n")
364-
.Append("@@ -8,8 +9,9 @@\n")
365-
.Append(" 8\n")
366-
.Append(" 9\n")
367-
.Append(" 10\n")
368-
.Append("-12\n")
369-
.Append("+11\n")
370-
.Append(" 12\n")
371-
.Append(" 13\n")
372-
.Append(" 14\n")
373-
.Append(" 15\n")
374-
.Append("+16\n")
375-
.Append("diff --git a/super-file.txt b/super-file.txt\n")
376-
.Append("new file mode 100644\n")
377-
.Append("index 0000000..16bdf1d\n")
378-
.Append("--- /dev/null\n")
379-
.Append("+++ b/super-file.txt\n")
380-
.Append("@@ -0,0 +1,5 @@\n")
381-
.Append("+That's a terrible name!\n")
382-
.Append("+I don't like it.\n")
383-
.Append("+People look down at me and laugh. :-(\n")
384-
.Append("+Really!!!!\n")
385-
.Append("+Yeah! Better!\n");
386-
387-
Assert.Equal(expected.ToString(), changes.Patch);
342+
Assert.Equal(Expected("f8d44d7...7252fe2/numbers.txt-{0}-{1}.diff", contextLines, interhunkLines),
343+
treeEntryChanges.Patch);
344+
Assert.Equal(Expected("f8d44d7...7252fe2/full-{0}-{1}.diff", contextLines, interhunkLines),
345+
changes.Patch);
388346
}
389347
}
390348

@@ -479,7 +437,7 @@ public void ComparingTwoNullTreesReturnsAnEmptyTreeChanges()
479437
{
480438
using (var repo = new Repository(StandardTestRepoPath))
481439
{
482-
TreeChanges changes = repo.Diff.Compare(null, null, null);
440+
TreeChanges changes = repo.Diff.Compare(default(Tree), default(Tree));
483441

484442
Assert.Equal(0, changes.Count());
485443
}

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ public class MetaFixture
1212
{
1313
private static readonly Type[] excludedTypes = new[]
1414
{
15+
typeof(CompareOptions),
1516
typeof(Credentials),
17+
typeof(ExplicitPathsOptions),
1618
typeof(Filter),
1719
typeof(ObjectId),
1820
typeof(Repository),
1921
typeof(RepositoryOptions),
2022
typeof(Signature),
21-
typeof(ExplicitPathsOptions),
2223
};
2324

2425
// Related to https://github.com/libgit2/libgit2sharp/pull/251
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt
2+
deleted file mode 100644
3+
index e8953ab..0000000
4+
--- a/my-name-does-not-feel-right.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-That's a terrible name!
8+
-I don't like it.
9+
-People look down at me and laugh. :-(
10+
-Really!!!!
11+
diff --git a/numbers.txt b/numbers.txt
12+
index 7909961..4e935b7 100644
13+
--- a/numbers.txt
14+
+++ b/numbers.txt
15+
@@ -1,0 +2 @@
16+
+2
17+
@@ -11 +12 @@
18+
-12
19+
+11
20+
@@ -15,0 +17 @@
21+
+16
22+
diff --git a/super-file.txt b/super-file.txt
23+
new file mode 100644
24+
index 0000000..16bdf1d
25+
--- /dev/null
26+
+++ b/super-file.txt
27+
@@ -0,0 +1,5 @@
28+
+That's a terrible name!
29+
+I don't like it.
30+
+People look down at me and laugh. :-(
31+
+Really!!!!
32+
+Yeah! Better!
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt
2+
deleted file mode 100644
3+
index e8953ab..0000000
4+
--- a/my-name-does-not-feel-right.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-That's a terrible name!
8+
-I don't like it.
9+
-People look down at me and laugh. :-(
10+
-Really!!!!
11+
diff --git a/numbers.txt b/numbers.txt
12+
index 7909961..4e935b7 100644
13+
--- a/numbers.txt
14+
+++ b/numbers.txt
15+
@@ -1,0 +2 @@
16+
+2
17+
@@ -11,5 +12,6 @@
18+
-12
19+
+11
20+
12
21+
13
22+
14
23+
15
24+
+16
25+
diff --git a/super-file.txt b/super-file.txt
26+
new file mode 100644
27+
index 0000000..16bdf1d
28+
--- /dev/null
29+
+++ b/super-file.txt
30+
@@ -0,0 +1,5 @@
31+
+That's a terrible name!
32+
+I don't like it.
33+
+People look down at me and laugh. :-(
34+
+Really!!!!
35+
+Yeah! Better!
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt
2+
deleted file mode 100644
3+
index e8953ab..0000000
4+
--- a/my-name-does-not-feel-right.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-That's a terrible name!
8+
-I don't like it.
9+
-People look down at me and laugh. :-(
10+
-Really!!!!
11+
diff --git a/numbers.txt b/numbers.txt
12+
index 7909961..4e935b7 100644
13+
--- a/numbers.txt
14+
+++ b/numbers.txt
15+
@@ -1,2 +1,3 @@
16+
1
17+
+2
18+
3
19+
@@ -10,3 +11,3 @@
20+
10
21+
-12
22+
+11
23+
12
24+
@@ -15 +16,2 @@
25+
15
26+
+16
27+
diff --git a/super-file.txt b/super-file.txt
28+
new file mode 100644
29+
index 0000000..16bdf1d
30+
--- /dev/null
31+
+++ b/super-file.txt
32+
@@ -0,0 +1,5 @@
33+
+That's a terrible name!
34+
+I don't like it.
35+
+People look down at me and laugh. :-(
36+
+Really!!!!
37+
+Yeah! Better!
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt
2+
deleted file mode 100644
3+
index e8953ab..0000000
4+
--- a/my-name-does-not-feel-right.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-That's a terrible name!
8+
-I don't like it.
9+
-People look down at me and laugh. :-(
10+
-Really!!!!
11+
diff --git a/numbers.txt b/numbers.txt
12+
index 7909961..4e935b7 100644
13+
--- a/numbers.txt
14+
+++ b/numbers.txt
15+
@@ -1,2 +1,3 @@
16+
1
17+
+2
18+
3
19+
@@ -10,6 +11,7 @@
20+
10
21+
-12
22+
+11
23+
12
24+
13
25+
14
26+
15
27+
+16
28+
diff --git a/super-file.txt b/super-file.txt
29+
new file mode 100644
30+
index 0000000..16bdf1d
31+
--- /dev/null
32+
+++ b/super-file.txt
33+
@@ -0,0 +1,5 @@
34+
+That's a terrible name!
35+
+I don't like it.
36+
+People look down at me and laugh. :-(
37+
+Really!!!!
38+
+Yeah! Better!
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt
2+
deleted file mode 100644
3+
index e8953ab..0000000
4+
--- a/my-name-does-not-feel-right.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-That's a terrible name!
8+
-I don't like it.
9+
-People look down at me and laugh. :-(
10+
-Really!!!!
11+
diff --git a/numbers.txt b/numbers.txt
12+
index 7909961..4e935b7 100644
13+
--- a/numbers.txt
14+
+++ b/numbers.txt
15+
@@ -1,3 +1,4 @@
16+
1
17+
+2
18+
3
19+
4
20+
@@ -9,7 +10,8 @@
21+
9
22+
10
23+
-12
24+
+11
25+
12
26+
13
27+
14
28+
15
29+
+16
30+
diff --git a/super-file.txt b/super-file.txt
31+
new file mode 100644
32+
index 0000000..16bdf1d
33+
--- /dev/null
34+
+++ b/super-file.txt
35+
@@ -0,0 +1,5 @@
36+
+That's a terrible name!
37+
+I don't like it.
38+
+People look down at me and laugh. :-(
39+
+Really!!!!
40+
+Yeah! Better!

0 commit comments

Comments
 (0)