Merge pull request #400 from ramanenka/git_index_add_frombuffer
Add binding for `git_index_add_frombuffer`
(cherry picked from commit 06764f48dc
)
This commit is contained in:
parent
5acdcfaf86
commit
23e13acf73
26
index.go
26
index.go
|
@ -90,7 +90,9 @@ func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
|
||||||
dest.uid = C.uint32_t(source.Uid)
|
dest.uid = C.uint32_t(source.Uid)
|
||||||
dest.gid = C.uint32_t(source.Gid)
|
dest.gid = C.uint32_t(source.Gid)
|
||||||
dest.file_size = C.uint32_t(source.Size)
|
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)
|
dest.path = C.CString(source.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +197,28 @@ func (v *Index) AddByPath(path string) error {
|
||||||
return nil
|
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, ¢ry)
|
||||||
|
defer freeCIndexEntry(¢ry)
|
||||||
|
|
||||||
|
var cbuffer unsafe.Pointer
|
||||||
|
if len(buffer) > 0 {
|
||||||
|
cbuffer = unsafe.Pointer(&buffer[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
if err := C.git_index_add_frombuffer(v.ptr, ¢ry, cbuffer, C.size_t(len(buffer))); err < 0 {
|
||||||
|
return MakeGitError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error {
|
func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error {
|
||||||
cpathspecs := C.git_strarray{}
|
cpathspecs := C.git_strarray{}
|
||||||
cpathspecs.count = C.size_t(len(pathspecs))
|
cpathspecs.count = C.size_t(len(pathspecs))
|
||||||
|
|
|
@ -149,6 +149,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) {
|
func TestIndexAddAllNoCallback(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
repo := createTestRepo(t)
|
repo := createTestRepo(t)
|
||||||
|
|
Loading…
Reference in New Issue