Skip to content

Commit 20a788e

Browse files
committed
Add a general Write<T> method to ObjectDatabase
We have some methods for creating objects from specific data, but not for creating it out of an existing buffer.
1 parent 26b61fc commit 20a788e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,9 @@ internal static extern unsafe int git_odb_foreach(
916916
[DllImport(libgit2)]
917917
internal static extern unsafe void git_odb_stream_free(git_odb_stream* stream);
918918

919+
[DllImport(libgit2)]
920+
internal static extern unsafe int git_odb_write(out GitOid id, git_odb *odb, byte* data, UIntPtr len, GitObjectType type);
921+
919922
[DllImport(libgit2)]
920923
internal static extern unsafe git_oid* git_object_id(git_object* obj);
921924

LibGit2Sharp/Core/Proxy.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,19 @@ public static unsafe ObjectId git_odb_stream_finalize_write(OdbStreamHandle stre
15011501
return id;
15021502
}
15031503

1504+
public static unsafe ObjectId git_odb_write(ObjectDatabaseHandle odb, byte[] data, ObjectType type)
1505+
{
1506+
GitOid id;
1507+
int res;
1508+
fixed(byte* p = data)
1509+
{
1510+
res = NativeMethods.git_odb_write(out id, odb, p, new UIntPtr((ulong)data.LongLength), type.ToGitObjectType());
1511+
}
1512+
Ensure.ZeroResult(res);
1513+
1514+
return id;
1515+
}
1516+
15041517
#endregion
15051518

15061519
#region git_patch_

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public int Provider(IntPtr content, int max_length, IntPtr data)
177177
}
178178
}
179179

180+
/// <summary>
181+
/// Write an object to the object database
182+
/// </summary>
183+
/// <param name="data">The contents of the object</param>
184+
/// <typeparam name="T">The type of object to write</typeparam>
185+
public virtual ObjectId Write<T>(byte[] data) where T : GitObject
186+
{
187+
return Proxy.git_odb_write(handle, data, GitObject.TypeToKindMap[typeof(T)]);
188+
}
189+
180190
/// <summary>
181191
/// Inserts a <see cref="Blob"/> into the object database, created from the content of a stream.
182192
/// <para>Optionally, git filters will be applied to the content before storing it.</para>

0 commit comments

Comments
 (0)