summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlhchavez <[email protected]>2020-02-23 06:47:18 -0800
committerGitHub <[email protected]>2020-02-23 06:47:18 -0800
commit06764f48dce903bf95701c6ef75ad0fe46c0dedf (patch)
treeddae8b1265953acf97fd71b8cb0ee2ea6ae8cee7
parent4bca045e5aa98b0b791fb467705de0692fe3514f (diff)
parent00374b39aad728f34eeadbb575ac607acbfa205a (diff)
Merge pull request #400 from ramanenka/git_index_add_frombuffer
Add binding for `git_index_add_frombuffer`
-rw-r--r--index.go26
-rw-r--r--index_test.go24
2 files changed, 49 insertions, 1 deletions
diff --git a/index.go b/index.go
index dd13460..50538bd 100644
--- a/index.go
+++ b/index.go
@@ -90,7 +90,9 @@ func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
dest.uid = C.uint32_t(source.Uid)
dest.gid = C.uint32_t(source.Gid)
dest.file_size = C.uint32_t(source.Size)
- dest.id = *source.Id.toC()
+ if source.Id != nil {
+ dest.id = *source.Id.toC()
+ }
dest.path = C.CString(source.Path)
}
@@ -195,6 +197,28 @@ func (v *Index) AddByPath(path string) error {
return nil
}
+// AddFromBuffer adds or replaces an index entry from a buffer in memory
+func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error {
+ var centry C.git_index_entry
+
+ populateCIndexEntry(entry, &centry)
+ defer freeCIndexEntry(&centry)
+
+ var cbuffer unsafe.Pointer
+ if len(buffer) > 0 {
+ cbuffer = unsafe.Pointer(&buffer[0])
+ }
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if err := C.git_index_add_from_buffer(v.ptr, &centry, cbuffer, C.size_t(len(buffer))); err < 0 {
+ return MakeGitError(err)
+ }
+
+ return nil
+}
+
func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error {
cpathspecs := C.git_strarray{}
cpathspecs.count = C.size_t(len(pathspecs))
diff --git a/index_test.go b/index_test.go
index 43644fa..5fa3f9f 100644
--- a/index_test.go
+++ b/index_test.go
@@ -165,6 +165,30 @@ func TestIndexRemoveDirectory(t *testing.T) {
}
}
+func TestIndexAddFromBuffer(t *testing.T) {
+ t.Parallel()
+ repo := createTestRepo(t)
+ defer cleanupTestRepo(t, repo)
+
+ idx, err := repo.Index()
+ checkFatal(t, err)
+
+ entry := IndexEntry{
+ Path: "README",
+ Mode: FilemodeBlob,
+ }
+
+ err = idx.AddFromBuffer(&entry, []byte("foo\n"))
+ checkFatal(t, err)
+
+ treeId, err := idx.WriteTreeTo(repo)
+ checkFatal(t, err)
+
+ if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
+ t.Fatalf("%v", treeId.String())
+ }
+}
+
func TestIndexAddAllNoCallback(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)