diff options
author | Russell Belfer <rb@github.com> | 2012-11-14 02:02:59 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-11-15 10:55:40 +0400 |
commit | bad68c0a998116685ad75cab84210004dd2c5be1 (patch) | |
tree | d8b6db9be06ea1fcc150b5a7d2ba4cd635a174aa /src/iterator.c | |
parent | 5735bf5e6ab4da347b601d4b85c09c5c701dc002 (diff) |
Add iterator for git_index object
The index iterator could previously only be created from a repo
object, but this allows creating an iterator from a `git_index`
object instead (while keeping, though renaming, the old function).
Diffstat (limited to 'src/iterator.c')
-rw-r--r-- | src/iterator.c | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/src/iterator.c b/src/iterator.c index 33b775ce1..ee83a4fda 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -330,6 +330,7 @@ typedef struct { git_iterator base; git_index *index; unsigned int current; + bool free_index; } index_iterator; static int index_iterator__current( @@ -387,32 +388,47 @@ static int index_iterator__reset(git_iterator *self) static void index_iterator__free(git_iterator *self) { index_iterator *ii = (index_iterator *)self; - git_index_free(ii->index); + if (ii->free_index) + git_index_free(ii->index); ii->index = NULL; } int git_iterator_for_index_range( git_iterator **iter, - git_repository *repo, + git_index *index, const char *start, const char *end) { - int error; index_iterator *ii; ITERATOR_BASE_INIT(ii, index, INDEX); - if ((error = git_repository_index(&ii->index, repo)) < 0) - git__free(ii); - else { - ii->base.ignore_case = ii->index->ignore_case; - ii->current = start ? git_index__prefix_position(ii->index, start) : 0; - *iter = (git_iterator *)ii; - } + ii->index = index; + ii->base.ignore_case = ii->index->ignore_case; + ii->current = start ? git_index__prefix_position(ii->index, start) : 0; - return error; + *iter = (git_iterator *)ii; + + return 0; } +int git_iterator_for_repo_index_range( + git_iterator **iter, + git_repository *repo, + const char *start, + const char *end) +{ + int error; + git_index *index; + + if ((error = git_repository_index(&index, repo)) < 0) + return error; + + if (!(error = git_iterator_for_index_range(iter, index, start, end))) + ((index_iterator *)(*iter))->free_index = true; + + return error; +} typedef struct workdir_iterator_frame workdir_iterator_frame; struct workdir_iterator_frame { @@ -690,24 +706,21 @@ int git_iterator_for_workdir_range( assert(iter && repo); - if ((error = git_repository__ensure_not_bare(repo, "scan working directory")) < 0) + if ((error = git_repository__ensure_not_bare( + repo, "scan working directory")) < 0) return error; ITERATOR_BASE_INIT(wi, workdir, WORKDIR); - wi->repo = repo; - if ((error = git_repository_index(&index, repo)) < 0) { + if ((error = git_repository_index__weakptr(&index, repo)) < 0) { git__free(wi); return error; } - /* Set the ignore_case flag for the workdir iterator to match - * that of the index. */ + /* Match ignore_case flag for iterator to that of the index */ wi->base.ignore_case = index->ignore_case; - git_index_free(index); - if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 || git_path_to_dir(&wi->path) < 0 || git_ignore__for_path(repo, "", &wi->ignores) < 0) |