Repository: libgit2/git2go
Pull request #448 (2018-07-07 23:19:29)

Add support for mempack
This allows the creation of git packfiles without needing to write them
to the filesystem.


Patch:
diff --git a/mempack.go b/mempack.go
new file mode 100644
index 0000000..337e67d
--- /dev/null
+++ b/mempack.go
@@ -0,0 +1,84 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/sys/mempack.h>
+
+extern int git_mempack_new(git_odb_backend **out);
+extern int git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_backend *backend);
+extern void git_mempack_reset(git_odb_backend *backend);
+extern void _go_git_odb_backend_free(git_odb_backend *backend);
+*/
+import "C"
+
+import (
+	"runtime"
+	"unsafe"
+)
+
+// Mempack is a custom ODB backend that permits packing object in-memory.
+type Mempack struct {
+	ptr *C.git_odb_backend
+}
+
+// NewMempack creates a new mempack instance and registers it to the ODB.
+func NewMempack(odb *Odb) (mempack *Mempack, err error) {
+	mempack = new(Mempack)
+
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
+
+	ret := C.git_mempack_new(&mempack.ptr)
+	if ret < 0 {
+		return nil, MakeGitError(ret)
+	}
+
+	ret = C.git_odb_add_backend(odb.ptr, mempack.ptr, C.int(999))
+	runtime.KeepAlive(odb)
+	if ret < 0 {
+		// Since git_odb_add_alternate() takes ownership of the ODB backend, the
+		// only case in which we free the mempack's memory is if it fails to be
+		// added to the ODB.
+		C._go_git_odb_backend_free(mempack.ptr)
+		return nil, MakeGitError(ret)
+	}
+
+	return mempack, nil
+}
+
+// Dump dumps all the queued in-memory writes to a packfile.
+//
+// It is the caller's responsibility to ensure that the generated packfile is
+// available to the repository (e.g. by writing it to disk, or doing something
+// crazy like distributing it across several copies of the repository over a
+// network).
+//
+// Once the generated packfile is available to the repository, call
+// Mempack.Reset to cleanup the memory store.
+//
+// Calling Mempack.Reset before the packfile has been written to disk will
+// result in an inconsistent repository (the objects in the memory store won't
+// be accessible).
+func (mempack *Mempack) Dump(repository *Repository) ([]byte, error) {
+	buf := C.git_buf{}
+
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
+
+	ret := C.git_mempack_dump(&buf, repository.ptr, mempack.ptr)
+	runtime.KeepAlive(repository)
+	if ret < 0 {
+		return nil, MakeGitError(ret)
+	}
+	defer C.git_buf_dispose(&buf)
+
+	return C.GoBytes(unsafe.Pointer(buf.ptr), C.int(buf.size)), nil
+}
+
+// Reset resets the memory packer by clearing all the queued objects.
+//
+// This assumes that Mempack.Dump has been called before to store all the
+// queued objects into a single packfile.
+func (mempack *Mempack) Reset() {
+	C.git_mempack_reset(mempack.ptr)
+}


Test patch:
diff --git a/mempack_test.go b/mempack_test.go
new file mode 100644
index 0000000..3e31dcf
--- /dev/null
+++ b/mempack_test.go
@@ -0,0 +1,60 @@
+package git
+
+import (
+	"bytes"
+	"testing"
+)
+
+func TestMempack(t *testing.T) {
+	t.Parallel()
+
+	odb, err := NewOdb()
+	checkFatal(t, err)
+
+	repo, err := NewRepositoryWrapOdb(odb)
+	checkFatal(t, err)
+
+	mempack, err := NewMempack(odb)
+	checkFatal(t, err)
+
+	id, err := odb.Write([]byte("hello, world!"), ObjectBlob)
+	checkFatal(t, err)
+
+	expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
+	checkFatal(t, err)
+	if !expectedId.Equal(id) {
+		t.Errorf("mismatched id. expected %v, got %v", expectedId.String(), id.String())
+	}
+
+	// The object should be available from the odb.
+	{
+		obj, err := odb.Read(expectedId)
+		checkFatal(t, err)
+		defer obj.Free()
+	}
+
+	data, err := mempack.Dump(repo)
+	checkFatal(t, err)
+
+	expectedData := []byte{
+		0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
+		0x02, 0x9d, 0x08, 0x82, 0x3b, 0xd8, 0xa8, 0xea, 0xb5, 0x10, 0xad, 0x6a,
+		0xc7, 0x5c, 0x82, 0x3c, 0xfd, 0x3e, 0xd3, 0x1e,
+	}
+	if !bytes.Equal(expectedData, data) {
+		t.Errorf("mismatched mempack data. expected %v, got %v", expectedData, data)
+	}
+
+	mempack.Reset()
+
+	// After the reset, the object should now be unavailable.
+	{
+		obj, err := odb.Read(expectedId)
+		if err == nil {
+			t.Errorf("object %s unexpectedly found", obj.Id().String())
+			obj.Free()
+		} else if !IsErrorCode(err, ErrNotFound) {
+			t.Errorf("unexpected error %v", err)
+		}
+	}
+}