8000 Make Diff.Compare() able to compare null Blobs · Saaman/libgit2sharp@ec1a6b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec1a6b7

Browse files
committed
Make Diff.Compare() able to compare null Blobs
1 parent c57f258 commit ec1a6b7

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,40 @@ public void CanCompareATextualBlobAgainstABinaryBlob()
8989
Assert.Equal(0, changes.LinesDeleted);
9090
}
9191
}
92+
93+
[Fact]
94+
public void CanCompareABlobAgainstANullBlob()
95+
{
96+
using (var repo = new Repository(StandardTestRepoPath))
97+
{
98+
Blob blob = repo.Head.Tip.Tree.Blobs.First();
99+
100+
ContentChanges changes = repo.Diff.Compare(null, blob);
101+
102+
Assert.NotEqual(0, changes.LinesAdded);
103+
Assert.Equal(0, changes.LinesDeleted);
104+
Assert.NotEqual(string.Empty, changes.Patch);
105+
106+
changes = repo.Diff.Compare(blob, null);
107+
108+
Assert.Equal(0, changes.LinesAdded);
109+
Assert.NotEqual(0, changes.LinesDeleted);
110+
Assert.NotEqual(string.Empty, changes.Patch);
111+
}
112+
}
113+
114+
[Fact]
115+
public void ComparingTwoNullBlobsReturnsAnEmptyContentChanges()
116+
{
117+
using (var repo = new Repository(StandardTestRepoPath))
118+
{
119+
ContentChanges changes = repo.Diff.Compare((Blob)null, (Blob)null);
120+
121+
Assert.False(changes.IsBinaryComparison);
122+
123+
Assert.Equal(0, changes.LinesAdded);
124+
Assert.Equal(0, changes.LinesDeleted);
125+
}
126+
}
92127
}
93128
}

LibGit2Sharp/ContentChanges.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ protected ContentChanges()
1717

1818
internal ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options)
1919
{
20-
Proxy.git_diff_blobs(repo.Handle, oldBlob, newBlob, options, FileCallback, HunkCallback, LineCallback);
20+
Proxy.git_diff_blobs(repo.Handle,
21+
oldBlob != null ? oldBlob.Id : null,
22+
newBlob != null ? newBlob.Id : null,
23+
options, FileCallback, HunkCallback, LineCallback);
2124
}
2225

2326
private int FileCallback(IntPtr data, GitDiffDelta delta, float progress)

LibGit2Sharp/Core/Proxy.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,16 @@ public static ICollection<TResult> git_config_foreach<TResult>(
488488

489489
public static void git_diff_blobs(
490490
RepositorySafeHandle repo,
491-
Blob oldBlob,
492-
Blob newBlob,
491+
ObjectId oldBlob,
492+
ObjectId newBlob,
493493
GitDiffOptions options,
494494
NativeMethods.git_diff_file_fn fileCallback,
495495
NativeMethods.git_diff_hunk_fn hunkCallback,
496496
NativeMethods.git_diff_data_fn lineCallback)
497497
{
498498
using (ThreadAffinity())
499-
using (var osw1 = new ObjectSafeWrapper(oldBlob.Id, repo))
500-
using (var osw2 = new ObjectSafeWrapper(newBlob.Id, repo))
499+
using (var osw1 = new ObjectSafeWrapper(oldBlob, repo, true))
500+
using (var osw2 = new ObjectSafeWrapper(newBlob, repo, true))
501501
{
502502
int res = NativeMethods.git_diff_blobs(osw1.ObjectPtr, osw2.ObjectPtr, options, IntPtr.Zero, fileCallback, hunkCallback, lineCallback);
503503
Ensure.Success(res);

0 commit comments

Comments
 (0)
0