Skip to content

[RFC] Index: add wrapper for git_index_remove_directory #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/index_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Iterate over all entries of the index::

Index write::

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

Custom entries::
>>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode)
Expand Down
1 change: 1 addition & 0 deletions pygit2/decl/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path);
int git_index_add_bypath(git_index *index, const char *path);
int git_index_add(git_index *index, const git_index_entry *source_entry);
int git_index_remove(git_index *index, const char *path, int stage);
int git_index_remove_directory(git_index *index, const char *path, int stage);
int git_index_read_tree(git_index *index, const git_tree *tree);
int git_index_clear(git_index *index);
int git_index_write_tree(git_oid *out, git_index *index);
Expand Down
5 changes: 5 additions & 0 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ def remove(self, path, level=0):
err = C.git_index_remove(self._index, to_bytes(path), level)
check_error(err, io=True)

def remove_directory(self, path, level=0):
"""Remove a directory from the Index."""
err = C.git_index_remove_directory(self._index, to_bytes(path), level)
check_error(err, io=True)

def remove_all(self, pathspecs):
"""Remove all index entries matching pathspecs."""
with StrArray(pathspecs) as arr:
Expand Down
14 changes: 14 additions & 0 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ def test_remove(testrepo):
assert 'hello.txt' not in index


def test_remove_directory(dirtyrepo):
index = dirtyrepo.index
assert 'subdir/current_file' in index
index.remove_directory('subdir')
assert 'subdir/current_file' not in index


def test_remove_all(testrepo):
index = testrepo.index
assert 'hello.txt' in index
Expand All @@ -208,6 +215,13 @@ def test_remove_aspath(testrepo):
assert 'hello.txt' not in index


def test_remove_directory_aspath(dirtyrepo):
index = dirtyrepo.index
assert 'subdir/current_file' in index
index.remove_directory(Path('subdir'))
assert 'subdir/current_file' not in index


def test_remove_all_aspath(testrepo):
index = testrepo.index
assert 'hello.txt' in index
Expand Down
Loading