Skip to content

Commit ddeea0c

Browse files
committed
Make GIT_DIFF_SHOW_BINARY flag settable by users through CompareOptions
Give users access to GIT_DIFF_SHOW_BINARY flag through CompareOptions to allow Patch data to contain binary deltas. Add 2 test cases to verify default behaviour has not changed and that when the new option is exercised patches contains binary delta info as expected
1 parent 1af4954 commit ddeea0c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

+56
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,62 @@ public void CanDetectABinaryDeletion()
148148
}
149149
}
150150

151+
[Fact]
152+
public void CanProvideBinaryDeflateDeltaInfo()
153+
{
154+
using (var repo = new Repository(SandboxStandardTestRepo()))
155+
{
156+
const string filename = "newBin.file";
157+
var filepath = Path.Combine(repo.Info.WorkingDirectory, filename);
158+
159+
CreateBinaryFile(filepath);
160+
161+
var compareOptions = new CompareOptions();
162+
compareOptions.ShowBinary = true;
163+
using (Patch patch = repo.Diff.Compare<Patch>(repo.Commits.FirstOrDefault().Tree, DiffTargets.WorkingDirectory, null, null, compareOptions))
164+
{
165+
var withBinaryDeltaDiff = new StringBuilder()
166+
.Append("diff --git a/newBin.file b/newBin.file\n")
167+
.Append("new file mode 100644\n")
168+
.Append("index 0000000000000000000000000000000000000000..689732707188cb8aedfe69d84c9536fd5655d814\n")
169+
.Append("GIT binary patch\n")
170+
.Append("literal 4000\n")
171+
.Append("dc%1FSF%19!3<IEc{zX^D9!O{|RaI40Uk|E93%LLQ\n\n")
172+
.Append("literal 0\n")
173+
.Append("Hc$@<O00001");
174+
175+
Assert.True(patch[filename].IsBinaryComparison);
176+
Assert.Contains(withBinaryDeltaDiff.ToString(), patch[filename].Patch.Trim());
177+
}
178+
179+
}
180+
}
181+
182+
[Fact]
183+
public void DoesNotProvideBinaryDeflateDeltaInfoByDefault()
184+
{
185+
using (var repo = new Repository(SandboxStandardTestRepo()))
186+
{
187+
const string filename = "newBin.file";
188+
var filepath = Path.Combine(repo.Info.WorkingDirectory, filename);
189+
190+
CreateBinaryFile(filepath);
191+
192+
var compareOptions = new CompareOptions();
193+
using (Patch patch = repo.Diff.Compare<Patch>(repo.Commits.FirstOrDefault().Tree, DiffTargets.WorkingDirectory, null, null, compareOptions))
194+
{
195+
var noBinaryDeltaDiff = new StringBuilder()
196+
.Append("diff --git a/newBin.file b/newBin.file\n")
197+
.Append("new file mode 100644\n")
198+
.Append("index 0000000..6897327\n")
199+
.Append("Binary files /dev/null and b/newBin.file differ");
200+
201+
Assert.True(patch[filename].IsBinaryComparison);
202+
Assert.Equal(noBinaryDeltaDiff.ToString(), patch[filename].Patch.Trim());
203+
}
204+
}
205+
}
206+
151207
/*
152208
* $ git diff 9fd738e..HEAD -- "1" "2/"
153209
* diff --git a/1/branch_file.txt b/1/branch_file.txt

LibGit2Sharp/CompareOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,11 @@ public CompareOptions()
5050
/// By default, this option will be false.
5151
/// </summary>
5252
public bool IndentHeuristic { get; set; }
53+
54+
/// <summary>
55+
/// Include the necessary deflate / delta information so that `git-apply`
56+
/// can apply given diff information to binary files.
57+
/// </summary>
58+
public bool ShowBinary { get; set; }
5359
}
5460
}

LibGit2Sharp/Diff.cs

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ private static GitDiffOptions BuildOptions(DiffModifiers diffOptions, FilePath[]
6363
options.Flags |= GitDiffOptionFlags.GIT_DIFF_DISABLE_PATHSPEC_MATCH;
6464
}
6565

66+
if (compareOptions.ShowBinary)
67+
{
68+
options.Flags |= GitDiffOptionFlags.GIT_DIFF_SHOW_BINARY;
69+
}
70+
6671
if (compareOptions.IndentHeuristic)
6772
{
6873
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INDENT_HEURISTIC;

0 commit comments

Comments
 (0)