diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index 832b4e5c8..a40070b5c 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -4,6 +4,7 @@ using System.Text; using LibGit2Sharp.Tests.TestHelpers; using Xunit; +using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -222,23 +223,31 @@ public void CanDetectTheRenamingOfAModifiedFile() * $ git diff --shortstat f8d44d7..ec9e401 * 1 file changed, 2 insertions(+), 1 deletion(-) */ - [Fact] - public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion() + [Theory] + [InlineData(0, 175)] + [InlineData(1, 191)] + [InlineData(2, 184)] + [InlineData(3, 187)] + [InlineData(4, 193)] + public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion(int contextLines, int expectedPatchLength) { using (var repo = new Repository(StandardTestRepoPath)) { Tree rootCommitTree = repo.Lookup("f8d44d7").Tree; Tree commitTreeWithUpdatedFile = repo.Lookup("ec9e401").Tree; - TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithUpdatedFile); + TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithUpdatedFile, + compareOptions: new CompareOptions { ContextLines = contextLines }); Assert.Equal(1, changes.Count()); Assert.Equal(1, changes.Modified.Count()); + Assert.Equal(expectedPatchLength, changes.Patch.Length); TreeEntryChanges treeEntryChanges = changes.Modified.Single(); Assert.Equal(2, treeEntryChanges.LinesAdded); Assert.Equal(1, treeEntryChanges.LinesDeleted); + Assert.Equal(expectedPatchLength, treeEntryChanges.Patch.Length); } } @@ -293,15 +302,31 @@ public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion() * super-file.txt | 5 +++++ * 3 files changed, 8 insertions(+), 5 deletions(-) */ - [Fact] - public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks() + [Theory] + [InlineData(0, 3)] + [InlineData(0, 4)] + [InlineData(1, 1)] + [InlineData(1, 2)] + [InlineData(2, 4)] + [InlineData(2, 5)] + [InlineData(3, 2)] + [InlineData(3, 3)] + [InlineData(4, 0)] + [InlineData(4, 1)] + public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, int interhunkLines) { + var compareOptions = new CompareOptions + { + ContextLines = contextLines, + InterhunkLines = interhunkLines, + }; + using (var repo = new Repository(StandardTestRepoPath)) { Tree rootCommitTree = repo.Lookup("f8d44d7").Tree; Tree mergedCommitTree = repo.Lookup("7252fe2").Tree; - TreeChanges changes = repo.Diff.Compare(rootCommitTree, mergedCommitTree); + TreeChanges changes = repo.Diff.Compare(rootCommitTree, mergedCommitTree, compareOptions: compareOptions); Assert.Equal(3, changes.Count()); Assert.Equal(1, changes.Modified.Count()); @@ -314,77 +339,10 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks() Assert.Equal(1, treeEntryChanges.LinesDeleted); Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Mode); - - var expected = new StringBuilder() - .Append("diff --git a/numbers.txt b/numbers.txt\n") - .Append("index 7909961..4e935b7 100644\n") - .Append("--- a/numbers.txt\n") - .Append("+++ b/numbers.txt\n") - .Append("@@ -1,4 +1,5 @@\n") - .Append(" 1\n") - .Append("+2\n") - .Append(" 3\n") - .Append(" 4\n") - .Append(" 5\n") - .Append("@@ -8,8 +9,9 @@\n") - .Append(" 8\n") - .Append(" 9\n") - .Append(" 10\n") - .Append("-12\n") - .Append("+11\n") - .Append(" 12\n") - .Append(" 13\n") - .Append(" 14\n") - .Append(" 15\n") - .Append("+16\n"); - - Assert.Equal(expected.ToString(), treeEntryChanges.Patch); - - expected = new StringBuilder() - .Append("diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt\n") - .Append("deleted file mode 100644\n") - .Append("index e8953ab..0000000\n") - .Append("--- a/my-name-does-not-feel-right.txt\n") - .Append("+++ /dev/null\n") - .Append("@@ -1,4 +0,0 @@\n") - .Append("-That's a terrible name!\n") - .Append("-I don't like it.\n") - .Append("-People look down at me and laugh. :-(\n") - .Append("-Really!!!!\n") - .Append("diff --git a/numbers.txt b/numbers.txt\n") - .Append("index 7909961..4e935b7 100644\n") - .Append("--- a/numbers.txt\n") - .Append("+++ b/numbers.txt\n") - .Append("@@ -1,4 +1,5 @@\n") - .Append(" 1\n") - .Append("+2\n") - .Append(" 3\n") - .Append(" 4\n") - .Append(" 5\n") - .Append("@@ -8,8 +9,9 @@\n") - .Append(" 8\n") - .Append(" 9\n") - .Append(" 10\n") - .Append("-12\n") - .Append("+11\n") - .Append(" 12\n") - .Append(" 13\n") - .Append(" 14\n") - .Append(" 15\n") - .Append("+16\n") - .Append("diff --git a/super-file.txt b/super-file.txt\n") - .Append("new file mode 100644\n") - .Append("index 0000000..16bdf1d\n") - .Append("--- /dev/null\n") - .Append("+++ b/super-file.txt\n") - .Append("@@ -0,0 +1,5 @@\n") - .Append("+That's a terrible name!\n") - .Append("+I don't like it.\n") - .Append("+People look down at me and laugh. :-(\n") - .Append("+Really!!!!\n") - .Append("+Yeah! Better!\n"); - - Assert.Equal(expected.ToString(), changes.Patch); + Assert.Equal(Expected("f8d44d7...7252fe2/numbers.txt-{0}-{1}.diff", contextLines, interhunkLines), + treeEntryChanges.Patch); + Assert.Equal(Expected("f8d44d7...7252fe2/full-{0}-{1}.diff", contextLines, interhunkLines), + changes.Patch); } } @@ -479,7 +437,7 @@ public void ComparingTwoNullTreesReturnsAnEmptyTreeChanges() { using (var repo = new Repository(StandardTestRepoPath)) { - TreeChanges changes = repo.Diff.Compare(null, null, null); + TreeChanges changes = repo.Diff.Compare(default(Tree), default(Tree)); Assert.Equal(0, changes.Count()); } diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs index 91ca9bbad..340f1352e 100644 --- a/LibGit2Sharp.Tests/MetaFixture.cs +++ b/LibGit2Sharp.Tests/MetaFixture.cs @@ -12,13 +12,14 @@ public class MetaFixture { private static readonly Type[] excludedTypes = new[] { + typeof(CompareOptions), typeof(Credentials), + typeof(ExplicitPathsOptions), typeof(Filter), typeof(ObjectId), typeof(Repository), typeof(RepositoryOptions), typeof(Signature), - typeof(ExplicitPathsOptions), }; // Related to https://github.com/libgit2/libgit2sharp/pull/251 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-3.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-3.diff new file mode 100644 index 000000000..8f1024340 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-3.diff @@ -0,0 +1,32 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,0 +2 @@ ++2 +@@ -11 +12 @@ +-12 ++11 +@@ -15,0 +17 @@ ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-4.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-4.diff new file mode 100644 index 000000000..ac0db32e5 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-0-4.diff @@ -0,0 +1,35 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,0 +2 @@ ++2 +@@ -11,5 +12,6 @@ +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-1.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-1.diff new file mode 100644 index 000000000..0447955c4 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-1.diff @@ -0,0 +1,37 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,2 +1,3 @@ + 1 ++2 + 3 +@@ -10,3 +11,3 @@ + 10 +-12 ++11 + 12 +@@ -15 +16,2 @@ + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-2.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-2.diff new file mode 100644 index 000000000..6f1be86c9 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-1-2.diff @@ -0,0 +1,38 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,2 +1,3 @@ + 1 ++2 + 3 +@@ -10,6 +11,7 @@ + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-4.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-4.diff new file mode 100644 index 000000000..a470e39c0 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-4.diff @@ -0,0 +1,40 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,3 +1,4 @@ + 1 ++2 + 3 + 4 +@@ -9,7 +10,8 @@ + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-5.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-5.diff new file mode 100644 index 000000000..ae602bcc5 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-2-5.diff @@ -0,0 +1,44 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-2.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-2.diff new file mode 100644 index 000000000..2037795d1 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-2.diff @@ -0,0 +1,42 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,4 +1,5 @@ + 1 ++2 + 3 + 4 + 5 +@@ -8,8 +9,9 @@ + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-3.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-3.diff new file mode 100644 index 000000000..ae602bcc5 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-3-3.diff @@ -0,0 +1,44 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-0.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-0.diff new file mode 100644 index 000000000..d624e0d73 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-0.diff @@ -0,0 +1,44 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,5 +1,6 @@ + 1 ++2 + 3 + 4 + 5 + 6 +@@ -7,9 +8,10 @@ + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-1.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-1.diff new file mode 100644 index 000000000..ae602bcc5 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/full-4-1.diff @@ -0,0 +1,44 @@ +diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt +deleted file mode 100644 +index e8953ab..0000000 +--- a/my-name-does-not-feel-right.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-That's a terrible name! +-I don't like it. +-People look down at me and laugh. :-( +-Really!!!! +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 +diff --git a/super-file.txt b/super-file.txt +new file mode 100644 +index 0000000..16bdf1d +--- /dev/null ++++ b/super-file.txt +@@ -0,0 +1,5 @@ ++That's a terrible name! ++I don't like it. ++People look down at me and laugh. :-( ++Really!!!! ++Yeah! Better! diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-3.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-3.diff new file mode 100644 index 000000000..c14970b01 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-3.diff @@ -0,0 +1,11 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,0 +2 @@ ++2 +@@ -11 +12 @@ +-12 ++11 +@@ -15,0 +17 @@ ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-4.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-4.diff new file mode 100644 index 000000000..4c2236c7b --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-0-4.diff @@ -0,0 +1,14 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,0 +2 @@ ++2 +@@ -11,5 +12,6 @@ +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-1.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-1.diff new file mode 100644 index 000000000..5ad039099 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-1.diff @@ -0,0 +1,16 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,2 +1,3 @@ + 1 ++2 + 3 +@@ -10,3 +11,3 @@ + 10 +-12 ++11 + 12 +@@ -15 +16,2 @@ + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-2.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-2.diff new file mode 100644 index 000000000..7a5fda643 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-1-2.diff @@ -0,0 +1,17 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,2 +1,3 @@ + 1 ++2 + 3 +@@ -10,6 +11,7 @@ + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-4.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-4.diff new file mode 100644 index 000000000..9311b08fc --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-4.diff @@ -0,0 +1,19 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,3 +1,4 @@ + 1 ++2 + 3 + 4 +@@ -9,7 +10,8 @@ + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-5.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-5.diff new file mode 100644 index 000000000..26e01b51f --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-2-5.diff @@ -0,0 +1,23 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-2.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-2.diff new file mode 100644 index 000000000..9f55b2e05 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-2.diff @@ -0,0 +1,21 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,4 +1,5 @@ + 1 ++2 + 3 + 4 + 5 +@@ -8,8 +9,9 @@ + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-3.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-3.diff new file mode 100644 index 000000000..26e01b51f --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-3-3.diff @@ -0,0 +1,23 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-0.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-0.diff new file mode 100644 index 000000000..9ec9021ef --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-0.diff @@ -0,0 +1,23 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,5 +1,6 @@ + 1 ++2 + 3 + 4 + 5 + 6 +@@ -7,9 +8,10 @@ + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-1.diff b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-1.diff new file mode 100644 index 000000000..26e01b51f --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/expected/f8d44d7...7252fe2/numbers.txt-4-1.diff @@ -0,0 +1,23 @@ +diff --git a/numbers.txt b/numbers.txt +index 7909961..4e935b7 100644 +--- a/numbers.txt ++++ b/numbers.txt +@@ -1,15 +1,17 @@ + 1 ++2 + 3 + 4 + 5 + 6 + 7 + 7.2 + 8 + 9 + 10 +-12 ++11 + 12 + 13 + 14 + 15 ++16 diff --git a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs index 2158b590f..c566f94f8 100644 --- a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs +++ b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -221,5 +222,15 @@ protected string Touch(string parent, string file, string content = null) return file; } + + protected string Expected(string filename) + { + return File.ReadAllText(Path.Combine(ResourcesDirectory.FullName, "expected/" + filename)); + } + + protected string Expected(string filenameFormat, params object[] args) + { + return Expected(string.Format(CultureInfo.InvariantCulture, filenameFormat, args)); + } } } diff --git a/LibGit2Sharp/CompareOptions.cs b/LibGit2Sharp/CompareOptions.cs new file mode 100644 index 000000000..c5bee7ed1 --- /dev/null +++ b/LibGit2Sharp/CompareOptions.cs @@ -0,0 +1,29 @@ +namespace LibGit2Sharp +{ + /// + /// Options to define file comparison behavior. + /// + public class CompareOptions + { + /// + /// Initializes a new instance of the class. + /// + public CompareOptions() + { + ContextLines = 3; + InterhunkLines = 0; + } + + /// + /// The number of unchanged lines that define the boundary of a hunk (and to display before and after). + /// (Default = 3) + /// + public int ContextLines { get; set; } + + /// + /// The maximum number of unchanged lines between hunk boundaries before the hunks will be merged into a one. + /// (Default = 0) + /// + public int InterhunkLines { get; set; } + } +} diff --git a/LibGit2Sharp/Diff.cs b/LibGit2Sharp/Diff.cs index a14071935..c5e1f12ea 100644 --- a/LibGit2Sharp/Diff.cs +++ b/LibGit2Sharp/Diff.cs @@ -19,12 +19,15 @@ public class Diff { private readonly Repository repo; - private static GitDiffOptions BuildOptions(DiffOptions diffOptions, FilePath[] filePaths = null, MatchedPathsAggregator matchedPathsAggregator = null) + private static GitDiffOptions BuildOptions(DiffOptions diffOptions, FilePath[] filePaths = null, MatchedPathsAggregator matchedPathsAggregator = null, CompareOptions compareOptions = null) { var options = new GitDiffOptions(); options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_TYPECHANGE; - options.ContextLines = 3; + + compareOptions = compareOptions ?? new CompareOptions(); + options.ContextLines = (ushort)compareOptions.ContextLines; + options.InterhunkLines = (ushort)compareOptions.InterhunkLines; if (diffOptions.HasFlag(DiffOptions.IncludeUntracked)) { @@ -110,8 +113,9 @@ internal Diff(Repository repo) /// If set, the passed will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + /// Additional options to define comparison behavior. /// A containing the changes between the and the . - public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null) + public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null) { var comparer = TreeToTree(repo); ObjectId oldTreeId = oldTree != null ? oldTree.Id : null; @@ -129,7 +133,7 @@ public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable @@ -137,10 +141,11 @@ public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable /// The you want to compare from. /// The you want to compare to. + /// Additional options to define comparison behavior. /// A containing the changes between the and the . - public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob) + public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions compareOptions = null) { - using (GitDiffOptions options = BuildOptions(DiffOptions.None)) + using (GitDiffOptions options = BuildOptions(DiffOptions.None, compareOptions: compareOptions)) { return new ContentChanges(repo, oldBlob, newBlob, options); } @@ -168,8 +173,9 @@ private static IDictionary will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + /// Additional options to define comparison behavior. /// A containing the changes between the and the selected target. - public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null) + public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null) { var comparer = handleRetrieverDispatcher[diffTargets](repo); ObjectId oldTreeId = oldTree != null ? oldTree.Id : null; @@ -188,7 +194,7 @@ public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnume } } - return BuildTreeChangesFromComparer(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions); + return BuildTreeChangesFromComparer(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions); } /// @@ -200,14 +206,15 @@ public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnume /// If set, the passed will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + /// Additional options to define comparison behavior. /// A containing the changes between the working directory and the index. - public virtual TreeChanges Compare(IEnumerable paths = null, bool includeUntracked = false, ExplicitPathsOptions explicitPathsOptions = null) + public virtual TreeChanges Compare(IEnumerable paths = null, bool includeUntracked = false, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null) { - return Compare(includeUntracked ? DiffOptions.IncludeUntracked : DiffOptions.None, paths, explicitPathsOptions); + return Compare(includeUntracked ? DiffOptions.IncludeUntracked : DiffOptions.None, paths, explicitPathsOptions, compareOptions); } internal virtual TreeChanges Compare(DiffOptions diffOptions, IEnumerable paths = null, - ExplicitPathsOptions explicitPathsOptions = null) + ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null) { var comparer = WorkdirToIndex(repo); @@ -222,7 +229,7 @@ internal virtual TreeChanges Compare(DiffOptions diffOptions, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null) + DiffOptions diffOptions, IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null, CompareOptions compareOptions = null) { var matchedPaths = new MatchedPathsAggregator(); var filePaths = ToFilePaths(repo, paths); - using (GitDiffOptions options = BuildOptions(diffOptions, filePaths, matchedPaths)) + using (GitDiffOptions options = BuildOptions(diffOptions, filePaths, matchedPaths, compareOptions)) using (DiffListSafeHandle diffList = comparisonHandleRetriever(oldTreeId, newTreeId, options)) { if (explicitPathsOptions != null) diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index aedf03034..da4a719d3 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -67,6 +67,7 @@ +