Skip to content

Commit c18c869

Browse files
committed
Implement git_index_remove_directory in index wrapper
1 parent f720800 commit c18c869

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

index.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ func (v *Index) RemoveByPath(path string) error {
278278
return nil
279279
}
280280

281+
// RemoveDirectory removes all entries from the index under a given directory.
282+
func (v *Index) RemoveDirectory(dir string, stage int) error {
283+
cstr := C.CString(dir)
284+
defer C.free(unsafe.Pointer(cstr))
285+
286+
runtime.LockOSThread()
287+
defer runtime.UnlockOSThread()
288+
289+
ret := C.git_index_remove_directory(v.ptr, cstr, C.int(stage))
290+
if ret < 0 {
291+
return MakeGitError(ret)
292+
}
293+
294+
return nil
295+
}
296+
281297
func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
282298
oid := new(Oid)
283299

index_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,46 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) {
105105
}
106106
}
107107

108+
func TestIndexRemoveDirectory(t *testing.T) {
109+
repo := createTestRepo(t)
110+
defer cleanupTestRepo(t, repo)
111+
112+
odb, err := repo.Odb()
113+
checkFatal(t, err)
114+
115+
blobID, err := odb.Write([]byte("fou\n"), ObjectBlob)
116+
checkFatal(t, err)
117+
118+
idx, err := NewIndex()
119+
checkFatal(t, err)
120+
121+
entryCount := idx.EntryCount()
122+
if entryCount != 0 {
123+
t.Fatal("Index should count 0 entry")
124+
}
125+
126+
entry := IndexEntry{
127+
Path: "path/to/LISEZ_MOI",
128+
Id: blobID,
129+
Mode: FilemodeBlob,
130+
}
131+
132+
err = idx.Add(&entry)
133+
checkFatal(t, err)
134+
135+
entryCount = idx.EntryCount()
136+
if entryCount != 1 {
137+
t.Fatal("Index should count 1 entry")
138+
}
139+
140+
err = idx.RemoveDirectory("path", 0)
141+
142+
entryCount = idx.EntryCount()
143+
if entryCount != 0 {
144+
t.Fatal("Index should count 0 entry")
145+
}
146+
}
147+
108148
func TestIndexAddAllNoCallback(t *testing.T) {
109149
repo := createTestRepo(t)
110150
defer cleanupTestRepo(t, repo)

0 commit comments

Comments
 (0)