8000 Add a general Write<T> method to ObjectDatabase · MustafaJamal/libgit2sharp@e9f5778 · GitHub
[go: up one dir, main page]

Skip to content

Commit e9f5778

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 ccee01a commit e9f5778

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
@@ -919,6 +919,9 @@ internal static extern unsafe int git_odb_foreach(
919919
[DllImport(libgit2)]
920920
internal static extern unsafe void git_odb_stream_free(git_odb_stream* stream);
921921

922+
[DllImport(libgit2)]
923+
internal static extern unsafe int git_odb_write(out GitOid id, git_odb *odb, byte* data, UIntPtr len, GitObjectType type);
924+
922925
[DllImport(libgit2)]
923926
internal static extern unsafe git_oid* git_object_id(git_object* obj);
924927

LibGit2Sharp/Core/Proxy.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,19 @@ public static unsafe ObjectId git_odb_stream_finalize_write(OdbStreamHandle stre
15101510
return id;
15111511
}
15121512

1513+
public static unsafe ObjectId git_odb_write(ObjectDatabaseHandle odb, byte[] data, ObjectType type)
1514+
{
1515+
GitOid id;
1516+
int res;
1517+
fixed(byte* p = data)
1518+
{
1519+
res = NativeMethods.git_odb_write(out id, odb, p, new UIntPtr((ulong)data.LongLength), type.ToGitObjectType());
1520+
}
1521+
Ensure.ZeroResult(res);
1522+
1523+
return id;
1524+
}
1525+
15131526
#endregion
15141527

15151528
#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)
0