diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-05-20 16:36:36 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-05-20 18:28:07 +0400 |
commit | 1c23443d0b179c0357f3d3297d250dd4c8ef051c (patch) | |
tree | d19a7fde8ba51215040bdbbe7ef57db335a35cf5 | |
parent | 2054391b299149f06fceadea357ffe8ceb395e87 (diff) |
Make Diff.Compare able to cope with binary content
-rw-r--r-- | LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs | 33 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 1 | ||||
-rw-r--r-- | LibGit2Sharp/ContentChanges.cs | 28 | ||||
-rw-r--r-- | LibGit2Sharp/TreeChanges.cs | 2 | ||||
-rw-r--r-- | LibGit2Sharp/TreeEntryChanges.cs | 3 |
5 files changed, 63 insertions, 4 deletions
diff --git a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs index 15f36460..b62a57a2 100644 --- a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs +++ b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.IO; +using System.Linq; using System.Text; using LibGit2Sharp.Tests.TestHelpers; using Xunit; @@ -32,6 +33,8 @@ namespace LibGit2Sharp.Tests ContentChanges changes = repo.Diff.Compare(oldblob, newblob); + Assert.False(changes.IsBinaryComparison); + Assert.Equal(3, changes.LinesAdded); Assert.Equal(1, changes.LinesDeleted); @@ -57,5 +60,33 @@ namespace LibGit2Sharp.Tests Assert.Equal(expected.ToString(), changes.Patch); } } + + Blob CreateBinaryBlob(Repository repo) + { + var scd = BuildSelfCleaningDirectory(); + Directory.CreateDirectory(scd.RootedDirectoryPath); + string fullpath = Path.Combine(scd.RootedDirectoryPath, "binary.bin"); + File.WriteAllBytes(fullpath, new byte[] { 17, 16, 0, 4, 65 }); + + return repo.ObjectDatabase.CreateBlob(fullpath); + } + + [Fact] + public void CanCompareATextualBlobAgainstABinaryBlob() + { + using (var repo = new Repository(StandardTestRepoPath)) + { + Blob binBlob = CreateBinaryBlob(repo); + + Blob blob = repo.Head.Tip.Tree.Blobs.First(); + + ContentChanges changes = repo.Diff.Compare(blob, binBlob); + + Assert.True(changes.IsBinaryComparison); + + Assert.Equal(0, changes.LinesAdded); + Assert.Equal(0, changes.LinesDeleted); + } + } } } diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index f877d61d..3a69d358 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -57,6 +57,7 @@ namespace LibGit2Sharp.Tests Assert.Equal(1, changes.Added.Count()); TreeEntryChanges treeEntryChanges = changes["1.txt"]; + Assert.False(treeEntryChanges.IsBinaryComparison); Assert.Equal("1.txt", treeEntryChanges.Path); Assert.Equal(ChangeKind.Added, treeEntryChanges.Status); diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index 23a90c56..c5c6cdac 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using System.Text; using LibGit2Sharp.Core; @@ -20,10 +21,30 @@ namespace LibGit2Sharp using (var osw1 = new ObjectSafeWrapper(oldBlob.Id, repo)) using (var osw2 = new ObjectSafeWrapper(newBlob.Id, repo)) { - Ensure.Success(NativeMethods.git_diff_blobs(osw1.ObjectPtr, osw2.ObjectPtr, options, IntPtr.Zero, null, HunkCallback, LineCallback)); + Ensure.Success(NativeMethods.git_diff_blobs(osw1.ObjectPtr, osw2.ObjectPtr, options, IntPtr.Zero, FileCallback, HunkCallback, LineCallback)); } } + private int FileCallback(IntPtr data, GitDiffDelta delta, float progress) + { + IsBinaryComparison = IsBinaryDelta(delta); + + if (!IsBinaryComparison) + { + return 0; + } + + PatchBuilder.Append("Binary content differ\n"); + + return 0; + } + + internal static bool IsBinaryDelta(GitDiffDelta delta) + { + //TODO Fix the interop issue on amd64 and use GitDiffDelta.Binary + return delta.OldFile.Flags.Has(GitDiffFileFlags.GIT_DIFF_FILE_BINARY) || delta.NewFile.Flags.Has(GitDiffFileFlags.GIT_DIFF_FILE_BINARY); + } + private static string NativeToString(IntPtr content, IntPtr contentlen) { return ((Utf8Marshaler)(Utf8Marshaler.GetInstance(string.Empty))).NativeToString(content, contentlen.ToInt32()); @@ -69,6 +90,11 @@ namespace LibGit2Sharp } /// <summary> + /// Determines if at least one of the compared <see cref="Blob"/>s holds some binary content. + /// </summary> + public bool IsBinaryComparison { get; protected set; } + + /// <summary> /// The number of lines added. /// </summary> public int LinesAdded { get; internal set; } diff --git a/LibGit2Sharp/TreeChanges.cs b/LibGit2Sharp/TreeChanges.cs index 0e81ad87..3bf3a7c0 100644 --- a/LibGit2Sharp/TreeChanges.cs +++ b/LibGit2Sharp/TreeChanges.cs @@ -89,7 +89,7 @@ namespace LibGit2Sharp var newMode = (Mode)delta.NewFile.Mode; var oldMode = (Mode)delta.OldFile.Mode; - var diffFile = new TreeEntryChanges(newFilePath, newMode, delta.Status, oldFilePath, oldMode); + var diffFile = new TreeEntryChanges(newFilePath, newMode, delta.Status, oldFilePath, oldMode, ContentChanges.IsBinaryDelta(delta)); fileDispatcher[delta.Status](this, diffFile); changes.Add(diffFile.Path, diffFile); diff --git a/LibGit2Sharp/TreeEntryChanges.cs b/LibGit2Sharp/TreeEntryChanges.cs index ab78bd45..f19fbafa 100644 --- a/LibGit2Sharp/TreeEntryChanges.cs +++ b/LibGit2Sharp/TreeEntryChanges.cs @@ -7,13 +7,14 @@ namespace LibGit2Sharp /// </summary> public class TreeEntryChanges : ContentChanges { - internal TreeEntryChanges(string path, Mode mode, ChangeKind status, string oldPath, Mode oldMode) + internal TreeEntryChanges(string path, Mode mode, ChangeKind status, string oldPath, Mode oldMode, bool isBinaryComparison) { Path = path; Mode = mode; Status = status; OldPath = oldPath; OldMode = oldMode; + IsBinaryComparison = isBinaryComparison; } /// <summary> |