]> git.proxmox.com Git - libgit2.git/blobdiff - src/iterator.c
Merge remote-tracking branch 'libgit2/development' into blame
[libgit2.git] / src / iterator.c
index 7061067037dc0b102bef5660eebcbaae97fc7ada..c0d7862ffb0ad377ccfdc54f3925a7c2635294d4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
@@ -7,28 +7,54 @@
 
 #include "iterator.h"
 #include "tree.h"
+#include "index.h"
 #include "ignore.h"
 #include "buffer.h"
 #include "git2/submodule.h"
 #include <ctype.h>
 
-#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \
-       (P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \
-       GITERR_CHECK_ALLOC(P); \
-       (P)->base.type    = GIT_ITERATOR_ ## NAME_UC; \
+#define ITERATOR_SET_CB(P,NAME_LC) do { \
+       (P)->cb.current = NAME_LC ## _iterator__current; \
+       (P)->cb.advance = NAME_LC ## _iterator__advance; \
+       (P)->cb.advance_into = NAME_LC ## _iterator__advance_into; \
+       (P)->cb.seek    = NAME_LC ## _iterator__seek; \
+       (P)->cb.reset   = NAME_LC ## _iterator__reset; \
+       (P)->cb.at_end  = NAME_LC ## _iterator__at_end; \
+       (P)->cb.free    = NAME_LC ## _iterator__free; \
+       } while (0)
+
+#define ITERATOR_CASE_FLAGS \
+       (GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_IGNORE_CASE)
+
+#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC,REPO) do { \
+       (P)->base.type    = GIT_ITERATOR_TYPE_ ## NAME_UC; \
+       (P)->base.cb      = &(P)->cb; \
+       ITERATOR_SET_CB(P,NAME_LC); \
+       (P)->base.repo    = (REPO); \
        (P)->base.start   = start ? git__strdup(start) : NULL; \
        (P)->base.end     = end ? git__strdup(end) : NULL; \
-       (P)->base.ignore_case = false; \
-       (P)->base.current = NAME_LC ## _iterator__current; \
-       (P)->base.at_end  = NAME_LC ## _iterator__at_end; \
-       (P)->base.advance = NAME_LC ## _iterator__advance; \
-       (P)->base.seek    = NAME_LC ## _iterator__seek; \
-       (P)->base.reset   = NAME_LC ## _iterator__reset; \
-       (P)->base.free    = NAME_LC ## _iterator__free; \
-       if ((start && !(P)->base.start) || (end && !(P)->base.end)) \
-               return -1; \
+       if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
+               git__free(P); return -1; } \
+       (P)->base.prefixcomp = git__prefixcmp; \
+       (P)->base.flags = flags & ~ITERATOR_CASE_FLAGS; \
+       if ((P)->base.flags & GIT_ITERATOR_DONT_AUTOEXPAND) \
+               (P)->base.flags |= GIT_ITERATOR_INCLUDE_TREES; \
        } while (0)
 
+#define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
+#define iterator__ignore_case(I)     iterator__flag(I,IGNORE_CASE)
+#define iterator__include_trees(I)   iterator__flag(I,INCLUDE_TREES)
+#define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
+#define iterator__do_autoexpand(I)   !iterator__flag(I,DONT_AUTOEXPAND)
+
+#define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
+#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
+
+#define iterator__end(I) ((git_iterator *)(I))->end
+#define iterator__past_end(I,PATH) \
+       (iterator__end(I) && ((git_iterator *)(I))->prefixcomp((PATH),iterator__end(I)) > 0)
+
+
 static int iterator__reset_range(
        git_iterator *iter, const char *start, const char *end)
 {
@@ -46,297 +72,539 @@ static int iterator__reset_range(
                GITERR_CHECK_ALLOC(iter->end);
        }
 
+       iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS;
+
        return 0;
 }
 
-static int empty_iterator__no_item(
-       git_iterator *iter, const git_index_entry **entry)
+static int iterator__update_ignore_case(
+       git_iterator *iter,
+       git_iterator_flag_t flags)
 {
-       GIT_UNUSED(iter);
-       *entry = NULL;
-       return 0;
+       int error = 0, ignore_case = -1;
+
+       if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
+               ignore_case = true;
+       else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0)
+               ignore_case = false;
+       else {
+               git_index *index;
+
+               if (!(error = git_repository_index__weakptr(&index, iter->repo)))
+                       ignore_case = (index->ignore_case != false);
+       }
+
+       if (ignore_case > 0)
+               iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
+       else if (ignore_case == 0)
+               iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
+
+       iter->prefixcomp = iterator__ignore_case(iter) ?
+               git__prefixcmp_icase : git__prefixcmp;
+
+       return error;
 }
 
-static int empty_iterator__at_end(git_iterator *iter)
+GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
 {
-       GIT_UNUSED(iter);
-       return 1;
+       if (entry) *entry = NULL;
 }
 
-static int empty_iterator__reset(
-       git_iterator *iter, const char *start, const char *end)
+
+static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
 {
-       GIT_UNUSED(iter); GIT_UNUSED(start); GIT_UNUSED(end);
-       return 0;
+       GIT_UNUSED(i);
+       iterator__clear_entry(e);
+       return GIT_ITEROVER;
 }
 
-static int empty_iterator__seek(git_iterator *iter, const char *prefix)
+static int empty_iterator__seek(git_iterator *i, const char *p)
 {
-       GIT_UNUSED(iter); GIT_UNUSED(prefix);
+       GIT_UNUSED(i); GIT_UNUSED(p);
        return -1;
 }
 
-static void empty_iterator__free(git_iterator *iter)
+static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
 {
-       GIT_UNUSED(iter);
+       GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
+       return 0;
 }
 
-int git_iterator_for_nothing(git_iterator **iter)
+static int empty_iterator__at_end(git_iterator *i)
+{
+       GIT_UNUSED(i);
+       return 1;
+}
+
+static void empty_iterator__free(git_iterator *i)
+{
+       GIT_UNUSED(i);
+}
+
+typedef struct {
+       git_iterator base;
+       git_iterator_callbacks cb;
+} empty_iterator;
+
+int git_iterator_for_nothing(
+       git_iterator **iter,
+       git_iterator_flag_t flags,
+       const char *start,
+       const char *end)
 {
-       git_iterator *i = git__calloc(1, sizeof(git_iterator));
+       empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
        GITERR_CHECK_ALLOC(i);
 
-       i->type    = GIT_ITERATOR_EMPTY;
-       i->current = empty_iterator__no_item;
-       i->at_end  = empty_iterator__at_end;
-       i->advance = empty_iterator__no_item;
-       i->seek    = empty_iterator__seek;
-       i->reset   = empty_iterator__reset;
-       i->free    = empty_iterator__free;
+#define empty_iterator__current empty_iterator__noop
+#define empty_iterator__advance empty_iterator__noop
+#define empty_iterator__advance_into empty_iterator__noop
+
+       ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
 
-       *iter = i;
+       if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
+               i->base.flags |= GIT_ITERATOR_IGNORE_CASE;
 
+       *iter = (git_iterator *)i;
        return 0;
 }
 
 
+typedef struct tree_iterator_entry tree_iterator_entry;
+struct tree_iterator_entry {
+       tree_iterator_entry *parent;
+       const git_tree_entry *te;
+       git_tree *tree;
+};
+
 typedef struct tree_iterator_frame tree_iterator_frame;
 struct tree_iterator_frame {
-       tree_iterator_frame *next, *prev;
-       git_tree *tree;
-       char *start;
-       size_t index;
+       tree_iterator_frame *up, *down;
+
+       size_t n_entries; /* items in this frame */
+       size_t current;   /* start of currently active range in frame */
+       size_t next;      /* start of next range in frame */
+
+       const char *start;
+       size_t startlen;
+
+       tree_iterator_entry *entries[GIT_FLEX_ARRAY];
 };
 
 typedef struct {
        git_iterator base;
-       tree_iterator_frame *stack, *tail;
+       git_iterator_callbacks cb;
+       tree_iterator_frame *head, *root;
+       git_pool pool;
        git_index_entry entry;
        git_buf path;
+       int path_ambiguities;
        bool path_has_filename;
+       bool entry_is_current;
+       int (*strncomp)(const char *a, const char *b, size_t sz);
 } tree_iterator;
 
-GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti)
-{
-       return git_tree_entry_byindex(ti->stack->tree, ti->stack->index);
-}
-
 static char *tree_iterator__current_filename(
        tree_iterator *ti, const git_tree_entry *te)
 {
        if (!ti->path_has_filename) {
                if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
                        return NULL;
+
+               if (git_tree_entry__is_tree(te) && git_buf_putc(&ti->path, '/') < 0)
+                       return NULL;
+
                ti->path_has_filename = true;
        }
 
        return ti->path.ptr;
 }
 
-static void tree_iterator__free_frame(tree_iterator_frame *tf)
+static void tree_iterator__rewrite_filename(tree_iterator *ti)
 {
-       if (!tf)
-               return;
-       git_tree_free(tf->tree);
-       git__free(tf);
+       tree_iterator_entry *scan = ti->head->entries[ti->head->current];
+       ssize_t strpos = ti->path.size;
+       const git_tree_entry *te;
+
+       if (strpos && ti->path.ptr[strpos - 1] == '/')
+               strpos--;
+
+       for (; scan && (te = scan->te); scan = scan->parent) {
+               strpos -= te->filename_len;
+               memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
+               strpos -= 1; /* separator */
+       }
 }
 
-static bool tree_iterator__pop_frame(tree_iterator *ti)
+static int tree_iterator__te_cmp(
+       const git_tree_entry *a,
+       const git_tree_entry *b,
+       int (*compare)(const char *, const char *, size_t))
 {
-       tree_iterator_frame *tf = ti->stack;
+       return git_path_cmp(
+               a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
+               b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
+               compare);
+}
 
-       /* don't free the initial tree/frame */
-       if (!tf->next)
-               return false;
+static int tree_iterator__ci_cmp(const void *a, const void *b, void *p)
+{
+       const tree_iterator_entry *ae = a, *be = b;
+       int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp);
+
+       if (!cmp) {
+               /* stabilize sort order among equivalent names */
+               if (!ae->parent->te || !be->parent->te)
+                       cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncmp);
+               else
+                       cmp = tree_iterator__ci_cmp(ae->parent, be->parent, p);
+       }
 
-       ti->stack = tf->next;
-       ti->stack->prev = NULL;
+       return cmp;
+}
 
-       tree_iterator__free_frame(tf);
+static int tree_iterator__search_cmp(const void *key, const void *val, void *p)
+{
+       const tree_iterator_frame *tf = key;
+       const git_tree_entry *te = ((tree_iterator_entry *)val)->te;
 
-       return true;
+       return git_path_cmp(
+               tf->start, tf->startlen, false,
+               te->filename, te->filename_len, te->attr == GIT_FILEMODE_TREE,
+               ((tree_iterator *)p)->strncomp);
 }
 
-static int tree_iterator__to_end(tree_iterator *ti)
+static bool tree_iterator__move_to_next(
+       tree_iterator *ti, tree_iterator_frame *tf)
 {
-       while (tree_iterator__pop_frame(ti)) /* pop all */;
-       ti->stack->index = git_tree_entrycount(ti->stack->tree);
-       return 0;
+       if (tf->next > tf->current + 1)
+               ti->path_ambiguities--;
+
+       if (!tf->up) { /* at root */
+               tf->current = tf->next;
+               return false;
+       }
+
+       for (; tf->current < tf->next; tf->current++) {
+               git_tree_free(tf->entries[tf->current]->tree);
+               tf->entries[tf->current]->tree = NULL;
+       }
+
+       return (tf->current < tf->n_entries);
 }
 
-static int tree_iterator__current(
-       git_iterator *self, const git_index_entry **entry)
+static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
 {
-       tree_iterator *ti = (tree_iterator *)self;
-       const git_tree_entry *te = tree_iterator__tree_entry(ti);
+       int error = 0;
+       const git_tree_entry *te, *last = NULL;
 
-       if (entry)
-               *entry = NULL;
+       tf->next = tf->current;
 
-       if (te == NULL)
-               return 0;
+       for (; tf->next < tf->n_entries; tf->next++, last = te) {
+               te = tf->entries[tf->next]->te;
 
-       ti->entry.mode = te->attr;
-       git_oid_cpy(&ti->entry.oid, &te->oid);
+               if (last && tree_iterator__te_cmp(last, te, ti->strncomp))
+                       break;
 
-       ti->entry.path = tree_iterator__current_filename(ti, te);
-       if (ti->entry.path == NULL)
-               return -1;
+               /* try to load trees for items in [current,next) range */
+               if (!error && git_tree_entry__is_tree(te))
+                       error = git_tree_lookup(
+                               &tf->entries[tf->next]->tree, ti->base.repo, &te->oid);
+       }
 
-       if (ti->base.end && git__prefixcmp(ti->entry.path, ti->base.end) > 0)
-               return tree_iterator__to_end(ti);
+       if (tf->next > tf->current + 1)
+               ti->path_ambiguities++;
 
-       if (entry)
-               *entry = &ti->entry;
+       /* if a tree lookup failed, advance over this span and return failure */
+       if (error < 0) {
+               tree_iterator__move_to_next(ti, tf);
+               return error;
+       }
+
+       if (last && !tree_iterator__current_filename(ti, last))
+               return -1; /* must have been allocation failure */
 
        return 0;
 }
 
-static int tree_iterator__at_end(git_iterator *self)
+GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
 {
-       return (tree_iterator__tree_entry((tree_iterator *)self) == NULL);
+       return (ti->head->current < ti->head->n_entries &&
+                       ti->head->entries[ti->head->current]->tree != NULL);
 }
 
