Skip to content

Commit e43846b

Browse files
committed
Merge pull request #400 from ramanenka/git_index_add_frombuffer
Add binding for `git_index_add_frombuffer` (cherry picked from commit 06764f4)
1 parent bd2a0c8 commit e43846b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

index.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
9090
dest.uid = C.uint32_t(source.Uid)
9191
dest.gid = C.uint32_t(source.Gid)
9292
dest.file_size = C.uint32_t(source.Size)
93-
dest.id = *source.Id.toC()
93+
if source.Id != nil {
94+
dest.id = *source.Id.toC()
95+
}
9496
dest.path = C.CString(source.Path)
9597
}
9698

@@ -195,6 +197,28 @@ func (v *Index) AddByPath(path string) error {
195197
return nil
196198
}
197199

200+
// AddFromBuffer adds or replaces an index entry from a buffer in memory
201+
func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error {
202+
var centry C.git_index_entry
203+
204+
populateCIndexEntry(entry, &centry)
205+
defer freeCIndexEntry(&centry)
206+
207+
var cbuffer unsafe.Pointer
208+
if len(buffer) > 0 {
209+
cbuffer = unsafe.Pointer(&buffer[0])
210+
}
211+
212+
runtime.LockOSThread()
213+
defer runtime.UnlockOSThread()
214+
215+
if err := C.git_index_add_frombuffer(v.ptr, &centry, cbuffer, C.size_t(len(buffer))); err < 0 {
216+
return MakeGitError(err)
217+
}
218+
219+
return nil
220+
}
221+
198222
func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error {
199223
cpathspecs := C.git_strarray{}
200224
cpathspecs.count = C.size_t(len(pathspecs))

index_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ func TestIndexRemoveDirectory(t *testing.T) {
165165
}
166166
}
167167

168+
func TestIndexAddFromBuffer(t *testing.T) {
169+
t.Parallel()
170+
repo := createTestRepo(t)
171+
defer cleanupTestRepo(t, repo)
172+
173+
idx, err := repo.Index()
174+
checkFatal(t, err)
175+
176+
entry := IndexEntry{
177+
Path: "README",
178+
Mode: FilemodeBlob,
179+
}
180+
181+
err = idx.AddFromBuffer(&entry, []byte("foo\n"))
182+
checkFatal(t, err)
183+
184+
treeId, err := idx.WriteTreeTo(repo)
185+
checkFatal(t, err)
186+
187+
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
188+
t.Fatalf("%v", treeId.String())
189+
}
190+
}
191+
168192
func TestIndexAddAllNoCallback(t *testing.T) {
169193
t.Parallel()
170194
repo := createTestRepo(t)

0 commit comments

Comments
 (0)