diff options
| author | Russell Belfer <rb@github.com> | 2013-01-03 15:48:52 -0800 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2013-01-04 15:47:44 -0800 |
| commit | 7fc00435829d24021a477c6d6413f3d7b3e37e27 (patch) | |
| tree | c8d3e6895606afd3be8913c01d03feefcae0fdaf /src/index.c | |
| parent | 1b88faf7aea53a72a4906f48ec30f16f1c157503 (diff) | |
| download | libgit2-7fc00435829d24021a477c6d6413f3d7b3e37e27.tar.gz | |
Add index API to remove all files in a directory
This adds the git_index_remove_directory API plus tests.
Diffstat (limited to 'src/index.c')
| -rw-r--r-- | src/index.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/index.c b/src/index.c index 9f2012b3a..ce902c5ef 100644 --- a/src/index.c +++ b/src/index.c @@ -794,6 +794,44 @@ int git_index_remove(git_index *index, const char *path, int stage) return error; } +int git_index_remove_directory(git_index *index, const char *dir, int stage) +{ + git_buf pfx = GIT_BUF_INIT; + int error = 0; + size_t pos; + git_index_entry *entry; + + if (git_buf_sets(&pfx, dir) < 0 || git_path_to_dir(&pfx) < 0) + return -1; + + git_vector_sort(&index->entries); + + pos = git_index__prefix_position(index, pfx.ptr); + + while (1) { + entry = git_vector_get(&index->entries, pos); + if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) + break; + + if (index_entry_stage(entry) != stage) { + ++pos; + continue; + } + + git_tree_cache_invalidate_path(index->tree, entry->path); + + if ((error = git_vector_remove(&index->entries, pos)) < 0) + break; + index_entry_free(entry); + + /* removed entry at 'pos' so we don't need to increment it */ + } + + git_buf_free(&pfx); + + return error; +} + static int index_find(git_index *index, const char *path, int stage) { struct entry_srch_key srch_key; |