-static tree_iterator_frame *tree_iterator__alloc_frame(
-       git_tree *tree, char *start)
+static int tree_iterator__push_frame(tree_iterator *ti)
 {
-       tree_iterator_frame *tf = git__calloc(1, sizeof(tree_iterator_frame));
-       if (!tf)
-               return NULL;
+       int error = 0;
+       tree_iterator_frame *head = ti->head, *tf = NULL;
+       size_t i, n_entries = 0;
+
+       if (head->current >= head->n_entries || !head->entries[head->current]->tree)
+               return GIT_ITEROVER;
+
+       for (i = head->current; i < head->next; ++i)
+               n_entries += git_tree_entrycount(head->entries[i]->tree);
+
+       tf = git__calloc(sizeof(tree_iterator_frame) +
+               n_entries * sizeof(tree_iterator_entry *), 1);
+       GITERR_CHECK_ALLOC(tf);
+
+       tf->n_entries = n_entries;
 
-       tf->tree = tree;
+       tf->up     = head;
+       head->down = tf;
+       ti->head   = tf;
 
-       if (start && *start) {
-               tf->start = start;
-               tf->index = git_tree__prefix_position(tree, start);
+       for (i = head->current, n_entries = 0; i < head->next; ++i) {
+               git_tree *tree = head->entries[i]->tree;
+               size_t j, max_j = git_tree_entrycount(tree);
+
+               for (j = 0; j < max_j; ++j) {
+                       tree_iterator_entry *entry = git_pool_malloc(&ti->pool, 1);
+                       GITERR_CHECK_ALLOC(entry);
+
+                       entry->parent = head->entries[i];
+                       entry->te     = git_tree_entry_byindex(tree, j);
+                       entry->tree   = NULL;
+
+                       tf->entries[n_entries++] = entry;
+               }
        }
 
-       return tf;
+       /* if ignore_case, sort entries case insensitively */
+       if (iterator__ignore_case(ti))
+               git__tsort_r(
+                       (void **)tf->entries, tf->n_entries, tree_iterator__ci_cmp, tf);
+
+       /* pick tf->current based on "start" (or start at zero) */
+       if (head->startlen > 0) {
+               git__bsearch_r((void **)tf->entries, tf->n_entries, head,
+                       tree_iterator__search_cmp, ti, &tf->current);
+
+               while (tf->current &&
+                          !tree_iterator__search_cmp(head, tf->entries[tf->current-1], ti))
+                       tf->current--;
+
+               if ((tf->start = strchr(head->start, '/')) != NULL) {
+                       tf->start++;
+                       tf->startlen = strlen(tf->start);
+               }
+       }
+
+       ti->path_has_filename = ti->entry_is_current = false;
+
+       if ((error = tree_iterator__set_next(ti, tf)) < 0)
+               return error;
+
+       /* autoexpand as needed */
+       if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
+               return tree_iterator__push_frame(ti);
+
+       return 0;
 }
 
-static int tree_iterator__expand_tree(tree_iterator *ti)
+static bool tree_iterator__pop_frame(tree_iterator *ti, bool final)
 {
-       int error;
-       git_tree *subtree;
-       const git_tree_entry *te = tree_iterator__tree_entry(ti);
-       tree_iterator_frame *tf;
-       char *relpath;
+       tree_iterator_frame *tf = ti->head;
 
-       while (te != NULL && git_tree_entry__is_tree(te)) {
-               if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
-                       return -1;
+       if (!tf->up)
+               return false;
 
-               /* check that we have not passed the range end */
-               if (ti->base.end != NULL &&
-                       git__prefixcmp(ti->path.ptr, ti->base.end) > 0)
-                       return tree_iterator__to_end(ti);
+       ti->head = tf->up;
+       ti->head->down = NULL;
 
-               if ((error = git_tree_lookup(&subtree, ti->base.repo, &te->oid)) < 0)
-                       return error;
+       tree_iterator__move_to_next(ti, tf);
 
-               relpath = NULL;
+       if (!final) { /* if final, don't bother to clean up */
+               git_pool_free_array(&ti->pool, tf->n_entries, (void **)tf->entries);
+               git_buf_rtruncate_at_char(&ti->path, '/');
+       }
 
-               /* apply range start to new frame if relevant */
-               if (ti->stack->start &&
-                       git__prefixcmp(ti->stack->start, te->filename) == 0)
-               {
-                       size_t namelen = strlen(te->filename);
-                       if (ti->stack->start[namelen] == '/')
-                               relpath = ti->stack->start + namelen + 1;
-               }
+       git__free(tf);
 
-               if ((tf = tree_iterator__alloc_frame(subtree, relpath)) == NULL)
-                       return -1;
+       return true;
+}
+
+static void tree_iterator__pop_all(tree_iterator *ti, bool to_end, bool final)
+{
+       while (tree_iterator__pop_frame(ti, final)) /* pop to root */;
 
-               tf->next  = ti->stack;
-               ti->stack = tf;
-               tf->next->prev = tf;
+       if (!final) {
+               ti->head->current = to_end ? ti->head->n_entries : 0;
+               ti->path_ambiguities = 0;
+               git_buf_clear(&ti->path);
+       }
+}
+
+static int tree_iterator__update_entry(tree_iterator *ti)
+{
+       tree_iterator_frame *tf;
+    const git_tree_entry *te;
+
+       if (ti->entry_is_current)
+        return 0;
+
+       tf = ti->head;
+    te = tf->entries[tf->current]->te;
+
+       ti->entry.mode = te->attr;
+       git_oid_cpy(&ti->entry.oid, &te->oid);
+
+       ti->entry.path = tree_iterator__current_filename(ti, te);
+       GITERR_CHECK_ALLOC(ti->entry.path);
 
-               te = tree_iterator__tree_entry(ti);
+       if (ti->path_ambiguities > 0)
+               tree_iterator__rewrite_filename(ti);
+
+       if (iterator__past_end(ti, ti->entry.path)) {
+               tree_iterator__pop_all(ti, true, false);
+               return GIT_ITEROVER;
        }
 
+       ti->entry_is_current = true;
+
        return 0;
 }
 
-static int tree_iterator__advance(
-       git_iterator *self, const git_index_entry **entry)
+static int tree_iterator__current(
+       const git_index_entry **entry, git_iterator *self)
+{
+       int error;
+       tree_iterator *ti = (tree_iterator *)self;
+       tree_iterator_frame *tf = ti->head;
+
+       iterator__clear_entry(entry);
+
+       if (tf->current >= tf->n_entries)
+               return GIT_ITEROVER;
+
+       if ((error = tree_iterator__update_entry(ti)) < 0)
+               return error;
+
+       if (entry)
+               *entry = &ti->entry;
+
+       ti->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+
+       return 0;
+}
+
+static int tree_iterator__advance_into(
+       const git_index_entry **entry, git_iterator *self)
 {
        int error = 0;
        tree_iterator *ti = (tree_iterator *)self;
-       const git_tree_entry *te = NULL;
 
-       if (entry != NULL)
-               *entry = NULL;
+       iterator__clear_entry(entry);
 
-       if (ti->path_has_filename) {
-               git_buf_rtruncate_at_char(&ti->path, '/');
-               ti->path_has_filename = false;
-       }
+       if (tree_iterator__at_tree(ti))
+               error = tree_iterator__push_frame(ti);
 
-       while (1) {
-               te = git_tree_entry_byindex(ti->stack->tree, ++ti->stack->index);
-               if (te != NULL)
-                       break;
+       if (!error && entry)
+               error = tree_iterator__current(entry, self);
+
+       return error;
+}
+
+static int tree_iterator__advance(
+       const git_index_entry **entry, git_iterator *self)
+{
+       int error;
+       tree_iterator *ti = (tree_iterator *)self;
+       tree_iterator_frame *tf = ti->head;
+
+       iterator__clear_entry(entry);
+
+       if (tf->current >= tf->n_entries)
+               return GIT_ITEROVER;
 
-               if (!tree_iterator__pop_frame(ti))
-                       break; /* no frames left to pop */
+       if (!iterator__has_been_accessed(ti))
+               return tree_iterator__current(entry, self);
 
+       if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
+               tree_iterator__at_tree(ti))
+               return tree_iterator__advance_into(entry, self);
+
+       if (ti->path_has_filename) {
                git_buf_rtruncate_at_char(&ti->path, '/');
+               ti->path_has_filename = ti->entry_is_current = false;
        }
 
-       if (te && git_tree_entry__is_tree(te))
-               error = tree_iterator__expand_tree(ti);
+       /* scan forward and up, advancing in frame or popping frame when done */
+       while (!tree_iterator__move_to_next(ti, tf) &&
+                  tree_iterator__pop_frame(ti, false))
+               tf = ti->head;
 
-       if (!error)
-               error = tree_iterator__current(self, entry);
+       /* find next and load trees */
+       if ((error = tree_iterator__set_next(ti, tf)) < 0)
+               return error;
 
-       return error;
+       /* deal with include_trees / auto_expand as needed */
+       if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
+               return tree_iterator__advance_into(entry, self);
+
+       return tree_iterator__current(entry, self);
 }
 
 static int tree_iterator__seek(git_iterator *self, const char *prefix)
 {
-       GIT_UNUSED(self);
-       GIT_UNUSED(prefix);
-       /* pop stack until matches prefix */
-       /* seek item in current frame matching prefix */
-       /* push stack which matches prefix */
+       GIT_UNUSED(self); GIT_UNUSED(prefix);
        return -1;
 }
 
-static void tree_iterator__free(git_iterator *self)
+static int tree_iterator__reset(
+       git_iterator *self, const char *start, const char *end)
 {
        tree_iterator *ti = (tree_iterator *)self;
 
-       while (tree_iterator__pop_frame(ti)) /* pop all */;
+       tree_iterator__pop_all(ti, false, false);
 
-       tree_iterator__free_frame(ti->stack);
-       ti->stack = ti->tail = NULL;
+       if (iterator__reset_range(self, start, end) < 0)
+               return -1;
 
-       git_buf_free(&ti->path);
+       return tree_iterator__push_frame(ti); /* re-expand root tree */
 }
 
-static int tree_iterator__reset(
-       git_iterator *self, const char *start, const char *end)
+static int tree_iterator__at_end(git_iterator *self)
+{
+       tree_iterator *ti = (tree_iterator *)self;
+       return (ti->head->current >= ti->head->n_entries);
+}
+
+static void tree_iterator__free(git_iterator *self)
 {
        tree_iterator *ti = (tree_iterator *)self;
 
-       while (tree_iterator__pop_frame(ti)) /* pop all */;
+       tree_iterator__pop_all(ti, true, false);
 
-       if (iterator__reset_range(self, start, end) < 0)
-               return -1;
+       git_tree_free(ti->head->entries[0]->tree);
+       git__free(ti->head);
+       git_pool_clear(&ti->pool);
+       git_buf_free(&ti->path);
+}
+
+static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree)
+{
+       size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
+       tree_iterator_frame *root = git__calloc(sz, sizeof(char));
+       GITERR_CHECK_ALLOC(root);
 
-       ti->stack->index =
-               git_tree__prefix_position(ti->stack->tree, ti->base.start);
+       root->n_entries  = 1;
+       root->next       = 1;
+       root->start      = ti->base.start;
+       root->startlen   = root->start ? strlen(root->start) : 0;
+       root->entries[0] = git_pool_mallocz(&ti->pool, 1);
+       GITERR_CHECK_ALLOC(root->entries[0]);
+       root->entries[0]->tree = tree;
 
-       git_buf_clear(&ti->path);
-       ti->path_has_filename = false;
+       ti->head = ti->root = root;
 
-       return tree_iterator__expand_tree(ti);
+       return 0;
 }
 
-int git_iterator_for_tree_range(
+int git_iterator_for_tree(
        git_iterator **iter,
        git_tree *tree,
+       git_iterator_flag_t flags,
        const char *start,
        const char *end)
 {
@@ -344,42 +612,133 @@ int git_iterator_for_tree_range(
        tree_iterator *ti;
 
        if (tree == NULL)
-               return git_iterator_for_nothing(iter);
+               return git_iterator_for_nothing(iter, flags, start, end);
 
-       if ((error = git_tree__dup(&tree, tree)) < 0)
+       if ((error = git_object_dup((git_object **)&tree, (git_object *)tree)) < 0)
                return error;
 
-       ITERATOR_BASE_INIT(ti, tree, TREE);
+       ti = git__calloc(1, sizeof(tree_iterator));
+       GITERR_CHECK_ALLOC(ti);
 
-       ti->base.repo = git_tree_owner(tree);
-       ti->stack = ti->tail = tree_iterator__alloc_frame(tree, ti->base.start);
+       ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
 
-       if ((error = tree_iterator__expand_tree(ti)) < 0)
-               git_iterator_free((git_iterator *)ti);
-       else
-               *iter = (git_iterator *)ti;
+       if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
+               goto fail;
+       ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
 
+       if ((error = git_pool_init(&ti->pool, sizeof(tree_iterator_entry),0)) < 0 ||
+               (error = tree_iterator__create_root_frame(ti, tree)) < 0 ||
+               (error = tree_iterator__push_frame(ti)) < 0) /* expand root now */
+               goto fail;
+
+       *iter = (git_iterator *)ti;
+       return 0;
+
+fail:
+       git_iterator_free((git_iterator *)ti);
        return error;
 }
 
 
 typedef struct {
        git_iterator base;
+       git_iterator_callbacks cb;
        git_index *index;
        size_t current;
-       bool free_index;
+       /* when not in autoexpand mode, use these to represent "tree" state */
+       git_buf partial;
+       size_t partial_pos;
+       char restore_terminator;
+       git_index_entry tree_entry;
 } index_iterator;
 
+static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
+{
+       const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
+
+       if (ie != NULL && iterator__past_end(ii, ie->path)) {
+               ii->current = git_index_entrycount(ii->index);
+               ie = NULL;
+       }
+
+       return ie;
+}
+
+static const git_index_entry *index_iterator__skip_conflicts(index_iterator *ii)
+{
+       const git_index_entry *ie;
+
+       while ((ie = index_iterator__index_entry(ii)) != NULL &&
+                  git_index_entry_stage(ie) != 0)
+               ii->current++;
+
+       return ie;
+}
+
+static void index_iterator__next_prefix_tree(index_iterator *ii)
+{
+       const char *slash;
+
+       if (!iterator__include_trees(ii))
+               return;
+
+       slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');
+
+       if (slash != NULL) {
+               ii->partial_pos = (slash - ii->partial.ptr) + 1;
+               ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
+               ii->partial.ptr[ii->partial_pos] = '\0';
+       } else {
+               ii->partial_pos = ii->partial.size;
+       }
+
+       if (index_iterator__index_entry(ii) == NULL)
+               ii->partial_pos = ii->partial.size;
+}
+
+static int index_iterator__first_prefix_tree(index_iterator *ii)
+{
+       const git_index_entry *ie = index_iterator__skip_conflicts(ii);
+       const char *scan, *prior, *slash;
+
+       if (!ie || !iterator__include_trees(ii))
+               return 0;
+
+       /* find longest common prefix with prior index entry */
+       for (scan = slash = ie->path, prior = ii->partial.ptr;
+                *scan && *scan == *prior; ++scan, ++prior)
+               if (*scan == '/')
+                       slash = scan;
+
+       if (git_buf_sets(&ii->partial, ie->path) < 0)
+               return -1;
+
+       ii->partial_pos = (slash - ie->path) + 1;
+       index_iterator__next_prefix_tree(ii);
+
+       return 0;
+}
+
+#define index_iterator__at_tree(I) \
+       (iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)
+
 static int index_iterator__current(
-       git_iterator *self, const git_index_entry **entry)
+       const git_index_entry **entry, git_iterator *self)
 {
        index_iterator *ii = (index_iterator *)self;
        const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
 
+       if (ie != NULL && index_iterator__at_tree(ii)) {
+               ii->tree_entry.path = ii->partial.ptr;
+               ie = &ii->tree_entry;
+       }
+
        if (entry)
                *entry = ie;
 
-       return 0;
+       ii->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+
+       return (ie != NULL) ? 0 : GIT_ITEROVER;
 }
 
 static int index_iterator__at_end(git_iterator *self)
@@ -388,47 +747,62 @@ static int index_iterator__at_end(git_iterator *self)
        return (ii->current >= git_index_entrycount(ii->index));
 }
 
-static void index_iterator__skip_conflicts(
-       index_iterator *ii)
+static int index_iterator__advance(
+       const git_index_entry **entry, git_iterator *self)
 {
+       index_iterator *ii = (index_iterator *)self;
        size_t entrycount = git_index_entrycount(ii->index);
        const git_index_entry *ie;
 
-       while (ii->current < entrycount) {
-               ie = git_index_get_byindex(ii->index, ii->current);
-
-               if (ie == NULL ||
-                       (ii->base.end != NULL &&
-                       ITERATOR_PREFIXCMP(ii->base, ie->path, ii->base.end) > 0)) {
-                       ii->current = entrycount;
-                       break;
+       if (!iterator__has_been_accessed(ii))
+               return index_iterator__current(entry, self);
+
+       if (index_iterator__at_tree(ii)) {
+               if (iterator__do_autoexpand(ii)) {
+                       ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
+                       index_iterator__next_prefix_tree(ii);
+               } else {
+                       /* advance to sibling tree (i.e. find entry with new prefix) */
+                       while (ii->current < entrycount) {
+                               ii->current++;
+
+                               if (!(ie = git_index_get_byindex(ii->index, ii->current)) ||
+                                       ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
+                                       break;
+                       }
+
+                       if (index_iterator__first_prefix_tree(ii) < 0)
+                               return -1;
                }
+       } else {
+               if (ii->current < entrycount)
+                       ii->current++;
 
-               if (git_index_entry_stage(ie) == 0)
-                       break;
-
-               ii->current++;
+               if (index_iterator__first_prefix_tree(ii) < 0)
+                       return -1;
        }
+
+       return index_iterator__current(entry, self);
 }
 
-static int index_iterator__advance(
-       git_iterator *self, const git_index_entry **entry)
+static int index_iterator__advance_into(
+       const git_index_entry **entry, git_iterator *self)
 {
        index_iterator *ii = (index_iterator *)self;
+       const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
 
-       if (ii->current < git_index_entrycount(ii->index))
-               ii->current++;
-
-       index_iterator__skip_conflicts(ii);
+       if (ie != NULL && index_iterator__at_tree(ii)) {
+               if (ii->restore_terminator)
+                       ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
+               index_iterator__next_prefix_tree(ii);
+       }
 
-       return index_iterator__current(self, entry);
+       return index_iterator__current(entry, self);
 }
 
 static int index_iterator__seek(git_iterator *self, const char *prefix)
 {
-       GIT_UNUSED(self);
-       GIT_UNUSED(prefix);
-       /* find last item before prefix */
+       GIT_UNUSED(self); GIT_UNUSED(prefix);
        return -1;
 }
 
@@ -436,35 +810,65 @@ static int index_iterator__reset(
        git_iterator *self, const char *start, const char *end)
 {
        index_iterator *ii = (index_iterator *)self;
+       const git_index_entry *ie;
+
        if (iterator__reset_range(self, start, end) < 0)
                return -1;
+
        ii->current = ii->base.start ?
                git_index__prefix_position(ii->index, ii->base.start) : 0;
-       index_iterator__skip_conflicts(ii);
+
+       if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
+               return 0;
+
+       if (git_buf_sets(&ii->partial, ie->path) < 0)
+               return -1;
+
+       ii->partial_pos = 0;
+
+       if (ii->base.start) {
+               size_t startlen = strlen(ii->base.start);
+
+               ii->partial_pos = (startlen > ii->partial.size) ?
+                       ii->partial.size : startlen;
+       }
+
+       index_iterator__next_prefix_tree(ii);
+
        return 0;
 }
 
 static void index_iterator__free(git_iterator *self)
 {
        index_iterator *ii = (index_iterator *)self;
-       if (ii->free_index)
-               git_index_free(ii->index);
+       git_index_free(ii->index);
        ii->index = NULL;
+
+       git_buf_free(&ii->partial);
 }
 
-int git_iterator_for_index_range(
+int git_iterator_for_index(
        git_iterator **iter,
        git_index  *index,
+       git_iterator_flag_t flags,
        const char *start,
        const char *end)
 {
-       index_iterator *ii;
+       index_iterator *ii = git__calloc(1, sizeof(index_iterator));
+       GITERR_CHECK_ALLOC(ii);
+
+       ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
 
-       ITERATOR_BASE_INIT(ii, index, INDEX);
+       if (index->ignore_case) {
+               ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
+               ii->base.prefixcomp = git__prefixcmp_icase;
+       }
 
        ii->index = index;
-       ii->base.repo = git_index_owner(index);
-       ii->base.ignore_case = ii->index->ignore_case;
+       GIT_REFCOUNT_INC(index);
+
+       git_buf_init(&ii->partial, 0);
+       ii->tree_entry.mode = GIT_FILEMODE_TREE;
 
        index_iterator__reset((git_iterator *)ii, NULL, NULL);
 
@@ -473,210 +877,242 @@ int git_iterator_for_index_range(
        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 {
-       workdir_iterator_frame *next;
+typedef struct fs_iterator_frame fs_iterator_frame;
+struct fs_iterator_frame {
+       fs_iterator_frame *next;
        git_vector entries;
        size_t index;
 };
 
-typedef struct {
+typedef struct fs_iterator fs_iterator;
+struct fs_iterator {
        git_iterator base;
-       workdir_iterator_frame *stack;
-       int (*entrycmp)(const void *pfx, const void *item);
-       git_ignores ignores;
+       git_iterator_callbacks cb;
+       fs_iterator_frame *stack;
        git_index_entry entry;
        git_buf path;
        size_t root_len;
-       int is_ignored;
-} workdir_iterator;
+       uint32_t dirload_flags;
+       int depth;
 
-GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps)
-{
-       if (!ps)
-               return false;
-       else {
-               const char *path = ps->path;
-               size_t len  = ps->path_len;
-
-               if (len < 4)
-                       return false;
-               if (path[len - 1] == '/')
-                       len--;
-               if (tolower(path[len - 1]) != 't' ||
-                       tolower(path[len - 2]) != 'i' ||
-                       tolower(path[len - 3]) != 'g' ||
-                       tolower(path[len - 4]) != '.')
-                       return false;
-               return (len == 4 || path[len - 5] == '/');
-       }
-}
+       int (*enter_dir_cb)(fs_iterator *self);
+       int (*leave_dir_cb)(fs_iterator *self);
+       int (*update_entry_cb)(fs_iterator *self);
+};
+
+#define FS_MAX_DEPTH 100
 
-static workdir_iterator_frame *workdir_iterator__alloc_frame(
-       workdir_iterator *wi)
+static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
 {
-       workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
-       git_vector_cmp entry_compare = CASESELECT(wi->base.ignore_case,
+       fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame));
+       git_vector_cmp entry_compare = CASESELECT(
+               iterator__ignore_case(fi),
                git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
 
-       if (wf == NULL)
-               return NULL;
-
-       if (git_vector_init(&wf->entries, 0, entry_compare) != 0) {
-               git__free(wf);
-               return NULL;
+       if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) {
+               git__free(ff);
+               ff = NULL;
        }
 
-       return wf;
+       return ff;
 }
 
-static void workdir_iterator__free_frame(workdir_iterator_frame *wf)
+static void fs_iterator__free_frame(fs_iterator_frame *ff)
 {
-       unsigned int i;
+       size_t i;
        git_path_with_stat *path;
 
-       git_vector_foreach(&wf->entries, i, path)
+       git_vector_foreach(&ff->entries, i, path)
                git__free(path);
-       git_vector_free(&wf->entries);
-       git__free(wf);
+       git_vector_free(&ff->entries);
+       git__free(ff);
 }
 
-static int workdir_iterator__update_entry(workdir_iterator *wi);
-
-static int workdir_iterator__entry_cmp_case(const void *pfx, const void *item)
+static void fs_iterator__pop_frame(
+       fs_iterator *fi, fs_iterator_frame *ff, bool pop_last)
 {
-       const git_path_with_stat *ps = item;
-       return git__prefixcmp((const char *)pfx, ps->path);
+       if (fi && fi->stack == ff) {
+               if (!ff->next && !pop_last) {
+                       memset(&fi->entry, 0, sizeof(fi->entry));
+                       return;
+               }
+
+               if (fi->leave_dir_cb)
+                       (void)fi->leave_dir_cb(fi);
+
+               fi->stack = ff->next;
+               fi->depth--;
+       }
+
+       fs_iterator__free_frame(ff);
 }
 
-static int workdir_iterator__entry_cmp_icase(const void *pfx, const void *item)
+static int fs_iterator__update_entry(fs_iterator *fi);
+static int fs_iterator__advance_over(
+       const git_index_entry **entry, git_iterator *self);
+
+static int fs_iterator__entry_cmp(const void *i, const void *item)
 {
+       const fs_iterator *fi = (const fs_iterator *)i;
        const git_path_with_stat *ps = item;
-       return git__prefixcmp_icase((const char *)pfx, ps->path);
+       return fi->base.prefixcomp(fi->base.start, ps->path);
 }
 
-static void workdir_iterator__seek_frame_start(
-       workdir_iterator *wi, workdir_iterator_frame *wf)
+static void fs_iterator__seek_frame_start(
+       fs_iterator *fi, fs_iterator_frame *ff)
 {
-       if (!wf)
+       if (!ff)
                return;
 
-       if (wi->base.start)
-               git_vector_bsearch3(
-                       &wf->index, &wf->entries, wi->entrycmp, wi->base.start);
+       if (fi->base.start)
+               git_vector_bsearch2(
+                       &ff->index, &ff->entries, fs_iterator__entry_cmp, fi);
        else
-               wf->index = 0;
-
-       if (path_is_dotgit(git_vector_get(&wf->entries, wf->index)))
-               wf->index++;
+               ff->index = 0;
 }
 
-static int workdir_iterator__expand_dir(workdir_iterator *wi)
+static int fs_iterator__expand_dir(fs_iterator *fi)
 {
        int error;
-       workdir_iterator_frame *wf = workdir_iterator__alloc_frame(wi);
-       GITERR_CHECK_ALLOC(wf);
+       fs_iterator_frame *ff;
+
+       if (fi->depth > FS_MAX_DEPTH) {
+               giterr_set(GITERR_REPOSITORY,
+                       "Directory nesting is too deep (%d)", fi->depth);
+               return -1;
+       }
+
+       ff = fs_iterator__alloc_frame(fi);
+       GITERR_CHECK_ALLOC(ff);
 
        error = git_path_dirload_with_stat(
-               wi->path.ptr, wi->root_len, wi->base.ignore_case,
-               wi->base.start, wi->base.end, &wf->entries);
+               fi->path.ptr, fi->root_len, fi->dirload_flags,
+               fi->base.start, fi->base.end, &ff->entries);
+
+       if (error < 0) {
+               fs_iterator__free_frame(ff);
+               fs_iterator__advance_over(NULL, (git_iterator *)fi);
+               return error;
+       }
 
-       if (error < 0 || wf->entries.length == 0) {
-               workdir_iterator__free_frame(wf);
+       if (ff->entries.length == 0) {
+               fs_iterator__free_frame(ff);
                return GIT_ENOTFOUND;
        }
 
-       workdir_iterator__seek_frame_start(wi, wf);
+       fs_iterator__seek_frame_start(fi, ff);
 
-       /* only push new ignores if this is not top level directory */
-       if (wi->stack != NULL) {
-               ssize_t slash_pos = git_buf_rfind_next(&wi->path, '/');
-               (void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]);
-       }
+       ff->next  = fi->stack;
+       fi->stack = ff;
+       fi->depth++;
 
-       wf->next  = wi->stack;
-       wi->stack = wf;
+       if (fi->enter_dir_cb && (error = fi->enter_dir_cb(fi)) < 0)
+               return error;
 
-       return workdir_iterator__update_entry(wi);
+       return fs_iterator__update_entry(fi);
 }
 
-static int workdir_iterator__current(
-       git_iterator *self, const git_index_entry **entry)
+static int fs_iterator__current(
+       const git_index_entry **entry, git_iterator *self)
 {
-       workdir_iterator *wi = (workdir_iterator *)self;
-       *entry = (wi->entry.path == NULL) ? NULL : &wi->entry;
-       return 0;
+       fs_iterator *fi = (fs_iterator *)self;
+       const git_index_entry *fe = (fi->entry.path == NULL) ? NULL : &fi->entry;
+
+       if (entry)
+               *entry = fe;
+
+       fi->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
+
+       return (fe != NULL) ? 0 : GIT_ITEROVER;
 }
 
-static int workdir_iterator__at_end(git_iterator *self)
+static int fs_iterator__at_end(git_iterator *self)
 {
-       return (((workdir_iterator *)self)->entry.path == NULL);
+       return (((fs_iterator *)self)->entry.path == NULL);
 }
 
-static int workdir_iterator__advance(
-       git_iterator *self, const git_index_entry **entry)
+static int fs_iterator__advance_into(
+       const git_index_entry **entry, git_iterator *iter)
 {
-       int error;
-       workdir_iterator *wi = (workdir_iterator *)self;
-       workdir_iterator_frame *wf;
+       int error = 0;
+       fs_iterator *fi = (fs_iterator *)iter;
+
+       iterator__clear_entry(entry);
+
+       /* Allow you to explicitly advance into a commit/submodule (as well as a
+        * tree) to avoid cases where an entry is mislabeled as a submodule in
+        * the working directory.  The fs iterator will never have COMMMIT
+        * entries on it's own, but a wrapper might add them.
+        */
+       if (fi->entry.path != NULL &&
+               (fi->entry.mode == GIT_FILEMODE_TREE ||
+                fi->entry.mode == GIT_FILEMODE_COMMIT))
+               /* returns GIT_ENOTFOUND if the directory is empty */
+               error = fs_iterator__expand_dir(fi);
+
+       if (!error && entry)
+               error = fs_iterator__current(entry, iter);
+
+       if (!error && !fi->entry.path)
+               error = GIT_ITEROVER;
+
+       return error;
+}
+
+static int fs_iterator__advance_over(
+       const git_index_entry **entry, git_iterator *self)
+{
+       int error = 0;
+       fs_iterator *fi = (fs_iterator *)self;
+       fs_iterator_frame *ff;
        git_path_with_stat *next;
 
        if (entry != NULL)
                *entry = NULL;
 
-       if (wi->entry.path == NULL)
-               return 0;
+       while (fi->entry.path != NULL) {
+               ff   = fi->stack;
+               next = git_vector_get(&ff->entries, ++ff->index);
 
-       while (1) {
-               wf   = wi->stack;
-               next = git_vector_get(&wf->entries, ++wf->index);
-
-               if (next != NULL) {
-                       /* match git's behavior of ignoring anything named ".git" */
-                       if (path_is_dotgit(next))
-                               continue;
-                       /* else found a good entry */
+               if (next != NULL)
                        break;
-               }
-
-               /* pop stack if anything is left to pop */
-               if (!wf->next) {
-                       memset(&wi->entry, 0, sizeof(wi->entry));
-                       return 0;
-               }
 
-               wi->stack = wf->next;
-               workdir_iterator__free_frame(wf);
-               git_ignore__pop_dir(&wi->ignores);
+               fs_iterator__pop_frame(fi, ff, false);
        }
 
-       error = workdir_iterator__update_entry(wi);
+       error = fs_iterator__update_entry(fi);
 
        if (!error && entry != NULL)
-               error = workdir_iterator__current(self, entry);
+               error = fs_iterator__current(entry, self);
 
        return error;
 }
 
-static int workdir_iterator__seek(git_iterator *self, const char *prefix)
+static int fs_iterator__advance(
+       const git_index_entry **entry, git_iterator *self)
+{
+       fs_iterator *fi = (fs_iterator *)self;
+
+       if (!iterator__has_been_accessed(fi))
+               return fs_iterator__current(entry, self);
+
+       /* given include_trees & autoexpand, we might have to go into a tree */
+       if (iterator__do_autoexpand(fi) &&
+               fi->entry.path != NULL &&
+               fi->entry.mode == GIT_FILEMODE_TREE)
+       {
+               int error = fs_iterator__advance_into(entry, self);
+               if (error != GIT_ENOTFOUND)
+                       return error;
+               /* continue silently past empty directories if autoexpanding */
+               giterr_clear();
+       }
+
+       return fs_iterator__advance_over(entry, self);
+}
+
+static int fs_iterator__seek(git_iterator *self, const char *prefix)
 {
        GIT_UNUSED(self);
        GIT_UNUSED(prefix);
@@ -686,385 +1122,384 @@ static int workdir_iterator__seek(git_iterator *self, const char *prefix)
        return 0;
 }
 
-static int workdir_iterator__reset(
+static int fs_iterator__reset(
        git_iterator *self, const char *start, const char *end)
 {
-       workdir_iterator *wi = (workdir_iterator *)self;
+       int error;
+       fs_iterator *fi = (fs_iterator *)self;
 
-       while (wi->stack != NULL && wi->stack->next != NULL) {
-               workdir_iterator_frame *wf = wi->stack;
-               wi->stack = wf->next;
-               workdir_iterator__free_frame(wf);
-               git_ignore__pop_dir(&wi->ignores);
-       }
+       while (fi->stack != NULL && fi->stack->next != NULL)
+               fs_iterator__pop_frame(fi, fi->stack, false);
+       fi->depth = 0;
 
-       if (iterator__reset_range(self, start, end) < 0)
-               return -1;
+       if ((error = iterator__reset_range(self, start, end)) < 0)
+               return error;
+
+       fs_iterator__seek_frame_start(fi, fi->stack);
 
-       workdir_iterator__seek_frame_start(wi, wi->stack);
+       error = fs_iterator__update_entry(fi);
+       if (error == GIT_ITEROVER)
+               error = 0;
 
-       return workdir_iterator__update_entry(wi);
+       return error;
 }
 
-static void workdir_iterator__free(git_iterator *self)
+static void fs_iterator__free(git_iterator *self)
 {
-       workdir_iterator *wi = (workdir_iterator *)self;
+       fs_iterator *fi = (fs_iterator *)self;
 
-       while (wi->stack != NULL) {
-               workdir_iterator_frame *wf = wi->stack;
-               wi->stack = wf->next;
-               workdir_iterator__free_frame(wf);
-       }
+       while (fi->stack != NULL)
+               fs_iterator__pop_frame(fi, fi->stack, true);
 
-       git_ignore__free(&wi->ignores);
-       git_buf_free(&wi->path);
+       git_buf_free(&fi->path);
 }
 
-static int workdir_iterator__update_entry(workdir_iterator *wi)
+static int fs_iterator__update_entry(fs_iterator *fi)
 {
-       git_path_with_stat *ps =
-               git_vector_get(&wi->stack->entries, wi->stack->index);
+       git_path_with_stat *ps;
+
+       memset(&fi->entry, 0, sizeof(fi->entry));
 
-       git_buf_truncate(&wi->path, wi->root_len);
-       memset(&wi->entry, 0, sizeof(wi->entry));
+       if (!fi->stack)
+               return GIT_ITEROVER;
 
+       ps = git_vector_get(&fi->stack->entries, fi->stack->index);
        if (!ps)
-               return 0;
+               return GIT_ITEROVER;
 
-       if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0)
+       git_buf_truncate(&fi->path, fi->root_len);
+       if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0)
                return -1;
 
-       if (wi->base.end && ITERATOR_PREFIXCMP(
-                       wi->base, wi->path.ptr + wi->root_len, wi->base.end) > 0)
-               return 0;
-
-       wi->entry.path = ps->path;
+       if (iterator__past_end(fi, fi->path.ptr + fi->root_len))
+               return GIT_ITEROVER;
 
-       /* skip over .git entries */
-       if (path_is_dotgit(ps))
-               return workdir_iterator__advance((git_iterator *)wi, NULL);
-
-       wi->is_ignored = -1;
-
-       git_index_entry__init_from_stat(&wi->entry, &ps->st);
+       fi->entry.path = ps->path;
+       git_index_entry__init_from_stat(&fi->entry, &ps->st, true);
 
        /* need different mode here to keep directories during iteration */
-       wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
+       fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
 
-       /* if this is a file type we don't handle, treat as ignored */
-       if (wi->entry.mode == 0) {
-               wi->is_ignored = 1;
-               return 0;
-       }
-
-       /* detect submodules */
-       if (S_ISDIR(wi->entry.mode)) {
-               int res = git_submodule_lookup(NULL, wi->base.repo, wi->entry.path);
-               bool is_submodule = (res == 0);
-               if (res == GIT_ENOTFOUND)
-                       giterr_clear();
+       /* allow wrapper to check/update the entry (can force skip) */
+       if (fi->update_entry_cb &&
+               fi->update_entry_cb(fi) == GIT_ENOTFOUND)
+               return fs_iterator__advance_over(NULL, (git_iterator *)fi);
 
-               /* if submodule, mark as GITLINK and remove trailing slash */
-               if (is_submodule) {
-                       size_t len = strlen(wi->entry.path);
-                       assert(wi->entry.path[len - 1] == '/');
-                       wi->entry.path[len - 1] = '\0';
-                       wi->entry.mode = S_IFGITLINK;
-               }
+       /* if this is a tree and trees aren't included, then skip */
+       if (fi->entry.mode == GIT_FILEMODE_TREE && !iterator__include_trees(fi)) {
+               int error = fs_iterator__advance_into(NULL, (git_iterator *)fi);
+               if (error != GIT_ENOTFOUND)
+                       return error;
+               giterr_clear();
+               return fs_iterator__advance_over(NULL, (git_iterator *)fi);
        }
 
        return 0;
 }
 
-int git_iterator_for_workdir_range(
-       git_iterator **iter,
-       git_repository *repo,
-       const char *start,
-       const char *end)
+static int fs_iterator__initialize(
+       git_iterator **out, fs_iterator *fi, const char *root)
 {
        int error;
-       workdir_iterator *wi;
-       git_index *index;
 
-       assert(iter && repo);
-
-       if ((error = git_repository__ensure_not_bare(
-                       repo, "scan working directory")) < 0)
-               return error;
-
-       ITERATOR_BASE_INIT(wi, workdir, WORKDIR);
-       wi->base.repo = repo;
-
-       if ((error = git_repository_index__weakptr(&index, repo)) < 0) {
-               git_iterator_free((git_iterator *)wi);
-               return error;
-       }
-
-       /* Match ignore_case flag for iterator to that of the index */
-       wi->base.ignore_case = index->ignore_case;
-
-       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)
-       {
-               git__free(wi);
+       if (git_buf_sets(&fi->path, root) < 0 || git_path_to_dir(&fi->path) < 0) {
+               git__free(fi);
                return -1;
        }
+       fi->root_len = fi->path.size;
 
-       wi->root_len = wi->path.size;
-       wi->entrycmp = wi->base.ignore_case ?
-               workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case;
+       fi->dirload_flags =
+               (iterator__ignore_case(fi) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
+               (iterator__flag(fi, PRECOMPOSE_UNICODE) ?
+                       GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);
 
-       if ((error = workdir_iterator__expand_dir(wi)) < 0) {
-               if (error == GIT_ENOTFOUND)
+       if ((error = fs_iterator__expand_dir(fi)) < 0) {
+               if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) {
+                       giterr_clear();
                        error = 0;
-               else {
-                       git_iterator_free((git_iterator *)wi);
-                       wi = NULL;
+               else {
+                       git_iterator_free((git_iterator *)fi);
+                       fi = NULL;
                }
        }
 
-       *iter = (git_iterator *)wi;
-
+       *out = (git_iterator *)fi;
        return error;
 }
 
-typedef struct {
-       git_iterator base;
-       git_iterator *wrapped;
-       git_vector entries;
-       git_vector_cmp comparer;
-       git_pool entry_pool;
-       git_pool string_pool;
-       unsigned int position;
-} spoolandsort_iterator;
-
-static int spoolandsort_iterator__current(
-       git_iterator *self, const git_index_entry **entry)
+int git_iterator_for_filesystem(
+       git_iterator **out,
+       const char *root,
+       git_iterator_flag_t flags,
+       const char *start,
+       const char *end)
 {
-       spoolandsort_iterator *si = (spoolandsort_iterator *)self;
+       fs_iterator *fi = git__calloc(1, sizeof(fs_iterator));
+       GITERR_CHECK_ALLOC(fi);
 
-       if (si->position < si->entries.length)
-               *entry = (const git_index_entry *)git_vector_get(
-                       &si->entries, si->position);
-       else
-               *entry = NULL;
+       ITERATOR_BASE_INIT(fi, fs, FS, NULL);
 
-       return 0;
+       if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
+               fi->base.flags |= GIT_ITERATOR_IGNORE_CASE;
+
+       return fs_iterator__initialize(out, fi, root);
 }
 
-static int spoolandsort_iterator__at_end(git_iterator *self)
+
+typedef struct {
+       fs_iterator fi;
+       git_ignores ignores;
+       int is_ignored;
+} workdir_iterator;
+
+GIT_INLINE(bool) workdir_path_is_dotgit(const git_buf *path)
 {
-       spoolandsort_iterator *si = (spoolandsort_iterator *)self;
+       size_t len;
+
+       if (!path || (len = path->size) < 4)
+               return false;
+
+       if (path->ptr[len - 1] == '/')
+               len--;
 
-       return 0 == si->entries.length || si->entries.length - 1 <= si->position;
+       if (tolower(path->ptr[len - 1]) != 't' ||
+               tolower(path->ptr[len - 2]) != 'i' ||
+               tolower(path->ptr[len - 3]) != 'g' ||
+               tolower(path->ptr[len - 4]) != '.')
+               return false;
+
+       return (len == 4 || path->ptr[len - 5] == '/');
 }
 
-static int spoolandsort_iterator__advance(
-       git_iterator *self, const git_index_entry **entry)
+static int workdir_iterator__enter_dir(fs_iterator *fi)
 {
-       spoolandsort_iterator *si = (spoolandsort_iterator *)self;
+       /* only push new ignores if this is not top level directory */
+       if (fi->stack->next != NULL) {
+               workdir_iterator *wi = (workdir_iterator *)fi;
+               ssize_t slash_pos = git_buf_rfind_next(&fi->path, '/');
 
-       if (si->position < si->entries.length)
-               *entry = (const git_index_entry *)git_vector_get(
-                       &si->entries, ++si->position);
-       else
-               *entry = NULL;
+               (void)git_ignore__push_dir(&wi->ignores, &fi->path.ptr[slash_pos + 1]);
+       }
 
        return 0;
 }
 
-static int spoolandsort_iterator__seek(git_iterator *self, const char *prefix)
+static int workdir_iterator__leave_dir(fs_iterator *fi)
 {
-       GIT_UNUSED(self);
-       GIT_UNUSED(prefix);
-
-       return -1;
+       workdir_iterator *wi = (workdir_iterator *)fi;
+       git_ignore__pop_dir(&wi->ignores);
+       return 0;
 }
 
-static int spoolandsort_iterator__reset(
-       git_iterator *self, const char *start, const char *end)
+static int workdir_iterator__update_entry(fs_iterator *fi)
 {
-       spoolandsort_iterator *si = (spoolandsort_iterator *)self;
+       int error = 0;
+       workdir_iterator *wi = (workdir_iterator *)fi;
 
-       GIT_UNUSED(start); GIT_UNUSED(end);
+       /* skip over .git entries */
+       if (workdir_path_is_dotgit(&fi->path))
+               return GIT_ENOTFOUND;
 
-       si->position = 0;
+       /* reset is_ignored since we haven't checked yet */
+       wi->is_ignored = -1;
+
+       /* check if apparent tree entries are actually submodules */
+       if (fi->entry.mode != GIT_FILEMODE_TREE)
+               return 0;
+
+       error = git_submodule_lookup(NULL, fi->base.repo, fi->entry.path);
+       if (error < 0)
+               giterr_clear();
+
+       /* mark submodule (or any dir with .git) as GITLINK and remove slash */
+       if (!error || error == GIT_EEXISTS) {
+               fi->entry.mode = S_IFGITLINK;
+               fi->entry.path[strlen(fi->entry.path) - 1] = '\0';
+       }
 
        return 0;
 }
 
-static void spoolandsort_iterator__free(git_iterator *self)
+static void workdir_iterator__free(git_iterator *self)
 {
-       spoolandsort_iterator *si = (spoolandsort_iterator *)self;
-
-       git_pool_clear(&si->string_pool);
-       git_pool_clear(&si->entry_pool);
-       git_vector_free(&si->entries);
-       git_iterator_free(si->wrapped);
+       workdir_iterator *wi = (workdir_iterator *)self;
+       fs_iterator__free(self);
+       git_ignore__free(&wi->ignores);
 }
 
-int git_iterator_spoolandsort_range(
-       git_iterator **iter,
-       git_iterator *towrap,
-       git_vector_cmp comparer,
-       bool ignore_case,
+int git_iterator_for_workdir_ext(
+       git_iterator **out,
+       git_repository *repo,
+       const char *repo_workdir,
+       git_iterator_flag_t flags,
        const char *start,
        const char *end)
 {
-       spoolandsort_iterator *si;
-       const git_index_entry *item;
+       int error, precompose = 0;
+       workdir_iterator *wi;
+
+       if (!repo_workdir) {
+               if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
+                       return GIT_EBAREREPO;
+               repo_workdir = git_repository_workdir(repo);
+       }
 
-       assert(iter && towrap && comparer);
+       /* initialize as an fs iterator then do overrides */
+       wi = git__calloc(1, sizeof(workdir_iterator));
+       GITERR_CHECK_ALLOC(wi);
+       ITERATOR_BASE_INIT((&wi->fi), fs, FS, repo);
 
-       ITERATOR_BASE_INIT(si, spoolandsort, SPOOLANDSORT);
-       si->base.ignore_case = ignore_case;
-       si->wrapped = towrap;
-       si->comparer = comparer;
-       si->position = 0;
+       wi->fi.base.type = GIT_ITERATOR_TYPE_WORKDIR;
+       wi->fi.cb.free = workdir_iterator__free;
+       wi->fi.enter_dir_cb = workdir_iterator__enter_dir;
+       wi->fi.leave_dir_cb = workdir_iterator__leave_dir;
+       wi->fi.update_entry_cb = workdir_iterator__update_entry;
 
-       if (git_vector_init(&si->entries, 16, si->comparer) < 0 ||
-               git_iterator_current(towrap, &item) < 0 ||
-               git_pool_init(&si->entry_pool, sizeof(git_index_entry), 0) ||
-               git_pool_init(&si->string_pool, 1, 0))
+       if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0 ||
+               (error = git_ignore__for_path(repo, ".gitignore", &wi->ignores)) < 0)
        {
-               git__free(si);
-               return -1;
+               git_iterator_free((git_iterator *)wi);
+               return error;
        }
 
-       while (item)
-       {
-               git_index_entry *clone = git_pool_malloc(&si->entry_pool, 1);
-               memcpy(clone, item, sizeof(git_index_entry));
+       /* try to look up precompose and set flag if appropriate */
+       if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
+               giterr_clear();
+       else if (precompose)
+               wi->fi.base.flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
 
-               if (item->path)
-               {
-                       clone->path = git_pool_strdup(&si->string_pool, item->path);
-               }
+       return fs_iterator__initialize(out, &wi->fi, repo_workdir);
+}
 
-               git_vector_insert(&si->entries, clone);
 
-               if (git_iterator_advance(towrap, &item) < 0)
-               {
-                       git__free(si);
-                       return -1;
-               }
-       }
+void git_iterator_free(git_iterator *iter)
+{
+       if (iter == NULL)
+               return;
+
+       iter->cb->free(iter);
+
+       git__free(iter->start);
+       git__free(iter->end);
 
-       git_vector_sort(&si->entries);
+       memset(iter, 0, sizeof(*iter));
 
-       *iter = (git_iterator *)si;
+       git__free(iter);
+}
+
+int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
+{
+       bool desire_ignore_case  = (ignore_case != 0);
+
+       if (iterator__ignore_case(iter) == desire_ignore_case)
+               return 0;
+
+       if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
+               if (desire_ignore_case)
+                       iter->flags |= GIT_ITERATOR_IGNORE_CASE;
+               else
+                       iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
+       } else {
+               giterr_set(GITERR_INVALID,
+                       "Cannot currently set ignore case on non-empty iterators");
+               return -1;
+       }
 
        return 0;
 }
 
+git_index *git_iterator_get_index(git_iterator *iter)
+{
+       if (iter->type == GIT_ITERATOR_TYPE_INDEX)
+               return ((index_iterator *)iter)->index;
+       return NULL;
+}
+
 int git_iterator_current_tree_entry(
-       git_iterator *iter, const git_tree_entry **tree_entry)
+       const git_tree_entry **tree_entry, git_iterator *iter)
 {
-       *tree_entry = (iter->type != GIT_ITERATOR_TREE) ? NULL :
-               tree_iterator__tree_entry((tree_iterator *)iter);
+       if (iter->type != GIT_ITERATOR_TYPE_TREE)
+               *tree_entry = NULL;
+       else {
+               tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
+               *tree_entry = (tf->current < tf->n_entries) ?
+                       tf->entries[tf->current]->te : NULL;
+       }
+
        return 0;
 }
 
 int git_iterator_current_parent_tree(
+       const git_tree **tree_ptr,
        git_iterator *iter,
-       const char *parent_path,
-       const git_tree **tree_ptr)
+       const char *parent_path)
 {
        tree_iterator *ti = (tree_iterator *)iter;
        tree_iterator_frame *tf;
        const char *scan = parent_path;
+       const git_tree_entry *te;
 
-       if (iter->type != GIT_ITERATOR_TREE || ti->stack == NULL)
-               goto notfound;
+       *tree_ptr = NULL;
 
-       for (tf = ti->tail; tf != NULL; tf = tf->prev) {
-               const git_tree_entry *te;
+       if (iter->type != GIT_ITERATOR_TYPE_TREE)
+               return 0;
 
-               if (!*scan) {
-                       *tree_ptr = tf->tree;
+       for (tf = ti->root; *scan; ) {
+               if (!(tf = tf->down) ||
+                       tf->current >= tf->n_entries ||
+                       !(te = tf->entries[tf->current]->te) ||
+                       ti->strncomp(scan, te->filename, te->filename_len) != 0)
                        return 0;
-               }
-
-               te = git_tree_entry_byindex(tf->tree, tf->index);
-
-               if (strncmp(scan, te->filename, te->filename_len) != 0)
-                       goto notfound;
 
                scan += te->filename_len;
-
-               if (*scan) {
-                       if (*scan != '/')
-                               goto notfound;
+               if (*scan == '/')
                        scan++;
-               }
        }
 
-notfound:
-       *tree_ptr = NULL;
+       *tree_ptr = tf->entries[tf->current]->tree;
        return 0;
 }
 
-int git_iterator_current_is_ignored(git_iterator *iter)
+bool git_iterator_current_is_ignored(git_iterator *iter)
 {
        workdir_iterator *wi = (workdir_iterator *)iter;
 
-       if (iter->type != GIT_ITERATOR_WORKDIR)
-               return 0;
+       if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
+               return false;
 
        if (wi->is_ignored != -1)
-               return wi->is_ignored;
+               return (bool)(wi->is_ignored != 0);
 
-       if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0)
-               wi->is_ignored = 1;
+       if (git_ignore__lookup(
+                       &wi->ignores, wi->fi.entry.path, &wi->is_ignored) < 0)
+               wi->is_ignored = true;
 
-       return wi->is_ignored;
+       return (bool)wi->is_ignored;
 }
 
-int git_iterator_advance_into_directory(
-       git_iterator *iter, const git_index_entry **entry)
-{
-       workdir_iterator *wi = (workdir_iterator *)iter;
-
-       if (iter->type == GIT_ITERATOR_WORKDIR &&
-               wi->entry.path &&
-               S_ISDIR(wi->entry.mode) &&
-               !S_ISGITLINK(wi->entry.mode))
-       {
-               if (workdir_iterator__expand_dir(wi) < 0)
-                       /* if error loading or if empty, skip the directory. */
-                       return workdir_iterator__advance(iter, entry);
-       }
-
-       return entry ? git_iterator_current(iter, entry) : 0;
-}
-
-int git_iterator_cmp(
-       git_iterator *iter, const char *path_prefix)
+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(iter, &entry) < 0 ||
-               entry == NULL)
+       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 ITERATOR_PREFIXCMP(*iter, entry->path, path_prefix);
+       return iter->prefixcomp(entry->path, path_prefix);
 }
 
-int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path)
+int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
 {
        workdir_iterator *wi = (workdir_iterator *)iter;
 
-       if (iter->type != GIT_ITERATOR_WORKDIR || !wi->entry.path)
+       if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->fi.entry.path)
                *path = NULL;
        else
-               *path = &wi->path;
+               *path = &wi->fi.path;
 
        return 0;
 }
-