Commit 82a1aab647c9a587e0b8959719a6ea507a68ea31

Edward Thomson 2016-03-18T12:59:35

iterator: move the index into the iterator itself

diff --git a/src/iterator.c b/src/iterator.c
index 88b7ed2..188f0cf 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1321,7 +1321,7 @@ static int is_submodule(
 		}
 	}
 
-	if (!is_submodule && iter->index) {
+	if (!is_submodule && iter->base.index) {
 		size_t pos;
 
 		error = git_index_snapshot_find(&pos,
@@ -2090,7 +2090,7 @@ static int iterator_for_filesystem(
 	if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0)
 		goto on_error;
 
-	if ((iter->index = index) != NULL &&
+	if ((iter->base.index = index) != NULL &&
 		(error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0)
 		goto on_error;
 
@@ -2156,7 +2156,6 @@ int git_iterator_for_workdir_ext(
 typedef struct {
 	git_iterator base;
 	git_iterator_callbacks cb;
-	git_index *index;
 	git_vector entries;
 	git_vector_cmp entry_srch;
 	size_t current;
@@ -2389,8 +2388,8 @@ static int index_iterator__reset_range(
 static void index_iterator__free(git_iterator *self)
 {
 	index_iterator *ii = (index_iterator *)self;
-	git_index_snapshot_release(&ii->entries, ii->index);
-	ii->index = NULL;
+	git_index_snapshot_release(&ii->entries, ii->base.index);
+	ii->base.index = NULL;
 	git_buf_free(&ii->partial);
 }
 
@@ -2408,7 +2407,7 @@ int git_iterator_for_index(
 		git__free(ii);
 		return error;
 	}
-	ii->index = index;
+	ii->base.index = index;
 
 	ITERATOR_BASE_INIT(ii, index, INDEX, repo);
 
@@ -2434,6 +2433,9 @@ int git_iterator_for_index(
 }
 
 
+/* Iterator API */
+
+
 void git_iterator_free(git_iterator *iter)
 {
 	if (iter == NULL)
@@ -2450,33 +2452,6 @@ void git_iterator_free(git_iterator *iter)
 	git__free(iter);
 }
 
-int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
-{
-	const git_index_entry *entry;
-
-	/* a "done" iterator is after every prefix */
-	if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
-		return 1;
-
-	/* a NULL prefix is after any valid iterator */
-	if (!path_prefix)
-		return -1;
-
-	return iter->prefixcomp(entry->path, path_prefix);
-}
-
-git_index *git_iterator_index(git_iterator *iter)
-{
-	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
-		return ((index_iterator *)iter)->index;
-
-	if (iter->type == GIT_ITERATOR_TYPE_FS ||
-		iter->type == GIT_ITERATOR_TYPE_WORKDIR)
-		return ((filesystem_iterator *)iter)->index;
-
-	return NULL;
-}
-
 int git_iterator_walk(
 	git_iterator **iterators,
 	size_t cnt,
diff --git a/src/iterator.h b/src/iterator.h
index 85444f1..460f947 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -77,7 +77,9 @@ typedef struct {
 struct git_iterator {
 	git_iterator_type_t type;
 	git_iterator_callbacks *cb;
+
 	git_repository *repo;
+	git_index *index;
 
 	char *start;
 	size_t start_len;
@@ -260,6 +262,11 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
 	return iter->repo;
 }
 
+GIT_INLINE(git_index *) git_iterator_index(git_iterator *iter)
+{
+	return iter->index;
+}
+
 GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
 {
 	return iter->flags;
@@ -282,9 +289,6 @@ extern bool git_iterator_current_is_ignored(git_iterator *iter);
 
 extern bool git_iterator_current_tree_is_ignored(git_iterator *iter);
 
-extern int git_iterator_cmp(
-	git_iterator *iter, const char *path_prefix);
-
 /**
  * Get full path of the current item from a workdir iterator.  This will
  * return NULL for a non-workdir iterator.  The git_buf is still owned by