Skip to content

PatienceAlgorithm flag in CompareOptions #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,5 +1133,75 @@ public void CallingCompareWithAnUnsupportedGenericParamThrows()
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>());
}
}

[Fact]
public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
{
string repoPath = InitNewRepository();
using (var repo = new Repository(repoPath))
{
Func<string, Tree> fromString =
s =>
repo.ObjectDatabase.CreateTree(new TreeDefinition().Add("file.txt",
OdbHelper.CreateBlob(repo, s), Mode.NonExecutableFile));

Tree treeOld = fromString(new StringBuilder()
.Append("aaaaaa\n")
.Append("aaaaaa\n")
.Append("bbbbbb\n")
.Append("bbbbbb\n")
.Append("cccccc\n")
.Append("cccccc\n")
.Append("abc\n").ToString());

Tree treeNew = fromString(new StringBuilder()
.Append("abc\n")
.Append("aaaaaa\n")
.Append("aaaaaa\n")
.Append("bbbbbb\n")
.Append("bbbbbb\n")
.Append("cccccc\n")
.Append("cccccc\n").ToString());

string diffDefault = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index 3299d68..accc3bd 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1,7 +1,7 @@\n")
.Append("+abc\n")
.Append(" aaaaaa\n")
.Append(" aaaaaa\n")
.Append(" bbbbbb\n")
.Append(" bbbbbb\n")
.Append(" cccccc\n")
.Append(" cccccc\n")
.Append("-abc\n").ToString();

string diffPatience = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index 3299d68..accc3bd 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1,7 +1,7 @@\n")
.Append("-aaaaaa\n")
.Append("-aaaaaa\n")
.Append("-bbbbbb\n")
.Append("-bbbbbb\n")
.Append("-cccccc\n")
.Append("-cccccc\n")
.Append(" abc\n")
.Append("+aaaaaa\n")
.Append("+aaaaaa\n")
.Append("+bbbbbb\n")
.Append("+bbbbbb\n")
.Append("+cccccc\n")
.Append("+cccccc\n").ToString();

Assert.Equal(diffDefault, repo.Diff.Compare<Patch>(treeOld, treeNew));
Assert.Equal(diffPatience, repo.Diff.Compare<Patch>(treeOld, treeNew,
compareOptions: new CompareOptions { UsePatienceAlgorithm = true }));
}
}
}
}
5 changes: 5 additions & 0 deletions LibGit2Sharp/CompareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public CompareOptions()
/// Include "unmodified" entries in the results.
/// </summary>
public bool IncludeUnmodified { get; set; }

/// <summary>
/// Use the "patience diff" algorithm.
/// </summary>
public bool UsePatienceAlgorithm { get; set; }
}
}
5 changes: 5 additions & 0 deletions LibGit2Sharp/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private static GitDiffOptions BuildOptions(DiffModifiers diffOptions, FilePath[]
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNMODIFIED;
}

if (compareOptions.UsePatienceAlgorithm)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_PATIENCE;
}

if (diffOptions.HasFlag(DiffModifiers.DisablePathspecMatch))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_DISABLE_PATHSPEC_MATCH;
Expand Down