Skip to content

Commit 588f48a

Browse files
committed
index - Add remove_directory
Add support for libgit2's `git_index_remove_directory` to pygit2's `Index` class.
1 parent eb876ee commit 588f48a

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

docs/index_file.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ Iterate over all entries of the index::
1818

1919
Index write::
2020

21-
>>> index.add('path/to/file') # git add
22-
>>> index.remove('path/to/file') # git rm
23-
>>> index.write() # don't forget to save the changes
21+
>>> index.add('path/to/file') # git add
22+
>>> index.remove('path/to/file') # git rm
23+
>>> index.remove_directory('path/to/directory/') # git rm -r
24+
>>> index.write() # don't forget to save the changes
2425

2526
Custom entries::
2627
>>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode)

pygit2/decl/index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path);
3434
int git_index_add_bypath(git_index *index, const char *path);
3535
int git_index_add(git_index *index, const git_index_entry *source_entry);
3636
int git_index_remove(git_index *index, const char *path, int stage);
37+
int git_index_remove_directory(git_index *index, const char *path, int stage);
3738
int git_index_read_tree(git_index *index, const git_tree *tree);
3839
int git_index_clear(git_index *index);
3940
int git_index_write_tree(git_oid *out, git_index *index);

pygit2/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ def remove(self, path, level=0):
177177
err = C.git_index_remove(self._index, to_bytes(path), level)
178178
check_error(err, io=True)
179179

180+
def remove_directory(self, path, level=0):
181+
"""Remove a directory from the Index."""
182+
err = C.git_index_remove_directory(self._index, to_bytes(path), level)
183+
check_error(err, io=True)
184+
180185
def remove_all(self, pathspecs):
181186
"""Remove all index entries matching pathspecs."""
182187
with StrArray(pathspecs) as arr:

test/test_index.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ def test_remove(testrepo):
192192
assert 'hello.txt' not in index
193193

194194

195+
def test_remove_directory(dirtyrepo):
196+
index = dirtyrepo.index
197+
assert 'subdir/current_file' in index
198+
index.remove_directory('subdir')
199+
assert 'subdir/current_file' not in index
200+
201+
195202
def test_remove_all(testrepo):
196203
index = testrepo.index
197204
assert 'hello.txt' in index
@@ -208,6 +215,13 @@ def test_remove_aspath(testrepo):
208215
assert 'hello.txt' not in index
209216

210217

218+
def test_remove_directory_aspath(dirtyrepo):
219+
index = dirtyrepo.index
220+
assert 'subdir/current_file' in index
221+
index.remove_directory(Path('subdir'))
222+
assert 'subdir/current_file' not in index
223+
224+
211225
def test_remove_all_aspath(testrepo):
212226
index = testrepo.index
213227
assert 'hello.txt' in index

0 commit comments

Comments
 (0)