diff options
author | Metalrom <romain.magny@gmail.com> | 2013-01-14 14:46:45 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-01-14 18:15:37 +0400 |
commit | 95a50c31370c43aba6ee66b8705df45810dac0e8 (patch) | |
tree | caa19274df006fe86692949ac0cfd95e89ecaf44 | |
parent | d31f95ad0de083086ca7f2a2e8e75d588ec45025 (diff) |
Introduce Blob.IsBinary
-rwxr-xr-x | LibGit2Sharp.Tests/BlobFixture.cs | 10 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | 19 | ||||
-rw-r--r-- | LibGit2Sharp/Blob.cs | 7 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 3 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Proxy.cs | 5 |
5 files changed, 44 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/BlobFixture.cs b/LibGit2Sharp.Tests/BlobFixture.cs index ec56f380..68651bc5 100755 --- a/LibGit2Sharp.Tests/BlobFixture.cs +++ b/LibGit2Sharp.Tests/BlobFixture.cs @@ -113,5 +113,15 @@ namespace LibGit2Sharp.Tests Assert.Equal("baae1fb3760a73481ced1fa03dc15614142c19ef", newentry.Id.Sha); } } + + [Fact] + public void CanTellIfTheBlobContentLooksLikeBinary() + { + using (var repo = new Repository(BareTestRepoPath)) + { + var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + Assert.Equal(false, blob.IsBinary); + } + } } } diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs index dbe2ecca..bb565bd9 100644 --- a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs @@ -286,5 +286,24 @@ namespace LibGit2Sharp.Tests Assert.Equal("Ü message\n", commit.Message); } } + + [Fact] + public void CanCreateABinaryBlobFromABinaryReader() + { + TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); + + var binaryContent = new byte[] { 0, 1, 2, 3, 4, 5 }; + + using (var repo = new Repository(scd.RepositoryPath)) + { + using (var stream = new MemoryStream(binaryContent)) + using (var binReader = new BinaryReader(stream)) + { + Blob blob = repo.ObjectDatabase.CreateBlob(binReader); + Assert.Equal(6, blob.Size); + Assert.Equal(true, blob.IsBinary); + } + } + } } } diff --git a/LibGit2Sharp/Blob.cs b/LibGit2Sharp/Blob.cs index 40396080..ef4b31ce 100644 --- a/LibGit2Sharp/Blob.cs +++ b/LibGit2Sharp/Blob.cs @@ -10,6 +10,7 @@ namespace LibGit2Sharp public class Blob : GitObject { private readonly ILazy<Int64> lazySize; + private readonly ILazy<bool> lazyIsBinary; /// <summary> /// Needed for mocking purposes. @@ -21,6 +22,7 @@ namespace LibGit2Sharp : base(repo, id) { lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize); + lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary); } /// <summary> @@ -29,6 +31,11 @@ namespace LibGit2Sharp public virtual int Size { get { return (int)lazySize.Value; } } /// <summary> + /// Determine if the blob content is most certainly binary or not. + /// </summary> + public virtual bool IsBinary { get { return lazyIsBinary.Value; } } + + /// <summary> /// Gets the blob content in a <see cref="byte" /> array. /// </summary> public virtual byte[] Content diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 6ccaceff..07a4bce3 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -887,6 +887,9 @@ namespace LibGit2Sharp.Core [DllImport(libgit2)] internal static extern void git_treebuilder_free(IntPtr bld); + + [DllImport(libgit2)] + internal static extern bool git_blob_is_binary(GitObjectSafeHandle blob); } } // ReSharper restore InconsistentNaming diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index c75c4369..97973dda 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -99,6 +99,11 @@ namespace LibGit2Sharp.Core return NativeMethods.git_blob_rawsize(obj); } + public static bool git_blob_is_binary(GitObjectSafeHandle obj) + { + return NativeMethods.git_blob_is_binary(obj); + } + #endregion #region git_branch_ |