summaryrefslogtreecommitdiff
path: root/src/iterator.c
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2012-02-22 11:22:33 -0800
committerRussell Belfer <arrbee@arrbee.com>2012-02-22 11:22:33 -0800
commitda337c806468d2d8a27dfa9ee5e75e476f5ad546 (patch)
tree74a4d71539163c32ccc019eb0d5d8d6082f168af /src/iterator.c
parentb6c93aef4276051f9c4536ecbed48f4cd093bd1b (diff)
downloadlibgit2-da337c806468d2d8a27dfa9ee5e75e476f5ad546.tar.gz
Iterator improvements from diff implementation
This makes two changes to iterator behavior: first, advance can optionally do the work of returning the new current value. This is such a common pattern that it really cleans up usage. Second, for workdir iterators, this removes automatically iterating into directories. That seemed like a good idea, but when an entirely new directory hierarchy is introduced into the workdir, there is no reason to iterate into it if there are no corresponding entries in the tree/index that it is being compared to. This second change actually wasn't a lot of code because not descending into directories was already the behavior for ignored directories. This just extends that to all directories.
Diffstat (limited to 'src/iterator.c')
-rw-r--r--src/iterator.c63
1 files changed, 39 insertions, 24 deletions
diff --git a/src/iterator.c b/src/iterator.c
index 8511d53eb..805ff643e 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -80,7 +80,7 @@ static int expand_tree_if_needed(git_iterator_tree *ti)
te = git_tree_entry_byindex(tree, tree_idx);
if (!entry_is_tree(te))
- return GIT_SUCCESS;
+ break;
error = git_tree_lookup(&subtree, ti->repo, &te->oid);
if (error != GIT_SUCCESS)
@@ -98,13 +98,18 @@ static int expand_tree_if_needed(git_iterator_tree *ti)
return GIT_SUCCESS;
}
-static int git_iterator__tree_advance(git_iterator *self)
+static int git_iterator__tree_advance(
+ git_iterator *self, const git_index_entry **entry)
{
+ int error = GIT_SUCCESS;
git_iterator_tree *ti = (git_iterator_tree *)self;
git_tree *tree = git_vector_last(&ti->tree_stack);
unsigned int tree_idx = PTR_AS_IDX(git_vector_last(&ti->idx_stack));
const git_tree_entry *te = git_tree_entry_byindex(tree, tree_idx);
+ if (entry != NULL)
+ *entry = NULL;
+
if (te == NULL)
return GIT_SUCCESS;
@@ -133,9 +138,12 @@ static int git_iterator__tree_advance(git_iterator *self)
}
if (te && entry_is_tree(te))
- return expand_tree_if_needed(ti);
+ error = expand_tree_if_needed(ti);
- return GIT_SUCCESS;
+ if (error == GIT_SUCCESS && entry != NULL)
+ error = git_iterator__tree_current(self, entry);
+
+ return error;
}
static void git_iterator__tree_free(git_iterator *self)
@@ -204,11 +212,14 @@ static int git_iterator__index_at_end(git_iterator *self)
return (ii->current >= git_index_entrycount(ii->index));
}
-static int git_iterator__index_advance(git_iterator *self)
+static int git_iterator__index_advance(
+ git_iterator *self, const git_index_entry **entry)
{
git_iterator_index *ii = (git_iterator_index *)self;
if (ii->current < git_index_entrycount(ii->index))
ii->current++;
+ if (entry)
+ *entry = git_index_get(ii->index, ii->current);
return GIT_SUCCESS;
}
@@ -315,13 +326,18 @@ static int git_iterator__workdir_at_end(git_iterator *self)
return (wi->entry.path == NULL);
}
-static int git_iterator__workdir_advance(git_iterator *self)
+static int git_iterator__workdir_advance(
+ git_iterator *self, const git_index_entry **entry)
{
+ int error;
git_iterator_workdir *wi = (git_iterator_workdir *)self;
git_vector *dir;
unsigned int pos;
const char *next;
+ if (entry)
+ *entry = NULL;
+
if (wi->entry.path == NULL)
return GIT_SUCCESS;
@@ -348,26 +364,32 @@ static int git_iterator__workdir_advance(git_iterator *self)
git_ignore__pop_dir(&wi->ignores);
}
- return load_workdir_entry(wi);
+ error = load_workdir_entry(wi);
+
+ if (error == GIT_SUCCESS && entry)
+ return git_iterator__workdir_current(self, entry);
+
+ return error;
}
-int git_iterator_advance_into_ignored_directory(git_iterator *iter)
+int git_iterator_advance_into_directory(
+ git_iterator *iter, const git_index_entry **entry)
{
git_iterator_workdir *wi = (git_iterator_workdir *)iter;
if (iter->type != GIT_ITERATOR_WORKDIR)
- return GIT_SUCCESS;
+ return git_iterator_current(iter, entry);
/* Loop because the first entry in the ignored directory could itself be
* an ignored directory, but we want to descend to find an actual entry.
*/
- while (wi->entry.path && wi->is_ignored && S_ISDIR(wi->entry.mode)) {
- int error = push_directory(wi);
- if (error != GIT_SUCCESS)
- return error;
+ if (wi->entry.path && S_ISDIR(wi->entry.mode)) {
+ if (push_directory(wi) < GIT_SUCCESS)
+ /* If error loading or if empty, skip the directory. */
+ return git_iterator__workdir_advance((git_iterator *)wi, entry);
}
- return GIT_SUCCESS;
+ return git_iterator__workdir_current(iter, entry);
}
static void git_iterator__workdir_free(git_iterator *self)
@@ -404,7 +426,7 @@ static int load_workdir_entry(git_iterator_workdir *wi)
wi->entry.path = relpath;
if (strcmp(relpath, DOT_GIT) == 0)
- return git_iterator__workdir_advance((git_iterator *)wi);
+ return git_iterator__workdir_advance((git_iterator *)wi, NULL);
/* if there is an error processing the entry, treat as ignored */
wi->is_ignored = 1;
@@ -433,19 +455,12 @@ static int load_workdir_entry(git_iterator_workdir *wi)
if (git_path_contains(&wi->path, DOT_GIT) == GIT_SUCCESS) {
/* create submodule entry */
wi->entry.mode = S_IFGITLINK;
- } else if (wi->is_ignored) {
- /* create path entry (which is otherwise impossible), but is
- * needed in case descent into ignore dir is required.
- */
+ } else {
+ /* create directory entry that can be advanced into as needed */
size_t pathlen = strlen(wi->entry.path);
wi->entry.path[pathlen] = '/';
wi->entry.path[pathlen + 1] = '\0';
wi->entry.mode = S_IFDIR;
- } else if ((error = push_directory(wi)) < GIT_SUCCESS) {
- /* if there is an error loading the directory or if empty
- * then skip over the directory completely.
- */
- return git_iterator__workdir_advance((git_iterator *)wi);
}
}