Skip to content
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

Make GIT_DIFF_SHOW_BINARY flag settable by users through CompareOptions #1967

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,62 @@ public void CanDetectABinaryDeletion()
}
}

[Fact]
public void CanProvideBinaryDeflateDeltaInfo()
{
using (var repo = new Repository(SandboxStandardTestRepo()))
{
const string filename = "newBin.file";
var filepath = Path.Combine(repo.Info.WorkingDirectory, filename);

CreateBinaryFile(filepath);

var compareOptions = new CompareOptions();
compareOptions.ShowBinary = true;
using (Patch patch = repo.Diff.Compare<Patch>(repo.Commits.FirstOrDefault().Tree, DiffTargets.WorkingDirectory, null, null, compareOptions))
{
var withBinaryDeltaDiff = new StringBuilder()
.Append("diff --git a/newBin.file b/newBin.file\n")
.Append("new file mode 100644\n")
.Append("index 0000000000000000000000000000000000000000..689732707188cb8aedfe69d84c9536fd5655d814\n")
.Append("GIT binary patch\n")
.Append("literal 4000\n")
.Append("dc%1FSF%19!3<IEc{zX^D9!O{|RaI40Uk|E93%LLQ\n\n")
.Append("literal 0\n")
.Append("Hc$@<O00001");

Assert.True(patch[filename].IsBinaryComparison);
Assert.Contains(withBinaryDeltaDiff.ToString(), patch[filename].Patch.Trim());
}

}
}

[Fact]
public void DoesNotProvideBinaryDeflateDeltaInfoByDefault()
{
using (var repo = new Repository(SandboxStandardTestRepo()))
{
const string filename = "newBin.file";
var filepath = Path.Combine(repo.Info.WorkingDirectory, filename);

CreateBinaryFile(filepath);

var compareOptions = new CompareOptions();
using (Patch patch = repo.Diff.Compare<Patch>(repo.Commits.FirstOrDefault().Tree, DiffTargets.WorkingDirectory, null, null, compareOptions))
{
var noBinaryDeltaDiff = new StringBuilder()
.Append("diff --git a/newBin.file b/newBin.file\n")
.Append("new file mode 100644\n")
.Append("index 0000000..6897327\n")
.Append("Binary files /dev/null and b/newBin.file differ");

Assert.True(patch[filename].IsBinaryComparison);
Assert.Equal(noBinaryDeltaDiff.ToString(), patch[filename].Patch.Trim());
}
}
}

/*
* $ git diff 9fd738e..HEAD -- "1" "2/"
* diff --git a/1/branch_file.txt b/1/branch_file.txt
Expand Down
6 changes: 6 additions & 0 deletions LibGit2Sharp/CompareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ public CompareOptions()
/// By default, this option will be false.
/// </summary>
public bool IndentHeuristic { get; set; }

/// <summary>
/// Include the necessary deflate / delta information so that `git-apply`
/// can apply given diff information to binary files.
/// </summary>
public bool ShowBinary { get; set; }
}
}
5 changes: 5 additions & 0 deletions LibGit2Sharp/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ private static GitDiffOptions BuildOptions(DiffModifiers diffOptions, FilePath[]
options.Flags |= GitDiffOptionFlags.GIT_DIFF_DISABLE_PATHSPEC_MATCH;
}

if (compareOptions.ShowBinary)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_SHOW_BINARY;
}

if (compareOptions.IndentHeuristic)
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INDENT_HEURISTIC;
Expand Down