Created
April 23, 2012 19:08
-
-
Save redoz/2473146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | |
| index 5102f61..612568a 100644 | |
| --- a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | |
| +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | |
| @@ -2,6 +2,7 @@ | |
| using LibGit2Sharp.Tests.TestHelpers; | |
| using Xunit; | |
| using Xunit.Extensions; | |
| +using System.Linq; | |
| namespace LibGit2Sharp.Tests | |
| { | |
| @@ -46,6 +47,27 @@ public void CanCreateABlobFromAFileInTheWorkingDirectory() | |
| } | |
| } | |
| + [Fact] | |
| + public void CanCreateABlobFromAByteArray() | |
| + { | |
| + TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); | |
| + | |
| + using (var repo = new Repository(scd.DirectoryPath)) | |
| + { | |
| + byte[] content = new byte[] {65, 66, 67, 68}; | |
| + Blob blob = repo.ObjectDatabase.CreateBlob(content, 3); | |
| + Assert.Equal(3, blob.Size); | |
| + | |
| + Assert.NotNull(blob); | |
| + Assert.Equal("48b83b862ebc57bd3f7c34ed47262f4b402935af", blob.Sha); | |
| + | |
| + /* check to make sure it is indeed stored in the repository. */ | |
| + var fetchedBlob = repo.Lookup<Blob>(blob.Id); | |
| + Assert.Equal(content.Take(3), fetchedBlob.Content); | |
| + Assert.Equal(blob, fetchedBlob); | |
| + } | |
| + } | |
| + | |
| [Theory] | |
| [InlineData("README")] | |
| [InlineData("README AS WELL")] | |
| diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs | |
| index 864b07d..1266958 100644 | |
| --- a/LibGit2Sharp/Core/NativeMethods.cs | |
| +++ b/LibGit2Sharp/Core/NativeMethods.cs | |
| @@ -71,6 +71,14 @@ public static bool RepositoryStateChecker(RepositorySafeHandle repositoryPtr, Fu | |
| RepositorySafeHandle repo, | |
| [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath path); | |
| + | |
| + [DllImport(libgit2)] | |
| + public static extern int git_blob_create_frombuffer( | |
| + ref GitOid oid, | |
| + RepositorySafeHandle repo, | |
| + byte[] buffer, | |
| + int len); | |
| + | |
| [DllImport(libgit2)] | |
| public static extern IntPtr git_blob_rawcontent(GitObjectSafeHandle blob); | |
| diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs | |
| index dfaf565..882097e 100644 | |
| --- a/LibGit2Sharp/ObjectDatabase.cs | |
| +++ b/LibGit2Sharp/ObjectDatabase.cs | |
| @@ -53,6 +53,22 @@ public Blob CreateBlob(string path) | |
| } | |
| /// <summary> | |
| + /// Inserts a <see cref="Blob"/> into the object database, created from the first <paramref name="length"/> bytes of <paramref name="buffer"/>. | |
| + /// </summary> | |
| + /// <param name="buffer">The <see cref="byte"/> array containing the data to insert.</param> | |
| + /// <param name="length">The number of bytes from <paramref name="buffer"/> to insert.</param> | |
| + /// <returns>The created <see cref="Blob"/>.</returns> | |
| + public Blob CreateBlob(byte[] buffer, int length) | |
| + { | |
| + Ensure.ArgumentNotNull(buffer, "buffer"); | |
| + Ensure.ArgumentConformsTo(length, i => i >= 0 && i <= buffer.Length, "length"); | |
| + | |
| + var oid = new GitOid(); | |
| + Ensure.Success(NativeMethods.git_blob_create_frombuffer(ref oid, repo.Handle, buffer, length)); | |
| + return repo.Lookup<Blob>(new ObjectId(oid)); | |
| + } | |
| + | |
| + /// <summary> | |
| /// Inserts a <see cref = "Tree"/> into the object database, created from a <see cref = "TreeDefinition"/>. | |
| /// </summary> | |
| /// <param name = "treeDefinition">The <see cref = "TreeDefinition"/>.</param> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment