From 8a5a2e2f0e71584b7dd89fa706c2a2c374c6f6e8 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 17 Mar 2016 00:47:50 -0400 Subject: [PATCH 01/35] status: update test to include valid OID --- tests/status/worktree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/status/worktree.c b/tests/status/worktree.c index fc4afc6be..6b823785c 100644 --- a/tests/status/worktree.c +++ b/tests/status/worktree.c @@ -657,7 +657,7 @@ void test_status_worktree__conflict_has_no_oid(void) entry.mode = 0100644; entry.path = "modified_file"; - git_oid_fromstr(&entry.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + git_oid_fromstr(&entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_conflict_add(index, &entry, &entry, &entry)); From ac05086c40266bdd4541c06d3be532ee118ed204 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 25 Feb 2016 14:51:23 -0500 Subject: [PATCH 02/35] iterator: drop unused/unimplemented `seek` --- src/iterator.c | 29 ----------------------------- src/iterator.h | 8 -------- 2 files changed, 37 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index cb1ea6a87..7b3ad40ed 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -17,7 +17,6 @@ (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; \ @@ -271,12 +270,6 @@ static int empty_iterator__noop(const git_index_entry **e, git_iterator *i) return GIT_ITEROVER; } -static int empty_iterator__seek(git_iterator *i, const char *p) -{ - GIT_UNUSED(i); GIT_UNUSED(p); - return -1; -} - static int empty_iterator__reset(git_iterator *i, const char *s, const char *e) { GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e); @@ -748,12 +741,6 @@ static int tree_iterator__advance_into( return tree_iterator__current(entry, self); } -static int tree_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); GIT_UNUSED(prefix); - return -1; -} - static int tree_iterator__reset( git_iterator *self, const char *start, const char *end) { @@ -1030,12 +1017,6 @@ static int index_iterator__advance_into( return index_iterator__current(entry, self); } -static int index_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); GIT_UNUSED(prefix); - return -1; -} - static int index_iterator__reset( git_iterator *self, const char *start, const char *end) { @@ -1498,16 +1479,6 @@ static int fs_iterator__advance( return fs_iterator__advance_over(entry, self); } -static int fs_iterator__seek(git_iterator *self, const char *prefix) -{ - GIT_UNUSED(self); - GIT_UNUSED(prefix); - /* pop stack until matching prefix */ - /* find prefix item in current frame */ - /* push subdirectories as deep as possible while matching */ - return 0; -} - static int fs_iterator__reset( git_iterator *self, const char *start, const char *end) { diff --git a/src/iterator.h b/src/iterator.h index ac17d2970..3f5c82870 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -57,7 +57,6 @@ typedef struct { int (*current)(const git_index_entry **, git_iterator *); int (*advance)(const git_index_entry **, git_iterator *); int (*advance_into)(const git_index_entry **, git_iterator *); - int (*seek)(git_iterator *, const char *prefix); int (*reset)(git_iterator *, const char *start, const char *end); int (*at_end)(git_iterator *); void (*free)(git_iterator *); @@ -200,13 +199,6 @@ GIT_INLINE(int) git_iterator_advance_into_or_over( return error; } -/* Seek is currently unimplemented */ -GIT_INLINE(int) git_iterator_seek( - git_iterator *iter, const char *prefix) -{ - return iter->cb->seek(iter, prefix); -} - /** * Go back to the start of the iteration. * From 684b35c41b9166645e2edb9bc708aa7ddf9c1f24 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 25 Feb 2016 15:11:14 -0500 Subject: [PATCH 03/35] iterator: disambiguate reset and reset_range Disambiguate the reset and reset_range functions. Now reset_range with a NULL path will clear the start or end; reset will leave the existing start and end unchanged. --- src/checkout.c | 2 +- src/iterator.c | 71 +++++++++++++++++++++++++++++++------------ src/iterator.h | 20 +++++++----- src/pathspec.c | 2 +- tests/diff/iterator.c | 4 +-- tests/repo/iterator.c | 2 +- 6 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index deeee62e0..cf505f1b7 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2508,7 +2508,7 @@ int git_checkout_iterator( workdir_opts.start = data.pfx; workdir_opts.end = data.pfx; - if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 || + if ((error = git_iterator_reset_range(target, data.pfx, data.pfx)) < 0 || (error = git_iterator_for_workdir_ext( &workdir, data.repo, data.opts.target_directory, index, NULL, &workdir_opts)) < 0) diff --git a/src/iterator.c b/src/iterator.c index 7b3ad40ed..f7b87fc1e 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -18,6 +18,7 @@ (P)->cb.advance = NAME_LC ## _iterator__advance; \ (P)->cb.advance_into = NAME_LC ## _iterator__advance_into; \ (P)->cb.reset = NAME_LC ## _iterator__reset; \ + (P)->cb.reset_range = NAME_LC ## _iterator__reset_range; \ (P)->cb.at_end = NAME_LC ## _iterator__at_end; \ (P)->cb.free = NAME_LC ## _iterator__free; \ } while (0) @@ -199,16 +200,18 @@ static void iterator_pathlist__update_ignore_case(git_iterator *iter) static int iterator__reset_range( git_iterator *iter, const char *start, const char *end) { + if (iter->start) + git__free(iter->start); + if (start) { - if (iter->start) - git__free(iter->start); iter->start = git__strdup(start); GITERR_CHECK_ALLOC(iter->start); } + if (iter->end) + git__free(iter->end); + if (end) { - if (iter->end) - git__free(iter->end); iter->end = git__strdup(end); GITERR_CHECK_ALLOC(iter->end); } @@ -270,7 +273,14 @@ static int empty_iterator__noop(const git_index_entry **e, git_iterator *i) return GIT_ITEROVER; } -static int empty_iterator__reset(git_iterator *i, const char *s, const char *e) +static int empty_iterator__reset(git_iterator *i) +{ + GIT_UNUSED(i); + return 0; +} + +static int empty_iterator__reset_range( + git_iterator *i, const char *s, const char *e) { GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e); return 0; @@ -741,17 +751,23 @@ static int tree_iterator__advance_into( return tree_iterator__current(entry, self); } -static int tree_iterator__reset( - git_iterator *self, const char *start, const char *end) +static int tree_iterator__reset(git_iterator *self) { tree_iterator *ti = (tree_iterator *)self; - tree_iterator__pop_all(ti, false, false); + ti->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + tree_iterator__pop_all(ti, false, false); + return tree_iterator__push_frame(ti); /* re-expand root tree */ +} + +static int tree_iterator__reset_range( + git_iterator *self, const char *start, const char *end) +{ if (iterator__reset_range(self, start, end) < 0) return -1; - return tree_iterator__push_frame(ti); /* re-expand root tree */ + return tree_iterator__reset(self); } static int tree_iterator__at_end(git_iterator *self) @@ -1017,16 +1033,13 @@ static int index_iterator__advance_into( return index_iterator__current(entry, self); } -static int index_iterator__reset( - git_iterator *self, const char *start, const char *end) +static int index_iterator__reset(git_iterator *self) { index_iterator *ii = (index_iterator *)self; const git_index_entry *ie; - if (iterator__reset_range(self, start, end) < 0) - return -1; - ii->current = 0; + ii->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; iterator_pathlist_walk__reset(self); @@ -1057,6 +1070,15 @@ static int index_iterator__reset( return 0; } +static int index_iterator__reset_range( + git_iterator *self, const char *start, const char *end) +{ + if (iterator__reset_range(self, start, end) < 0) + return -1; + + return index_iterator__reset(self); +} + static void index_iterator__free(git_iterator *self) { index_iterator *ii = (index_iterator *)self; @@ -1098,7 +1120,7 @@ int git_iterator_for_index( git_buf_init(&ii->partial, 0); ii->tree_entry.mode = GIT_FILEMODE_TREE; - index_iterator__reset((git_iterator *)ii, NULL, NULL); + index_iterator__reset((git_iterator *)ii); *iter = (git_iterator *)ii; return 0; @@ -1479,19 +1501,17 @@ static int fs_iterator__advance( return fs_iterator__advance_over(entry, self); } -static int fs_iterator__reset( - git_iterator *self, const char *start, const char *end) +static int fs_iterator__reset(git_iterator *self) { int error; fs_iterator *fi = (fs_iterator *)self; + fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + while (fi->stack != NULL && fi->stack->next != NULL) fs_iterator__pop_frame(fi, fi->stack, false); fi->depth = 0; - if ((error = iterator__reset_range(self, start, end)) < 0) - return error; - fs_iterator__seek_frame_start(fi, fi->stack); error = fs_iterator__update_entry(fi); @@ -1501,6 +1521,17 @@ static int fs_iterator__reset( return error; } +static int fs_iterator__reset_range( + git_iterator *self, const char *start, const char *end) +{ + int error; + + if ((error = iterator__reset_range(self, start, end)) < 0) + return error; + + return fs_iterator__reset(self); +} + static void fs_iterator__free(git_iterator *self) { fs_iterator *fi = (fs_iterator *)self; diff --git a/src/iterator.h b/src/iterator.h index 3f5c82870..019f2e621 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -57,7 +57,8 @@ typedef struct { int (*current)(const git_index_entry **, git_iterator *); int (*advance)(const git_index_entry **, git_iterator *); int (*advance_into)(const git_index_entry **, git_iterator *); - int (*reset)(git_iterator *, const char *start, const char *end); + int (*reset)(git_iterator *); + int (*reset_range)(git_iterator *, const char *start, const char *end); int (*at_end)(git_iterator *); void (*free)(git_iterator *); } git_iterator_callbacks; @@ -201,15 +202,20 @@ GIT_INLINE(int) git_iterator_advance_into_or_over( /** * Go back to the start of the iteration. - * - * This resets the iterator to the start of the iteration. It also allows - * you to reset the `start` and `end` pathname boundaries of the iteration - * when doing so. */ -GIT_INLINE(int) git_iterator_reset( +GIT_INLINE(int) git_iterator_reset(git_iterator *iter) +{ + return iter->cb->reset(iter); +} + +/** + * Go back to the start of the iteration after updating the `start` and + * `end` pathname boundaries of the iteration. + */ +GIT_INLINE(int) git_iterator_reset_range( git_iterator *iter, const char *start, const char *end) { - return iter->cb->reset(iter, start, end); + return iter->cb->reset_range(iter, start, end); } /** diff --git a/src/pathspec.c b/src/pathspec.c index 8a93cdd50..361b398b8 100644 --- a/src/pathspec.c +++ b/src/pathspec.c @@ -418,7 +418,7 @@ static int pathspec_match_from_iterator( GITERR_CHECK_ALLOC(m); } - if ((error = git_iterator_reset(iter, ps->prefix, ps->prefix)) < 0) + if ((error = git_iterator_reset_range(iter, ps->prefix, ps->prefix)) < 0) goto done; if (git_iterator_type(iter) == GIT_ITERATOR_TYPE_WORKDIR && diff --git a/tests/diff/iterator.c b/tests/diff/iterator.c index 25a23eda7..b64c95415 100644 --- a/tests/diff/iterator.c +++ b/tests/diff/iterator.c @@ -54,7 +54,7 @@ static void tree_iterator_test( cl_assert_equal_i(expected_count, count); /* test reset */ - cl_git_pass(git_iterator_reset(i, NULL, NULL)); + cl_git_pass(git_iterator_reset(i)); while (!(error = git_iterator_advance(&entry, i))) { cl_assert(entry); @@ -634,7 +634,7 @@ static void workdir_iterator_test( cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count + expected_ignores, count_all); - cl_git_pass(git_iterator_reset(i, NULL, NULL)); + cl_git_pass(git_iterator_reset(i)); error = git_iterator_current(&entry, i); cl_assert((error == 0 && entry != NULL) || diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index c18e24a4f..5e5e4b551 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -59,7 +59,7 @@ static void expect_iterator_items( cl_assert_equal_i(expected_flat, count); - cl_git_pass(git_iterator_reset(i, NULL, NULL)); + cl_git_pass(git_iterator_reset(i)); count = 0; cl_git_pass(git_iterator_current(&entry, i)); From f0224772ee4300d55e11ab6f84cb3dd64b35ecfd Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 17 Feb 2016 18:04:19 +0000 Subject: [PATCH 04/35] git_object_dup: introduce typesafe versions --- include/git2/blob.h | 9 +++++++++ include/git2/commit.h | 9 +++++++++ include/git2/tag.h | 9 +++++++++ include/git2/tree.h | 9 +++++++++ src/commit.c | 2 +- src/describe.c | 2 +- src/iterator.c | 4 ++-- src/object_api.c | 20 +++++++++++++++++++- 8 files changed, 59 insertions(+), 5 deletions(-) diff --git a/include/git2/blob.h b/include/git2/blob.h index f451593cd..6377fc2a2 100644 --- a/include/git2/blob.h +++ b/include/git2/blob.h @@ -259,6 +259,15 @@ GIT_EXTERN(int) git_blob_create_frombuffer( */ GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob); +/** + * Create an in-memory copy of a blob. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the object + * @param source Original object to copy + */ +GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source); + /** @} */ GIT_END_DECL #endif diff --git a/include/git2/commit.h b/include/git2/commit.h index f63a90685..4cc637466 100644 --- a/include/git2/commit.h +++ b/include/git2/commit.h @@ -461,6 +461,15 @@ GIT_EXTERN(int) git_commit_create_with_signature( const char *signature, const char *signature_field); +/** + * Create an in-memory copy of a commit. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the commit + * @param source Original commit to copy + */ +GIT_EXTERN(int) git_commit_dup(git_commit **out, git_commit *source); + /** @} */ GIT_END_DECL #endif diff --git a/include/git2/tag.h b/include/git2/tag.h index c822cee7c..cb95fb5ef 100644 --- a/include/git2/tag.h +++ b/include/git2/tag.h @@ -347,6 +347,15 @@ GIT_EXTERN(int) git_tag_peel( git_object **tag_target_out, const git_tag *tag); +/** + * Create an in-memory copy of a tag. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the tag + * @param source Original tag to copy + */ +GIT_EXTERN(int) git_tag_dup(git_tag **out, git_tag *source); + /** @} */ GIT_END_DECL #endif diff --git a/include/git2/tree.h b/include/git2/tree.h index 550a44857..8a2be2102 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -409,6 +409,15 @@ GIT_EXTERN(int) git_tree_walk( git_treewalk_cb callback, void *payload); +/** + * Create an in-memory copy of a tree. The copy must be explicitly + * free'd or it will leak. + * + * @param out Pointer to store the copy of the tree + * @param source Original tree to copy + */ +GIT_EXTERN(int) git_tree_dup(git_tree **out, git_tree *source); + /** @} */ GIT_END_DECL diff --git a/src/commit.c b/src/commit.c index aaefacdab..5456751fe 100644 --- a/src/commit.c +++ b/src/commit.c @@ -615,7 +615,7 @@ int git_commit_nth_gen_ancestor( assert(ancestor && commit); - if (git_object_dup((git_object **) ¤t, (git_object *) commit) < 0) + if (git_commit_dup(¤t, (git_commit *)commit) < 0) return -1; if (n == 0) { diff --git a/src/describe.c b/src/describe.c index 13ddad5be..fc48fbde4 100644 --- a/src/describe.c +++ b/src/describe.c @@ -197,7 +197,7 @@ static int commit_name_dup(struct commit_name **out, struct commit_name *in) name->tag = NULL; name->path = NULL; - if (in->tag && git_object_dup((git_object **) &name->tag, (git_object *) in->tag) < 0) + if (in->tag && git_tag_dup(&name->tag, in->tag) < 0) return -1; name->path = git__strdup(in->path); diff --git a/src/iterator.c b/src/iterator.c index f7b87fc1e..14182a850 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -820,7 +820,7 @@ int git_iterator_for_tree( if (tree == NULL) return git_iterator_for_nothing(iter, options); - if ((error = git_object_dup((git_object **)&tree, (git_object *)tree)) < 0) + if ((error = git_tree_dup(&tree, tree)) < 0) return error; ti = git__calloc(1, sizeof(tree_iterator)); @@ -1849,7 +1849,7 @@ int git_iterator_for_workdir_ext( return error; } - if (tree && (error = git_object_dup((git_object **)&wi->tree, (git_object *)tree)) < 0) + if (tree && (error = git_tree_dup(&wi->tree, tree)) < 0) return error; wi->index = index; diff --git a/src/object_api.c b/src/object_api.c index 838bba323..e0d8760e7 100644 --- a/src/object_api.c +++ b/src/object_api.c @@ -15,7 +15,7 @@ #include "tag.h" /** - * Blob + * Commit */ int git_commit_lookup(git_commit **out, git_repository *repo, const git_oid *id) { @@ -42,6 +42,10 @@ git_repository *git_commit_owner(const git_commit *obj) return git_object_owner((const git_object *)obj); } +int git_commit_dup(git_commit **out, git_commit *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} /** * Tree @@ -71,6 +75,10 @@ git_repository *git_tree_owner(const git_tree *obj) return git_object_owner((const git_object *)obj); } +int git_tree_dup(git_tree **out, git_tree *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} /** * Tag @@ -100,6 +108,11 @@ git_repository *git_tag_owner(const git_tag *obj) return git_object_owner((const git_object *)obj); } +int git_tag_dup(git_tag **out, git_tag *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} + /** * Blob */ @@ -127,3 +140,8 @@ git_repository *git_blob_owner(const git_blob *obj) { return git_object_owner((const git_object *)obj); } + +int git_blob_dup(git_blob **out, git_blob *obj) +{ + return git_object_dup((git_object **)out, (git_object *)obj); +} From 277c85eb1c54804ab503ade69be058a0afd426f4 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 2 Mar 2016 15:38:13 -0500 Subject: [PATCH 05/35] repo::iterator: don't go out of bounds --- tests/repo/iterator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 5e5e4b551..0ab8d68c0 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -53,7 +53,7 @@ static void expect_iterator_items( cl_assert(entry->mode != GIT_FILEMODE_TREE); } - if (++count > expected_flat) + if (++count >= expected_flat) break; } @@ -99,7 +99,7 @@ static void expect_iterator_items( cl_assert(!error || error == GIT_ITEROVER); } - if (++count > expected_total) + if (++count >= expected_total) break; } From be30387e8b95cbc626e2a4a4ebba9ac9678a1c06 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 25 Feb 2016 16:05:18 -0500 Subject: [PATCH 06/35] iterators: refactored tree iterator Refactored the tree iterator to never recurse; simply process the next entry in order in `advance`. Additionally, reduce the number of allocations and sorting as much as possible to provide a ~30% speedup on case-sensitive iteration. (The gains for case-insensitive iteration are less majestic.) --- src/iterator.c | 1222 ++++++++++++++++++++++++----------------- src/iterator.h | 4 +- tests/diff/iterator.c | 13 +- tests/repo/iterator.c | 2 +- 4 files changed, 736 insertions(+), 505 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index 14182a850..91a71452d 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -266,6 +266,225 @@ GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry) } +static int iterator_range_init( + git_iterator *iter, const char *start, const char *end) +{ + if (start) { + iter->start = git__strdup(start); + GITERR_CHECK_ALLOC(iter->start); + } + + if (end) { + iter->end = git__strdup(end); + GITERR_CHECK_ALLOC(iter->end); + } + + iter->started = (iter->start == NULL); + iter->ended = false; + + return 0; +} + +static void iterator_range_free(git_iterator *iter) +{ + if (iter->start) { + git__free(iter->start); + iter->start = NULL; + } + + if (iter->end) { + git__free(iter->end); + iter->end = NULL; + } +} + +static int iterator_range_reset( + git_iterator *iter, const char *start, const char *end) +{ + iterator_range_free(iter); + return iterator_range_init(iter, start, end); +} + +static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist) +{ + size_t i; + + if (git_vector_init(&iter->pathlist, pathlist->count, + (git_vector_cmp)iter->strcomp) < 0) + return -1; + + for (i = 0; i < pathlist->count; i++) { + if (!pathlist->strings[i]) + continue; + + if (git_vector_insert(&iter->pathlist, pathlist->strings[i]) < 0) + return -1; + } + + git_vector_sort(&iter->pathlist); + return 0; +} + +static int iterator_init_common( + git_iterator *iter, + git_repository *repo, + git_iterator_options *given_opts) +{ + static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT; + git_iterator_options *options = given_opts ? given_opts : &default_opts; + bool ignore_case; + int error; + + assert(repo); + + iter->repo = repo; + iter->flags = options->flags; + + if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) { + ignore_case = true; + } else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) { + ignore_case = false; + } else { + git_index *index; + + if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0) + return error; + + ignore_case = !!index->ignore_case; + + if (ignore_case == 1) + iter->flags |= GIT_ITERATOR_IGNORE_CASE; + else + iter->flags |= GIT_ITERATOR_DONT_IGNORE_CASE; + } + + if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND)) + iter->flags |= GIT_ITERATOR_INCLUDE_TREES; + + iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; + iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; + iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; + + if ((error = iterator_range_init(iter, options->start, options->end)) < 0 || + (error = iterator_pathlist_init(iter, &options->pathlist)) < 0) + return error; + + return 0; +} + +static void iterator_clear(git_iterator *iter) +{ + iter->started = false; + iter->ended = false; + iter->pathlist_walk_idx = 0; + iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS; +} + +GIT_INLINE(bool) iterator_has_started(git_iterator *iter, const char *path) +{ + size_t path_len; + + if (iter->start == NULL || iter->started == true) + return true; + + /* the starting path is generally a prefix - we have started once we + * are prefixed by this path + */ + iter->started = (iter->prefixcomp(path, iter->start) >= 0); + + /* if, however, our current path is a directory, and our starting path + * is _beneath_ that directory, then recurse into the directory (even + * though we have not yet "started") + */ + if (!iter->started && + (path_len = strlen(path)) > 0 && path[path_len-1] == '/' && + iter->strncomp(path, iter->start, path_len) == 0) + return true; + + return iter->started; +} + +GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path) +{ + if (iter->end == NULL || iter->ended == true) + return false; + + iter->ended = (iter->prefixcomp(path, iter->end) > 0); + return iter->ended; +} + +/* walker for the index iterator that allows it to walk the sorted pathlist + * entries alongside sorted iterator entries. + */ +static bool iterator_pathlist_contains(git_iterator *iter, const char *path) +{ + char *p; + size_t path_len, p_len, cmp_len, i; + int cmp; + + if (iter->pathlist.length == 0) + return true; + + path_len = strlen(path); + + /* for comparison, drop the trailing slash on the current '/' */ + if (path_len && path[path_len-1] == '/') + path_len--; + + for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) { + p = iter->pathlist.contents[i]; + p_len = strlen(p); + + if (p_len && p[p_len-1] == '/') + p_len--; + + cmp_len = min(path_len, p_len); + + /* see if the pathlist entry is a prefix of this path */ + cmp = iter->strncomp(p, path, cmp_len); + + /* prefix match - see if there's an exact match, or if we were + * given a path that matches the directory + */ + if (cmp == 0) { + /* if this pathlist entry is not suffixed with a '/' then + * it matches a path that is a file or a directory. + * (eg, pathlist = "foo" and path is "foo" or "foo/" or + * "foo/something") + */ + if (p[cmp_len] == '\0' && + (path[cmp_len] == '\0' || path[cmp_len] == '/')) + return true; + + /* if this pathlist entry _is_ suffixed with a '/' then + * it matches only paths that are directories. + * (eg, pathlist = "foo/" and path is "foo/" or "foo/something") + */ + if (p[cmp_len] == '/' && path[cmp_len] == '/') + return true; + + /* examine the next character */ + cmp = (int)((const unsigned char)p[cmp_len]) - + (int)((const unsigned char)path[cmp_len]); + } + + /* this pathlist entry sorts before the given path, try the next */ + if (cmp < 0) { + iter->pathlist_walk_idx++; + continue; + } + + /* this pathlist sorts after the given path, no match. */ + else if (cmp > 0) { + break; + } + } + + return false; +} + +/* Empty iterator */ + static int empty_iterator__noop(const git_index_entry **e, git_iterator *i) { GIT_UNUSED(i); @@ -322,529 +541,591 @@ int git_iterator_for_nothing( return 0; } +/* Tree iterator */ -typedef struct tree_iterator_entry tree_iterator_entry; -struct tree_iterator_entry { - tree_iterator_entry *parent; - const git_tree_entry *te; +typedef struct { + git_tree_entry *tree_entry; + const char *parent_path; +} tree_iterator_entry; + +typedef struct { git_tree *tree; -}; -typedef struct tree_iterator_frame tree_iterator_frame; -struct tree_iterator_frame { - tree_iterator_frame *up, *down; + /* a sorted list of the entries for this frame (folder), these are + * actually pointers to the iterator's entry pool. + */ + git_vector entries; + tree_iterator_entry *current; - 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 */ + size_t next_idx; - const char *start; - size_t startlen; - - tree_iterator_entry *entries[GIT_FLEX_ARRAY]; -}; + /* the path to this particular frame (folder); on case insensitive + * iterations, we also have an array of other paths that we were + * case insensitively equal to this one, whose contents we have + * coalesced into this frame. a child `tree_iterator_entry` will + * contain a pointer to its actual parent path. + */ + git_buf path; + git_array_t(git_buf) similar_paths; +} tree_iterator_frame; typedef struct { git_iterator base; - git_iterator_callbacks cb; - tree_iterator_frame *head, *root; - git_pool pool; + git_tree *root; + git_array_t(tree_iterator_frame) frames; + git_index_entry entry; - git_buf path; - int path_ambiguities; - bool path_has_filename; - bool entry_is_current; + git_buf entry_path; + + /* a pool of entries to reduce the number of allocations */ + git_pool entry_pool; } tree_iterator; -static char *tree_iterator__current_filename( - tree_iterator *ti, const git_tree_entry *te) +GIT_INLINE(tree_iterator_frame *) tree_iterator_parent_frame( + tree_iterator *iter) { - 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; + return iter->frames.size > 1 ? + &iter->frames.ptr[iter->frames.size-2] : NULL; } -static void tree_iterator__rewrite_filename(tree_iterator *ti) +GIT_INLINE(tree_iterator_frame *) tree_iterator_current_frame( + tree_iterator *iter) { - 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 */ - } + return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; } -static int tree_iterator__te_cmp( - const git_tree_entry *a, - const git_tree_entry *b, - int (*compare)(const char *, const char *, size_t)) +GIT_INLINE(int) tree_entry_cmp( + const git_tree_entry *a, const git_tree_entry *b, bool icase) { 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); + icase ? git__strncasecmp : git__strncmp); } -static int tree_iterator__ci_cmp(const void *a, const void *b, void *p) +GIT_INLINE(int) tree_iterator_entry_cmp(const void *ptr_a, const void *ptr_b) { - const tree_iterator_entry *ae = a, *be = b; - int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp); + const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a; + const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b; - 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); - } - - return cmp; + return tree_entry_cmp(a->tree_entry, b->tree_entry, false); } -static int tree_iterator__search_cmp(const void *key, const void *val, void *p) +GIT_INLINE(int) tree_iterator_entry_cmp_icase( + const void *ptr_a, const void *ptr_b) { - const tree_iterator_frame *tf = key; - const git_tree_entry *te = ((tree_iterator_entry *)val)->te; + const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a; + const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b; - return git_path_cmp( - tf->start, tf->startlen, false, - te->filename, te->filename_len, te->attr == GIT_FILEMODE_TREE, - ((git_iterator *)p)->strncomp); + return tree_entry_cmp(a->tree_entry, b->tree_entry, true); } -static bool tree_iterator__move_to_next( - tree_iterator *ti, tree_iterator_frame *tf) +static int tree_iterator_entry_sort_icase(const void *ptr_a, const void *ptr_b) { - if (tf->next > tf->current + 1) - ti->path_ambiguities--; + const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a; + const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b; - if (!tf->up) { /* at root */ - tf->current = tf->next; - return false; - } + int c = tree_entry_cmp(a->tree_entry, b->tree_entry, true); - for (; tf->current < tf->next; tf->current++) { - git_tree_free(tf->entries[tf->current]->tree); - tf->entries[tf->current]->tree = NULL; - } + /* stabilize the sort order for filenames that are (case insensitively) + * the same by examining the parent path (case sensitively) before + * falling back to a case sensitive sort of the filename. + */ + if (!c && a->parent_path != b->parent_path) + c = git__strcmp(a->parent_path, b->parent_path); - return (tf->current < tf->n_entries); + if (!c) + c = tree_entry_cmp(a->tree_entry, b->tree_entry, false); + + return c; } -static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf) +static int tree_iterator_compute_path( + git_buf *out, + tree_iterator_entry *entry) { - int error = 0; - const git_tree_entry *te, *last = NULL; + git_buf_clear(out); - tf->next = tf->current; + if (entry->parent_path) + git_buf_joinpath(out, entry->parent_path, entry->tree_entry->filename); + else + git_buf_puts(out, entry->tree_entry->filename); - for (; tf->next < tf->n_entries; tf->next++, last = te) { - te = tf->entries[tf->next]->te; + if (git_tree_entry__is_tree(entry->tree_entry)) + git_buf_putc(out, '/'); - if (last && tree_iterator__te_cmp(last, te, ti->base.strncomp)) - break; - - /* 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 (tf->next > tf->current + 1) - ti->path_ambiguities++; - - /* 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; -} - -GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti) -{ - return (ti->head->current < ti->head->n_entries && - ti->head->entries[ti->head->current]->tree != NULL); -} - -static int tree_iterator__push_frame(tree_iterator *ti) -{ - int error = 0; - tree_iterator_frame *head = ti->head, *tf = NULL; - size_t i, n_entries = 0, alloclen; - - 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); - - GITERR_CHECK_ALLOC_MULTIPLY(&alloclen, sizeof(tree_iterator_entry *), n_entries); - GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, sizeof(tree_iterator_frame)); - - tf = git__calloc(1, alloclen); - GITERR_CHECK_ALLOC(tf); - - tf->n_entries = n_entries; - - tf->up = head; - head->down = tf; - ti->head = tf; - - 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; - } - } - - /* 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 bool tree_iterator__pop_frame(tree_iterator *ti, bool final) -{ - tree_iterator_frame *tf = ti->head; - - assert(tf); - - if (!tf->up) - return false; - - ti->head = tf->up; - ti->head->down = NULL; - - tree_iterator__move_to_next(ti, tf); - - if (!final) { /* if final, don't bother to clean up */ - // TODO: maybe free the pool so far? - git_buf_rtruncate_at_char(&ti->path, '/'); - } - - git__free(tf); - - 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 */; - - if (!final) { - assert(ti->head); - - 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.id, te->oid); - - ti->entry.path = tree_iterator__current_filename(ti, te); - GITERR_CHECK_ALLOC(ti->entry.path); - - 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__current_internal( - 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_internal(git_iterator *self) -{ - int error = 0; - tree_iterator *ti = (tree_iterator *)self; - - if (tree_iterator__at_tree(ti)) - error = tree_iterator__push_frame(ti); - - return error; -} - -static int tree_iterator__advance_internal(git_iterator *self) -{ - int error; - tree_iterator *ti = (tree_iterator *)self; - tree_iterator_frame *tf = ti->head; - - if (tf->current >= tf->n_entries) - return GIT_ITEROVER; - - if (!iterator__has_been_accessed(ti)) - return 0; - - if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) && - tree_iterator__at_tree(ti)) - return tree_iterator__advance_into_internal(self); - - if (ti->path_has_filename) { - git_buf_rtruncate_at_char(&ti->path, '/'); - ti->path_has_filename = ti->entry_is_current = false; - } - - /* 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; - - /* find next and load trees */ - if ((error = tree_iterator__set_next(ti, tf)) < 0) - 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_internal(self); - - return 0; -} - -static int tree_iterator__current( - const git_index_entry **out, git_iterator *self) -{ - const git_index_entry *entry = NULL; - iterator_pathlist__match_t m; - int error; - - do { - if ((error = tree_iterator__current_internal(&entry, self)) < 0) - return error; - - if (self->pathlist.length) { - m = iterator_pathlist__match( - self, entry->path, strlen(entry->path)); - - if (m != ITERATOR_PATHLIST_MATCH) { - if ((error = tree_iterator__advance_internal(self)) < 0) - return error; - - entry = NULL; - } - } - } while (!entry); - - if (out) - *out = entry; - - return error; -} - -static int tree_iterator__advance( - const git_index_entry **entry, git_iterator *self) -{ - int error = tree_iterator__advance_internal(self); - - iterator__clear_entry(entry); - - if (error < 0) - return error; - - return tree_iterator__current(entry, self); -} - -static int tree_iterator__advance_into( - const git_index_entry **entry, git_iterator *self) -{ - int error = tree_iterator__advance_into_internal(self); - - iterator__clear_entry(entry); - - if (error < 0) - return error; - - return tree_iterator__current(entry, self); -} - -static int tree_iterator__reset(git_iterator *self) -{ - tree_iterator *ti = (tree_iterator *)self; - - ti->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; - - tree_iterator__pop_all(ti, false, false); - return tree_iterator__push_frame(ti); /* re-expand root tree */ -} - -static int tree_iterator__reset_range( - git_iterator *self, const char *start, const char *end) -{ - if (iterator__reset_range(self, start, end) < 0) + if (git_buf_oom(out)) return -1; - return tree_iterator__reset(self); + return 0; } -static int tree_iterator__at_end(git_iterator *self) +static int tree_iterator_frame_init( + tree_iterator *iter, + git_tree *tree, + tree_iterator_entry *frame_entry) { - tree_iterator *ti = (tree_iterator *)self; - return (ti->head->current >= ti->head->n_entries); -} + tree_iterator_frame *new_frame = NULL; + tree_iterator_entry *new_entry; + git_tree *dup = NULL; + git_tree_entry *tree_entry; + git_vector_cmp cmp; + size_t i; + int error = 0; -static void tree_iterator__free(git_iterator *self) -{ - tree_iterator *ti = (tree_iterator *)self; + new_frame = git_array_alloc(iter->frames); + GITERR_CHECK_ALLOC(new_frame); - if (ti->head) { - tree_iterator__pop_all(ti, true, false); - git_tree_free(ti->head->entries[0]->tree); - git__free(ti->head); + memset(new_frame, 0, sizeof(tree_iterator_frame)); + + if ((error = git_tree_dup(&dup, tree)) < 0) + goto done; + + memset(new_frame, 0x0, sizeof(tree_iterator_frame)); + new_frame->tree = dup; + + + + if (frame_entry && + (error = tree_iterator_compute_path(&new_frame->path, frame_entry)) < 0) + goto done; + + cmp = iterator__ignore_case(&iter->base) ? + tree_iterator_entry_sort_icase : NULL; + + if ((error = git_vector_init( + &new_frame->entries, dup->entries.size, cmp)) < 0) + goto done; + + git_array_foreach(dup->entries, i, tree_entry) { + new_entry = git_pool_malloc(&iter->entry_pool, 1); + GITERR_CHECK_ALLOC(new_entry); + + new_entry->tree_entry = tree_entry; + new_entry->parent_path = new_frame->path.ptr; + + if ((error = git_vector_insert(&new_frame->entries, new_entry)) < 0) + goto done; } - git_pool_clear(&ti->pool); - git_buf_free(&ti->path); + git_vector_set_sorted(&new_frame->entries, + !iterator__ignore_case(&iter->base)); + +done: + if (error < 0) { + git_tree_free(dup); + git_array_pop(iter->frames); + } + + return error; } -static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree) +GIT_INLINE(tree_iterator_entry *) tree_iterator_current_entry( + tree_iterator_frame *frame) { - size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry); - tree_iterator_frame *root = git__calloc(sz, sizeof(char)); - GITERR_CHECK_ALLOC(root); + return frame->current; +} - 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_INLINE(int) tree_iterator_frame_push_neighbors( + tree_iterator *iter, + tree_iterator_frame *parent_frame, + tree_iterator_frame *frame, + const char *filename) +{ + tree_iterator_entry *entry, *new_entry; + git_tree *tree = NULL; + git_tree_entry *tree_entry; + git_buf *path; + size_t new_size, i; + int error = 0; - ti->head = ti->root = root; + while (parent_frame->next_idx < parent_frame->entries.length) { + entry = parent_frame->entries.contents[parent_frame->next_idx]; + + if (strcasecmp(filename, entry->tree_entry->filename) != 0) + break; + + if ((error = git_tree_lookup(&tree, + iter->base.repo, entry->tree_entry->oid)) < 0) + break; + + path = git_array_alloc(parent_frame->similar_paths); + GITERR_CHECK_ALLOC(path); + + memset(path, 0, sizeof(git_buf)); + + if ((error = tree_iterator_compute_path(path, entry)) < 0) + break; + + GITERR_CHECK_ALLOC_ADD(&new_size, + frame->entries.length, tree->entries.size); + git_vector_size_hint(&frame->entries, new_size); + + git_array_foreach(tree->entries, i, tree_entry) { + new_entry = git_pool_malloc(&iter->entry_pool, 1); + GITERR_CHECK_ALLOC(new_entry); + + new_entry->tree_entry = tree_entry; + new_entry->parent_path = path->ptr; + + if ((error = git_vector_insert(&frame->entries, new_entry)) < 0) + break; + } + + if (error) + break; + + parent_frame->next_idx++; + } + + return error; +} + +GIT_INLINE(int) tree_iterator_frame_push( + tree_iterator *iter, tree_iterator_entry *entry) +{ + tree_iterator_frame *parent_frame, *frame; + git_tree *tree = NULL; + int error; + + if ((error = git_tree_lookup(&tree, + iter->base.repo, entry->tree_entry->oid)) < 0 || + (error = tree_iterator_frame_init(iter, tree, entry)) < 0) + goto done; + + parent_frame = tree_iterator_parent_frame(iter); + frame = tree_iterator_current_frame(iter); + + /* if we're case insensitive, then we may have another directory that + * is (case insensitively) equal to this one. coalesce those children + * into this tree. + */ + if (iterator__ignore_case(&iter->base)) + error = tree_iterator_frame_push_neighbors(iter, + parent_frame, frame, entry->tree_entry->filename); + +done: + git_tree_free(tree); + return error; +} + +static void tree_iterator_frame_pop(tree_iterator *iter) +{ + tree_iterator_frame *frame; + + assert(iter->frames.size); + + frame = git_array_pop(iter->frames); + + git_vector_free(&frame->entries); + git_tree_free(frame->tree); +} + +static int tree_iterator_current( + const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (!iter->frames.size) { + *out = NULL; + return GIT_ITEROVER; + } + + *out = &iter->entry; + return 0; +} + +static void tree_iterator_set_current( + tree_iterator *iter, + tree_iterator_frame *frame, + tree_iterator_entry *entry) +{ + git_tree_entry *tree_entry = entry->tree_entry; + + frame->current = entry; + + memset(&iter->entry, 0x0, sizeof(git_index_entry)); + + iter->entry.mode = tree_entry->attr; + iter->entry.path = iter->entry_path.ptr; + git_oid_cpy(&iter->entry.id, tree_entry->oid); +} + +static int tree_iterator_advance(const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + /* examine tree entries until we find the next one to return */ + while (true) { + tree_iterator_entry *prev_entry, *entry; + tree_iterator_frame *frame; + bool is_tree; + + if ((frame = tree_iterator_current_frame(iter)) == NULL) { + error = GIT_ITEROVER; + break; + } + + /* no more entries in this frame. pop the frame out */ + if (frame->next_idx == frame->entries.length) { + tree_iterator_frame_pop(iter); + continue; + } + + /* we may have coalesced the contents of case-insensitively same-named + * directories, so do the sort now. + */ + if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries)) + git_vector_sort(&frame->entries); + + /* we have more entries in the current frame, that's our next entry */ + prev_entry = tree_iterator_current_entry(frame); + entry = frame->entries.contents[frame->next_idx]; + frame->next_idx++; + + /* we can have collisions when iterating case insensitively. (eg, + * 'A/a' and 'a/A'). squash this one if it's already been seen. + */ + if (iterator__ignore_case(&iter->base) && + prev_entry && + tree_iterator_entry_cmp_icase(prev_entry, entry) == 0) + continue; + + if ((error = tree_iterator_compute_path(&iter->entry_path, entry)) < 0) + break; + + /* if this path is before our start, advance over this entry */ + if (!iterator_has_started(&iter->base, iter->entry_path.ptr)) + continue; + + /* if this path is after our end, stop */ + if (iterator_has_ended(&iter->base, iter->entry_path.ptr)) { + error = GIT_ITEROVER; + break; + } + + /* if we have a list of paths we're interested in, examine it */ + if (!iterator_pathlist_contains(&iter->base, iter->entry_path.ptr)) + continue; + + is_tree = git_tree_entry__is_tree(entry->tree_entry); + + /* if we are *not* including trees then advance over this entry */ + if (is_tree && !iterator__include_trees(iter)) { + + /* if we've found a tree (and are not returning it to the caller) + * and we are autoexpanding, then we want to return the first + * child. push the new directory and advance. + */ + if (iterator__do_autoexpand(iter)) { + if ((error = tree_iterator_frame_push(iter, entry)) < 0) + break; + } + + continue; + } + + tree_iterator_set_current(iter, frame, entry); + + /* if we are autoexpanding, then push this as a new frame, so that + * the next call to `advance` will dive into this directory. + */ + if (is_tree && iterator__do_autoexpand(iter)) + error = tree_iterator_frame_push(iter, entry); + + break; + } + + if (out) + *out = (error == 0) ? &iter->entry : NULL; + + return error; +} + +static int tree_iterator_advance_into( + const git_index_entry **out, git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + tree_iterator_frame *frame; + tree_iterator_entry *prev_entry; + int error; + + if (out) + *out = NULL; + + if ((frame = tree_iterator_current_frame(iter)) == NULL) + return GIT_ITEROVER; + + /* get the last seen entry */ + prev_entry = tree_iterator_current_entry(frame); + + /* it's legal to call advance_into when auto-expand is on. in this case, + * we will have pushed a new (empty) frame on to the stack for this + * new directory. since it's empty, its current_entry should be null. + */ + assert(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); + + if (prev_entry) { + if (!git_tree_entry__is_tree(prev_entry->tree_entry)) + return 0; + + if ((error = tree_iterator_frame_push(iter, prev_entry)) < 0) + return error; + } + + /* we've advanced into the directory in question, let advance + * find the first entry + */ + return tree_iterator_advance(out, i); +} + +static void tree_iterator_clear(tree_iterator *iter) +{ + while (iter->frames.size) + tree_iterator_frame_pop(iter); + + git_array_clear(iter->frames); + + git_pool_clear(&iter->entry_pool); + git_buf_clear(&iter->entry_path); + + iterator_clear(&iter->base); +} + +static int tree_iterator_init(tree_iterator *iter) +{ + int error; + + git_pool_init(&iter->entry_pool, sizeof(tree_iterator_entry)); + + if ((error = tree_iterator_frame_init(iter, iter->root, NULL)) < 0) + return error; + + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; return 0; } +static int tree_iterator_reset(git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + tree_iterator_clear(iter); + return tree_iterator_init(iter); +} + +static int tree_iterator_reset_range( + git_iterator *i, const char *start, const char *end) +{ + if (iterator_range_reset(i, start, end) < 0) + return -1; + + return tree_iterator_reset(i); +} + +static int tree_iterator_at_end(git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + return (iter->frames.size == 0); +} + +static void tree_iterator_free(git_iterator *i) +{ + tree_iterator *iter = (tree_iterator *)i; + + tree_iterator_clear(iter); + + git_tree_free(iter->root); + git_buf_free(&iter->entry_path); +} + int git_iterator_for_tree( - git_iterator **iter, + git_iterator **out, git_tree *tree, git_iterator_options *options) { + tree_iterator *iter; int error; - tree_iterator *ti; + + static git_iterator_callbacks callbacks = { + tree_iterator_current, + tree_iterator_advance, + tree_iterator_advance_into, + tree_iterator_reset, + tree_iterator_reset_range, + tree_iterator_at_end, + tree_iterator_free + }; + + *out = NULL; if (tree == NULL) - return git_iterator_for_nothing(iter, options); + return git_iterator_for_nothing(out, options); - if ((error = git_tree_dup(&tree, tree)) < 0) - return error; + iter = git__calloc(1, sizeof(tree_iterator)); + GITERR_CHECK_ALLOC(iter); - ti = git__calloc(1, sizeof(tree_iterator)); - GITERR_CHECK_ALLOC(ti); + iter->base.type = GIT_ITERATOR_TYPE_TREE; + iter->base.cb = &callbacks; - ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree)); + if ((error = iterator_init_common(&iter->base, + git_tree_owner(tree), options)) < 0 || + (error = git_tree_dup(&iter->root, tree)) < 0 || + (error = tree_iterator_init(iter)) < 0) + goto on_error; - if ((error = iterator__update_ignore_case((git_iterator *)ti, options ? options->flags : 0)) < 0) - goto fail; - - git_pool_init(&ti->pool, sizeof(tree_iterator_entry)); - - if ((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; + *out = &iter->base; return 0; -fail: - git_iterator_free((git_iterator *)ti); +on_error: + git_iterator_free(&iter->base); return error; } +int git_iterator_current_tree_entry( + const git_tree_entry **tree_entry, git_iterator *i) +{ + tree_iterator *iter; + tree_iterator_frame *frame; + tree_iterator_entry *entry; + + assert(i->type == GIT_ITERATOR_TYPE_TREE); + + iter = (tree_iterator *)i; + + frame = tree_iterator_current_frame(iter); + entry = tree_iterator_current_entry(frame); + + *tree_entry = entry->tree_entry; + return 0; +} + +int git_iterator_current_parent_tree( + const git_tree **parent_tree, git_iterator *i, size_t depth) +{ + tree_iterator *iter; + tree_iterator_frame *frame; + + assert(i->type == GIT_ITERATOR_TYPE_TREE); + + iter = (tree_iterator *)i; + + assert(depth < iter->frames.size); + frame = &iter->frames.ptr[iter->frames.size-depth-1]; + + *parent_tree = frame->tree; + return 0; +} + +/* Index iterator */ + typedef struct { git_iterator base; @@ -1914,51 +2195,6 @@ git_index *git_iterator_get_index(git_iterator *iter) return NULL; } -int git_iterator_current_tree_entry( - const git_tree_entry **tree_entry, git_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) -{ - tree_iterator *ti = (tree_iterator *)iter; - tree_iterator_frame *tf; - const char *scan = parent_path; - const git_tree_entry *te; - - *tree_ptr = NULL; - - if (iter->type != GIT_ITERATOR_TYPE_TREE) - return 0; - - for (tf = ti->root; *scan; ) { - if (!(tf = tf->down) || - tf->current >= tf->n_entries || - !(te = tf->entries[tf->current]->te) || - ti->base.strncomp(scan, te->filename, te->filename_len) != 0) - return 0; - - scan += te->filename_len; - if (*scan == '/') - scan++; - } - - *tree_ptr = tf->entries[tf->current]->tree; - return 0; -} - static void workdir_iterator_update_is_ignored(workdir_iterator *wi) { git_dir_flag dir_flag = git_entry__dir_flag(&wi->fi.entry); diff --git a/src/iterator.h b/src/iterator.h index 019f2e621..8cd774b9d 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -69,6 +69,8 @@ struct git_iterator { git_repository *repo; char *start; char *end; + bool started; + bool ended; git_vector pathlist; size_t pathlist_walk_idx; int (*strcomp)(const char *a, const char *b); @@ -254,7 +256,7 @@ extern int git_iterator_current_tree_entry( const git_tree_entry **entry_out, git_iterator *iter); extern int git_iterator_current_parent_tree( - const git_tree **tree_out, git_iterator *iter, const char *parent_path); + const git_tree **tree_out, git_iterator *iter, size_t depth); extern bool git_iterator_current_is_ignored(git_iterator *iter); diff --git a/tests/diff/iterator.c b/tests/diff/iterator.c index b64c95415..4c9585047 100644 --- a/tests/diff/iterator.c +++ b/tests/diff/iterator.c @@ -264,37 +264,30 @@ static void check_tree_entry( const git_index_entry *ie; const git_tree_entry *te; const git_tree *tree; - git_buf path = GIT_BUF_INIT; cl_git_pass(git_iterator_current_tree_entry(&te, i)); cl_assert(te); cl_assert(git_oid_streq(te->oid, oid) == 0); cl_git_pass(git_iterator_current(&ie, i)); - cl_git_pass(git_buf_sets(&path, ie->path)); if (oid_p) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 0)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); } if (oid_pp) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 1)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); } if (oid_ppp) { - git_buf_rtruncate_at_char(&path, '/'); - cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 2)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); } - - git_buf_free(&path); } void test_diff_iterator__tree_special_functions(void) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 0ab8d68c0..f3a0682f9 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1455,7 +1455,7 @@ void test_repo_iterator__treefilelist(void) git_repository_head_tree(&tree, g_repo); /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ - /* In this test we DO NOT force a case on the iteratords and verify default behavior. */ + /* In this test we DO NOT force a case on the iterators and verify default behavior. */ i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; From 702b23d7c40a4672d22898db93ca8978fff530ee Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 11 Mar 2016 11:27:58 -0500 Subject: [PATCH 07/35] checkout: provide internal func to compute target path Many code paths in checkout need the final, full on-disk path of the file they're writing. (No surprise). However, they all munge the `data->path` buffer themselves to get there. Provide a nice helper method for them. Plus, drop the use `git_iterator_current_workdir_path` which does the same thing but different. Checkout is the only caller of this silly function, which lets us remove it. --- src/checkout.c | 86 +++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index cf505f1b7..bf3de2ece 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -66,8 +66,8 @@ typedef struct { git_vector update_conflicts; git_vector *update_reuc; git_vector *update_names; - git_buf path; - size_t workdir_len; + git_buf target_path; + size_t target_len; git_buf tmp; unsigned int strategy; int can_symlink; @@ -294,14 +294,30 @@ static int checkout_action_no_wd( return checkout_action_common(action, data, delta, NULL); } -static bool wd_item_is_removable(git_iterator *iter, const git_index_entry *wd) +static int checkout_target_fullpath( + git_buf **out, checkout_data *data, const char *path) { - git_buf *full = NULL; + git_buf_truncate(&data->target_path, data->target_len); + + if (path && git_buf_puts(&data->target_path, path) < 0) + return -1; + + *out = &data->target_path; + + return 0; +} + +static bool wd_item_is_removable( + checkout_data *data, const git_index_entry *wd) +{ + git_buf *full; if (wd->mode != GIT_FILEMODE_TREE) return true; - if (git_iterator_current_workdir_path(&full, iter) < 0) - return true; + + if (checkout_target_fullpath(&full, data, wd->path) < 0) + return false; + return !full || !git_path_contains(full, DOT_GIT); } @@ -363,14 +379,14 @@ static int checkout_action_wd_only( if ((error = checkout_notify(data, notify, NULL, wd)) != 0) return error; - if (remove && wd_item_is_removable(workdir, wd)) + if (remove && wd_item_is_removable(data, wd)) error = checkout_queue_remove(data, wd->path); if (!error) error = git_iterator_advance(wditem, workdir); } else { /* untracked or ignored - can't know which until we advance through */ - bool over = false, removable = wd_item_is_removable(workdir, wd); + bool over = false, removable = wd_item_is_removable(data, wd); git_iterator_status_t untracked_state; /* copy the entry for issuing notification callback later */ @@ -428,10 +444,12 @@ static bool submodule_is_config_only( static bool checkout_is_empty_dir(checkout_data *data, const char *path) { - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, path) < 0) + git_buf *fullpath; + + if (checkout_target_fullpath(&fullpath, data, path) < 0) return false; - return git_path_is_empty_dir(data->path.ptr); + + return git_path_is_empty_dir(fullpath->ptr); } static int checkout_action_with_wd( @@ -1582,18 +1600,18 @@ static int checkout_submodule_update_index( checkout_data *data, const git_diff_file *file) { + git_buf *fullpath; struct stat st; /* update the index unless prevented */ if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0) return 0; - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, file->path) < 0) + if (checkout_target_fullpath(&fullpath, data, file->path) < 0) return -1; data->perfdata.stat_calls++; - if (p_stat(git_buf_cstr(&data->path), &st) < 0) { + if (p_stat(fullpath->ptr, &st) < 0) { giterr_set( GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path); return GIT_ENOTFOUND; @@ -1718,22 +1736,23 @@ static int checkout_blob( checkout_data *data, const git_diff_file *file) { - int error = 0; + git_buf *fullpath; struct stat st; + int error = 0; - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, file->path) < 0) + if (checkout_target_fullpath(&fullpath, data, file->path) < 0) return -1; if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) { int rval = checkout_safe_for_update_only( - data, git_buf_cstr(&data->path), file->mode); + data, fullpath->ptr, file->mode); + if (rval <= 0) return rval; } error = checkout_write_content( - data, &file->id, git_buf_cstr(&data->path), NULL, file->mode, &st); + data, &file->id, fullpath->ptr, NULL, file->mode, &st); /* update the index unless prevented */ if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) @@ -1754,18 +1773,21 @@ static int checkout_remove_the_old( git_diff_delta *delta; const char *str; size_t i; - const char *workdir = git_buf_cstr(&data->path); + git_buf *fullpath; uint32_t flg = GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS; if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES) flg |= GIT_RMDIR_SKIP_NONEMPTY; - git_buf_truncate(&data->path, data->workdir_len); + if (checkout_target_fullpath(&fullpath, data, NULL) < 0) + return -1; git_vector_foreach(&data->diff->deltas, i, delta) { if (actions[i] & CHECKOUT_ACTION__REMOVE) { - error = git_futils_rmdir_r(delta->old_file.path, workdir, flg); + error = git_futils_rmdir_r( + delta->old_file.path, fullpath->ptr, flg); + if (error < 0) return error; @@ -1782,7 +1804,7 @@ static int checkout_remove_the_old( } git_vector_foreach(&data->removes, i, str) { - error = git_futils_rmdir_r(str, workdir, flg); + error = git_futils_rmdir_r(str, fullpath->ptr, flg); if (error < 0) return error; @@ -1949,13 +1971,13 @@ static int checkout_write_entry( const git_index_entry *side) { const char *hint_path = NULL, *suffix; + git_buf *fullpath; struct stat st; int error; assert (side == conflict->ours || side == conflict->theirs); - git_buf_truncate(&data->path, data->workdir_len); - if (git_buf_puts(&data->path, side->path) < 0) + if (checkout_target_fullpath(&fullpath, data, side->path) < 0) return -1; if ((conflict->name_collision || conflict->directoryfile) && @@ -1969,18 +1991,18 @@ static int checkout_write_entry( suffix = data->opts.their_label ? data->opts.their_label : "theirs"; - if (checkout_path_suffixed(&data->path, suffix) < 0) + if (checkout_path_suffixed(fullpath, suffix) < 0) return -1; hint_path = side->path; } if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 && - (error = checkout_safe_for_update_only(data, git_buf_cstr(&data->path), side->mode)) <= 0) + (error = checkout_safe_for_update_only(data, fullpath->ptr, side->mode)) <= 0) return error; return checkout_write_content(data, - &side->id, git_buf_cstr(&data->path), hint_path, side->mode, &st); + &side->id, fullpath->ptr, hint_path, side->mode, &st); } static int checkout_write_entries( @@ -2293,7 +2315,7 @@ static void checkout_data_clear(checkout_data *data) git_strmap_free(data->mkdir_map); - git_buf_free(&data->path); + git_buf_free(&data->target_path); git_buf_free(&data->tmp); git_index_free(data->index); @@ -2447,12 +2469,12 @@ static int checkout_data_init( if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || - (error = git_buf_puts(&data->path, data->opts.target_directory)) < 0 || - (error = git_path_to_dir(&data->path)) < 0 || + (error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 || + (error = git_path_to_dir(&data->target_path)) < 0 || (error = git_strmap_alloc(&data->mkdir_map)) < 0) goto cleanup; - data->workdir_len = git_buf_len(&data->path); + data->target_len = git_buf_len(&data->target_path); git_attr_session__init(&data->attr_session, data->repo); From a4f520a6f60a5493ff8289a23c6a9a40f2bc5c50 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 10 Mar 2016 11:07:13 -0500 Subject: [PATCH 08/35] iterator: skip unreadable directories in fs iterator Do not abort iteration in the middle when encountering an unreadable directory. Instead, skip it, as if it didn't exist. --- tests/repo/iterator.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index f3a0682f9..ae14ab427 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1012,7 +1012,7 @@ void test_repo_iterator__fs2(void) git_iterator_free(i); } -void test_repo_iterator__unreadable_dir(void) +void test_repo_iterator__skips_unreadable_dirs(void) { git_iterator *i; const git_index_entry *e; @@ -1028,14 +1028,20 @@ void test_repo_iterator__unreadable_dir(void) cl_git_mkfile("empty_standard_repo/r/b/problem", "not me"); cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000)); cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777)); + cl_git_mkfile("empty_standard_repo/r/c/foo", "aloha"); cl_git_mkfile("empty_standard_repo/r/d", "final"); cl_git_pass(git_iterator_for_filesystem( &i, "empty_standard_repo/r", NULL)); cl_git_pass(git_iterator_advance(&e, i)); /* a */ - cl_git_fail(git_iterator_advance(&e, i)); /* b */ - cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i)); + cl_assert_equal_s("a", e->path); + + cl_git_pass(git_iterator_advance(&e, i)); /* c/foo */ + cl_assert_equal_s("c/foo", e->path); + + cl_git_pass(git_iterator_advance(&e, i)); /* d */ + cl_assert_equal_s("d", e->path); cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777)); From d051de243c28be01525d1bb2f2e716fd000892d8 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 10 Mar 2016 12:54:33 -0500 Subject: [PATCH 09/35] iterator: test fs iterator w/ many nested empty dirs --- tests/repo/iterator.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index ae14ab427..ea2b37d10 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1012,6 +1012,36 @@ void test_repo_iterator__fs2(void) git_iterator_free(i); } +void test_repo_iterator__fs_gunk(void) +{ + git_iterator *i; + git_buf parent = GIT_BUF_INIT; + int n; + + if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) + cl_skip(); + + g_repo = cl_git_sandbox_init("testrepo"); + + for (n = 0; n < 100000; n++) { + git_buf_clear(&parent); + git_buf_printf(&parent, "%s/refs/heads/foo/%d/subdir", + git_repository_path(g_repo), n); + cl_assert(!git_buf_oom(&parent)); + + cl_git_pass(git_futils_mkdir(parent.ptr, 0775, GIT_MKDIR_PATH)); + } + + cl_git_pass(git_iterator_for_filesystem(&i, "testrepo/.git/refs", NULL)); + + /* should only have 13 items, since we're not asking for trees to be + * returned. the goal of this test is simply to not crash. + */ + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + git_buf_free(&parent); +} + void test_repo_iterator__skips_unreadable_dirs(void) { git_iterator *i; From 0e0589fcc383a0ca96d342896103e01d715df755 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 10 Mar 2016 00:04:26 -0500 Subject: [PATCH 10/35] iterator: combine fs+workdir iterators more completely Drop some of the layers of indirection between the workdir and the filesystem iterators. This makes the code a little bit easier to follow, and reduces the number of unnecessary allocations a bit as well. (Prior to this, when we filter entries, we would allocate them, filter them and then free them; now we do the filtering before allocation.) Also, rename `git_iterator_advance_over_with_status` to just `git_iterator_advance_over`. Mostly because it's a fucking long-ass function name otherwise. --- src/checkout.c | 12 +- src/diff.c | 12 +- src/iterator.c | 2004 +++++++++++++++++++++++++----------------------- src/iterator.h | 68 +- 4 files changed, 1117 insertions(+), 979 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index bf3de2ece..0fbb7fc16 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -394,7 +394,7 @@ static int checkout_action_wd_only( git_buf_sets(&data->tmp, wd->path); saved_wd.path = data->tmp.ptr; - error = git_iterator_advance_over_with_status( + error = git_iterator_advance_over( wditem, &untracked_state, workdir); if (error == GIT_ITEROVER) over = true; @@ -930,7 +930,7 @@ static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, g git_index *index; /* Only write conficts from sources that have them: indexes. */ - if ((index = git_iterator_get_index(data->target)) == NULL) + if ((index = git_iterator_index(data->target)) == NULL) return 0; data->update_conflicts._cmp = checkout_conflictdata_cmp; @@ -1080,7 +1080,7 @@ static int checkout_conflicts_coalesce_renames( size_t i, names; int error = 0; - if ((index = git_iterator_get_index(data->target)) == NULL) + if ((index = git_iterator_index(data->target)) == NULL) return 0; /* Juggle entries based on renames */ @@ -1138,7 +1138,7 @@ static int checkout_conflicts_mark_directoryfile( const char *path; int prefixed, error = 0; - if ((index = git_iterator_get_index(data->target)) == NULL) + if ((index = git_iterator_index(data->target)) == NULL) return 0; len = git_index_entrycount(index); @@ -2378,7 +2378,7 @@ static int checkout_data_init( if ((error = git_repository_index(&data->index, data->repo)) < 0) goto cleanup; - if (data->index != git_iterator_get_index(target)) { + if (data->index != git_iterator_index(target)) { if ((error = git_index_read(data->index, true)) < 0) goto cleanup; @@ -2600,7 +2600,7 @@ int git_checkout_iterator( (error = checkout_create_conflicts(&data)) < 0) goto cleanup; - if (data.index != git_iterator_get_index(target) && + if (data.index != git_iterator_index(target) && (error = checkout_extensions_update_index(&data)) < 0) goto cleanup; diff --git a/src/diff.c b/src/diff.c index 9ac5b9250..a2bfcbf6a 100644 --- a/src/diff.c +++ b/src/diff.c @@ -826,8 +826,7 @@ static int maybe_modified( */ } else if (git_oid_iszero(&nitem->id) && new_is_workdir) { bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0); - git_index *index; - git_iterator_index(&index, info->new_iter); + git_index *index = git_iterator_index(info->new_iter); status = GIT_DELTA_UNMODIFIED; @@ -980,15 +979,14 @@ static int iterator_advance_into( return error; } -static int iterator_advance_over_with_status( +static int iterator_advance_over( const git_index_entry **entry, git_iterator_status_t *status, git_iterator *iterator) { - int error; + int error = git_iterator_advance_over(entry, status, iterator); - if ((error = git_iterator_advance_over_with_status( - entry, status, iterator)) == GIT_ITEROVER) { + if (error == GIT_ITEROVER) { *entry = NULL; error = 0; } @@ -1056,7 +1054,7 @@ static int handle_unmatched_new_item( return iterator_advance(&info->nitem, info->new_iter); /* iterate into dir looking for an actual untracked file */ - if ((error = iterator_advance_over_with_status( + if ((error = iterator_advance_over( &info->nitem, &untracked_state, info->new_iter)) < 0) return error; diff --git a/src/iterator.c b/src/iterator.c index 91a71452d..ce0fb0ec9 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -49,15 +49,19 @@ git__free(P); return -1; } \ } 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 iterator__include_conflicts(I) iterator__flag(I, INCLUDE_CONFLICTS) +#define GIT_ITERATOR_FIRST_ACCESS (1 << 15) +#define GIT_ITERATOR_HONOR_IGNORES (1 << 16) +#define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17) -#define GIT_ITERATOR_FIRST_ACCESS (1 << 15) +#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 iterator__include_conflicts(I) iterator__flag(I,INCLUDE_CONFLICTS) #define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS) +#define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES) +#define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT) #define iterator__end(I) ((git_iterator *)(I))->end #define iterator__past_end(I,PATH) \ @@ -221,6 +225,29 @@ static int iterator__reset_range( return 0; } +int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case) +{ + if (ignore_case) { + iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE); + + iter->strcomp = git__strcasecmp; + iter->strncomp = git__strncasecmp; + iter->prefixcomp = git__prefixcmp_icase; + iter->entry_srch = git_index_entry_isrch; + } else { + iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE); + + iter->strcomp = git__strcmp; + iter->strncomp = git__strncmp; + iter->prefixcomp = git__prefixcmp; + iter->entry_srch = git_index_entry_srch; + } + + iterator_pathlist__update_ignore_case(iter); + + return 0; +} + static int iterator__update_ignore_case( git_iterator *iter, git_iterator_flag_t flags) @@ -241,23 +268,7 @@ static int iterator__update_ignore_case( ignore_case = (index->ignore_case == 1); } - if (ignore_case) { - iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE); - - iter->strcomp = git__strcasecmp; - iter->strncomp = git__strncasecmp; - iter->prefixcomp = git__prefixcmp_icase; - } else { - iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE); - - iter->strcomp = git__strcmp; - iter->strncomp = git__strncmp; - iter->prefixcomp = git__prefixcmp; - } - - iterator_pathlist__update_ignore_case(iter); - - return 0; + return git_iterator_set_ignore_case(iter, ignore_case); } GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry) @@ -269,14 +280,18 @@ GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry) static int iterator_range_init( git_iterator *iter, const char *start, const char *end) { - if (start) { + if (start && *start) { iter->start = git__strdup(start); GITERR_CHECK_ALLOC(iter->start); + + iter->start_len = strlen(iter->start); } - if (end) { + if (end && *end) { iter->end = git__strdup(end); GITERR_CHECK_ALLOC(iter->end); + + iter->end_len = strlen(iter->end); } iter->started = (iter->start == NULL); @@ -290,11 +305,13 @@ static void iterator_range_free(git_iterator *iter) if (iter->start) { git__free(iter->start); iter->start = NULL; + iter->start_len = 0; } if (iter->end) { git__free(iter->end); iter->end = NULL; + iter->end_len = 0; } } @@ -333,10 +350,9 @@ static int iterator_init_common( static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options *options = given_opts ? given_opts : &default_opts; bool ignore_case; + int precompose; int error; - assert(repo); - iter->repo = repo; iter->flags = options->flags; @@ -344,7 +360,7 @@ static int iterator_init_common( ignore_case = true; } else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) { ignore_case = false; - } else { + } else if (repo) { git_index *index; if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0) @@ -356,6 +372,19 @@ static int iterator_init_common( iter->flags |= GIT_ITERATOR_IGNORE_CASE; else iter->flags |= GIT_ITERATOR_DONT_IGNORE_CASE; + } else { + ignore_case = false; + } + + /* try to look up precompose and set flag if appropriate */ + if (repo && + (iter->flags & GIT_ITERATOR_PRECOMPOSE_UNICODE) == 0 && + (iter->flags & GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE) == 0) { + + if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0) + giterr_clear(); + else if (precompose) + iter->flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE; } if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND)) @@ -364,6 +393,7 @@ static int iterator_init_common( iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; + iter->entry_srch = ignore_case ? git_index_entry_srch : git_index_entry_isrch; if ((error = iterator_range_init(iter, options->start, options->end)) < 0 || (error = iterator_pathlist_init(iter, &options->pathlist)) < 0) @@ -376,6 +406,7 @@ static void iterator_clear(git_iterator *iter) { iter->started = false; iter->ended = false; + iter->stat_calls = 0; iter->pathlist_walk_idx = 0; iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS; } @@ -416,7 +447,7 @@ GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path) /* walker for the index iterator that allows it to walk the sorted pathlist * entries alongside sorted iterator entries. */ -static bool iterator_pathlist_contains(git_iterator *iter, const char *path) +static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) { char *p; size_t path_len, p_len, cmp_len, i; @@ -483,6 +514,62 @@ static bool iterator_pathlist_contains(git_iterator *iter, const char *path) return false; } +typedef enum { + ITERATOR_PATHLIST_NOT_FOUND = 0, + ITERATOR_PATHLIST_IS_FILE = 1, + ITERATOR_PATHLIST_IS_DIR = 2, + ITERATOR_PATHLIST_IS_PARENT = 3, + ITERATOR_PATHLIST_FULL = 4, +} iterator_pathlist_search_t; + +static iterator_pathlist_search_t iterator_pathlist_search( + git_iterator *iter, const char *path, size_t path_len) +{ + const char *p; + size_t idx; + int error; + + error = git_vector_bsearch2(&idx, &iter->pathlist, + (git_vector_cmp)iter->strcomp, path); + + /* the given path was found in the pathlist. since the pathlist only + * matches directories when they're suffixed with a '/', analyze the + * path string to determine whether it's a directory or not. + */ + if (error == 0) { + if (path_len && path[path_len-1] == '/') + return ITERATOR_PATHLIST_IS_DIR; + + return ITERATOR_PATHLIST_IS_FILE; + } + + /* at this point, the path we're examining may be a directory (though we + * don't know that yet, since we're avoiding a stat unless it's necessary) + * so walk the pathlist looking for the given path with a '/' after it, + */ + while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) { + if (iter->prefixcomp(p, path) != 0) + break; + + /* an exact match would have been matched by the bsearch above */ + assert(p[path_len]); + + /* is this a literal directory entry (eg `foo/`) or a file beneath */ + if (p[path_len] == '/') { + return (p[path_len+1] == '\0') ? + ITERATOR_PATHLIST_IS_DIR : + ITERATOR_PATHLIST_IS_PARENT; + } + + if (p[path_len] > '/') + break; + + idx++; + } + + return ITERATOR_PATHLIST_NOT_FOUND; +} + /* Empty iterator */ static int empty_iterator__noop(const git_index_entry **e, git_iterator *i) @@ -684,8 +771,6 @@ static int tree_iterator_frame_init( memset(new_frame, 0x0, sizeof(tree_iterator_frame)); new_frame->tree = dup; - - if (frame_entry && (error = tree_iterator_compute_path(&new_frame->path, frame_entry)) < 0) goto done; @@ -911,7 +996,7 @@ static int tree_iterator_advance(const git_index_entry **out, git_iterator *i) } /* if we have a list of paths we're interested in, examine it */ - if (!iterator_pathlist_contains(&iter->base, iter->entry_path.ptr)) + if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr)) continue; is_tree = git_tree_entry__is_tree(entry->tree_entry); @@ -1058,6 +1143,7 @@ int git_iterator_for_tree( tree_iterator_current, tree_iterator_advance, tree_iterator_advance_into, + NULL, /* advance_over */ tree_iterator_reset, tree_iterator_reset_range, tree_iterator_at_end, @@ -1124,6 +1210,944 @@ int git_iterator_current_parent_tree( return 0; } +/* Filesystem iterator */ + +typedef struct { + struct stat st; + size_t path_len; + iterator_pathlist_search_t match; + char path[GIT_FLEX_ARRAY]; +} filesystem_iterator_entry; + +typedef struct { + git_vector entries; + git_pool entry_pool; + size_t next_idx; + + size_t path_len; + int is_ignored; +} filesystem_iterator_frame; + +typedef struct { + git_iterator base; + char *root; + size_t root_len; + + unsigned int dirload_flags; + + git_tree *tree; + git_index *index; + git_vector index_snapshot; + + git_array_t(filesystem_iterator_frame) frames; + git_ignores ignores; + + /* info about the current entry */ + git_index_entry entry; + git_buf current_path; + int current_is_ignored; + + /* temporary buffer for advance_over */ + git_buf tmp_buf; +} filesystem_iterator; + + +GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_parent_frame( + filesystem_iterator *iter) +{ + return iter->frames.size > 1 ? + &iter->frames.ptr[iter->frames.size-2] : NULL; +} + +GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_current_frame( + filesystem_iterator *iter) +{ + return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; +} + +GIT_INLINE(filesystem_iterator_entry *) filesystem_iterator_current_entry( + filesystem_iterator_frame *frame) +{ + return frame->next_idx == 0 ? + NULL : frame->entries.contents[frame->next_idx-1]; +} + +static int filesystem_iterator_entry_cmp(const void *_a, const void *_b) +{ + const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a; + const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b; + + return git__strcmp(a->path, b->path); +} + +static int filesystem_iterator_entry_cmp_icase(const void *_a, const void *_b) +{ + const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a; + const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b; + + return git__strcasecmp(a->path, b->path); +} + +#define FILESYSTEM_MAX_DEPTH 100 + +/** + * Figure out if an entry is a submodule. + * + * We consider it a submodule if the path is listed as a submodule in + * either the tree or the index. + */ +static int is_submodule( + bool *out, filesystem_iterator *iter, const char *path, size_t path_len) +{ + bool is_submodule = false; + int error; + + *out = false; + + /* first see if this path is a submodule in HEAD */ + if (iter->tree) { + git_tree_entry *entry; + + error = git_tree_entry_bypath(&entry, iter->tree, path); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + if (!error) { + is_submodule = (entry->attr == GIT_FILEMODE_COMMIT); + git_tree_entry_free(entry); + } + } + + if (!is_submodule && iter->index) { + size_t pos; + + error = git_index_snapshot_find(&pos, + &iter->index_snapshot, iter->base.entry_srch, path, path_len, 0); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + if (!error) { + git_index_entry *e = git_vector_get(&iter->index_snapshot, pos); + is_submodule = (e->mode == GIT_FILEMODE_COMMIT); + } + } + + *out = is_submodule; + return 0; +} + +GIT_INLINE(git_dir_flag) filesystem_iterator_dir_flag(git_index_entry *entry) +{ +#if defined(GIT_WIN32) && !defined(__MINGW32__) + return (entry && entry->mode) ? + (S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) : + GIT_DIR_FLAG_UNKNOWN; +#else + GIT_UNUSED(entry); + return GIT_DIR_FLAG_UNKNOWN; +#endif +} + +static void filesystem_iterator_frame_push_ignores( + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry, + filesystem_iterator_frame *new_frame) +{ + filesystem_iterator_frame *previous_frame; + const char *path = frame_entry ? frame_entry->path : ""; + + if (!iterator__honor_ignores(&iter->base)) + return; + + if (git_ignore__lookup(&new_frame->is_ignored, + &iter->ignores, path, GIT_DIR_FLAG_TRUE) < 0) { + giterr_clear(); + new_frame->is_ignored = GIT_IGNORE_NOTFOUND; + } + + /* if this is not the top level directory... */ + if (frame_entry) { + const char *relative_path; + + previous_frame = filesystem_iterator_parent_frame(iter); + + /* push new ignores for files in this directory */ + relative_path = frame_entry->path + previous_frame->path_len; + + /* inherit ignored from parent if no rule specified */ + if (new_frame->is_ignored <= GIT_IGNORE_NOTFOUND) + new_frame->is_ignored = previous_frame->is_ignored; + + git_ignore__push_dir(&iter->ignores, relative_path); + } +} + +static void filesystem_iterator_frame_pop_ignores( + filesystem_iterator *iter) +{ + if (iterator__honor_ignores(&iter->base)) + git_ignore__pop_dir(&iter->ignores); +} + +GIT_INLINE(bool) filesystem_iterator_examine_path( + bool *is_dir_out, + iterator_pathlist_search_t *match_out, + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry, + const char *path, + size_t path_len) +{ + bool is_dir = 0; + iterator_pathlist_search_t match = ITERATOR_PATHLIST_FULL; + + *is_dir_out = false; + *match_out = ITERATOR_PATHLIST_NOT_FOUND; + + if (iter->base.start_len) { + int cmp = iter->base.strncomp(path, iter->base.start, path_len); + + /* we haven't stat'ed `path` yet, so we don't yet know if it's a + * directory or not. special case if the current path may be a + * directory that matches the start prefix. + */ + if (cmp == 0) { + if (iter->base.start[path_len] == '/') + is_dir = true; + + else if (iter->base.start[path_len] != '\0') + cmp = -1; + } + + if (cmp < 0) + return false; + } + + if (iter->base.end_len) { + int cmp = iter->base.strncomp(path, iter->base.end, iter->base.end_len); + + if (cmp > 0) + return false; + } + + /* if we have a pathlist that we're limiting to, examine this path now + * to avoid a `stat` if we're not interested in the path. + */ + if (iter->base.pathlist.length) { + /* if our parent was explicitly included, so too are we */ + if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT) + match = ITERATOR_PATHLIST_FULL; + else + match = iterator_pathlist_search(&iter->base, path, path_len); + + if (match == ITERATOR_PATHLIST_NOT_FOUND) + return false; + + /* Ensure that the pathlist entry lines up with what we expected */ + if (match == ITERATOR_PATHLIST_IS_DIR || + match == ITERATOR_PATHLIST_IS_PARENT) + is_dir = true; + } + + *is_dir_out = is_dir; + *match_out = match; + return true; +} + +GIT_INLINE(bool) filesystem_iterator_is_dot_git( + filesystem_iterator *iter, const char *path, size_t path_len) +{ + size_t len; + + if (!iterator__ignore_dot_git(&iter->base)) + return false; + + if ((len = path_len) < 4) + return false; + + if (path[len - 1] == '/') + len--; + + if (git__tolower(path[len - 1]) != 't' || + git__tolower(path[len - 2]) != 'i' || + git__tolower(path[len - 3]) != 'g' || + git__tolower(path[len - 4]) != '.') + return false; + + return (len == 4 || path[len - 5] == '/'); +} + +static filesystem_iterator_entry *filesystem_iterator_entry_init( + filesystem_iterator_frame *frame, + const char *path, + size_t path_len, + struct stat *statbuf, + iterator_pathlist_search_t pathlist_match) +{ + filesystem_iterator_entry *entry; + size_t entry_size; + + /* Make sure to append two bytes, one for the path's null + * termination, one for a possible trailing '/' for folders. + */ + if (GIT_ADD_SIZET_OVERFLOW(&entry_size, + sizeof(filesystem_iterator_entry), path_len) || + GIT_ADD_SIZET_OVERFLOW(&entry_size, entry_size, 2) || + (entry = git_pool_malloc(&frame->entry_pool, entry_size)) == NULL) + return NULL; + + entry->path_len = path_len; + entry->match = pathlist_match; + memcpy(entry->path, path, path_len); + memcpy(&entry->st, statbuf, sizeof(struct stat)); + + /* Suffix directory paths with a '/' */ + if (S_ISDIR(entry->st.st_mode)) + entry->path[entry->path_len++] = '/'; + + entry->path[entry->path_len] = '\0'; + + return entry; +} + +static int filesystem_iterator_frame_push( + filesystem_iterator *iter, + filesystem_iterator_entry *frame_entry) +{ + filesystem_iterator_frame *new_frame = NULL; + git_path_diriter diriter = GIT_PATH_DIRITER_INIT; + git_buf root = GIT_BUF_INIT; + const char *path; + filesystem_iterator_entry *entry; + struct stat statbuf; + size_t path_len; + int error; + + if (iter->frames.size == FILESYSTEM_MAX_DEPTH) { + giterr_set(GITERR_REPOSITORY, + "directory nesting too deep (%d)", iter->frames.size); + return -1; + } + + new_frame = git_array_alloc(iter->frames); + GITERR_CHECK_ALLOC(new_frame); + + memset(new_frame, 0, sizeof(filesystem_iterator_frame)); + + if (frame_entry) + git_buf_joinpath(&root, iter->root, frame_entry->path); + else + git_buf_puts(&root, iter->root); + + if (git_buf_oom(&root)) { + error = -1; + goto done; + } + + new_frame->path_len = frame_entry ? frame_entry->path_len : 0; + + /* Any error here is equivalent to the dir not existing, skip over it */ + if ((error = git_path_diriter_init( + &diriter, root.ptr, iter->dirload_flags)) < 0) { + error = GIT_ENOTFOUND; + goto done; + } + + if ((error = git_vector_init(&new_frame->entries, 64, + iterator__ignore_case(&iter->base) ? + filesystem_iterator_entry_cmp_icase : + filesystem_iterator_entry_cmp)) < 0) + goto done; + + git_pool_init(&new_frame->entry_pool, 1); + + /* check if this directory is ignored */ + filesystem_iterator_frame_push_ignores(iter, frame_entry, new_frame); + + while ((error = git_path_diriter_next(&diriter)) == 0) { + iterator_pathlist_search_t pathlist_match = ITERATOR_PATHLIST_FULL; + bool dir_expected = false; + + if ((error = git_path_diriter_fullpath(&path, &path_len, &diriter)) < 0) + goto done; + + assert(path_len > iter->root_len); + + /* remove the prefix if requested */ + path += iter->root_len; + path_len -= iter->root_len; + + /* examine start / end and the pathlist to see if this path is in it. + * note that since we haven't yet stat'ed the path, we cannot know + * whether it's a directory yet or not, so this can give us an + * expected type (S_IFDIR or S_IFREG) that we should examine) + */ + if (!filesystem_iterator_examine_path(&dir_expected, &pathlist_match, + iter, frame_entry, path, path_len)) + continue; + + /* TODO: don't need to stat if assume unchanged for this path and + * we have an index, we can just copy the data out of it. + */ + + if ((error = git_path_diriter_stat(&statbuf, &diriter)) < 0) { + /* file was removed between readdir and lstat */ + if (error == GIT_ENOTFOUND) + continue; + + /* treat the file as unreadable */ + memset(&statbuf, 0, sizeof(statbuf)); + statbuf.st_mode = GIT_FILEMODE_UNREADABLE; + + error = 0; + } + + iter->base.stat_calls++; + + /* Ignore wacky things in the filesystem */ + if (!S_ISDIR(statbuf.st_mode) && + !S_ISREG(statbuf.st_mode) && + !S_ISLNK(statbuf.st_mode) && + statbuf.st_mode != GIT_FILEMODE_UNREADABLE) + continue; + + if (filesystem_iterator_is_dot_git(iter, path, path_len)) + continue; + + /* convert submodules to GITLINK and remove trailing slashes */ + if (S_ISDIR(statbuf.st_mode)) { + bool submodule = false; + + if ((error = is_submodule(&submodule, iter, path, path_len)) < 0) + goto done; + + if (submodule) + statbuf.st_mode = GIT_FILEMODE_COMMIT; + } + + /* Ensure that the pathlist entry lines up with what we expected */ + if (dir_expected && !S_ISDIR(statbuf.st_mode)) + continue; + + entry = filesystem_iterator_entry_init(new_frame, + path, path_len, &statbuf, pathlist_match); + GITERR_CHECK_ALLOC(entry); + + git_vector_insert(&new_frame->entries, entry); + } + + if (error == GIT_ITEROVER) + error = 0; + + /* sort now that directory suffix is added */ + git_vector_sort(&new_frame->entries); + +done: + if (error < 0) + git_array_pop(iter->frames); + + git_buf_free(&root); + git_path_diriter_free(&diriter); + return error; +} + +GIT_INLINE(void) filesystem_iterator_frame_pop(filesystem_iterator *iter) +{ + filesystem_iterator_frame *frame; + + assert(iter->frames.size); + + frame = git_array_pop(iter->frames); + filesystem_iterator_frame_pop_ignores(iter); + + git_pool_clear(&frame->entry_pool); + git_vector_free(&frame->entries); +} + +static void filesystem_iterator_set_current( + filesystem_iterator *iter, + filesystem_iterator_entry *entry) +{ + iter->entry.ctime.seconds = entry->st.st_ctime; + iter->entry.ctime.nanoseconds = entry->st.st_ctime_nsec; + + iter->entry.mtime.seconds = entry->st.st_mtime; + iter->entry.mtime.nanoseconds = entry->st.st_mtime_nsec; + + iter->entry.dev = entry->st.st_dev; + iter->entry.ino = entry->st.st_ino; + iter->entry.mode = git_futils_canonical_mode(entry->st.st_mode); + iter->entry.uid = entry->st.st_uid; + iter->entry.gid = entry->st.st_gid; + iter->entry.file_size = entry->st.st_size; + + iter->entry.path = entry->path; + + iter->current_is_ignored = GIT_IGNORE_UNCHECKED; +} + +static int filesystem_iterator_current( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (!iter->frames.size) { + *out = NULL; + return GIT_ITEROVER; + } + + *out = &iter->entry; + return 0; +} + +static int filesystem_iterator_advance( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + /* examine filesystem entries until we find the next one to return */ + while (true) { + filesystem_iterator_frame *frame; + filesystem_iterator_entry *entry; + + if ((frame = filesystem_iterator_current_frame(iter)) == NULL) { + error = GIT_ITEROVER; + break; + } + + /* no more entries in this frame. pop the frame out */ + if (frame->next_idx == frame->entries.length) { + filesystem_iterator_frame_pop(iter); + continue; + } + + /* we have more entries in the current frame, that's our next entry */ + entry = frame->entries.contents[frame->next_idx]; + frame->next_idx++; + + if (S_ISDIR(entry->st.st_mode)) { + if (iterator__do_autoexpand(iter)) { + error = filesystem_iterator_frame_push(iter, entry); + + /* may get GIT_ENOTFOUND due to races or permission problems + * that we want to quietly swallow + */ + if (error == GIT_ENOTFOUND) + continue; + else if (error < 0) + break; + } + + if (!iterator__include_trees(iter)) + continue; + } + + filesystem_iterator_set_current(iter, entry); + break; + } + + if (out) + *out = (error == 0) ? &iter->entry : NULL; + + return error; +} + +static int filesystem_iterator_advance_into( + const git_index_entry **out, git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + filesystem_iterator_frame *frame; + filesystem_iterator_entry *prev_entry; + int error; + + if (out) + *out = NULL; + + if ((frame = filesystem_iterator_current_frame(iter)) == NULL) + return GIT_ITEROVER; + + /* get the last seen entry */ + prev_entry = filesystem_iterator_current_entry(frame); + + /* it's legal to call advance_into when auto-expand is on. in this case, + * we will have pushed a new (empty) frame on to the stack for this + * new directory. since it's empty, its current_entry should be null. + */ + assert(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); + + if (prev_entry) { + if (prev_entry->st.st_mode != GIT_FILEMODE_COMMIT && + !S_ISDIR(prev_entry->st.st_mode)) + return 0; + + if ((error = filesystem_iterator_frame_push(iter, prev_entry)) < 0) + return error; + } + + /* we've advanced into the directory in question, let advance + * find the first entry + */ + return filesystem_iterator_advance(out, i); +} + +int git_iterator_current_workdir_path(git_buf **out, git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + const git_index_entry *entry; + + if (i->type != GIT_ITERATOR_TYPE_FS && + i->type != GIT_ITERATOR_TYPE_WORKDIR) { + *out = NULL; + return 0; + } + + git_buf_truncate(&iter->current_path, iter->root_len); + + if (git_iterator_current(&entry, i) < 0 || + git_buf_puts(&iter->current_path, entry->path) < 0) + return -1; + + *out = &iter->current_path; + return 0; +} + +GIT_INLINE(git_dir_flag) entry_dir_flag(git_index_entry *entry) +{ +#if defined(GIT_WIN32) && !defined(__MINGW32__) + return (entry && entry->mode) ? + (S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) : + GIT_DIR_FLAG_UNKNOWN; +#else + GIT_UNUSED(entry); + return GIT_DIR_FLAG_UNKNOWN; +#endif +} + +static void filesystem_iterator_update_ignored(filesystem_iterator *iter) +{ + filesystem_iterator_frame *frame; + git_dir_flag dir_flag = entry_dir_flag(&iter->entry); + + if (git_ignore__lookup(&iter->current_is_ignored, + &iter->ignores, iter->entry.path, dir_flag) < 0) { + giterr_clear(); + iter->current_is_ignored = GIT_IGNORE_NOTFOUND; + } + + /* use ignore from containing frame stack */ + if (iter->current_is_ignored <= GIT_IGNORE_NOTFOUND) { + frame = filesystem_iterator_current_frame(iter); + iter->current_is_ignored = frame->is_ignored; + } +} + +GIT_INLINE(bool) filesystem_iterator_current_is_ignored( + filesystem_iterator *iter) +{ + if (iter->current_is_ignored == GIT_IGNORE_UNCHECKED) + filesystem_iterator_update_ignored(iter); + + return (iter->current_is_ignored == GIT_IGNORE_TRUE); +} + +bool git_iterator_current_is_ignored(git_iterator *i) +{ + if (i->type != GIT_ITERATOR_TYPE_WORKDIR) + return false; + + return filesystem_iterator_current_is_ignored((filesystem_iterator *)i); +} + +bool git_iterator_current_tree_is_ignored(git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + filesystem_iterator_frame *frame; + + if (i->type != GIT_ITERATOR_TYPE_WORKDIR) + return false; + + frame = filesystem_iterator_current_frame(iter); + return (frame->is_ignored == GIT_IGNORE_TRUE); +} + +static int filesystem_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + filesystem_iterator_frame *current_frame; + filesystem_iterator_entry *current_entry; + const git_index_entry *entry = NULL; + const char *base; + int error = 0; + + *out = NULL; + *status = GIT_ITERATOR_STATUS_NORMAL; + + assert(iterator__has_been_accessed(i)); + + current_frame = filesystem_iterator_current_frame(iter); + assert(current_frame); + current_entry = filesystem_iterator_current_entry(current_frame); + assert(current_entry); + + if ((error = git_iterator_current(&entry, i)) < 0) + return error; + + if (!S_ISDIR(entry->mode)) { + if (filesystem_iterator_current_is_ignored(iter)) + *status = GIT_ITERATOR_STATUS_IGNORED; + + return filesystem_iterator_advance(out, i); + } + + git_buf_clear(&iter->tmp_buf); + if ((error = git_buf_puts(&iter->tmp_buf, entry->path)) < 0) + return error; + + base = iter->tmp_buf.ptr; + + /* scan inside the directory looking for files. if we find nothing, + * we will remain EMPTY. if we find any ignored item, upgrade EMPTY to + * IGNORED. if we find a real actual item, upgrade all the way to NORMAL + * and then stop. + * + * however, if we're here looking for a pathlist item (but are not + * actually in the pathlist ourselves) then start at FILTERED instead of + * EMPTY. callers then know that this path was not something they asked + * about. + */ + *status = current_entry->match == ITERATOR_PATHLIST_IS_PARENT ? + GIT_ITERATOR_STATUS_FILTERED : GIT_ITERATOR_STATUS_EMPTY; + + while (entry && !iter->base.prefixcomp(entry->path, base)) { + if (filesystem_iterator_current_is_ignored(iter)) { + /* if we found an explicitly ignored item, then update from + * EMPTY to IGNORED + */ + *status = GIT_ITERATOR_STATUS_IGNORED; + } else if (S_ISDIR(entry->mode)) { + error = filesystem_iterator_advance_into(&entry, i); + + if (!error) + continue; + + /* this directory disappeared, ignore it */ + else if (error == GIT_ENOTFOUND) + error = 0; + + /* a real error occurred */ + else + break; + } else { + /* we found a non-ignored item, treat parent as untracked */ + *status = GIT_ITERATOR_STATUS_NORMAL; + break; + } + + if ((error = git_iterator_advance(&entry, i)) < 0) + break; + } + + /* wrap up scan back to base directory */ + while (entry && !iter->base.prefixcomp(entry->path, base)) { + if ((error = git_iterator_advance(&entry, i)) < 0) + break; + } + + if (!error) + *out = entry; + + return error; +} + +static void filesystem_iterator_clear(filesystem_iterator *iter) +{ + while (iter->frames.size) + filesystem_iterator_frame_pop(iter); + + git_array_clear(iter->frames); + git_ignore__free(&iter->ignores); + + git_buf_free(&iter->tmp_buf); + + iterator_clear(&iter->base); +} + +static int filesystem_iterator_init(filesystem_iterator *iter) +{ + int error; + + if (iterator__honor_ignores(&iter->base) && + (error = git_ignore__for_path(iter->base.repo, + ".gitignore", &iter->ignores)) < 0) + return error; + + if ((error = filesystem_iterator_frame_push(iter, NULL)) < 0) + return error; + + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + + return 0; +} + +static int filesystem_iterator_reset(git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + + filesystem_iterator_clear(iter); + return filesystem_iterator_init(iter); +} + +static int filesystem_iterator_reset_range( + git_iterator *i, const char *start, const char *end) +{ + if (iterator_range_reset(i, start, end) < 0) + return -1; + + return filesystem_iterator_reset(i); +} + +static int filesystem_iterator_at_end(git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + + return (iter->frames.size == 0); +} + +static void filesystem_iterator_free(git_iterator *i) +{ + filesystem_iterator *iter = (filesystem_iterator *)i; + filesystem_iterator_clear(iter); +} + +static int iterator_for_filesystem( + git_iterator **out, + git_repository *repo, + const char *root, + git_index *index, + git_tree *tree, + git_iterator_type_t type, + git_iterator_options *options) +{ + filesystem_iterator *iter; + size_t root_len; + int error; + + static git_iterator_callbacks callbacks = { + filesystem_iterator_current, + filesystem_iterator_advance, + filesystem_iterator_advance_into, + filesystem_iterator_advance_over, + filesystem_iterator_reset, + filesystem_iterator_reset_range, + filesystem_iterator_at_end, + filesystem_iterator_free + }; + + *out = NULL; + + if (root == NULL) + return git_iterator_for_nothing(out, options); + + iter = git__calloc(1, sizeof(filesystem_iterator)); + GITERR_CHECK_ALLOC(iter); + + root_len = strlen(root); + + iter->root = git__malloc(root_len+2); + GITERR_CHECK_ALLOC(iter->root); + + memcpy(iter->root, root, root_len); + + if (root_len == 0 || root[root_len-1] != '/') { + iter->root[root_len] = '/'; + root_len++; + } + iter->root[root_len] = '\0'; + iter->root_len = root_len; + + if ((error = git_buf_puts(&iter->current_path, iter->root)) < 0) + goto on_error; + + iter->base.type = type; + iter->base.cb = &callbacks; + + + if ((error = iterator_init_common(&iter->base, repo, options)) < 0) + goto on_error; + + if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0) + goto on_error; + + if ((iter->index = index) != NULL && + (error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0) + goto on_error; + + iter->dirload_flags = + (iterator__ignore_case(&iter->base) ? GIT_PATH_DIR_IGNORE_CASE : 0) | + (iterator__flag(&iter->base, PRECOMPOSE_UNICODE) ? + GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0); + + if ((error = filesystem_iterator_init(iter)) < 0) + goto on_error; + + *out = &iter->base; + return 0; + +on_error: + git__free(iter->root); + git_buf_free(&iter->current_path); + git_iterator_free(&iter->base); + return error; +} + +int git_iterator_for_filesystem( + git_iterator **out, + const char *root, + git_iterator_options *options) +{ + return iterator_for_filesystem(out, + NULL, root, NULL, NULL, GIT_ITERATOR_TYPE_FS, options); +} + +int git_iterator_for_workdir_ext( + git_iterator **out, + git_repository *repo, + const char *repo_workdir, + git_index *index, + git_tree *tree, + git_iterator_options *given_opts) +{ + git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT; + + if (!repo_workdir) { + if (git_repository__ensure_not_bare(repo, "scan working directory") < 0) + return GIT_EBAREREPO; + + repo_workdir = git_repository_workdir(repo); + } + + /* upgrade to a workdir iterator, adding necessary internal flags */ + if (given_opts) + memcpy(&options, given_opts, sizeof(git_iterator_options)); + + options.flags |= GIT_ITERATOR_HONOR_IGNORES | + GIT_ITERATOR_IGNORE_DOT_GIT; + + return iterator_for_filesystem(out, + repo, repo_workdir, index, tree, GIT_ITERATOR_TYPE_WORKDIR, &options); +} + + /* Index iterator */ @@ -1408,749 +2432,6 @@ int git_iterator_for_index( } -typedef struct fs_iterator_frame fs_iterator_frame; -struct fs_iterator_frame { - fs_iterator_frame *next; - git_vector entries; - size_t index; - int is_ignored; -}; - -typedef struct fs_iterator fs_iterator; -struct fs_iterator { - git_iterator base; - git_iterator_callbacks cb; - fs_iterator_frame *stack; - git_index_entry entry; - git_buf path; - size_t root_len; - uint32_t dirload_flags; - int depth; - iterator_pathlist__match_t pathlist_match; - - 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 - -typedef struct { - struct stat st; - iterator_pathlist__match_t pathlist_match; - size_t path_len; - char path[GIT_FLEX_ARRAY]; -} fs_iterator_path_with_stat; - -static int fs_iterator_path_with_stat_cmp(const void *a, const void *b) -{ - const fs_iterator_path_with_stat *psa = a, *psb = b; - return strcmp(psa->path, psb->path); -} - -static int fs_iterator_path_with_stat_cmp_icase(const void *a, const void *b) -{ - const fs_iterator_path_with_stat *psa = a, *psb = b; - return strcasecmp(psa->path, psb->path); -} - -static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi) -{ - fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame)); - git_vector_cmp entry_compare = CASESELECT( - iterator__ignore_case(fi), - fs_iterator_path_with_stat_cmp_icase, - fs_iterator_path_with_stat_cmp); - - if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) { - git__free(ff); - ff = NULL; - } - - return ff; -} - -static void fs_iterator__free_frame(fs_iterator_frame *ff) -{ - git_vector_free_deep(&ff->entries); - git__free(ff); -} - -static void fs_iterator__pop_frame( - fs_iterator *fi, fs_iterator_frame *ff, bool pop_last) -{ - 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 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 fs_iterator_path_with_stat *ps = item; - return fi->base.prefixcomp(fi->base.start, ps->path); -} - -static void fs_iterator__seek_frame_start( - fs_iterator *fi, fs_iterator_frame *ff) -{ - if (!ff) - return; - - if (fi->base.start) - git_vector_bsearch2( - &ff->index, &ff->entries, fs_iterator__entry_cmp, fi); - else - ff->index = 0; -} - -static int dirload_with_stat(git_vector *contents, fs_iterator *fi) -{ - git_path_diriter diriter = GIT_PATH_DIRITER_INIT; - const char *path; - size_t start_len = fi->base.start ? strlen(fi->base.start) : 0; - size_t end_len = fi->base.end ? strlen(fi->base.end) : 0; - fs_iterator_path_with_stat *ps; - size_t path_len, cmp_len, ps_size; - iterator_pathlist__match_t pathlist_match = ITERATOR_PATHLIST_MATCH; - int error; - - /* Any error here is equivalent to the dir not existing, skip over it */ - if ((error = git_path_diriter_init( - &diriter, fi->path.ptr, fi->dirload_flags)) < 0) { - error = GIT_ENOTFOUND; - goto done; - } - - while ((error = git_path_diriter_next(&diriter)) == 0) { - if ((error = git_path_diriter_fullpath(&path, &path_len, &diriter)) < 0) - goto done; - - assert(path_len > fi->root_len); - - /* remove the prefix if requested */ - path += fi->root_len; - path_len -= fi->root_len; - - /* skip if before start_stat or after end_stat */ - cmp_len = min(start_len, path_len); - if (cmp_len && fi->base.strncomp(path, fi->base.start, cmp_len) < 0) - continue; - /* skip if after end_stat */ - cmp_len = min(end_len, path_len); - if (cmp_len && fi->base.strncomp(path, fi->base.end, cmp_len) > 0) - continue; - - /* if we have a pathlist that we're limiting to, examine this path. - * if the frame has already deemed us inside the path (eg, we're in - * `foo/bar` and the pathlist previously was detected to say `foo/`) - * then simply continue. otherwise, examine the pathlist looking for - * this path or children of this path. - */ - if (fi->base.pathlist.length && - fi->pathlist_match != ITERATOR_PATHLIST_MATCH && - fi->pathlist_match != ITERATOR_PATHLIST_MATCH_DIRECTORY && - !(pathlist_match = iterator_pathlist__match(&fi->base, path, path_len))) - continue; - - /* Make sure to append two bytes, one for the path's null - * termination, one for a possible trailing '/' for folders. - */ - GITERR_CHECK_ALLOC_ADD(&ps_size, sizeof(fs_iterator_path_with_stat), path_len); - GITERR_CHECK_ALLOC_ADD(&ps_size, ps_size, 2); - - ps = git__calloc(1, ps_size); - ps->path_len = path_len; - - memcpy(ps->path, path, path_len); - - /* TODO: don't stat if assume unchanged for this path */ - - if ((error = git_path_diriter_stat(&ps->st, &diriter)) < 0) { - if (error == GIT_ENOTFOUND) { - /* file was removed between readdir and lstat */ - git__free(ps); - continue; - } - - if (pathlist_match == ITERATOR_PATHLIST_MATCH_DIRECTORY) { - /* were looking for a directory, but this is a file */ - git__free(ps); - continue; - } - - /* Treat the file as unreadable if we get any other error */ - memset(&ps->st, 0, sizeof(ps->st)); - ps->st.st_mode = GIT_FILEMODE_UNREADABLE; - - giterr_clear(); - error = 0; - } else if (S_ISDIR(ps->st.st_mode)) { - /* Suffix directory paths with a '/' */ - ps->path[ps->path_len++] = '/'; - ps->path[ps->path_len] = '\0'; - } else if(!S_ISREG(ps->st.st_mode) && !S_ISLNK(ps->st.st_mode)) { - /* Ignore wacky things in the filesystem */ - git__free(ps); - continue; - } - - /* record whether this path was explicitly found in the path list - * or whether we're only examining it because something beneath it - * is in the path list. - */ - ps->pathlist_match = pathlist_match; - git_vector_insert(contents, ps); - } - - if (error == GIT_ITEROVER) - error = 0; - - /* sort now that directory suffix is added */ - git_vector_sort(contents); - -done: - git_path_diriter_free(&diriter); - return error; -} - - -static int fs_iterator__expand_dir(fs_iterator *fi) -{ - int error; - 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 = dirload_with_stat(&ff->entries, fi); - - if (error < 0) { - git_error_state last_error = { 0 }; - giterr_state_capture(&last_error, error); - - /* these callbacks may clear the error message */ - fs_iterator__free_frame(ff); - fs_iterator__advance_over(NULL, (git_iterator *)fi); - /* next time return value we skipped to */ - fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; - - return giterr_state_restore(&last_error); - } - - if (ff->entries.length == 0) { - fs_iterator__free_frame(ff); - return GIT_ENOTFOUND; - } - fi->base.stat_calls += ff->entries.length; - - fs_iterator__seek_frame_start(fi, ff); - - ff->next = fi->stack; - fi->stack = ff; - fi->depth++; - - if (fi->enter_dir_cb && (error = fi->enter_dir_cb(fi)) < 0) - return error; - - return fs_iterator__update_entry(fi); -} - -static int fs_iterator__current( - const git_index_entry **entry, git_iterator *self) -{ - 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 fs_iterator__at_end(git_iterator *self) -{ - return (((fs_iterator *)self)->entry.path == NULL); -} - -static int fs_iterator__advance_into( - const git_index_entry **entry, git_iterator *iter) -{ - 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 void fs_iterator__advance_over_internal(git_iterator *self) -{ - fs_iterator *fi = (fs_iterator *)self; - fs_iterator_frame *ff; - fs_iterator_path_with_stat *next; - - while (fi->entry.path != NULL) { - ff = fi->stack; - next = git_vector_get(&ff->entries, ++ff->index); - - if (next != NULL) - break; - - fs_iterator__pop_frame(fi, ff, false); - } -} - -static int fs_iterator__advance_over( - const git_index_entry **entry, git_iterator *self) -{ - int error; - - if (entry != NULL) - *entry = NULL; - - fs_iterator__advance_over_internal(self); - - error = fs_iterator__update_entry((fs_iterator *)self); - - if (!error && entry != NULL) - error = fs_iterator__current(entry, self); - - return error; -} - -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__reset(git_iterator *self) -{ - int error; - fs_iterator *fi = (fs_iterator *)self; - - fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; - - while (fi->stack != NULL && fi->stack->next != NULL) - fs_iterator__pop_frame(fi, fi->stack, false); - fi->depth = 0; - - fs_iterator__seek_frame_start(fi, fi->stack); - - error = fs_iterator__update_entry(fi); - if (error == GIT_ITEROVER) - error = 0; - - return error; -} - -static int fs_iterator__reset_range( - git_iterator *self, const char *start, const char *end) -{ - int error; - - if ((error = iterator__reset_range(self, start, end)) < 0) - return error; - - return fs_iterator__reset(self); -} - -static void fs_iterator__free(git_iterator *self) -{ - fs_iterator *fi = (fs_iterator *)self; - - while (fi->stack != NULL) - fs_iterator__pop_frame(fi, fi->stack, true); - - git_buf_free(&fi->path); -} - -static int fs_iterator__update_entry(fs_iterator *fi) -{ - fs_iterator_path_with_stat *ps; - - while (true) { - memset(&fi->entry, 0, sizeof(fi->entry)); - - if (!fi->stack) - return GIT_ITEROVER; - - ps = git_vector_get(&fi->stack->entries, fi->stack->index); - if (!ps) - return GIT_ITEROVER; - - git_buf_truncate(&fi->path, fi->root_len); - if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0) - return -1; - - if (iterator__past_end(fi, fi->path.ptr + fi->root_len)) - return GIT_ITEROVER; - - fi->entry.path = ps->path; - fi->pathlist_match = ps->pathlist_match; - git_index_entry__init_from_stat(&fi->entry, &ps->st, true); - - /* need different mode here to keep directories during iteration */ - fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode); - - /* allow wrapper to check/update the entry (can force skip) */ - if (fi->update_entry_cb && - fi->update_entry_cb(fi) == GIT_ENOTFOUND) { - fs_iterator__advance_over_internal(&fi->base); - continue; - } - - /* 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, &fi->base); - - if (error != GIT_ENOTFOUND) - return error; - - giterr_clear(); - fs_iterator__advance_over_internal(&fi->base); - continue; - } - - break; - } - - return 0; -} - -static int fs_iterator__initialize( - git_iterator **out, fs_iterator *fi, const char *root) -{ - int error; - - 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; - fi->pathlist_match = ITERATOR_PATHLIST_MATCH_CHILD; - - 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 = fs_iterator__expand_dir(fi)) < 0) { - if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) { - giterr_clear(); - error = 0; - } else { - git_iterator_free((git_iterator *)fi); - fi = NULL; - } - } - - *out = (git_iterator *)fi; - return error; -} - -int git_iterator_for_filesystem( - git_iterator **out, - const char *root, - git_iterator_options *options) -{ - fs_iterator *fi = git__calloc(1, sizeof(fs_iterator)); - GITERR_CHECK_ALLOC(fi); - - ITERATOR_BASE_INIT(fi, fs, FS, NULL); - - if (options && (options->flags & GIT_ITERATOR_IGNORE_CASE) != 0) - fi->base.flags |= GIT_ITERATOR_IGNORE_CASE; - - return fs_iterator__initialize(out, fi, root); -} - - -typedef struct { - fs_iterator fi; - git_ignores ignores; - int is_ignored; - - /* - * We may have a tree or the index+snapshot to compare against - * when checking for submodules. - */ - git_tree *tree; - git_index *index; - git_vector index_snapshot; - git_vector_cmp entry_srch; - -} workdir_iterator; - -GIT_INLINE(bool) workdir_path_is_dotgit(const git_buf *path) -{ - size_t len; - - if (!path || (len = path->size) < 4) - return false; - - if (path->ptr[len - 1] == '/') - len--; - - if (git__tolower(path->ptr[len - 1]) != 't' || - git__tolower(path->ptr[len - 2]) != 'i' || - git__tolower(path->ptr[len - 3]) != 'g' || - git__tolower(path->ptr[len - 4]) != '.') - return false; - - return (len == 4 || path->ptr[len - 5] == '/'); -} - -/** - * Figure out if an entry is a submodule. - * - * We consider it a submodule if the path is listed as a submodule in - * either the tree or the index. - */ -static int is_submodule(workdir_iterator *wi, fs_iterator_path_with_stat *ie) -{ - int error, is_submodule = 0; - - if (wi->tree) { - git_tree_entry *e; - - /* remove the trailing slash for finding */ - ie->path[ie->path_len-1] = '\0'; - error = git_tree_entry_bypath(&e, wi->tree, ie->path); - ie->path[ie->path_len-1] = '/'; - if (error < 0 && error != GIT_ENOTFOUND) - return 0; - if (!error) { - is_submodule = e->attr == GIT_FILEMODE_COMMIT; - git_tree_entry_free(e); - } - } - - if (!is_submodule && wi->index) { - git_index_entry *e; - size_t pos; - - error = git_index_snapshot_find(&pos, &wi->index_snapshot, wi->entry_srch, ie->path, ie->path_len-1, 0); - if (error < 0 && error != GIT_ENOTFOUND) - return 0; - - if (!error) { - e = git_vector_get(&wi->index_snapshot, pos); - - is_submodule = e->mode == GIT_FILEMODE_COMMIT; - } - } - - return is_submodule; -} - -GIT_INLINE(git_dir_flag) git_entry__dir_flag(git_index_entry *entry) { -#if defined(GIT_WIN32) && !defined(__MINGW32__) - return (entry && entry->mode) - ? S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE - : GIT_DIR_FLAG_UNKNOWN; -#else - GIT_UNUSED(entry); - return GIT_DIR_FLAG_UNKNOWN; -#endif -} - -static int workdir_iterator__enter_dir(fs_iterator *fi) -{ - workdir_iterator *wi = (workdir_iterator *)fi; - fs_iterator_frame *ff = fi->stack; - size_t pos; - fs_iterator_path_with_stat *entry; - bool found_submodules = false; - - git_dir_flag dir_flag = git_entry__dir_flag(&fi->entry); - - /* check if this directory is ignored */ - if (git_ignore__lookup(&ff->is_ignored, &wi->ignores, fi->path.ptr + fi->root_len, dir_flag) < 0) { - giterr_clear(); - ff->is_ignored = GIT_IGNORE_NOTFOUND; - } - - /* if this is not the top level directory... */ - if (ff->next != NULL) { - ssize_t slash_pos = git_buf_rfind_next(&fi->path, '/'); - - /* inherit ignored from parent if no rule specified */ - if (ff->is_ignored <= GIT_IGNORE_NOTFOUND) - ff->is_ignored = ff->next->is_ignored; - - /* push new ignores for files in this directory */ - (void)git_ignore__push_dir(&wi->ignores, &fi->path.ptr[slash_pos + 1]); - } - - /* convert submodules to GITLINK and remove trailing slashes */ - git_vector_foreach(&ff->entries, pos, entry) { - if (!S_ISDIR(entry->st.st_mode) || !strcmp(GIT_DIR, entry->path)) - continue; - - if (is_submodule(wi, entry)) { - entry->st.st_mode = GIT_FILEMODE_COMMIT; - entry->path_len--; - entry->path[entry->path_len] = '\0'; - found_submodules = true; - } - } - - /* if we renamed submodules, re-sort and re-seek to start */ - if (found_submodules) { - git_vector_set_sorted(&ff->entries, 0); - git_vector_sort(&ff->entries); - fs_iterator__seek_frame_start(fi, ff); - } - - return 0; -} - -static int workdir_iterator__leave_dir(fs_iterator *fi) -{ - workdir_iterator *wi = (workdir_iterator *)fi; - git_ignore__pop_dir(&wi->ignores); - return 0; -} - -static int workdir_iterator__update_entry(fs_iterator *fi) -{ - workdir_iterator *wi = (workdir_iterator *)fi; - - /* skip over .git entries */ - if (workdir_path_is_dotgit(&fi->path)) - return GIT_ENOTFOUND; - - /* reset is_ignored since we haven't checked yet */ - wi->is_ignored = GIT_IGNORE_UNCHECKED; - - return 0; -} - -static void workdir_iterator__free(git_iterator *self) -{ - workdir_iterator *wi = (workdir_iterator *)self; - if (wi->index) - git_index_snapshot_release(&wi->index_snapshot, wi->index); - git_tree_free(wi->tree); - fs_iterator__free(self); - git_ignore__free(&wi->ignores); -} - -int git_iterator_for_workdir_ext( - git_iterator **out, - git_repository *repo, - const char *repo_workdir, - git_index *index, - git_tree *tree, - git_iterator_options *options) -{ - 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); - } - - /* 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); - - 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 ((error = iterator__update_ignore_case((git_iterator *)wi, options ? options->flags : 0)) < 0 || - (error = git_ignore__for_path(repo, ".gitignore", &wi->ignores)) < 0) - { - git_iterator_free((git_iterator *)wi); - return error; - } - - if (tree && (error = git_tree_dup(&wi->tree, tree)) < 0) - return error; - - wi->index = index; - if (index && (error = git_index_snapshot_new(&wi->index_snapshot, index)) < 0) { - git_iterator_free((git_iterator *)wi); - return error; - } - wi->entry_srch = iterator__ignore_case(wi) ? - git_index_entry_isrch : git_index_entry_srch; - - - /* 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; - - return fs_iterator__initialize(out, &wi->fi, repo_workdir); -} - void git_iterator_free(git_iterator *iter) { if (iter == NULL) @@ -2167,73 +2448,6 @@ void git_iterator_free(git_iterator *iter) 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; -} - -static void workdir_iterator_update_is_ignored(workdir_iterator *wi) -{ - git_dir_flag dir_flag = git_entry__dir_flag(&wi->fi.entry); - - if (git_ignore__lookup(&wi->is_ignored, &wi->ignores, wi->fi.entry.path, dir_flag) < 0) { - giterr_clear(); - wi->is_ignored = GIT_IGNORE_NOTFOUND; - } - - /* use ignore from containing frame stack */ - if (wi->is_ignored <= GIT_IGNORE_NOTFOUND) - wi->is_ignored = wi->fi.stack->is_ignored; -} - -bool git_iterator_current_is_ignored(git_iterator *iter) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) - return false; - - if (wi->is_ignored != GIT_IGNORE_UNCHECKED) - return (bool)(wi->is_ignored == GIT_IGNORE_TRUE); - - workdir_iterator_update_is_ignored(wi); - - return (bool)(wi->is_ignored == GIT_IGNORE_TRUE); -} - -bool git_iterator_current_tree_is_ignored(git_iterator *iter) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) - return false; - - return (bool)(wi->fi.stack->is_ignored == GIT_IGNORE_TRUE); -} - int git_iterator_cmp(git_iterator *iter, const char *path_prefix) { const git_index_entry *entry; @@ -2249,106 +2463,16 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix) return iter->prefixcomp(entry->path, path_prefix); } -int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter) +git_index *git_iterator_index(git_iterator *iter) { - workdir_iterator *wi = (workdir_iterator *)iter; + if (iter->type == GIT_ITERATOR_TYPE_INDEX) + return ((index_iterator *)iter)->index; - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->fi.entry.path) - *path = NULL; - else - *path = &wi->fi.path; + if (iter->type == GIT_ITERATOR_TYPE_FS || + iter->type == GIT_ITERATOR_TYPE_WORKDIR) + return ((filesystem_iterator *)iter)->index; - return 0; -} - -int git_iterator_index(git_index **out, git_iterator *iter) -{ - workdir_iterator *wi = (workdir_iterator *)iter; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) - *out = NULL; - - *out = wi->index; - - return 0; -} - -int git_iterator_advance_over_with_status( - const git_index_entry **entryptr, - git_iterator_status_t *status, - git_iterator *iter) -{ - int error = 0; - workdir_iterator *wi = (workdir_iterator *)iter; - char *base = NULL; - const git_index_entry *entry; - - *status = GIT_ITERATOR_STATUS_NORMAL; - - if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) - return git_iterator_advance(entryptr, iter); - if ((error = git_iterator_current(&entry, iter)) < 0) - return error; - - if (!S_ISDIR(entry->mode)) { - workdir_iterator_update_is_ignored(wi); - if (wi->is_ignored == GIT_IGNORE_TRUE) - *status = GIT_ITERATOR_STATUS_IGNORED; - return git_iterator_advance(entryptr, iter); - } - - *status = GIT_ITERATOR_STATUS_EMPTY; - - base = git__strdup(entry->path); - GITERR_CHECK_ALLOC(base); - - /* scan inside directory looking for a non-ignored item */ - while (entry && !iter->prefixcomp(entry->path, base)) { - workdir_iterator_update_is_ignored(wi); - - /* if we found an explicitly ignored item, then update from - * EMPTY to IGNORED - */ - if (wi->is_ignored == GIT_IGNORE_TRUE) - *status = GIT_ITERATOR_STATUS_IGNORED; - else if (S_ISDIR(entry->mode)) { - error = git_iterator_advance_into(&entry, iter); - - if (!error) - continue; - - else if (error == GIT_ENOTFOUND) { - /* we entered this directory only hoping to find child matches to - * our pathlist (eg, this is `foo` and we had a pathlist entry for - * `foo/bar`). it should not be ignored, it should be excluded. - */ - if (wi->fi.pathlist_match == ITERATOR_PATHLIST_MATCH_CHILD) - *status = GIT_ITERATOR_STATUS_FILTERED; - else - wi->is_ignored = GIT_IGNORE_TRUE; /* mark empty dirs ignored */ - - error = 0; - } else - break; /* real error, stop here */ - } else { - /* we found a non-ignored item, treat parent as untracked */ - *status = GIT_ITERATOR_STATUS_NORMAL; - break; - } - - if ((error = git_iterator_advance(&entry, iter)) < 0) - break; - } - - /* wrap up scan back to base directory */ - while (entry && !iter->prefixcomp(entry->path, base)) - if ((error = git_iterator_advance(&entry, iter)) < 0) - break; - - *entryptr = entry; - git__free(base); - - return error; + return NULL; } int git_iterator_walk( diff --git a/src/iterator.h b/src/iterator.h index 8cd774b9d..d64d63f8d 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -34,10 +34,19 @@ typedef enum { GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3), /** convert precomposed unicode to decomposed unicode */ GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4), + /** never convert precomposed unicode to decomposed unicode */ + GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5), /** include conflicts */ - GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 5), + GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6), } git_iterator_flag_t; +typedef enum { + GIT_ITERATOR_STATUS_NORMAL = 0, + GIT_ITERATOR_STATUS_IGNORED = 1, + GIT_ITERATOR_STATUS_EMPTY = 2, + GIT_ITERATOR_STATUS_FILTERED = 3 +} git_iterator_status_t; + typedef struct { const char *start; const char *end; @@ -57,6 +66,8 @@ typedef struct { int (*current)(const git_index_entry **, git_iterator *); int (*advance)(const git_index_entry **, git_iterator *); int (*advance_into)(const git_index_entry **, git_iterator *); + int (*advance_over)( + const git_index_entry **, git_iterator_status_t *, git_iterator *); int (*reset)(git_iterator *); int (*reset_range)(git_iterator *, const char *start, const char *end); int (*at_end)(git_iterator *); @@ -67,8 +78,13 @@ struct git_iterator { git_iterator_type_t type; git_iterator_callbacks *cb; git_repository *repo; + char *start; + size_t start_len; + char *end; + size_t end_len; + bool started; bool ended; git_vector pathlist; @@ -76,6 +92,7 @@ struct git_iterator { int (*strcomp)(const char *a, const char *b); int (*strncomp)(const char *a, const char *b, size_t n); int (*prefixcomp)(const char *str, const char *prefix); + int (*entry_srch)(const void *key, const void *array_member); size_t stat_calls; unsigned int flags; }; @@ -183,6 +200,28 @@ GIT_INLINE(int) git_iterator_advance_into( return iter->cb->advance_into(entry, iter); } +/* Advance over a directory and check if it contains no files or just + * ignored files. + * + * In a tree or the index, all directories will contain files, but in the + * working directory it is possible to have an empty directory tree or a + * tree that only contains ignored files. Many Git operations treat these + * cases specially. This advances over a directory (presumably an + * untracked directory) but checks during the scan if there are any files + * and any non-ignored files. + */ +GIT_INLINE(int) git_iterator_advance_over( + const git_index_entry **entry, + git_iterator_status_t *status, + git_iterator *iter) +{ + if (iter->cb->advance_over) + return iter->cb->advance_over(entry, status, iter); + + *status = GIT_ITERATOR_STATUS_NORMAL; + return git_iterator_advance(entry, iter); +} + /** * Advance into a tree or skip over it if it is empty. * @@ -273,35 +312,12 @@ extern int git_iterator_cmp( extern int git_iterator_current_workdir_path( git_buf **path, git_iterator *iter); -/* Return index pointer if index iterator, else NULL */ -extern git_index *git_iterator_get_index(git_iterator *iter); - -typedef enum { - GIT_ITERATOR_STATUS_NORMAL = 0, - GIT_ITERATOR_STATUS_IGNORED = 1, - GIT_ITERATOR_STATUS_EMPTY = 2, - GIT_ITERATOR_STATUS_FILTERED = 3 -} git_iterator_status_t; - -/* Advance over a directory and check if it contains no files or just - * ignored files. - * - * In a tree or the index, all directories will contain files, but in the - * working directory it is possible to have an empty directory tree or a - * tree that only contains ignored files. Many Git operations treat these - * cases specially. This advances over a directory (presumably an - * untracked directory) but checks during the scan if there are any files - * and any non-ignored files. - */ -extern int git_iterator_advance_over_with_status( - const git_index_entry **entry, git_iterator_status_t *status, git_iterator *iter); - /** * Retrieve the index stored in the iterator. * - * Only implemented for the workdir iterator + * Only implemented for the workdir and index iterators. */ -extern int git_iterator_index(git_index **out, git_iterator *iter); +extern git_index *git_iterator_index(git_iterator *iter); typedef int (*git_iterator_walk_cb)( const git_index_entry **entries, From 4c88198a85932b69f779b2078f22b0231a18857e Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 10:17:20 -0400 Subject: [PATCH 11/35] iterator: test that we're at the end of iteration Ensure that we have hit the end of iteration; previously we tested that we saw all the values that we expected to see. We did not then ensure that we were at the end of the iteration (and that there were subsequently values in the iteration that we did *not* expect.) --- src/iterator.c | 4 +++- tests/repo/iterator.c | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/iterator.c b/src/iterator.c index ce0fb0ec9..88b7ed28a 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -437,8 +437,10 @@ GIT_INLINE(bool) iterator_has_started(git_iterator *iter, const char *path) GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path) { - if (iter->end == NULL || iter->ended == true) + if (iter->end == NULL) return false; + else if (iter->ended) + return true; iter->ended = (iter->prefixcomp(path, iter->end) > 0); return iter->ended; diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index ea2b37d10..e668caf44 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -16,6 +16,17 @@ void test_repo_iterator__cleanup(void) g_repo = NULL; } +static void assert_at_end(git_iterator *i, bool verbose) +{ + const git_index_entry *end; + int error = git_iterator_advance(&end, i); + + if (verbose && error != GIT_ITEROVER) + fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path); + + cl_git_fail_with(GIT_ITEROVER, error); +} + static void expect_iterator_items( git_iterator *i, int expected_flat, @@ -57,6 +68,7 @@ static void expect_iterator_items( break; } + assert_at_end(i, v); cl_assert_equal_i(expected_flat, count); cl_git_pass(git_iterator_reset(i)); @@ -103,6 +115,7 @@ static void expect_iterator_items( break; } + assert_at_end(i, v); cl_assert_equal_i(expected_total, count); } From c3d195f1d9a9ddbf6ac01ce40320ad122426da1f Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 11:45:44 -0400 Subject: [PATCH 12/35] iterator: expand workdir tests with pathlist Expand the workdir tests to validate the paths in case sensitive and insensitive tests. --- tests/repo/iterator.c | 286 +++++++++++++++++++++++++++++++++--------- 1 file changed, 224 insertions(+), 62 deletions(-) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index e668caf44..a8e668d17 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1370,15 +1370,13 @@ void test_repo_iterator__indexfilelist_icase(void) git_vector_free(&filelist); } -void test_repo_iterator__workdirfilelist(void) +void test_repo_iterator__workdir_pathlist(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; - bool default_icase; - int expect; - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_init(&filelist, 100, NULL)); cl_git_pass(git_vector_insert(&filelist, "a")); cl_git_pass(git_vector_insert(&filelist, "B")); cl_git_pass(git_vector_insert(&filelist, "c")); @@ -1393,87 +1391,251 @@ void test_repo_iterator__workdirfilelist(void) g_repo = cl_git_sandbox_init("icase"); - /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ - /* In this test we DO NOT force a case on the iteratords and verify default behavior. */ + /* Test iterators with default case sensitivity, without returning + * tree entries (but autoexpanding. + */ i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); + /* Case sensitive */ + { + const char *expected[] = { + "B", "D", "L/1", "a", "c", "e", "k/1", "k/a" }; + size_t expected_len = 8; - i_opts.start = "c"; - i_opts.end = NULL; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - default_icase = git_iterator_ignore_case(i); - /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ - expect = ((default_icase) ? 6 : 4); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.start = NULL; - i_opts.end = "e"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - default_icase = git_iterator_ignore_case(i); - /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ - expect = ((default_icase) ? 5 : 6); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Case INsensitive */ + { + const char *expected[] = { + "a", "B", "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 8; + + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case sensitive. */ + { + const char *expected[] = { "c", "e", "k/1", "k/a" }; + size_t expected_len = 4; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case INsensitive. */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 6; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case sensitive. */ + { + const char *expected[] = { "B", "D", "L/1", "a", "c", "e" }; + size_t expected_len = 6; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case INsensitive. */ + { + const char *expected[] = { "a", "B", "c", "D", "e" }; + size_t expected_len = 5; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "c", "e", "k/1" }; + size_t expected_len = 3; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "k/1" }; + size_t expected_len = 1; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a" }; + size_t expected_len = 5; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "k/1", "k/a" }; + size_t expected_len = 2; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } git_vector_free(&filelist); } -void test_repo_iterator__workdirfilelist_icase(void) +void test_repo_iterator__workdir_pathlist_with_dirs(void) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZ")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); + cl_git_pass(git_vector_init(&filelist, 5, NULL)); g_repo = cl_git_sandbox_init("icase"); - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; + /* Test that a prefix `k` matches folders, even without trailing slash */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k")); - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 1, NULL, 1, NULL); - git_iterator_free(i); + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); + /* Test that a `k/` matches a folder */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 2, NULL, 2, NULL); - git_iterator_free(i); + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* When the iterator is case sensitive, ensure we can't lookup the + * directory with the wrong case. + */ + { + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + /* Test that case insensitive matching works. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that case insensitive matching works without trailing slash. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } git_vector_free(&filelist); } From 908d8de8c31bc1be909da5825c4bcc16057141a4 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 12:15:55 -0400 Subject: [PATCH 13/35] iterator: workdir tests with submodules Ensure that when specifying start/end paths, or pathlists, that we deal correctly with submodules. --- tests/repo/iterator.c | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index a8e668d17..c49867925 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -2,6 +2,7 @@ #include "iterator.h" #include "repository.h" #include "fileops.h" +#include "../submodule/submodule_helpers.h" #include static git_repository *g_repo; @@ -1640,6 +1641,85 @@ void test_repo_iterator__workdir_pathlist_with_dirs(void) git_vector_free(&filelist); } +void test_repo_iterator__workdir_bounded_submodules(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_index *index; + git_tree *head; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = setup_fixture_submod2(); + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* Test that a submodule matches */ + { + const char *expected[] = { "sm_changed_head" }; + size_t expected_len = 1; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "sm_changed_head")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that a submodule never matches when suffixed with a '/' */ + { + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "sm_changed_head/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + /* Test that start/end work with a submodule */ + { + const char *expected[] = { "sm_changed_head", "sm_changed_index" }; + size_t expected_len = 2; + + i_opts.start = "sm_changed_head"; + i_opts.end = "sm_changed_index"; + i_opts.pathlist.strings = NULL; + i_opts.pathlist.count = 0; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that start and end do not allow '/' suffixes of submodules */ + { + i_opts.start = "sm_changed_head/"; + i_opts.end = "sm_changed_head/"; + i_opts.pathlist.strings = NULL; + i_opts.pathlist.count = 0; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + git_vector_free(&filelist); + git_index_free(index); + git_tree_free(head); +} + void test_repo_iterator__treefilelist(void) { git_iterator *i; From 85541f4390a6dc72f75b06ba6dd66c9d29173007 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 13:31:35 -0400 Subject: [PATCH 14/35] iterator: test workdir pathlist with deep paths In the workdir iterator we do some tricky things to step down into directories to look for things that are in our pathlist. Make sure that we don't confuse between folders that we're definitely going to return everything in and folders that we're only stepping down into to keep looking for matches. --- tests/repo/iterator.c | 159 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index c49867925..9b874751b 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1641,6 +1641,165 @@ void test_repo_iterator__workdir_pathlist_with_dirs(void) git_vector_free(&filelist); } +static void create_paths(const char *root, int depth) +{ + git_buf fullpath = GIT_BUF_INIT; + size_t root_len; + int i; + + cl_git_pass(git_buf_puts(&fullpath, root)); + cl_git_pass(git_buf_putc(&fullpath, '/')); + + root_len = fullpath.size; + + for (i = 0; i < 8; i++) { + bool file = (depth == 0 || (i % 2) == 0); + git_buf_truncate(&fullpath, root_len); + cl_git_pass(git_buf_printf(&fullpath, "item%d", i)); + + if (file) { + cl_git_rewritefile(fullpath.ptr, "This is a file!\n"); + } else { + cl_must_pass(p_mkdir(fullpath.ptr, 0777)); + + if (depth > 0) + create_paths(fullpath.ptr, (depth - 1)); + } + } +} + +void test_repo_iterator__workdir_pathlist_for_deeply_nested_item(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + create_paths(git_repository_workdir(g_repo), 3); + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { "item1/item3/item5/item7" }; + size_t expected_len = 1; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(4, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + }; + size_t expected_len = 8; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(11, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item1/item3/item0", + "item1/item3/item1/item0", "item1/item3/item1/item1", + "item1/item3/item1/item2", "item1/item3/item1/item3", + "item1/item3/item1/item4", "item1/item3/item1/item5", + "item1/item3/item1/item6", "item1/item3/item1/item7", + "item1/item3/item2", + "item1/item3/item3/item0", "item1/item3/item3/item1", + "item1/item3/item3/item2", "item1/item3/item3/item3", + "item1/item3/item3/item4", "item1/item3/item3/item5", + "item1/item3/item3/item6", "item1/item3/item3/item7", + "item1/item3/item4", + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + "item1/item3/item6", + "item1/item3/item7/item0", "item1/item3/item7/item1", + "item1/item3/item7/item2", "item1/item3/item7/item3", + "item1/item3/item7/item4", "item1/item3/item7/item5", + "item1/item3/item7/item6", "item1/item3/item7/item7", + }; + size_t expected_len = 36; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(42, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item0", "item1/item2", "item5/item7/item4", "item6", + "item7/item3/item1/item6" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6")); + cl_git_pass(git_vector_insert(&filelist, "item6")); + cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4")); + cl_git_pass(git_vector_insert(&filelist, "item1/item2")); + cl_git_pass(git_vector_insert(&filelist, "item0")); + + /* also add some things that don't exist or don't match the right type */ + cl_git_pass(git_vector_insert(&filelist, "item2/")); + cl_git_pass(git_vector_insert(&filelist, "itemN")); + cl_git_pass(git_vector_insert(&filelist, "item1/itemA")); + cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(14, i->stat_calls); + git_iterator_free(i); + } + + git_vector_free(&filelist); +} + void test_repo_iterator__workdir_bounded_submodules(void) { git_iterator *i; From 9fb2527f3cf24466ec5857323a22c4fe16dfb925 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 16:29:38 -0400 Subject: [PATCH 15/35] iterator: add tests for advance_over `git_iterator_advance_over` is a gnarly bit of code with no actual tests. --- tests/repo/iterator.c | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 9b874751b..8f5ab9efa 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1879,6 +1879,83 @@ void test_repo_iterator__workdir_bounded_submodules(void) git_tree_free(head); } +static void expect_advance_over( + git_iterator *i, + const char *expected_path, + git_iterator_status_t expected_status) +{ + const git_index_entry *entry; + git_iterator_status_t status; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + error = git_iterator_advance_over(&entry, &status, i); + cl_assert(!error || error == GIT_ITEROVER); + cl_assert_equal_i(expected_status, status); +} + +void test_repo_iterator__workdir_advance_over(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + + /* create an empty directory */ + cl_must_pass(p_mkdir("icase/empty", 0777)); + + /* create a directory in which all contents are ignored */ + cl_must_pass(p_mkdir("icase/all_ignored", 0777)); + cl_git_rewritefile("icase/all_ignored/one", "This is ignored\n"); + cl_git_rewritefile("icase/all_ignored/two", "This, too, is ignored\n"); + cl_git_rewritefile("icase/all_ignored/.gitignore", ".gitignore\none\ntwo\n"); + + /* create a directory in which not all contents are ignored */ + cl_must_pass(p_mkdir("icase/some_ignored", 0777)); + cl_git_rewritefile("icase/some_ignored/one", "This is ignored\n"); + cl_git_rewritefile("icase/some_ignored/two", "This is not ignored\n"); + cl_git_rewritefile("icase/some_ignored/.gitignore", ".gitignore\none\n"); + + /* create a directory which has some empty children */ + cl_must_pass(p_mkdir("icase/empty_children", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty1", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty2", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty3", 0777)); + + /* create a directory which will disappear! */ + cl_must_pass(p_mkdir("icase/missing_directory", 0777)); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + + cl_must_pass(p_rmdir("icase/missing_directory")); + + expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "all_ignored/", GIT_ITERATOR_STATUS_IGNORED); + expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "empty/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "empty_children/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "missing_directory/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "some_ignored/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); +} + void test_repo_iterator__treefilelist(void) { git_iterator *i; From 6bcddf88b3c7830d7c39727a025a81b638f8a265 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Wed, 16 Mar 2016 17:14:36 -0400 Subject: [PATCH 16/35] iterator: test `advance_over` with a pathlist --- tests/repo/iterator.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 8f5ab9efa..df3138640 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1956,6 +1956,66 @@ void test_repo_iterator__workdir_advance_over(void) git_iterator_free(i); } +void test_repo_iterator__workdir_advance_over_with_pathlist(void) +{ + git_vector pathlist = GIT_VECTOR_INIT; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + git_vector_insert(&pathlist, "dirA/subdir1/subdir2/file"); + git_vector_insert(&pathlist, "dirB/subdir1/subdir2"); + git_vector_insert(&pathlist, "dirC/subdir1/nonexistent"); + git_vector_insert(&pathlist, "dirD/subdir1/nonexistent"); + git_vector_insert(&pathlist, "dirD/subdir1/subdir2"); + git_vector_insert(&pathlist, "dirD/nonexistent"); + + i_opts.pathlist.strings = (char **)pathlist.contents; + i_opts.pathlist.count = pathlist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + + /* Create a directory that has a file that is included in our pathlist */ + cl_must_pass(p_mkdir("icase/dirA", 0777)); + cl_must_pass(p_mkdir("icase/dirA/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirA/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirA/subdir1/subdir2/file", "foo!"); + + /* Create a directory that has a directory that is included in our pathlist */ + cl_must_pass(p_mkdir("icase/dirB", 0777)); + cl_must_pass(p_mkdir("icase/dirB/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirB/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirB/subdir1/subdir2/file", "foo!"); + + /* Create a directory that would contain an entry in our pathlist, but + * that entry does not actually exist. We don't know this until we + * advance_over it. We want to distinguish this from an actually empty + * or ignored directory. + */ + cl_must_pass(p_mkdir("icase/dirC", 0777)); + cl_must_pass(p_mkdir("icase/dirC/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirC/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirC/subdir1/subdir2/file", "foo!"); + + /* Create a directory that has a mix of actual and nonexistent paths */ + cl_must_pass(p_mkdir("icase/dirD", 0777)); + cl_must_pass(p_mkdir("icase/dirD/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirD/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirD/subdir1/subdir2/file", "foo!"); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + + expect_advance_over(i, "dirA/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "dirB/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "dirC/", GIT_ITERATOR_STATUS_FILTERED); + expect_advance_over(i, "dirD/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + git_vector_free(&pathlist); +} + void test_repo_iterator__treefilelist(void) { git_iterator *i; From ae86aa5a68d584a6db0da320fd9b00c96cdaed47 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Wed, 16 Mar 2016 11:38:02 +0100 Subject: [PATCH 17/35] iterator: test pathlist handling for directories tree_iterator was only working properly for a pathlist containing file paths. In case of directory paths, it didn't match children which contradicts GIT_DIFF_DISABLE_PATHSPEC_MATCH and is different from index_iterator and fs_iterator. As a consequence head-to-index status reporting for a specific directory did not work properly -- all files have been reported as added. Include additional tests. --- tests/repo/iterator.c | 137 ++++++++++++++++++++++++++++++++++++++++ tests/status/worktree.c | 82 ++++++++++++++++++++++++ 2 files changed, 219 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index df3138640..158a6e453 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -1371,6 +1371,31 @@ void test_repo_iterator__indexfilelist_icase(void) git_vector_free(&filelist); } +void test_repo_iterator__indexfilelist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + git_index *index; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + git_index_free(index); + git_vector_free(&filelist); +} + void test_repo_iterator__workdir_pathlist(void) { git_iterator *i; @@ -2016,6 +2041,27 @@ void test_repo_iterator__workdir_advance_over_with_pathlist(void) git_vector_free(&pathlist); } +void test_repo_iterator__workdir_filelist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir/")); + + g_repo = cl_git_sandbox_init("testrepo2"); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + void test_repo_iterator__treefilelist(void) { git_iterator *i; @@ -2129,3 +2175,94 @@ void test_repo_iterator__treefilelist_icase(void) git_vector_free(&filelist); git_tree_free(tree); } + +void test_repo_iterator__tree_filelist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "subdir/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "subdir/subdir2")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 2, NULL, 2, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + +void test_repo_iterator__tree_filelist_with_directory_include_tree_nodes(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.flags |= GIT_ITERATOR_INCLUDE_TREES; + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + +void test_repo_iterator__tree_filelist_no_match(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + const git_index_entry *entry; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "nonexistent/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + cl_assert_equal_i(GIT_ITEROVER, git_iterator_current(&entry, i)); + + git_vector_free(&filelist); +} + diff --git a/tests/status/worktree.c b/tests/status/worktree.c index 6b823785c..d1117f410 100644 --- a/tests/status/worktree.c +++ b/tests/status/worktree.c @@ -1146,3 +1146,85 @@ void test_status_worktree__update_index_with_symlink_doesnt_change_mode(void) git_reference_free(head); } +static const char *testrepo2_subdir_paths[] = { + "subdir/README", + "subdir/new.txt", + "subdir/subdir2/README", + "subdir/subdir2/new.txt", +}; + +static const char *testrepo2_subdir_paths_icase[] = { + "subdir/new.txt", + "subdir/README", + "subdir/subdir2/new.txt", + "subdir/subdir2/README" +}; + +void test_status_worktree__with_directory_in_pathlist(void) +{ + git_repository *repo = cl_git_sandbox_init("testrepo2"); + git_index *index; + git_status_options opts = GIT_STATUS_OPTIONS_INIT; + git_status_list *statuslist; + const git_status_entry *status; + size_t i, entrycount; + bool native_ignore_case; + + cl_git_pass(git_repository_index(&index, repo)); + native_ignore_case = + (git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0; + git_index_free(index); + + opts.pathspec.count = 1; + opts.pathspec.strings = malloc(opts.pathspec.count * sizeof(char *)); + opts.pathspec.strings[0] = "subdir"; + opts.flags = + GIT_STATUS_OPT_DEFAULTS | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED | + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; + + opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY; + git_status_list_new(&statuslist, repo, &opts); + + entrycount = git_status_list_entrycount(statuslist); + cl_assert_equal_i(4, entrycount); + + for (i = 0; i < entrycount; i++) { + status = git_status_byindex(statuslist, i); + cl_assert_equal_i(0, status->status); + cl_assert_equal_s(native_ignore_case ? + testrepo2_subdir_paths_icase[i] : + testrepo2_subdir_paths[i], + status->index_to_workdir->old_file.path); + } + + opts.show = GIT_STATUS_SHOW_INDEX_ONLY; + git_status_list_new(&statuslist, repo, &opts); + + entrycount = git_status_list_entrycount(statuslist); + cl_assert_equal_i(4, entrycount); + + for (i = 0; i < entrycount; i++) { + status = git_status_byindex(statuslist, i); + cl_assert_equal_i(0, status->status); + cl_assert_equal_s(native_ignore_case ? + testrepo2_subdir_paths_icase[i] : + testrepo2_subdir_paths[i], + status->head_to_index->old_file.path); + } + + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + git_status_list_new(&statuslist, repo, &opts); + + entrycount = git_status_list_entrycount(statuslist); + cl_assert_equal_i(4, entrycount); + + for (i = 0; i < entrycount; i++) { + status = git_status_byindex(statuslist, i); + cl_assert_equal_i(0, status->status); + cl_assert_equal_s(native_ignore_case ? + testrepo2_subdir_paths_icase[i] : + testrepo2_subdir_paths[i], + status->index_to_workdir->old_file.path); + } +} From b6204260066843a00a271a11c2730a3069756d09 Mon Sep 17 00:00:00 2001 From: joshaber Date: Wed, 10 Feb 2016 13:46:14 -0500 Subject: [PATCH 18/35] Failing test. --- tests/status/worktree.c | 63 ++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/tests/status/worktree.c b/tests/status/worktree.c index d1117f410..97eff0b5c 100644 --- a/tests/status/worktree.c +++ b/tests/status/worktree.c @@ -218,6 +218,58 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void) cl_assert_equal_i(0, counts.wrong_sorted_path); } +static void stage_and_commit(git_repository *repo, const char *path) +{ + git_index *index; + + cl_git_pass(git_repository_index(&index, repo)); + cl_git_pass(git_index_add_bypath(index, path)); + cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n"); + git_index_free(index); +} + +void test_status_worktree__within_subdir(void) +{ + status_entry_counts counts; + git_repository *repo = cl_git_sandbox_init("status"); + git_status_options opts = GIT_STATUS_OPTIONS_INIT; + char *paths[] = { "zzz_new_dir" }; + git_strarray pathsArray; + + /* first alter the contents of the worktree */ + cl_git_mkfile("status/.new_file", "dummy"); + cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", 0777)); + cl_git_mkfile("status/zzz_new_dir/new_file", "dummy"); + cl_git_mkfile("status/zzz_new_file", "dummy"); + cl_git_mkfile("status/wut", "dummy"); + + stage_and_commit(repo, "zzz_new_dir/new_file"); + + /* now get status */ + memset(&counts, 0x0, sizeof(status_entry_counts)); + counts.expected_entry_count = entry_count4; + counts.expected_paths = entry_paths4; + counts.expected_statuses = entry_statuses4; + counts.debug = true; + + opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS | + GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH; + + pathsArray.count = 1; + pathsArray.strings = paths; + opts.pathspec = pathsArray; + + // We committed zzz_new_dir/new_file above. It shouldn't be reported. + cl_git_pass( + git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) + ); + + cl_assert_equal_i(0, counts.entry_count); + cl_assert_equal_i(0, counts.wrong_status_flags_count); + cl_assert_equal_i(0, counts.wrong_sorted_path); +} + /* this test is equivalent to t18-status.c:singlestatus0 */ void test_status_worktree__single_file(void) { @@ -692,16 +744,6 @@ void test_status_worktree__conflict_has_no_oid(void) git_status_list_free(statuslist); } -static void stage_and_commit(git_repository *repo, const char *path) -{ - git_index *index; - - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_add_bypath(index, path)); - cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n"); - git_index_free(index); -} - static void assert_ignore_case( bool should_ignore_case, int expected_lower_cased_file_status, @@ -1228,3 +1270,4 @@ void test_status_worktree__with_directory_in_pathlist(void) status->index_to_workdir->old_file.path); } } + From 6cd9573f54f0054618f23da585e0d8661b882e34 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 17 Mar 2016 15:09:38 -0400 Subject: [PATCH 19/35] iterator: test that we can `advance_into` empty dirs Prior iterator implementations returned `GIT_ENOTFOUND` when trying to advance into empty directories. Ensure that we no longer do that and simply handle them gracefully. --- tests/repo/iterator.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c index 158a6e453..b6017dae5 100644 --- a/tests/repo/iterator.c +++ b/tests/repo/iterator.c @@ -2041,6 +2041,65 @@ void test_repo_iterator__workdir_advance_over_with_pathlist(void) git_vector_free(&pathlist); } +static void expect_advance_into( + git_iterator *i, + const char *expected_path) +{ + const git_index_entry *entry; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + if (S_ISDIR(entry->mode)) + error = git_iterator_advance_into(&entry, i); + else + error = git_iterator_advance(&entry, i); + + cl_assert(!error || error == GIT_ITEROVER); +} + +void test_repo_iterator__workdir_advance_into(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_must_pass(p_mkdir("icase/Empty", 0777)); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_advance_into(i, "B"); + expect_advance_into(i, "D"); + expect_advance_into(i, "Empty/"); + expect_advance_into(i, "F"); + expect_advance_into(i, "H"); + expect_advance_into(i, "J"); + expect_advance_into(i, "L/"); + expect_advance_into(i, "L/1"); + expect_advance_into(i, "L/B"); + expect_advance_into(i, "L/D"); + expect_advance_into(i, "L/a"); + expect_advance_into(i, "L/c"); + expect_advance_into(i, "a"); + expect_advance_into(i, "c"); + expect_advance_into(i, "e"); + expect_advance_into(i, "g"); + expect_advance_into(i, "i"); + expect_advance_into(i, "k/"); + expect_advance_into(i, "k/1"); + expect_advance_into(i, "k/B"); + expect_advance_into(i, "k/D"); + expect_advance_into(i, "k/a"); + expect_advance_into(i, "k/c"); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); +} + void test_repo_iterator__workdir_filelist_with_directory(void) { git_iterator *i; From 0a2e10328aedae4e989c61f46c29f1fd26ae92d6 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 17 Mar 2016 15:19:45 -0400 Subject: [PATCH 20/35] iterator: drop `advance_into_or_over` Now that iterators do not return `GIT_ENOTFOUND` when advancing into an empty directory, we do not need a special `advance_into_or_over` function. --- src/checkout.c | 2 +- src/iterator.h | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index 0fbb7fc16..fed1819aa 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -657,7 +657,7 @@ static int checkout_action( if (cmp == 0) { if (wd->mode == GIT_FILEMODE_TREE) { /* case 2 - entry prefixed by workdir tree */ - error = git_iterator_advance_into_or_over(wditem, workdir); + error = git_iterator_advance_into(wditem, workdir); if (error < 0 && error != GIT_ITEROVER) goto done; continue; diff --git a/src/iterator.h b/src/iterator.h index d64d63f8d..85444f11f 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -222,25 +222,6 @@ GIT_INLINE(int) git_iterator_advance_over( return git_iterator_advance(entry, iter); } -/** - * Advance into a tree or skip over it if it is empty. - * - * Because `git_iterator_advance_into` may return GIT_ENOTFOUND if the - * directory is empty (only with filesystem and working directory - * iterators) and a common response is to just call `git_iterator_advance` - * when that happens, this bundles the two into a single simple call. - */ -GIT_INLINE(int) git_iterator_advance_into_or_over( - const git_index_entry **entry, git_iterator *iter) -{ - int error = iter->cb->advance_into(entry, iter); - if (error == GIT_ENOTFOUND) { - giterr_clear(); - error = iter->cb->advance(entry, iter); - } - return error; -} - /** * Go back to the start of the iteration. */ From 6788553231699d4bb8e0ea0c05fdf83407bfaf6f Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 17 Mar 2016 15:29:21 -0400 Subject: [PATCH 21/35] diff: stop processing nitem when its removed When a directory is removed out from underneath us, stop trying to manipulate it. --- src/diff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff.c b/src/diff.c index a2bfcbf6a..5b70998f4 100644 --- a/src/diff.c +++ b/src/diff.c @@ -1091,7 +1091,7 @@ static int handle_unmatched_new_item( /* if directory is empty, can't advance into it, so either skip * it or ignore it */ - if (contains_oitem) + if (error == GIT_ENOTFOUND || contains_oitem) return iterator_advance(&info->nitem, info->new_iter); delta_type = GIT_DELTA_IGNORED; } From df25daef9b07174124989aaf993cc20c9e9b0660 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 4 Jan 2016 12:12:24 -0500 Subject: [PATCH 22/35] Added clar test for #3568 --- tests/diff/racediffiter.c | 129 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/diff/racediffiter.c diff --git a/tests/diff/racediffiter.c b/tests/diff/racediffiter.c new file mode 100644 index 000000000..d364d6b21 --- /dev/null +++ b/tests/diff/racediffiter.c @@ -0,0 +1,129 @@ +/* This test exercises the problem described in +** https://github.com/libgit2/libgit2/pull/3568 +** where deleting a directory during a diff/status +** operation can cause an access violation. +** +** The "test_diff_racediffiter__basic() test confirms +** the normal operation of diff on the given repo. +** +** The "test_diff_racediffiter__racy_rmdir() test +** uses the new diff progress callback to delete +** a directory (after the initial readdir() and +** before the directory itself is visited) causing +** the recursion and iteration to fail. +*/ + +#include "clar_libgit2.h" +#include "diff_helpers.h" + +#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) + +void test_diff_racediffiter__initialize(void) +{ +} + +void test_diff_racediffiter__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +typedef struct +{ + const char *path; + git_delta_t t; + +} basic_payload; + +static int notify_cb__basic( + const git_diff *diff_so_far, + const git_diff_delta *delta_to_add, + const char *matched_pathspec, + void *payload) +{ + basic_payload *exp = (basic_payload *)payload; + basic_payload *e; + + GIT_UNUSED(diff_so_far); + GIT_UNUSED(matched_pathspec); + + for (e = exp; e->path; e++) { + if (strcmp(e->path, delta_to_add->new_file.path) == 0) { + cl_assert_equal_i(e->t, delta_to_add->status); + return 0; + } + } + cl_assert(0); + return GIT_ENOTFOUND; +} + +void test_diff_racediffiter__basic(void) +{ + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_repository *repo = cl_git_sandbox_init("diff"); + git_diff *diff; + + basic_payload exp_a[] = { + { "another.txt", GIT_DELTA_MODIFIED }, + { "readme.txt", GIT_DELTA_MODIFIED }, + { "zzzzz/", GIT_DELTA_IGNORED }, + { NULL, 0 } + }; + + cl_must_pass(p_mkdir("diff/zzzzz", 0777)); + + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; + opts.notify_cb = notify_cb__basic; + opts.payload = exp_a; + + cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); + + git_diff_free(diff); +} + + +typedef struct { + bool first_time; + const char *dir; + basic_payload *basic_payload; +} racy_payload; + +static int notify_cb__racy_rmdir( + const git_diff *diff_so_far, + const git_diff_delta *delta_to_add, + const char *matched_pathspec, + void *payload) +{ + racy_payload *pay = (racy_payload *)payload; + + if (pay->first_time) { + cl_must_pass(p_rmdir(pay->dir)); + pay->first_time = false; + } + + return notify_cb__basic(diff_so_far, delta_to_add, matched_pathspec, pay->basic_payload); +} + +void test_diff_racediffiter__racy(void) +{ + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_repository *repo = cl_git_sandbox_init("diff"); + git_diff *diff; + + basic_payload exp_a[] = { + { "another.txt", GIT_DELTA_MODIFIED }, + { "readme.txt", GIT_DELTA_MODIFIED }, + { NULL, 0 } + }; + + racy_payload pay = { true, "diff/zzzzz", exp_a }; + + cl_must_pass(p_mkdir("diff/zzzzz", 0777)); + + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; + opts.notify_cb = notify_cb__racy_rmdir; + opts.payload = &pay; + + cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); + + git_diff_free(diff); +} From de034cd23929734dde37e53ce5eba4563b9c914c Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Mar 2016 10:59:38 -0400 Subject: [PATCH 23/35] iterator: give the tests a proper hierarchy Iterator tests were split over repo::iterator and diff::iterator, with duplication between the two. Move them to iterator::index, iterator::tree, and iterator::workdir. --- tests/diff/iterator.c | 1005 ------------- tests/iterator/index.c | 731 +++++++++ tests/iterator/iterator_helpers.c | 110 ++ tests/iterator/iterator_helpers.h | 8 + tests/iterator/tree.c | 1064 +++++++++++++ tests/iterator/workdir.c | 1462 ++++++++++++++++++ tests/repo/iterator.c | 2327 ----------------------------- 7 files changed, 3375 insertions(+), 3332 deletions(-) delete mode 100644 tests/diff/iterator.c create mode 100644 tests/iterator/index.c create mode 100644 tests/iterator/iterator_helpers.c create mode 100644 tests/iterator/iterator_helpers.h create mode 100644 tests/iterator/tree.c create mode 100644 tests/iterator/workdir.c delete mode 100644 tests/repo/iterator.c diff --git a/tests/diff/iterator.c b/tests/diff/iterator.c deleted file mode 100644 index 4c9585047..000000000 --- a/tests/diff/iterator.c +++ /dev/null @@ -1,1005 +0,0 @@ -#include "clar_libgit2.h" -#include "diff_helpers.h" -#include "iterator.h" -#include "tree.h" - -void test_diff_iterator__initialize(void) -{ - /* since we are doing tests with different sandboxes, defer setup - * to the actual tests. cleanup will still be done in the global - * cleanup function so that assertion failures don't result in a - * missed cleanup. - */ -} - -void test_diff_iterator__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - - -/* -- TREE ITERATOR TESTS -- */ - -static void tree_iterator_test( - const char *sandbox, - const char *treeish, - const char *start, - const char *end, - int expected_count, - const char **expected_values) -{ - git_tree *t; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - const git_index_entry *entry; - int error, count = 0, count_post_reset = 0; - git_repository *repo = cl_git_sandbox_init(sandbox); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.start = start; - i_opts.end = end; - - cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); - cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); - - /* test loop */ - while (!(error = git_iterator_advance(&entry, i))) { - cl_assert(entry); - if (expected_values != NULL) - cl_assert_equal_s(expected_values[count], entry->path); - count++; - } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(!entry); - cl_assert_equal_i(expected_count, count); - - /* test reset */ - cl_git_pass(git_iterator_reset(i)); - - while (!(error = git_iterator_advance(&entry, i))) { - cl_assert(entry); - if (expected_values != NULL) - cl_assert_equal_s(expected_values[count_post_reset], entry->path); - count_post_reset++; - } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(!entry); - cl_assert_equal_i(count, count_post_reset); - - git_iterator_free(i); - git_tree_free(t); -} - -/* results of: git ls-tree -r --name-only 605812a */ -const char *expected_tree_0[] = { - ".gitattributes", - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_0(void) -{ - tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0); -} - -/* results of: git ls-tree -r --name-only 6bab5c79 */ -const char *expected_tree_1[] = { - ".gitattributes", - "attr0", - "attr1", - "attr2", - "attr3", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "subdir/.gitattributes", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_1(void) -{ - tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1); -} - -/* results of: git ls-tree -r --name-only 26a125ee1 */ -const char *expected_tree_2[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__tree_2(void) -{ - tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2); -} - -/* $ git ls-tree -r --name-only 0017bd4ab1e */ -const char *expected_tree_3[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file" -}; - -void test_diff_iterator__tree_3(void) -{ - tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3); -} - -/* $ git ls-tree -r --name-only 24fa9a9fc4e202313e24b648087495441dab432b */ -const char *expected_tree_4[] = { - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "sub/abc", - "sub/file", - "sub/sub/file", - "sub/sub/subsub.txt", - "sub/subdir_test1", - "sub/subdir_test2.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", - NULL -}; - -void test_diff_iterator__tree_4(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL, - 23, expected_tree_4); -} - -void test_diff_iterator__tree_4_ranged(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "sub", "sub", - 11, &expected_tree_4[12]); -} - -const char *expected_tree_ranged_0[] = { - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - NULL -}; - -void test_diff_iterator__tree_ranged_0(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "git", "root", - 7, expected_tree_ranged_0); -} - -const char *expected_tree_ranged_1[] = { - "sub/subdir_test2.txt", - NULL -}; - -void test_diff_iterator__tree_ranged_1(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "sub/subdir_test2.txt", "sub/subdir_test2.txt", - 1, expected_tree_ranged_1); -} - -void test_diff_iterator__tree_range_empty_0(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "empty", "empty", 0, NULL); -} - -void test_diff_iterator__tree_range_empty_1(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - "z_empty_after", NULL, 0, NULL); -} - -void test_diff_iterator__tree_range_empty_2(void) -{ - tree_iterator_test( - "attr", "24fa9a9fc4e202313e24b648087495441dab432b", - NULL, ".aaa_empty_before", 0, NULL); -} - -static void check_tree_entry( - git_iterator *i, - const char *oid, - const char *oid_p, - const char *oid_pp, - const char *oid_ppp) -{ - const git_index_entry *ie; - const git_tree_entry *te; - const git_tree *tree; - - cl_git_pass(git_iterator_current_tree_entry(&te, i)); - cl_assert(te); - cl_assert(git_oid_streq(te->oid, oid) == 0); - - cl_git_pass(git_iterator_current(&ie, i)); - - if (oid_p) { - cl_git_pass(git_iterator_current_parent_tree(&tree, i, 0)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); - } - - if (oid_pp) { - cl_git_pass(git_iterator_current_parent_tree(&tree, i, 1)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); - } - - if (oid_ppp) { - cl_git_pass(git_iterator_current_parent_tree(&tree, i, 2)); - cl_assert(tree); - cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); - } -} - -void test_diff_iterator__tree_special_functions(void) -{ - git_tree *t; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - const git_index_entry *entry; - git_repository *repo = cl_git_sandbox_init("attr"); - int error, cases = 0; - const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; - - t = resolve_commit_oid_to_tree( - repo, "24fa9a9fc4e202313e24b648087495441dab432b"); - cl_assert(t != NULL); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); - - while (!(error = git_iterator_advance(&entry, i))) { - cl_assert(entry); - - if (strcmp(entry->path, "sub/file") == 0) { - cases++; - check_tree_entry( - i, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "ecb97df2a174987475ac816e3847fc8e9f6c596b", - rootoid, NULL); - } - else if (strcmp(entry->path, "sub/sub/subsub.txt") == 0) { - cases++; - check_tree_entry( - i, "9e5bdc47d6a80f2be0ea3049ad74231b94609242", - "4e49ba8c5b6c32ff28cd9dcb60be34df50fcc485", - "ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid); - } - else if (strcmp(entry->path, "subdir/.gitattributes") == 0) { - cases++; - check_tree_entry( - i, "99eae476896f4907224978b88e5ecaa6c5bb67a9", - "9fb40b6675dde60b5697afceae91b66d908c02d9", - rootoid, NULL); - } - else if (strcmp(entry->path, "subdir2/subdir2_test1") == 0) { - cases++; - check_tree_entry( - i, "dccada462d3df8ac6de596fb8c896aba9344f941", - "2929de282ce999e95183aedac6451d3384559c4b", - rootoid, NULL); - } - } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(!entry); - cl_assert_equal_i(4, cases); - - git_iterator_free(i); - git_tree_free(t); -} - -/* -- INDEX ITERATOR TESTS -- */ - -static void index_iterator_test( - const char *sandbox, - const char *start, - const char *end, - git_iterator_flag_t flags, - int expected_count, - const char **expected_names, - const char **expected_oids) -{ - git_index *index; - git_iterator *i; - const git_index_entry *entry; - int error, count = 0, caps; - git_repository *repo = cl_git_sandbox_init(sandbox); - git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; - - cl_git_pass(git_repository_index(&index, repo)); - caps = git_index_caps(index); - - iter_opts.flags = flags; - iter_opts.start = start; - iter_opts.end = end; - - cl_git_pass(git_iterator_for_index(&i, repo, index, &iter_opts)); - - while (!(error = git_iterator_advance(&entry, i))) { - cl_assert(entry); - - if (expected_names != NULL) - cl_assert_equal_s(expected_names[count], entry->path); - - if (expected_oids != NULL) { - git_oid oid; - cl_git_pass(git_oid_fromstr(&oid, expected_oids[count])); - cl_assert_equal_oid(&oid, &entry->id); - } - - count++; - } - - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(!entry); - cl_assert_equal_i(expected_count, count); - - git_iterator_free(i); - - cl_assert(caps == git_index_caps(index)); - git_index_free(index); -} - -static const char *expected_index_0[] = { - "attr0", - "attr1", - "attr2", - "attr3", - "binfile", - "gitattributes", - "macro_bad", - "macro_test", - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", - "sub/abc", - "sub/file", - "sub/sub/file", - "sub/sub/subsub.txt", - "sub/subdir_test1", - "sub/subdir_test2.txt", - "subdir/.gitattributes", - "subdir/abc", - "subdir/subdir_test1", - "subdir/subdir_test2.txt", - "subdir2/subdir2_test1", -}; - -static const char *expected_index_oids_0[] = { - "556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3", - "3b74db7ab381105dc0d28f8295a77f6a82989292", - "2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2", - "c485abe35abd4aa6fd83b076a78bbea9e2e7e06c", - "d800886d9c86731ae5c4a62b0b77c437015e00d2", - "2b40c5aca159b04ea8d20ffe36cdf8b09369b14a", - "5819a185d77b03325aaf87cafc771db36f6ddca7", - "ff69f8639ce2e6010b3f33a74160aad98b48da2b", - "45141a79a77842c59a63229403220a4e4be74e3d", - "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", - "108bb4e7fd7b16490dc33ff7d972151e73d7166e", - "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", - "3e42ffc54a663f9401cc25843d6c0e71a33e4249", - "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", - "9e5bdc47d6a80f2be0ea3049ad74231b94609242", - "e563cf4758f0d646f1b14b76016aa17fa9e549a4", - "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", - "99eae476896f4907224978b88e5ecaa6c5bb67a9", - "3e42ffc54a663f9401cc25843d6c0e71a33e4249", - "e563cf4758f0d646f1b14b76016aa17fa9e549a4", - "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", - "dccada462d3df8ac6de596fb8c896aba9344f941" -}; - -void test_diff_iterator__index_0(void) -{ - index_iterator_test( - "attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0), - expected_index_0, expected_index_oids_0); -} - -static const char *expected_index_range[] = { - "root_test1", - "root_test2", - "root_test3", - "root_test4.txt", -}; - -static const char *expected_index_oids_range[] = { - "45141a79a77842c59a63229403220a4e4be74e3d", - "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", - "108bb4e7fd7b16490dc33ff7d972151e73d7166e", - "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", -}; - -void test_diff_iterator__index_range(void) -{ - index_iterator_test( - "attr", "root", "root", 0, ARRAY_SIZE(expected_index_range), - expected_index_range, expected_index_oids_range); -} - -void test_diff_iterator__index_range_empty_0(void) -{ - index_iterator_test( - "attr", "empty", "empty", 0, 0, NULL, NULL); -} - -void test_diff_iterator__index_range_empty_1(void) -{ - index_iterator_test( - "attr", "z_empty_after", NULL, 0, 0, NULL, NULL); -} - -void test_diff_iterator__index_range_empty_2(void) -{ - index_iterator_test( - "attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL); -} - -static const char *expected_index_1[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_new_file", - "staged_new_file_deleted_file", - "staged_new_file_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", -}; - -static const char* expected_index_oids_1[] = { - "a0de7e0ac200c489c41c59dfa910154a70264e6e", - "5452d32f1dd538eb0405e8a83cc185f79e25e80f", - "452e4244b5d083ddf0460acf1ecc74db9dcfa11a", - "55d316c9ba708999f1918e9677d01dfcae69c6b9", - "a6be623522ce87a1d862128ac42672604f7b468b", - "906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8", - "529a16e8e762d4acb7b9636ff540a00831f9155a", - "90b8c29d8ba39434d1c63e1b093daaa26e5bd972", - "ed062903b8f6f3dccb2fa81117ba6590944ef9bd", - "e8ee89e15bbe9b20137715232387b3de5b28972e", - "53ace0d1cc1145a5f4fe4f78a186a60263190733", - "1888c805345ba265b0ee9449b8877b6064592058", - "a6191982709b746d5650e93c2acf34ef74e11504" -}; - -void test_diff_iterator__index_1(void) -{ - index_iterator_test( - "status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1), - expected_index_1, expected_index_oids_1); -} - -static const char *expected_index_cs[] = { - "B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c", - "a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c", -}; - -static const char *expected_index_ci[] = { - "a", "B", "c", "D", "e", "F", "g", "H", "i", "J", - "k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D", -}; - -void test_diff_iterator__index_case_folding(void) -{ - git_buf path = GIT_BUF_INIT; - int fs_is_ci = 0; - - cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg")); - fs_is_ci = git_path_exists(path.ptr); - git_buf_free(&path); - - index_iterator_test( - "icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs), - fs_is_ci ? expected_index_ci : expected_index_cs, NULL); - - cl_git_sandbox_cleanup(); - - index_iterator_test( - "icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE, - ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL); - - cl_git_sandbox_cleanup(); - - index_iterator_test( - "icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE, - ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL); -} - -/* -- WORKDIR ITERATOR TESTS -- */ - -static void workdir_iterator_test( - const char *sandbox, - const char *start, - const char *end, - int expected_count, - int expected_ignores, - const char **expected_names, - const char *an_ignored_name) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - const git_index_entry *entry; - int error, count = 0, count_all = 0, count_all_post_reset = 0; - git_repository *repo = cl_git_sandbox_init(sandbox); - - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - i_opts.start = start; - i_opts.end = end; - - cl_git_pass(git_iterator_for_workdir(&i, repo, NULL, NULL, &i_opts)); - - error = git_iterator_current(&entry, i); - cl_assert((error == 0 && entry != NULL) || - (error == GIT_ITEROVER && entry == NULL)); - - while (entry != NULL) { - int ignored = git_iterator_current_is_ignored(i); - - if (S_ISDIR(entry->mode)) { - cl_git_pass(git_iterator_advance_into(&entry, i)); - continue; - } - - if (expected_names != NULL) - cl_assert_equal_s(expected_names[count_all], entry->path); - - if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0) - cl_assert(ignored); - - if (!ignored) - count++; - count_all++; - - error = git_iterator_advance(&entry, i); - - cl_assert((error == 0 && entry != NULL) || - (error == GIT_ITEROVER && entry == NULL)); - } - - cl_assert_equal_i(expected_count, count); - cl_assert_equal_i(expected_count + expected_ignores, count_all); - - cl_git_pass(git_iterator_reset(i)); - - error = git_iterator_current(&entry, i); - cl_assert((error == 0 && entry != NULL) || - (error == GIT_ITEROVER && entry == NULL)); - - while (entry != NULL) { - if (S_ISDIR(entry->mode)) { - cl_git_pass(git_iterator_advance_into(&entry, i)); - continue; - } - - if (expected_names != NULL) - cl_assert_equal_s( - expected_names[count_all_post_reset], entry->path); - count_all_post_reset++; - - error = git_iterator_advance(&entry, i); - cl_assert(error == 0 || error == GIT_ITEROVER); - } - - cl_assert_equal_i(count_all, count_all_post_reset); - - git_iterator_free(i); -} - -void test_diff_iterator__workdir_0(void) -{ - workdir_iterator_test("attr", NULL, NULL, 23, 5, NULL, "ign"); -} - -static const char *status_paths[] = { - "current_file", - "ignored_file", - "modified_file", - "new_file", - "staged_changes", - "staged_changes_modified_file", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/modified_file", - "subdir/new_file", - "\xe8\xbf\x99", - NULL -}; - -void test_diff_iterator__workdir_1(void) -{ - workdir_iterator_test( - "status", NULL, NULL, 13, 1, status_paths, "ignored_file"); -} - -static const char *status_paths_range_0[] = { - "staged_changes", - "staged_changes_modified_file", - "staged_delete_modified_file", - "staged_new_file", - "staged_new_file_modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_0(void) -{ - workdir_iterator_test( - "status", "staged", "staged", 5, 0, status_paths_range_0, NULL); -} - -static const char *status_paths_range_1[] = { - "modified_file", NULL -}; - -void test_diff_iterator__workdir_1_ranged_1(void) -{ - workdir_iterator_test( - "status", "modified_file", "modified_file", - 1, 0, status_paths_range_1, NULL); -} - -static const char *status_paths_range_3[] = { - "subdir.txt", - "subdir/current_file", - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_3(void) -{ - workdir_iterator_test( - "status", "subdir", "subdir/modified_file", - 3, 0, status_paths_range_3, NULL); -} - -static const char *status_paths_range_4[] = { - "subdir/current_file", - "subdir/modified_file", - "subdir/new_file", - "\xe8\xbf\x99", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_4(void) -{ - workdir_iterator_test( - "status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL); -} - -static const char *status_paths_range_5[] = { - "subdir/modified_file", - NULL -}; - -void test_diff_iterator__workdir_1_ranged_5(void) -{ - workdir_iterator_test( - "status", "subdir/modified_file", "subdir/modified_file", - 1, 0, status_paths_range_5, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_0(void) -{ - workdir_iterator_test( - "status", "\xff_does_not_exist", NULL, - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_1(void) -{ - workdir_iterator_test( - "status", "empty", "empty", - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_1_ranged_empty_2(void) -{ - workdir_iterator_test( - "status", NULL, "aaaa_empty_before", - 0, 0, NULL, NULL); -} - -void test_diff_iterator__workdir_builtin_ignores(void) -{ - git_repository *repo = cl_git_sandbox_init("attr"); - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - const git_index_entry *entry; - int idx; - static struct { - const char *path; - bool ignored; - } expected[] = { - { "dir/", true }, - { "file", false }, - { "ign", true }, - { "macro_bad", false }, - { "macro_test", false }, - { "root_test1", false }, - { "root_test2", false }, - { "root_test3", false }, - { "root_test4.txt", false }, - { "sub/", false }, - { "sub/.gitattributes", false }, - { "sub/abc", false }, - { "sub/dir/", true }, - { "sub/file", false }, - { "sub/ign/", true }, - { "sub/sub/", false }, - { "sub/sub/.gitattributes", false }, - { "sub/sub/dir", false }, /* file is not actually a dir */ - { "sub/sub/file", false }, - { NULL, false } - }; - - cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); - cl_git_mkfile("attr/sub/.git", "whatever"); - - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - i_opts.start = "dir"; - i_opts.end = "sub/sub/file"; - - cl_git_pass(git_iterator_for_workdir( - &i, repo, NULL, NULL, &i_opts)); - cl_git_pass(git_iterator_current(&entry, i)); - - for (idx = 0; entry != NULL; ++idx) { - int ignored = git_iterator_current_is_ignored(i); - - cl_assert_equal_s(expected[idx].path, entry->path); - cl_assert_(ignored == expected[idx].ignored, expected[idx].path); - - if (!ignored && - (entry->mode == GIT_FILEMODE_TREE || - entry->mode == GIT_FILEMODE_COMMIT)) - { - /* it is possible to advance "into" a submodule */ - cl_git_pass(git_iterator_advance_into(&entry, i)); - } else { - int error = git_iterator_advance(&entry, i); - cl_assert(!error || error == GIT_ITEROVER); - } - } - - cl_assert(expected[idx].path == NULL); - - git_iterator_free(i); -} - -static void check_wd_first_through_third_range( - git_repository *repo, const char *start, const char *end) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - const git_index_entry *entry; - int error, idx; - static const char *expected[] = { "FIRST", "second", "THIRD", NULL }; - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - i_opts.start = start; - i_opts.end = end; - - cl_git_pass(git_iterator_for_workdir( - &i, repo, NULL, NULL, &i_opts)); - cl_git_pass(git_iterator_current(&entry, i)); - - for (idx = 0; entry != NULL; ++idx) { - cl_assert_equal_s(expected[idx], entry->path); - - error = git_iterator_advance(&entry, i); - cl_assert(!error || error == GIT_ITEROVER); - } - - cl_assert(expected[idx] == NULL); - - git_iterator_free(i); -} - -void test_diff_iterator__workdir_handles_icase_range(void) -{ - git_repository *repo; - - repo = cl_git_sandbox_init("empty_standard_repo"); - cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); - - cl_git_mkfile("empty_standard_repo/before", "whatever\n"); - cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n"); - cl_git_mkfile("empty_standard_repo/second", "whatever\n"); - cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n"); - cl_git_mkfile("empty_standard_repo/zafter", "whatever\n"); - cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n"); - - check_wd_first_through_third_range(repo, "first", "third"); - check_wd_first_through_third_range(repo, "FIRST", "THIRD"); - check_wd_first_through_third_range(repo, "first", "THIRD"); - check_wd_first_through_third_range(repo, "FIRST", "third"); - check_wd_first_through_third_range(repo, "FirSt", "tHiRd"); -} - -static void check_tree_range( - git_repository *repo, - const char *start, - const char *end, - bool ignore_case, - int expected_count) -{ - git_tree *head; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - int error, count; - - i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.start = start; - i_opts.end = end; - - cl_git_pass(git_repository_head_tree(&head, repo)); - - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - - for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) - /* count em up */; - - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(expected_count, count); - - git_iterator_free(i); - git_tree_free(head); -} - -void test_diff_iterator__tree_handles_icase_range(void) -{ - git_repository *repo; - - repo = cl_git_sandbox_init("testrepo"); - - check_tree_range(repo, "B", "C", false, 0); - check_tree_range(repo, "B", "C", true, 1); - check_tree_range(repo, "b", "c", false, 1); - check_tree_range(repo, "b", "c", true, 1); - - check_tree_range(repo, "a", "z", false, 3); - check_tree_range(repo, "a", "z", true, 4); - check_tree_range(repo, "A", "Z", false, 1); - check_tree_range(repo, "A", "Z", true, 4); - check_tree_range(repo, "a", "Z", false, 0); - check_tree_range(repo, "a", "Z", true, 4); - check_tree_range(repo, "A", "z", false, 4); - check_tree_range(repo, "A", "z", true, 4); - - check_tree_range(repo, "new.txt", "new.txt", true, 1); - check_tree_range(repo, "new.txt", "new.txt", false, 1); - check_tree_range(repo, "README", "README", true, 1); - check_tree_range(repo, "README", "README", false, 1); -} - -static void check_index_range( - git_repository *repo, - const char *start, - const char *end, - bool ignore_case, - int expected_count) -{ - git_index *index; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - int error, count, caps; - bool is_ignoring_case; - - cl_git_pass(git_repository_index(&index, repo)); - - caps = git_index_caps(index); - is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); - - if (ignore_case != is_ignoring_case) - cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE)); - - i_opts.flags = 0; - i_opts.start = start; - i_opts.end = end; - - cl_git_pass(git_iterator_for_index(&i, repo, index, &i_opts)); - - cl_assert(git_iterator_ignore_case(i) == ignore_case); - - for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) - /* count em up */; - - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(expected_count, count); - - git_iterator_free(i); - git_index_free(index); -} - -void test_diff_iterator__index_handles_icase_range(void) -{ - git_repository *repo; - git_index *index; - git_tree *head; - - repo = cl_git_sandbox_init("testrepo"); - - /* reset index to match HEAD */ - cl_git_pass(git_repository_head_tree(&head, repo)); - cl_git_pass(git_repository_index(&index, repo)); - cl_git_pass(git_index_read_tree(index, head)); - cl_git_pass(git_index_write(index)); - git_tree_free(head); - git_index_free(index); - - /* do some ranged iterator checks toggling case sensitivity */ - check_index_range(repo, "B", "C", false, 0); - check_index_range(repo, "B", "C", true, 1); - check_index_range(repo, "a", "z", false, 3); - check_index_range(repo, "a", "z", true, 4); -} diff --git a/tests/iterator/index.c b/tests/iterator/index.c new file mode 100644 index 000000000..5524cdf8a --- /dev/null +++ b/tests/iterator/index.c @@ -0,0 +1,731 @@ +#include "clar_libgit2.h" +#include "iterator.h" +#include "repository.h" +#include "fileops.h" +#include "iterator_helpers.h" +#include "../submodule/submodule_helpers.h" +#include + +static git_repository *g_repo; + +void test_iterator_index__initialize(void) +{ +} + +void test_iterator_index__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +static void index_iterator_test( + const char *sandbox, + const char *start, + const char *end, + git_iterator_flag_t flags, + int expected_count, + const char **expected_names, + const char **expected_oids) +{ + git_index *index; + git_iterator *i; + const git_index_entry *entry; + int error, count = 0, caps; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init(sandbox); + + cl_git_pass(git_repository_index(&index, g_repo)); + caps = git_index_caps(index); + + iter_opts.flags = flags; + iter_opts.start = start; + iter_opts.end = end; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &iter_opts)); + + while (!(error = git_iterator_advance(&entry, i))) { + cl_assert(entry); + + if (expected_names != NULL) + cl_assert_equal_s(expected_names[count], entry->path); + + if (expected_oids != NULL) { + git_oid oid; + cl_git_pass(git_oid_fromstr(&oid, expected_oids[count])); + cl_assert_equal_oid(&oid, &entry->id); + } + + count++; + } + + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert(!entry); + cl_assert_equal_i(expected_count, count); + + git_iterator_free(i); + + cl_assert(caps == git_index_caps(index)); + git_index_free(index); +} + +static const char *expected_index_0[] = { + "attr0", + "attr1", + "attr2", + "attr3", + "binfile", + "gitattributes", + "macro_bad", + "macro_test", + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", + "sub/abc", + "sub/file", + "sub/sub/file", + "sub/sub/subsub.txt", + "sub/subdir_test1", + "sub/subdir_test2.txt", + "subdir/.gitattributes", + "subdir/abc", + "subdir/subdir_test1", + "subdir/subdir_test2.txt", + "subdir2/subdir2_test1", +}; + +static const char *expected_index_oids_0[] = { + "556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3", + "3b74db7ab381105dc0d28f8295a77f6a82989292", + "2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2", + "c485abe35abd4aa6fd83b076a78bbea9e2e7e06c", + "d800886d9c86731ae5c4a62b0b77c437015e00d2", + "2b40c5aca159b04ea8d20ffe36cdf8b09369b14a", + "5819a185d77b03325aaf87cafc771db36f6ddca7", + "ff69f8639ce2e6010b3f33a74160aad98b48da2b", + "45141a79a77842c59a63229403220a4e4be74e3d", + "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", + "108bb4e7fd7b16490dc33ff7d972151e73d7166e", + "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", + "3e42ffc54a663f9401cc25843d6c0e71a33e4249", + "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", + "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", + "9e5bdc47d6a80f2be0ea3049ad74231b94609242", + "e563cf4758f0d646f1b14b76016aa17fa9e549a4", + "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", + "99eae476896f4907224978b88e5ecaa6c5bb67a9", + "3e42ffc54a663f9401cc25843d6c0e71a33e4249", + "e563cf4758f0d646f1b14b76016aa17fa9e549a4", + "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78", + "dccada462d3df8ac6de596fb8c896aba9344f941" +}; + +void test_iterator_index__0(void) +{ + index_iterator_test( + "attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0), + expected_index_0, expected_index_oids_0); +} + +static const char *expected_index_1[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_new_file", + "staged_new_file_deleted_file", + "staged_new_file_modified_file", + "subdir.txt", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", +}; + +static const char* expected_index_oids_1[] = { + "a0de7e0ac200c489c41c59dfa910154a70264e6e", + "5452d32f1dd538eb0405e8a83cc185f79e25e80f", + "452e4244b5d083ddf0460acf1ecc74db9dcfa11a", + "55d316c9ba708999f1918e9677d01dfcae69c6b9", + "a6be623522ce87a1d862128ac42672604f7b468b", + "906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8", + "529a16e8e762d4acb7b9636ff540a00831f9155a", + "90b8c29d8ba39434d1c63e1b093daaa26e5bd972", + "ed062903b8f6f3dccb2fa81117ba6590944ef9bd", + "e8ee89e15bbe9b20137715232387b3de5b28972e", + "53ace0d1cc1145a5f4fe4f78a186a60263190733", + "1888c805345ba265b0ee9449b8877b6064592058", + "a6191982709b746d5650e93c2acf34ef74e11504" +}; + +void test_iterator_index__1(void) +{ + index_iterator_test( + "status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1), + expected_index_1, expected_index_oids_1); +} + +static const char *expected_index_range[] = { + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", +}; + +static const char *expected_index_oids_range[] = { + "45141a79a77842c59a63229403220a4e4be74e3d", + "4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d", + "108bb4e7fd7b16490dc33ff7d972151e73d7166e", + "a0f7217ae99f5ac3e88534f5cea267febc5fa85b", +}; + +void test_iterator_index__range(void) +{ + index_iterator_test( + "attr", "root", "root", 0, ARRAY_SIZE(expected_index_range), + expected_index_range, expected_index_oids_range); +} + +void test_iterator_index__range_empty_0(void) +{ + index_iterator_test( + "attr", "empty", "empty", 0, 0, NULL, NULL); +} + +void test_iterator_index__range_empty_1(void) +{ + index_iterator_test( + "attr", "z_empty_after", NULL, 0, 0, NULL, NULL); +} + +void test_iterator_index__range_empty_2(void) +{ + index_iterator_test( + "attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL); +} + +static void check_index_range( + git_repository *repo, + const char *start, + const char *end, + bool ignore_case, + int expected_count) +{ + git_index *index; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + int error, count, caps; + bool is_ignoring_case; + + cl_git_pass(git_repository_index(&index, repo)); + + caps = git_index_caps(index); + is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); + + if (ignore_case != is_ignoring_case) + cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE)); + + i_opts.flags = 0; + i_opts.start = start; + i_opts.end = end; + + cl_git_pass(git_iterator_for_index(&i, repo, index, &i_opts)); + + cl_assert(git_iterator_ignore_case(i) == ignore_case); + + for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) + /* count em up */; + + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert_equal_i(expected_count, count); + + git_iterator_free(i); + git_index_free(index); +} + +void test_iterator_index__range_icase(void) +{ + git_index *index; + git_tree *head; + + g_repo = cl_git_sandbox_init("testrepo"); + + /* reset index to match HEAD */ + cl_git_pass(git_repository_head_tree(&head, g_repo)); + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_index_read_tree(index, head)); + cl_git_pass(git_index_write(index)); + git_tree_free(head); + git_index_free(index); + + /* do some ranged iterator checks toggling case sensitivity */ + check_index_range(g_repo, "B", "C", false, 0); + check_index_range(g_repo, "B", "C", true, 1); + check_index_range(g_repo, "a", "z", false, 3); + check_index_range(g_repo, "a", "z", true, 4); +} + +static const char *expected_index_cs[] = { + "B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c", + "a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c", +}; + +static const char *expected_index_ci[] = { + "a", "B", "c", "D", "e", "F", "g", "H", "i", "J", + "k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D", +}; + +void test_iterator_index__case_folding(void) +{ + git_buf path = GIT_BUF_INIT; + int fs_is_ci = 0; + + cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg")); + fs_is_ci = git_path_exists(path.ptr); + git_buf_free(&path); + + index_iterator_test( + "icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs), + fs_is_ci ? expected_index_ci : expected_index_cs, NULL); + + cl_git_sandbox_cleanup(); + + index_iterator_test( + "icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE, + ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL); + + cl_git_sandbox_cleanup(); + + index_iterator_test( + "icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE, + ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL); +} + +/* Index contents (including pseudotrees): + * + * 0: a 5: F 10: k/ 16: L/ + * 1: B 6: g 11: k/1 17: L/1 + * 2: c 7: H 12: k/a 18: L/a + * 3: D 8: i 13: k/B 19: L/B + * 4: e 9: J 14: k/c 20: L/c + * 15: k/D 21: L/D + * + * 0: B 5: L/ 11: a 16: k/ + * 1: D 6: L/1 12: c 17: k/1 + * 2: F 7: L/B 13: e 18: k/B + * 3: H 8: L/D 14: g 19: k/D + * 4: J 9: L/a 15: i 20: k/a + * 10: L/c 21: k/c + */ + +void test_iterator_index__icase_0(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + /* autoexpand with no tree entries for index */ + cl_git_pass(git_iterator_for_index(&i, g_repo, index, NULL)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); + + git_index_free(index); +} + +void test_iterator_index__icase_1(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + int caps; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + caps = git_index_caps(index); + + /* force case sensitivity */ + cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE)); + + /* autoexpand with no tree entries over range */ + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + /* force case insensitivity */ + cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); + + /* autoexpand with no tree entries over range */ + i_opts.flags = 0; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); + + cl_git_pass(git_index_set_caps(index, caps)); + git_index_free(index); +} + +void test_iterator_index__pathlist(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist; + int default_icase; + int expect; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "a")); + cl_git_pass(git_vector_insert(&filelist, "B")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + cl_git_pass(git_vector_insert(&filelist, "L/1")); + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + /* In this test we DO NOT force a case setting on the index. */ + default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + /* All iterator tests are "autoexpand with no tree entries" */ + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "c"; + i_opts.end = NULL; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ + expect = ((default_icase) ? 6 : 4); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + i_opts.start = NULL; + i_opts.end = "e"; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ + expect = ((default_icase) ? 5 : 6); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_1(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist = GIT_VECTOR_INIT; + int default_icase, expect; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "0")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + + /* In this test we DO NOT force a case setting on the index. */ + default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + i_opts.start = "b"; + i_opts.end = "k/D"; + + /* (c D e k/1 k/a ==> 5) vs (c e k/1 ==> 3) */ + expect = default_icase ? 5 : 3; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_2(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist = GIT_VECTOR_INIT; + int default_icase, expect; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "0")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k/")); + cl_git_pass(git_vector_insert(&filelist, "k.a")); + cl_git_pass(git_vector_insert(&filelist, "k.b")); + cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); + + /* In this test we DO NOT force a case setting on the index. */ + default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + i_opts.start = "b"; + i_opts.end = "k/D"; + + /* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */ + expect = default_icase ? 8 : 5; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_four(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist = GIT_VECTOR_INIT; + int default_icase, expect; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "0")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k")); + cl_git_pass(git_vector_insert(&filelist, "k.a")); + cl_git_pass(git_vector_insert(&filelist, "k.b")); + cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); + + /* In this test we DO NOT force a case setting on the index. */ + default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + i_opts.start = "b"; + i_opts.end = "k/D"; + + /* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */ + expect = default_icase ? 8 : 5; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_icase(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + int caps; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "a")); + cl_git_pass(git_vector_insert(&filelist, "B")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + cl_git_pass(git_vector_insert(&filelist, "L/1")); + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + caps = git_index_caps(index); + + /* force case sensitivity */ + cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE)); + + /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 1, NULL, 1, NULL); + git_iterator_free(i); + + /* force case insensitivity */ + cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 2, NULL, 2, NULL); + git_iterator_free(i); + + cl_git_pass(git_index_set_caps(index, caps)); + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + git_index *index; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + git_index_free(index); + git_vector_free(&filelist); +} + diff --git a/tests/iterator/iterator_helpers.c b/tests/iterator/iterator_helpers.c new file mode 100644 index 000000000..c04969f63 --- /dev/null +++ b/tests/iterator/iterator_helpers.c @@ -0,0 +1,110 @@ +#include "clar_libgit2.h" +#include "iterator.h" +#include "repository.h" +#include "fileops.h" +#include "iterator_helpers.h" +#include + +static void assert_at_end(git_iterator *i, bool verbose) +{ + const git_index_entry *end; + int error = git_iterator_advance(&end, i); + + if (verbose && error != GIT_ITEROVER) + fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path); + + cl_git_fail_with(GIT_ITEROVER, error); +} + +void expect_iterator_items( + git_iterator *i, + int expected_flat, + const char **expected_flat_paths, + int expected_total, + const char **expected_total_paths) +{ + const git_index_entry *entry; + int count, error; + int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES); + bool v = false; + + if (expected_flat < 0) { v = true; expected_flat = -expected_flat; } + if (expected_total < 0) { v = true; expected_total = -expected_total; } + + if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees"); + + count = 0; + + while (!git_iterator_advance(&entry, i)) { + if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); + + if (no_trees) + cl_assert(entry->mode != GIT_FILEMODE_TREE); + + if (expected_flat_paths) { + const char *expect_path = expected_flat_paths[count]; + size_t expect_len = strlen(expect_path); + + cl_assert_equal_s(expect_path, entry->path); + + if (expect_path[expect_len - 1] == '/') + cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); + else + cl_assert(entry->mode != GIT_FILEMODE_TREE); + } + + if (++count >= expected_flat) + break; + } + + assert_at_end(i, v); + cl_assert_equal_i(expected_flat, count); + + cl_git_pass(git_iterator_reset(i)); + + count = 0; + cl_git_pass(git_iterator_current(&entry, i)); + + if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees"); + + while (entry != NULL) { + if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); + + if (no_trees) + cl_assert(entry->mode != GIT_FILEMODE_TREE); + + if (expected_total_paths) { + const char *expect_path = expected_total_paths[count]; + size_t expect_len = strlen(expect_path); + + cl_assert_equal_s(expect_path, entry->path); + + if (expect_path[expect_len - 1] == '/') + cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); + else + cl_assert(entry->mode != GIT_FILEMODE_TREE); + } + + if (entry->mode == GIT_FILEMODE_TREE) { + error = git_iterator_advance_into(&entry, i); + + /* could return NOTFOUND if directory is empty */ + cl_assert(!error || error == GIT_ENOTFOUND); + + if (error == GIT_ENOTFOUND) { + error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + } else { + error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + + if (++count >= expected_total) + break; + } + + assert_at_end(i, v); + cl_assert_equal_i(expected_total, count); +} + diff --git a/tests/iterator/iterator_helpers.h b/tests/iterator/iterator_helpers.h new file mode 100644 index 000000000..d92086e4a --- /dev/null +++ b/tests/iterator/iterator_helpers.h @@ -0,0 +1,8 @@ + +extern void expect_iterator_items( + git_iterator *i, + int expected_flat, + const char **expected_flat_paths, + int expected_total, + const char **expected_total_paths); + diff --git a/tests/iterator/tree.c b/tests/iterator/tree.c new file mode 100644 index 000000000..8e1130aab --- /dev/null +++ b/tests/iterator/tree.c @@ -0,0 +1,1064 @@ +#include "clar_libgit2.h" +#include "iterator.h" +#include "repository.h" +#include "fileops.h" +#include "tree.h" +#include "../submodule/submodule_helpers.h" +#include "../diff/diff_helpers.h" +#include "iterator_helpers.h" +#include + +static git_repository *g_repo; + +void test_iterator_tree__initialize(void) +{ +} + +void test_iterator_tree__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +static void tree_iterator_test( + const char *sandbox, + const char *treeish, + const char *start, + const char *end, + int expected_count, + const char **expected_values) +{ + git_tree *t; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int error, count = 0, count_post_reset = 0; + + g_repo = cl_git_sandbox_init(sandbox); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + i_opts.start = start; + i_opts.end = end; + + cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish)); + cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); + + /* test loop */ + while (!(error = git_iterator_advance(&entry, i))) { + cl_assert(entry); + if (expected_values != NULL) + cl_assert_equal_s(expected_values[count], entry->path); + count++; + } + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert(!entry); + cl_assert_equal_i(expected_count, count); + + /* test reset */ + cl_git_pass(git_iterator_reset(i)); + + while (!(error = git_iterator_advance(&entry, i))) { + cl_assert(entry); + if (expected_values != NULL) + cl_assert_equal_s(expected_values[count_post_reset], entry->path); + count_post_reset++; + } + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert(!entry); + cl_assert_equal_i(count, count_post_reset); + + git_iterator_free(i); + git_tree_free(t); +} + +/* results of: git ls-tree -r --name-only 605812a */ +const char *expected_tree_0[] = { + ".gitattributes", + "attr0", + "attr1", + "attr2", + "attr3", + "binfile", + "macro_test", + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", + "subdir/.gitattributes", + "subdir/abc", + "subdir/subdir_test1", + "subdir/subdir_test2.txt", + "subdir2/subdir2_test1", + NULL +}; + +void test_iterator_tree__0(void) +{ + tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0); +} + +/* results of: git ls-tree -r --name-only 6bab5c79 */ +const char *expected_tree_1[] = { + ".gitattributes", + "attr0", + "attr1", + "attr2", + "attr3", + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", + "subdir/.gitattributes", + "subdir/subdir_test1", + "subdir/subdir_test2.txt", + "subdir2/subdir2_test1", + NULL +}; + +void test_iterator_tree__1(void) +{ + tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1); +} + +/* results of: git ls-tree -r --name-only 26a125ee1 */ +const char *expected_tree_2[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", + NULL +}; + +void test_iterator_tree__2(void) +{ + tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2); +} + +/* $ git ls-tree -r --name-only 0017bd4ab1e */ +const char *expected_tree_3[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file" +}; + +void test_iterator_tree__3(void) +{ + tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3); +} + +/* $ git ls-tree -r --name-only 24fa9a9fc4e202313e24b648087495441dab432b */ +const char *expected_tree_4[] = { + "attr0", + "attr1", + "attr2", + "attr3", + "binfile", + "gitattributes", + "macro_bad", + "macro_test", + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", + "sub/abc", + "sub/file", + "sub/sub/file", + "sub/sub/subsub.txt", + "sub/subdir_test1", + "sub/subdir_test2.txt", + "subdir/.gitattributes", + "subdir/abc", + "subdir/subdir_test1", + "subdir/subdir_test2.txt", + "subdir2/subdir2_test1", + NULL +}; + +void test_iterator_tree__4(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL, + 23, expected_tree_4); +} + +void test_iterator_tree__4_ranged(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + "sub", "sub", + 11, &expected_tree_4[12]); +} + +const char *expected_tree_ranged_0[] = { + "gitattributes", + "macro_bad", + "macro_test", + "root_test1", + "root_test2", + "root_test3", + "root_test4.txt", + NULL +}; + +void test_iterator_tree__ranged_0(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + "git", "root", + 7, expected_tree_ranged_0); +} + +const char *expected_tree_ranged_1[] = { + "sub/subdir_test2.txt", + NULL +}; + +void test_iterator_tree__ranged_1(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + "sub/subdir_test2.txt", "sub/subdir_test2.txt", + 1, expected_tree_ranged_1); +} + +void test_iterator_tree__range_empty_0(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + "empty", "empty", 0, NULL); +} + +void test_iterator_tree__range_empty_1(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + "z_empty_after", NULL, 0, NULL); +} + +void test_iterator_tree__range_empty_2(void) +{ + tree_iterator_test( + "attr", "24fa9a9fc4e202313e24b648087495441dab432b", + NULL, ".aaa_empty_before", 0, NULL); +} + +static void check_tree_entry( + git_iterator *i, + const char *oid, + const char *oid_p, + const char *oid_pp, + const char *oid_ppp) +{ + const git_index_entry *ie; + const git_tree_entry *te; + const git_tree *tree; + + cl_git_pass(git_iterator_current_tree_entry(&te, i)); + cl_assert(te); + cl_assert(git_oid_streq(te->oid, oid) == 0); + + cl_git_pass(git_iterator_current(&ie, i)); + + if (oid_p) { + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 0)); + cl_assert(tree); + cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); + } + + if (oid_pp) { + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 1)); + cl_assert(tree); + cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); + } + + if (oid_ppp) { + cl_git_pass(git_iterator_current_parent_tree(&tree, i, 2)); + cl_assert(tree); + cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); + } +} + +void test_iterator_tree__special_functions(void) +{ + git_tree *t; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int error, cases = 0; + const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; + + g_repo = cl_git_sandbox_init("attr"); + + t = resolve_commit_oid_to_tree( + g_repo, "24fa9a9fc4e202313e24b648087495441dab432b"); + cl_assert(t != NULL); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); + + while (!(error = git_iterator_advance(&entry, i))) { + cl_assert(entry); + + if (strcmp(entry->path, "sub/file") == 0) { + cases++; + check_tree_entry( + i, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", + "ecb97df2a174987475ac816e3847fc8e9f6c596b", + rootoid, NULL); + } + else if (strcmp(entry->path, "sub/sub/subsub.txt") == 0) { + cases++; + check_tree_entry( + i, "9e5bdc47d6a80f2be0ea3049ad74231b94609242", + "4e49ba8c5b6c32ff28cd9dcb60be34df50fcc485", + "ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid); + } + else if (strcmp(entry->path, "subdir/.gitattributes") == 0) { + cases++; + check_tree_entry( + i, "99eae476896f4907224978b88e5ecaa6c5bb67a9", + "9fb40b6675dde60b5697afceae91b66d908c02d9", + rootoid, NULL); + } + else if (strcmp(entry->path, "subdir2/subdir2_test1") == 0) { + cases++; + check_tree_entry( + i, "dccada462d3df8ac6de596fb8c896aba9344f941", + "2929de282ce999e95183aedac6451d3384559c4b", + rootoid, NULL); + } + } + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert(!entry); + cl_assert_equal_i(4, cases); + + git_iterator_free(i); + git_tree_free(t); +} + +static void check_tree_range( + git_repository *repo, + const char *start, + const char *end, + bool ignore_case, + int expected_count) +{ + git_tree *head; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + int error, count; + + i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; + i_opts.start = start; + i_opts.end = end; + + cl_git_pass(git_repository_head_tree(&head, repo)); + + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + + for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) + /* count em up */; + + cl_assert_equal_i(GIT_ITEROVER, error); + cl_assert_equal_i(expected_count, count); + + git_iterator_free(i); + git_tree_free(head); +} + +void test_iterator_tree__range_icase(void) +{ + g_repo = cl_git_sandbox_init("testrepo"); + + check_tree_range(g_repo, "B", "C", false, 0); + check_tree_range(g_repo, "B", "C", true, 1); + check_tree_range(g_repo, "b", "c", false, 1); + check_tree_range(g_repo, "b", "c", true, 1); + + check_tree_range(g_repo, "a", "z", false, 3); + check_tree_range(g_repo, "a", "z", true, 4); + check_tree_range(g_repo, "A", "Z", false, 1); + check_tree_range(g_repo, "A", "Z", true, 4); + check_tree_range(g_repo, "a", "Z", false, 0); + check_tree_range(g_repo, "a", "Z", true, 4); + check_tree_range(g_repo, "A", "z", false, 4); + check_tree_range(g_repo, "A", "z", true, 4); + + check_tree_range(g_repo, "new.txt", "new.txt", true, 1); + check_tree_range(g_repo, "new.txt", "new.txt", false, 1); + check_tree_range(g_repo, "README", "README", true, 1); + check_tree_range(g_repo, "README", "README", false, 1); +} + +void test_iterator_tree__icase_0(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_tree *head; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, NULL)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); + + git_tree_free(head); +} + +void test_iterator_tree__icase_1(void) +{ + git_iterator *i; + git_tree *head; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + /* auto expand with no tree entries */ + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + /* auto expand with tree entries */ + i_opts.start = "c"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + /* auto expand with no tree entries */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); + + git_tree_free(head); +} + +void test_iterator_tree__icase_2(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_tree *head; + static const char *expect_basic[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", + NULL, + }; + static const char *expect_trees[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/", + "subdir/current_file", + "subdir/deleted_file", + "subdir/modified_file", + NULL, + }; + static const char *expect_noauto[] = { + "current_file", + "file_deleted", + "modified_file", + "staged_changes", + "staged_changes_file_deleted", + "staged_changes_modified_file", + "staged_delete_file_deleted", + "staged_delete_modified_file", + "subdir.txt", + "subdir/", + NULL + }; + + g_repo = cl_git_sandbox_init("status"); + + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_tree(&i, head, NULL)); + expect_iterator_items(i, 12, expect_basic, 12, expect_basic); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 13, expect_trees, 13, expect_trees); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); + expect_iterator_items(i, 10, expect_noauto, 13, expect_trees); + git_iterator_free(i); + + git_tree_free(head); +} + +/* "b=name,t=name", blob_id, tree_id */ +static void build_test_tree( + git_oid *out, git_repository *repo, const char *fmt, ...) +{ + git_oid *id; + git_treebuilder *builder; + const char *scan = fmt, *next; + char type, delimiter; + git_filemode_t mode = GIT_FILEMODE_BLOB; + git_buf name = GIT_BUF_INIT; + va_list arglist; + + cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); /* start builder */ + + va_start(arglist, fmt); + while (*scan) { + switch (type = *scan++) { + case 't': case 'T': mode = GIT_FILEMODE_TREE; break; + case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break; + default: + cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B'); + } + + delimiter = *scan++; /* read and skip delimiter */ + for (next = scan; *next && *next != delimiter; ++next) + /* seek end */; + cl_git_pass(git_buf_set(&name, scan, (size_t)(next - scan))); + for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan) + /* skip delimiter and optional comma */; + + id = va_arg(arglist, git_oid *); + + cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode)); + } + va_end(arglist); + + cl_git_pass(git_treebuilder_write(out, builder)); + + git_treebuilder_free(builder); + git_buf_free(&name); +} + +void test_iterator_tree__case_conflicts_0(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, biga_id, littlea_id, tree_id; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + const char *expect_cs[] = { + "A/1.file", "A/3.file", "a/2.file", "a/4.file" }; + const char *expect_ci[] = { + "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; + const char *expect_cs_trees[] = { + "A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" }; + const char *expect_ci_trees[] = { + "A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + /* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */ + build_test_tree( + &biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id); + build_test_tree( + &littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id); + build_test_tree( + &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, expect_cs, 4, expect_cs); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, expect_ci, 4, expect_ci); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_iterator_tree__case_conflicts_1(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + const char *expect_cs[] = { + "A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" }; + const char *expect_ci[] = { + "A/a", "a/b", "A/b/1", "A/c" }; + const char *expect_cs_trees[] = { + "A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" }; + const char *expect_ci_trees[] = { + "A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + /* create: A/a A/b/1 A/c a/a a/b a/C */ + build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id); + build_test_tree( + &biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id); + build_test_tree( + &littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id); + build_test_tree( + &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 6, expect_cs, 6, expect_cs); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, expect_ci, 4, expect_ci); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_iterator_tree__case_conflicts_2(void) +{ + const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; + git_tree *tree; + git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + const char *expect_cs[] = { + "A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO", + "A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO", + "A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO", + "A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO", + "a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO", + "a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO", + "a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO", + "a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", }; + const char *expect_ci[] = { + "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", + "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", + "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", + "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", + "A/B/C/D/foo", }; + const char *expect_ci_trees[] = { + "A/", "A/B/", "A/B/C/", "A/B/C/D/", + "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", + "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", + "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", + "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", + "A/B/C/D/foo", }; + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ + + build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2); + + build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id); + build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id); + build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); + build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); + + build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2); + + build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2); + + cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 32, expect_cs, 32, expect_cs); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 17, expect_ci, 17, expect_ci); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees); + git_iterator_free(i); + + git_tree_free(tree); +} + +void test_iterator_tree__pathlist(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + bool default_icase; + int expect; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "a")); + cl_git_pass(git_vector_insert(&filelist, "B")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k.a")); + cl_git_pass(git_vector_insert(&filelist, "k.b")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); + cl_git_pass(git_vector_insert(&filelist, "L/1")); + + g_repo = cl_git_sandbox_init("icase"); + git_repository_head_tree(&tree, g_repo); + + /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ + /* In this test we DO NOT force a case on the iterators and verify default behavior. */ + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "c"; + i_opts.end = NULL; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + default_icase = git_iterator_ignore_case(i); + /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ + expect = ((default_icase) ? 6 : 4); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + i_opts.start = NULL; + i_opts.end = "e"; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + default_icase = git_iterator_ignore_case(i); + /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ + expect = ((default_icase) ? 5 : 6); + expect_iterator_items(i, expect, NULL, expect, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); + git_tree_free(tree); +} + +void test_iterator_tree__pathlist_icase(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "a")); + cl_git_pass(git_vector_insert(&filelist, "B")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k.a")); + cl_git_pass(git_vector_insert(&filelist, "k.b")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + cl_git_pass(git_vector_insert(&filelist, "kZZZZ")); + cl_git_pass(git_vector_insert(&filelist, "L/1")); + + g_repo = cl_git_sandbox_init("icase"); + git_repository_head_tree(&tree, g_repo); + + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 1, NULL, 1, NULL); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 2, NULL, 2, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); + git_tree_free(tree); +} + +void test_iterator_tree__pathlist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "subdir/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "subdir/subdir2")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 2, NULL, 2, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + +void test_iterator_tree__pathlist_with_directory_include_tree_nodes(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir")); + + i_opts.flags |= GIT_ITERATOR_INCLUDE_TREES; + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + +void test_iterator_tree__pathlist_no_match(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_tree *tree; + const git_index_entry *entry; + + g_repo = cl_git_sandbox_init("testrepo2"); + git_repository_head_tree(&tree, g_repo); + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "nonexistent/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); + cl_assert_equal_i(GIT_ITEROVER, git_iterator_current(&entry, i)); + + git_vector_free(&filelist); +} + diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c new file mode 100644 index 000000000..4daa32330 --- /dev/null +++ b/tests/iterator/workdir.c @@ -0,0 +1,1462 @@ +#include "clar_libgit2.h" +#include "iterator.h" +#include "repository.h" +#include "fileops.h" +#include "../submodule/submodule_helpers.h" +#include "iterator_helpers.h" +#include + +static git_repository *g_repo; + +void test_iterator_workdir__initialize(void) +{ +} + +void test_iterator_workdir__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +static void workdir_iterator_test( + const char *sandbox, + const char *start, + const char *end, + int expected_count, + int expected_ignores, + const char **expected_names, + const char *an_ignored_name) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int error, count = 0, count_all = 0, count_all_post_reset = 0; + + g_repo = cl_git_sandbox_init(sandbox); + + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + i_opts.start = start; + i_opts.end = end; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + + error = git_iterator_current(&entry, i); + cl_assert((error == 0 && entry != NULL) || + (error == GIT_ITEROVER && entry == NULL)); + + while (entry != NULL) { + int ignored = git_iterator_current_is_ignored(i); + + if (S_ISDIR(entry->mode)) { + cl_git_pass(git_iterator_advance_into(&entry, i)); + continue; + } + + if (expected_names != NULL) + cl_assert_equal_s(expected_names[count_all], entry->path); + + if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0) + cl_assert(ignored); + + if (!ignored) + count++; + count_all++; + + error = git_iterator_advance(&entry, i); + + cl_assert((error == 0 && entry != NULL) || + (error == GIT_ITEROVER && entry == NULL)); + } + + cl_assert_equal_i(expected_count, count); + cl_assert_equal_i(expected_count + expected_ignores, count_all); + + cl_git_pass(git_iterator_reset(i)); + + error = git_iterator_current(&entry, i); + cl_assert((error == 0 && entry != NULL) || + (error == GIT_ITEROVER && entry == NULL)); + + while (entry != NULL) { + if (S_ISDIR(entry->mode)) { + cl_git_pass(git_iterator_advance_into(&entry, i)); + continue; + } + + if (expected_names != NULL) + cl_assert_equal_s( + expected_names[count_all_post_reset], entry->path); + count_all_post_reset++; + + error = git_iterator_advance(&entry, i); + cl_assert(error == 0 || error == GIT_ITEROVER); + } + + cl_assert_equal_i(count_all, count_all_post_reset); + + git_iterator_free(i); +} + +void test_iterator_workdir__0(void) +{ + workdir_iterator_test("attr", NULL, NULL, 23, 5, NULL, "ign"); +} + +static const char *status_paths[] = { + "current_file", + "ignored_file", + "modified_file", + "new_file", + "staged_changes", + "staged_changes_modified_file", + "staged_delete_modified_file", + "staged_new_file", + "staged_new_file_modified_file", + "subdir.txt", + "subdir/current_file", + "subdir/modified_file", + "subdir/new_file", + "\xe8\xbf\x99", + NULL +}; + +void test_iterator_workdir__1(void) +{ + workdir_iterator_test( + "status", NULL, NULL, 13, 1, status_paths, "ignored_file"); +} + +static const char *status_paths_range_0[] = { + "staged_changes", + "staged_changes_modified_file", + "staged_delete_modified_file", + "staged_new_file", + "staged_new_file_modified_file", + NULL +}; + +void test_iterator_workdir__1_ranged_0(void) +{ + workdir_iterator_test( + "status", "staged", "staged", 5, 0, status_paths_range_0, NULL); +} + +static const char *status_paths_range_1[] = { + "modified_file", NULL +}; + +void test_iterator_workdir__1_ranged_1(void) +{ + workdir_iterator_test( + "status", "modified_file", "modified_file", + 1, 0, status_paths_range_1, NULL); +} + +static const char *status_paths_range_3[] = { + "subdir.txt", + "subdir/current_file", + "subdir/modified_file", + NULL +}; + +void test_iterator_workdir__1_ranged_3(void) +{ + workdir_iterator_test( + "status", "subdir", "subdir/modified_file", + 3, 0, status_paths_range_3, NULL); +} + +static const char *status_paths_range_4[] = { + "subdir/current_file", + "subdir/modified_file", + "subdir/new_file", + "\xe8\xbf\x99", + NULL +}; + +void test_iterator_workdir__1_ranged_4(void) +{ + workdir_iterator_test( + "status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL); +} + +static const char *status_paths_range_5[] = { + "subdir/modified_file", + NULL +}; + +void test_iterator_workdir__1_ranged_5(void) +{ + workdir_iterator_test( + "status", "subdir/modified_file", "subdir/modified_file", + 1, 0, status_paths_range_5, NULL); +} + +void test_iterator_workdir__1_ranged_5_1_ranged_empty_0(void) +{ + workdir_iterator_test( + "status", "\xff_does_not_exist", NULL, + 0, 0, NULL, NULL); +} + +void test_iterator_workdir__1_ranged_empty_1(void) +{ + workdir_iterator_test( + "status", "empty", "empty", + 0, 0, NULL, NULL); +} + +void test_iterator_workdir__1_ranged_empty_2(void) +{ + workdir_iterator_test( + "status", NULL, "aaaa_empty_before", + 0, 0, NULL, NULL); +} + +void test_iterator_workdir__builtin_ignores(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int idx; + static struct { + const char *path; + bool ignored; + } expected[] = { + { "dir/", true }, + { "file", false }, + { "ign", true }, + { "macro_bad", false }, + { "macro_test", false }, + { "root_test1", false }, + { "root_test2", false }, + { "root_test3", false }, + { "root_test4.txt", false }, + { "sub/", false }, + { "sub/.gitattributes", false }, + { "sub/abc", false }, + { "sub/dir/", true }, + { "sub/file", false }, + { "sub/ign/", true }, + { "sub/sub/", false }, + { "sub/sub/.gitattributes", false }, + { "sub/sub/dir", false }, /* file is not actually a dir */ + { "sub/sub/file", false }, + { NULL, false } + }; + + g_repo = cl_git_sandbox_init("attr"); + + cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); + cl_git_mkfile("attr/sub/.git", "whatever"); + + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + i_opts.start = "dir"; + i_opts.end = "sub/sub/file"; + + cl_git_pass(git_iterator_for_workdir( + &i, g_repo, NULL, NULL, &i_opts)); + cl_git_pass(git_iterator_current(&entry, i)); + + for (idx = 0; entry != NULL; ++idx) { + int ignored = git_iterator_current_is_ignored(i); + + cl_assert_equal_s(expected[idx].path, entry->path); + cl_assert_(ignored == expected[idx].ignored, expected[idx].path); + + if (!ignored && + (entry->mode == GIT_FILEMODE_TREE || + entry->mode == GIT_FILEMODE_COMMIT)) + { + /* it is possible to advance "into" a submodule */ + cl_git_pass(git_iterator_advance_into(&entry, i)); + } else { + int error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + } + + cl_assert(expected[idx].path == NULL); + + git_iterator_free(i); +} + +static void check_wd_first_through_third_range( + git_repository *repo, const char *start, const char *end) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + const git_index_entry *entry; + int error, idx; + static const char *expected[] = { "FIRST", "second", "THIRD", NULL }; + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + i_opts.start = start; + i_opts.end = end; + + cl_git_pass(git_iterator_for_workdir( + &i, repo, NULL, NULL, &i_opts)); + cl_git_pass(git_iterator_current(&entry, i)); + + for (idx = 0; entry != NULL; ++idx) { + cl_assert_equal_s(expected[idx], entry->path); + + error = git_iterator_advance(&entry, i); + cl_assert(!error || error == GIT_ITEROVER); + } + + cl_assert(expected[idx] == NULL); + + git_iterator_free(i); +} + +void test_iterator_workdir__handles_icase_range(void) +{ + g_repo = cl_git_sandbox_init("empty_standard_repo"); + cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt"); + + cl_git_mkfile("empty_standard_repo/before", "whatever\n"); + cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n"); + cl_git_mkfile("empty_standard_repo/second", "whatever\n"); + cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n"); + cl_git_mkfile("empty_standard_repo/zafter", "whatever\n"); + cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n"); + + check_wd_first_through_third_range(g_repo, "first", "third"); + check_wd_first_through_third_range(g_repo, "FIRST", "THIRD"); + check_wd_first_through_third_range(g_repo, "first", "THIRD"); + check_wd_first_through_third_range(g_repo, "FIRST", "third"); + check_wd_first_through_third_range(g_repo, "FirSt", "tHiRd"); +} + +/* + * The workdir iterator is like the filesystem iterator, but honors + * special git type constructs (ignores, submodules, etc). + */ + +void test_iterator_workdir__icase(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 20, NULL, 20, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 22, NULL, 22, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 12, NULL, 22, NULL); + git_iterator_free(i); +} + +void test_iterator_workdir__icase_starts_and_ends(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + /* auto expand with no tree entries */ + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 7, NULL, 7, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 3, NULL, 3, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 8, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 5, NULL, 8, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 1, NULL, 4, NULL); + git_iterator_free(i); + + /* auto expand with no tree entries */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 5, NULL, 5, NULL); + git_iterator_free(i); + + /* auto expand with tree entries */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 14, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 6, NULL, 6, NULL); + git_iterator_free(i); + + /* no auto expand (implies trees included) */ + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; + + i_opts.start = "c"; + i_opts.end = "k/D"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 9, NULL, 14, NULL); + git_iterator_free(i); + + i_opts.start = "k"; + i_opts.end = "k/Z"; + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 1, NULL, 6, NULL); + git_iterator_free(i); +} + +static void build_workdir_tree(const char *root, int dirs, int subs) +{ + int i, j; + char buf[64], sub[64]; + + for (i = 0; i < dirs; ++i) { + if (i % 2 == 0) { + p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i); + cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH)); + + p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i); + cl_git_mkfile(buf, buf); + buf[strlen(buf) - 5] = '\0'; + } else { + p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i); + cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH)); + } + + for (j = 0; j < subs; ++j) { + switch (j % 4) { + case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break; + case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break; + case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break; + case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break; + } + cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH)); + + if (j % 2 == 0) { + size_t sublen = strlen(sub); + memcpy(&sub[sublen], "/file", sizeof("/file")); + cl_git_mkfile(sub, sub); + sub[sublen] = '\0'; + } + } + } +} + +void test_iterator_workdir__depth(void) +{ + git_iterator *iter; + git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + build_workdir_tree("icase", 10, 10); + build_workdir_tree("icase/DIR01/sUB01", 50, 0); + build_workdir_tree("icase/dir02/sUB01", 50, 0); + + /* auto expand with no tree entries */ + cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts)); + expect_iterator_items(iter, 125, NULL, 125, NULL); + git_iterator_free(iter); + + /* auto expand with tree entries (empty dirs silently skipped) */ + iter_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts)); + expect_iterator_items(iter, 337, NULL, 337, NULL); + git_iterator_free(iter); +} + +/* The filesystem iterator is a workdir iterator without any special + * workdir handling capabilities (ignores, submodules, etc). + */ +void test_iterator_workdir__filesystem(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + static const char *expect_base[] = { + "DIR01/Sub02/file", + "DIR01/sub00/file", + "current_file", + "dir00/Sub02/file", + "dir00/file", + "dir00/sub00/file", + "modified_file", + "new_file", + NULL, + }; + static const char *expect_trees[] = { + "DIR01/", + "DIR01/SUB03/", + "DIR01/Sub02/", + "DIR01/Sub02/file", + "DIR01/sUB01/", + "DIR01/sub00/", + "DIR01/sub00/file", + "current_file", + "dir00/", + "dir00/SUB03/", + "dir00/Sub02/", + "dir00/Sub02/file", + "dir00/file", + "dir00/sUB01/", + "dir00/sub00/", + "dir00/sub00/file", + "modified_file", + "new_file", + NULL, + }; + static const char *expect_noauto[] = { + "DIR01/", + "current_file", + "dir00/", + "modified_file", + "new_file", + NULL, + }; + + g_repo = cl_git_sandbox_init("status"); + + build_workdir_tree("status/subdir", 2, 4); + + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", NULL)); + expect_iterator_items(i, 8, expect_base, 8, expect_base); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); + expect_iterator_items(i, 18, expect_trees, 18, expect_trees); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); + expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); + git_iterator_free(i); + + git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp); + git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp); + git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); + expect_iterator_items(i, 8, expect_base, 8, expect_base); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); + expect_iterator_items(i, 18, expect_trees, 18, expect_trees); + git_iterator_free(i); + + i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; + cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); + expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); + git_iterator_free(i); +} + +void test_iterator_workdir__filesystem2(void) +{ + git_iterator *i; + static const char *expect_base[] = { + "heads/br2", + "heads/dir", + "heads/ident", + "heads/long-file-name", + "heads/master", + "heads/packed-test", + "heads/subtrees", + "heads/test", + "tags/e90810b", + "tags/foo/bar", + "tags/foo/foo/bar", + "tags/point_to_blob", + "tags/test", + NULL, + }; + + g_repo = cl_git_sandbox_init("testrepo"); + + cl_git_pass(git_iterator_for_filesystem( + &i, "testrepo/.git/refs", NULL)); + expect_iterator_items(i, 13, expect_base, 13, expect_base); + git_iterator_free(i); +} + +/* Lots of empty dirs, or nearly empty ones, make the old workdir + * iterator cry. Also, segfault. + */ +void test_iterator_workdir__filesystem_gunk(void) +{ + git_iterator *i; + git_buf parent = GIT_BUF_INIT; + int n; + + if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) + cl_skip(); + + g_repo = cl_git_sandbox_init("testrepo"); + + for (n = 0; n < 100000; n++) { + git_buf_clear(&parent); + git_buf_printf(&parent, "%s/refs/heads/foo/%d/subdir", + git_repository_path(g_repo), n); + cl_assert(!git_buf_oom(&parent)); + + cl_git_pass(git_futils_mkdir(parent.ptr, 0775, GIT_MKDIR_PATH)); + } + + cl_git_pass(git_iterator_for_filesystem(&i, "testrepo/.git/refs", NULL)); + + /* should only have 13 items, since we're not asking for trees to be + * returned. the goal of this test is simply to not crash. + */ + expect_iterator_items(i, 13, NULL, 13, NULL); + git_iterator_free(i); + git_buf_free(&parent); +} + +void test_iterator_workdir__skips_unreadable_dirs(void) +{ + git_iterator *i; + const git_index_entry *e; + + if (!cl_is_chmod_supported()) + return; + + g_repo = cl_git_sandbox_init("empty_standard_repo"); + + cl_must_pass(p_mkdir("empty_standard_repo/r", 0777)); + cl_git_mkfile("empty_standard_repo/r/a", "hello"); + cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777)); + cl_git_mkfile("empty_standard_repo/r/b/problem", "not me"); + cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000)); + cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777)); + cl_git_mkfile("empty_standard_repo/r/c/foo", "aloha"); + cl_git_mkfile("empty_standard_repo/r/d", "final"); + + cl_git_pass(git_iterator_for_filesystem( + &i, "empty_standard_repo/r", NULL)); + + cl_git_pass(git_iterator_advance(&e, i)); /* a */ + cl_assert_equal_s("a", e->path); + + cl_git_pass(git_iterator_advance(&e, i)); /* c/foo */ + cl_assert_equal_s("c/foo", e->path); + + cl_git_pass(git_iterator_advance(&e, i)); /* d */ + cl_assert_equal_s("d", e->path); + + cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777)); + + cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i)); + git_iterator_free(i); +} + +void test_iterator_workdir__skips_fifos_and_special_files(void) +{ +#ifndef GIT_WIN32 + git_iterator *i; + const git_index_entry *e; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("empty_standard_repo"); + + cl_must_pass(p_mkdir("empty_standard_repo/dir", 0777)); + cl_git_mkfile("empty_standard_repo/file", "not me"); + + cl_assert(!mkfifo("empty_standard_repo/fifo", 0777)); + cl_assert(!access("empty_standard_repo/fifo", F_OK)); + + i_opts.flags = GIT_ITERATOR_INCLUDE_TREES | + GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_git_pass(git_iterator_for_filesystem( + &i, "empty_standard_repo", &i_opts)); + + cl_git_pass(git_iterator_advance(&e, i)); /* .git */ + cl_assert(S_ISDIR(e->mode)); + cl_git_pass(git_iterator_advance(&e, i)); /* dir */ + cl_assert(S_ISDIR(e->mode)); + /* skips fifo */ + cl_git_pass(git_iterator_advance(&e, i)); /* file */ + cl_assert(S_ISREG(e->mode)); + + cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i)); + + git_iterator_free(i); +#endif +} + +void test_iterator_workdir__pathlist(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 100, NULL)); + cl_git_pass(git_vector_insert(&filelist, "a")); + cl_git_pass(git_vector_insert(&filelist, "B")); + cl_git_pass(git_vector_insert(&filelist, "c")); + cl_git_pass(git_vector_insert(&filelist, "D")); + cl_git_pass(git_vector_insert(&filelist, "e")); + cl_git_pass(git_vector_insert(&filelist, "k.a")); + cl_git_pass(git_vector_insert(&filelist, "k.b")); + cl_git_pass(git_vector_insert(&filelist, "k/1")); + cl_git_pass(git_vector_insert(&filelist, "k/a")); + cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); + cl_git_pass(git_vector_insert(&filelist, "L/1")); + + g_repo = cl_git_sandbox_init("icase"); + + /* Test iterators without returning tree entries (but autoexpanding.) */ + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + /* Case sensitive */ + { + const char *expected[] = { + "B", "D", "L/1", "a", "c", "e", "k/1", "k/a" }; + size_t expected_len = 8; + + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Case INsensitive */ + { + const char *expected[] = { + "a", "B", "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 8; + + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case sensitive. */ + { + const char *expected[] = { "c", "e", "k/1", "k/a" }; + size_t expected_len = 4; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case INsensitive. */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 6; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case sensitive. */ + { + const char *expected[] = { "B", "D", "L/1", "a", "c", "e" }; + size_t expected_len = 6; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case INsensitive. */ + { + const char *expected[] = { "a", "B", "c", "D", "e" }; + size_t expected_len = 5; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "c", "e", "k/1" }; + size_t expected_len = 3; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "k/1" }; + size_t expected_len = 1; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a" }; + size_t expected_len = 5; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "k/1", "k/a" }; + size_t expected_len = 2; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + git_vector_free(&filelist); +} + +void test_iterator_workdir__pathlist_with_dirs(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + + /* Test that a prefix `k` matches folders, even without trailing slash */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that a `k/` matches a folder */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* When the iterator is case sensitive, ensure we can't lookup the + * directory with the wrong case. + */ + { + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + /* Test that case insensitive matching works. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that case insensitive matching works without trailing slash. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + git_vector_free(&filelist); +} + +static void create_paths(const char *root, int depth) +{ + git_buf fullpath = GIT_BUF_INIT; + size_t root_len; + int i; + + cl_git_pass(git_buf_puts(&fullpath, root)); + cl_git_pass(git_buf_putc(&fullpath, '/')); + + root_len = fullpath.size; + + for (i = 0; i < 8; i++) { + bool file = (depth == 0 || (i % 2) == 0); + git_buf_truncate(&fullpath, root_len); + cl_git_pass(git_buf_printf(&fullpath, "item%d", i)); + + if (file) { + cl_git_rewritefile(fullpath.ptr, "This is a file!\n"); + } else { + cl_must_pass(p_mkdir(fullpath.ptr, 0777)); + + if (depth > 0) + create_paths(fullpath.ptr, (depth - 1)); + } + } +} + +void test_iterator_workdir__pathlist_for_deeply_nested_item(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + create_paths(git_repository_workdir(g_repo), 3); + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { "item1/item3/item5/item7" }; + size_t expected_len = 1; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(4, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + }; + size_t expected_len = 8; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(11, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item1/item3/item0", + "item1/item3/item1/item0", "item1/item3/item1/item1", + "item1/item3/item1/item2", "item1/item3/item1/item3", + "item1/item3/item1/item4", "item1/item3/item1/item5", + "item1/item3/item1/item6", "item1/item3/item1/item7", + "item1/item3/item2", + "item1/item3/item3/item0", "item1/item3/item3/item1", + "item1/item3/item3/item2", "item1/item3/item3/item3", + "item1/item3/item3/item4", "item1/item3/item3/item5", + "item1/item3/item3/item6", "item1/item3/item3/item7", + "item1/item3/item4", + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + "item1/item3/item6", + "item1/item3/item7/item0", "item1/item3/item7/item1", + "item1/item3/item7/item2", "item1/item3/item7/item3", + "item1/item3/item7/item4", "item1/item3/item7/item5", + "item1/item3/item7/item6", "item1/item3/item7/item7", + }; + size_t expected_len = 36; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(42, i->stat_calls); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item0", "item1/item2", "item5/item7/item4", "item6", + "item7/item3/item1/item6" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6")); + cl_git_pass(git_vector_insert(&filelist, "item6")); + cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4")); + cl_git_pass(git_vector_insert(&filelist, "item1/item2")); + cl_git_pass(git_vector_insert(&filelist, "item0")); + + /* also add some things that don't exist or don't match the right type */ + cl_git_pass(git_vector_insert(&filelist, "item2/")); + cl_git_pass(git_vector_insert(&filelist, "itemN")); + cl_git_pass(git_vector_insert(&filelist, "item1/itemA")); + cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + cl_assert_equal_i(14, i->stat_calls); + git_iterator_free(i); + } + + git_vector_free(&filelist); +} + +void test_iterator_workdir__bounded_submodules(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + git_index *index; + git_tree *head; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = setup_fixture_submod2(); + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_repository_head_tree(&head, g_repo)); + + /* Test that a submodule matches */ + { + const char *expected[] = { "sm_changed_head" }; + size_t expected_len = 1; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "sm_changed_head")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that a submodule never matches when suffixed with a '/' */ + { + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "sm_changed_head/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + /* Test that start/end work with a submodule */ + { + const char *expected[] = { "sm_changed_head", "sm_changed_index" }; + size_t expected_len = 2; + + i_opts.start = "sm_changed_head"; + i_opts.end = "sm_changed_index"; + i_opts.pathlist.strings = NULL; + i_opts.pathlist.count = 0; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that start and end do not allow '/' suffixes of submodules */ + { + i_opts.start = "sm_changed_head/"; + i_opts.end = "sm_changed_head/"; + i_opts.pathlist.strings = NULL; + i_opts.pathlist.count = 0; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + git_vector_free(&filelist); + git_index_free(index); + git_tree_free(head); +} + +static void expect_advance_over( + git_iterator *i, + const char *expected_path, + git_iterator_status_t expected_status) +{ + const git_index_entry *entry; + git_iterator_status_t status; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + error = git_iterator_advance_over(&entry, &status, i); + cl_assert(!error || error == GIT_ITEROVER); + cl_assert_equal_i(expected_status, status); +} + +void test_iterator_workdir__advance_over(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + + /* create an empty directory */ + cl_must_pass(p_mkdir("icase/empty", 0777)); + + /* create a directory in which all contents are ignored */ + cl_must_pass(p_mkdir("icase/all_ignored", 0777)); + cl_git_rewritefile("icase/all_ignored/one", "This is ignored\n"); + cl_git_rewritefile("icase/all_ignored/two", "This, too, is ignored\n"); + cl_git_rewritefile("icase/all_ignored/.gitignore", ".gitignore\none\ntwo\n"); + + /* create a directory in which not all contents are ignored */ + cl_must_pass(p_mkdir("icase/some_ignored", 0777)); + cl_git_rewritefile("icase/some_ignored/one", "This is ignored\n"); + cl_git_rewritefile("icase/some_ignored/two", "This is not ignored\n"); + cl_git_rewritefile("icase/some_ignored/.gitignore", ".gitignore\none\n"); + + /* create a directory which has some empty children */ + cl_must_pass(p_mkdir("icase/empty_children", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty1", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty2", 0777)); + cl_must_pass(p_mkdir("icase/empty_children/empty3", 0777)); + + /* create a directory which will disappear! */ + cl_must_pass(p_mkdir("icase/missing_directory", 0777)); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + + cl_must_pass(p_rmdir("icase/missing_directory")); + + expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "all_ignored/", GIT_ITERATOR_STATUS_IGNORED); + expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "empty/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "empty_children/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "missing_directory/", GIT_ITERATOR_STATUS_EMPTY); + expect_advance_over(i, "some_ignored/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); +} + +void test_iterator_workdir__advance_over_with_pathlist(void) +{ + git_vector pathlist = GIT_VECTOR_INIT; + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + git_vector_insert(&pathlist, "dirA/subdir1/subdir2/file"); + git_vector_insert(&pathlist, "dirB/subdir1/subdir2"); + git_vector_insert(&pathlist, "dirC/subdir1/nonexistent"); + git_vector_insert(&pathlist, "dirD/subdir1/nonexistent"); + git_vector_insert(&pathlist, "dirD/subdir1/subdir2"); + git_vector_insert(&pathlist, "dirD/nonexistent"); + + i_opts.pathlist.strings = (char **)pathlist.contents; + i_opts.pathlist.count = pathlist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + + /* Create a directory that has a file that is included in our pathlist */ + cl_must_pass(p_mkdir("icase/dirA", 0777)); + cl_must_pass(p_mkdir("icase/dirA/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirA/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirA/subdir1/subdir2/file", "foo!"); + + /* Create a directory that has a directory that is included in our pathlist */ + cl_must_pass(p_mkdir("icase/dirB", 0777)); + cl_must_pass(p_mkdir("icase/dirB/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirB/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirB/subdir1/subdir2/file", "foo!"); + + /* Create a directory that would contain an entry in our pathlist, but + * that entry does not actually exist. We don't know this until we + * advance_over it. We want to distinguish this from an actually empty + * or ignored directory. + */ + cl_must_pass(p_mkdir("icase/dirC", 0777)); + cl_must_pass(p_mkdir("icase/dirC/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirC/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirC/subdir1/subdir2/file", "foo!"); + + /* Create a directory that has a mix of actual and nonexistent paths */ + cl_must_pass(p_mkdir("icase/dirD", 0777)); + cl_must_pass(p_mkdir("icase/dirD/subdir1", 0777)); + cl_must_pass(p_mkdir("icase/dirD/subdir1/subdir2", 0777)); + cl_git_rewritefile("icase/dirD/subdir1/subdir2/file", "foo!"); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + + expect_advance_over(i, "dirA/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "dirB/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "dirC/", GIT_ITERATOR_STATUS_FILTERED); + expect_advance_over(i, "dirD/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + git_vector_free(&pathlist); +} + +static void expect_advance_into( + git_iterator *i, + const char *expected_path) +{ + const git_index_entry *entry; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + if (S_ISDIR(entry->mode)) + error = git_iterator_advance_into(&entry, i); + else + error = git_iterator_advance(&entry, i); + + cl_assert(!error || error == GIT_ITEROVER); +} + +void test_iterator_workdir__advance_into(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("icase"); + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_must_pass(p_mkdir("icase/Empty", 0777)); + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_advance_into(i, "B"); + expect_advance_into(i, "D"); + expect_advance_into(i, "Empty/"); + expect_advance_into(i, "F"); + expect_advance_into(i, "H"); + expect_advance_into(i, "J"); + expect_advance_into(i, "L/"); + expect_advance_into(i, "L/1"); + expect_advance_into(i, "L/B"); + expect_advance_into(i, "L/D"); + expect_advance_into(i, "L/a"); + expect_advance_into(i, "L/c"); + expect_advance_into(i, "a"); + expect_advance_into(i, "c"); + expect_advance_into(i, "e"); + expect_advance_into(i, "g"); + expect_advance_into(i, "i"); + expect_advance_into(i, "k/"); + expect_advance_into(i, "k/1"); + expect_advance_into(i, "k/B"); + expect_advance_into(i, "k/D"); + expect_advance_into(i, "k/a"); + expect_advance_into(i, "k/c"); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); +} + +void test_iterator_workdir__pathlist_with_directory(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir/")); + + g_repo = cl_git_sandbox_init("testrepo2"); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, 4, NULL, 4, NULL); + git_iterator_free(i); + + git_vector_free(&filelist); +} + diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c deleted file mode 100644 index b6017dae5..000000000 --- a/tests/repo/iterator.c +++ /dev/null @@ -1,2327 +0,0 @@ -#include "clar_libgit2.h" -#include "iterator.h" -#include "repository.h" -#include "fileops.h" -#include "../submodule/submodule_helpers.h" -#include - -static git_repository *g_repo; - -void test_repo_iterator__initialize(void) -{ -} - -void test_repo_iterator__cleanup(void) -{ - cl_git_sandbox_cleanup(); - g_repo = NULL; -} - -static void assert_at_end(git_iterator *i, bool verbose) -{ - const git_index_entry *end; - int error = git_iterator_advance(&end, i); - - if (verbose && error != GIT_ITEROVER) - fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path); - - cl_git_fail_with(GIT_ITEROVER, error); -} - -static void expect_iterator_items( - git_iterator *i, - int expected_flat, - const char **expected_flat_paths, - int expected_total, - const char **expected_total_paths) -{ - const git_index_entry *entry; - int count, error; - int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES); - bool v = false; - - if (expected_flat < 0) { v = true; expected_flat = -expected_flat; } - if (expected_total < 0) { v = true; expected_total = -expected_total; } - - if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees"); - - count = 0; - - while (!git_iterator_advance(&entry, i)) { - if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); - - if (no_trees) - cl_assert(entry->mode != GIT_FILEMODE_TREE); - - if (expected_flat_paths) { - const char *expect_path = expected_flat_paths[count]; - size_t expect_len = strlen(expect_path); - - cl_assert_equal_s(expect_path, entry->path); - - if (expect_path[expect_len - 1] == '/') - cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); - else - cl_assert(entry->mode != GIT_FILEMODE_TREE); - } - - if (++count >= expected_flat) - break; - } - - assert_at_end(i, v); - cl_assert_equal_i(expected_flat, count); - - cl_git_pass(git_iterator_reset(i)); - - count = 0; - cl_git_pass(git_iterator_current(&entry, i)); - - if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees"); - - while (entry != NULL) { - if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode); - - if (no_trees) - cl_assert(entry->mode != GIT_FILEMODE_TREE); - - if (expected_total_paths) { - const char *expect_path = expected_total_paths[count]; - size_t expect_len = strlen(expect_path); - - cl_assert_equal_s(expect_path, entry->path); - - if (expect_path[expect_len - 1] == '/') - cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode); - else - cl_assert(entry->mode != GIT_FILEMODE_TREE); - } - - if (entry->mode == GIT_FILEMODE_TREE) { - error = git_iterator_advance_into(&entry, i); - - /* could return NOTFOUND if directory is empty */ - cl_assert(!error || error == GIT_ENOTFOUND); - - if (error == GIT_ENOTFOUND) { - error = git_iterator_advance(&entry, i); - cl_assert(!error || error == GIT_ITEROVER); - } - } else { - error = git_iterator_advance(&entry, i); - cl_assert(!error || error == GIT_ITEROVER); - } - - if (++count >= expected_total) - break; - } - - assert_at_end(i, v); - cl_assert_equal_i(expected_total, count); -} - -/* Index contents (including pseudotrees): - * - * 0: a 5: F 10: k/ 16: L/ - * 1: B 6: g 11: k/1 17: L/1 - * 2: c 7: H 12: k/a 18: L/a - * 3: D 8: i 13: k/B 19: L/B - * 4: e 9: J 14: k/c 20: L/c - * 15: k/D 21: L/D - * - * 0: B 5: L/ 11: a 16: k/ - * 1: D 6: L/1 12: c 17: k/1 - * 2: F 7: L/B 13: e 18: k/B - * 3: H 8: L/D 14: g 19: k/D - * 4: J 9: L/a 15: i 20: k/a - * 10: L/c 21: k/c - */ - -void test_repo_iterator__index(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - - /* autoexpand with no tree entries for index */ - cl_git_pass(git_iterator_for_index(&i, g_repo, index, NULL)); - expect_iterator_items(i, 20, NULL, 20, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 22, NULL, 22, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 12, NULL, 22, NULL); - git_iterator_free(i); - - git_index_free(index); -} - -void test_repo_iterator__index_icase(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - int caps; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - caps = git_index_caps(index); - - /* force case sensitivity */ - cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE)); - - /* autoexpand with no tree entries over range */ - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 7, NULL, 7, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 5, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 1, NULL, 4, NULL); - git_iterator_free(i); - - /* force case insensitivity */ - cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); - - /* autoexpand with no tree entries over range */ - i_opts.flags = 0; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 13, NULL, 13, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 14, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 6, NULL, 6, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 9, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 1, NULL, 6, NULL); - git_iterator_free(i); - - cl_git_pass(git_index_set_caps(index, caps)); - git_index_free(index); -} - -void test_repo_iterator__tree(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_tree *head; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_head_tree(&head, g_repo)); - - /* auto expand with no tree entries */ - cl_git_pass(git_iterator_for_tree(&i, head, NULL)); - expect_iterator_items(i, 20, NULL, 20, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 22, NULL, 22, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 12, NULL, 22, NULL); - git_iterator_free(i); - - git_tree_free(head); -} - -void test_repo_iterator__tree_icase(void) -{ - git_iterator *i; - git_tree *head; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_head_tree(&head, g_repo)); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - /* auto expand with no tree entries */ - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 7, NULL, 7, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - - /* auto expand with tree entries */ - i_opts.start = "c"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 5, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 1, NULL, 4, NULL); - git_iterator_free(i); - - /* auto expand with no tree entries */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 13, NULL, 13, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 14, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 6, NULL, 6, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 9, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 1, NULL, 6, NULL); - git_iterator_free(i); - - git_tree_free(head); -} - -void test_repo_iterator__tree_more(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_tree *head; - static const char *expect_basic[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "subdir.txt", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - NULL, - }; - static const char *expect_trees[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "subdir.txt", - "subdir/", - "subdir/current_file", - "subdir/deleted_file", - "subdir/modified_file", - NULL, - }; - static const char *expect_noauto[] = { - "current_file", - "file_deleted", - "modified_file", - "staged_changes", - "staged_changes_file_deleted", - "staged_changes_modified_file", - "staged_delete_file_deleted", - "staged_delete_modified_file", - "subdir.txt", - "subdir/", - NULL - }; - - g_repo = cl_git_sandbox_init("status"); - - cl_git_pass(git_repository_head_tree(&head, g_repo)); - - /* auto expand with no tree entries */ - cl_git_pass(git_iterator_for_tree(&i, head, NULL)); - expect_iterator_items(i, 12, expect_basic, 12, expect_basic); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 13, expect_trees, 13, expect_trees); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - - cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); - expect_iterator_items(i, 10, expect_noauto, 13, expect_trees); - git_iterator_free(i); - - git_tree_free(head); -} - -/* "b=name,t=name", blob_id, tree_id */ -static void build_test_tree( - git_oid *out, git_repository *repo, const char *fmt, ...) -{ - git_oid *id; - git_treebuilder *builder; - const char *scan = fmt, *next; - char type, delimiter; - git_filemode_t mode = GIT_FILEMODE_BLOB; - git_buf name = GIT_BUF_INIT; - va_list arglist; - - cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); /* start builder */ - - va_start(arglist, fmt); - while (*scan) { - switch (type = *scan++) { - case 't': case 'T': mode = GIT_FILEMODE_TREE; break; - case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break; - default: - cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B'); - } - - delimiter = *scan++; /* read and skip delimiter */ - for (next = scan; *next && *next != delimiter; ++next) - /* seek end */; - cl_git_pass(git_buf_set(&name, scan, (size_t)(next - scan))); - for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan) - /* skip delimiter and optional comma */; - - id = va_arg(arglist, git_oid *); - - cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode)); - } - va_end(arglist); - - cl_git_pass(git_treebuilder_write(out, builder)); - - git_treebuilder_free(builder); - git_buf_free(&name); -} - -void test_repo_iterator__tree_case_conflicts_0(void) -{ - const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; - git_tree *tree; - git_oid blob_id, biga_id, littlea_id, tree_id; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - const char *expect_cs[] = { - "A/1.file", "A/3.file", "a/2.file", "a/4.file" }; - const char *expect_ci[] = { - "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; - const char *expect_cs_trees[] = { - "A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" }; - const char *expect_ci_trees[] = { - "A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" }; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ - - /* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */ - build_test_tree( - &biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id); - build_test_tree( - &littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id); - build_test_tree( - &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, expect_cs, 4, expect_cs); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, expect_ci, 4, expect_ci); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees); - git_iterator_free(i); - - git_tree_free(tree); -} - -void test_repo_iterator__tree_case_conflicts_1(void) -{ - const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; - git_tree *tree; - git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - const char *expect_cs[] = { - "A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" }; - const char *expect_ci[] = { - "A/a", "a/b", "A/b/1", "A/c" }; - const char *expect_cs_trees[] = { - "A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" }; - const char *expect_ci_trees[] = { - "A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" }; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ - - /* create: A/a A/b/1 A/c a/a a/b a/C */ - build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id); - build_test_tree( - &biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id); - build_test_tree( - &littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id); - build_test_tree( - &tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 6, expect_cs, 6, expect_cs); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, expect_ci, 4, expect_ci); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees); - git_iterator_free(i); - - git_tree_free(tree); -} - -void test_repo_iterator__tree_case_conflicts_2(void) -{ - const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6"; - git_tree *tree; - git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - const char *expect_cs[] = { - "A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO", - "A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO", - "A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO", - "A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO", - "a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO", - "a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO", - "a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO", - "a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", }; - const char *expect_ci[] = { - "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", - "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", - "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", - "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", - "A/B/C/D/foo", }; - const char *expect_ci_trees[] = { - "A/", "A/B/", "A/B/C/", "A/B/C/D/", - "a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04", - "a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08", - "A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12", - "A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16", - "A/B/C/D/foo", }; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */ - - build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); - - build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); - - build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2); - - build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2); - - build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id); - build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id); - build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2); - build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2); - - build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2); - - build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2); - - cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 32, expect_cs, 32, expect_cs); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 17, expect_ci, 17, expect_ci); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees); - git_iterator_free(i); - - git_tree_free(tree); -} - -void test_repo_iterator__workdir(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("icase"); - - /* auto expand with no tree entries */ - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 20, NULL, 20, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 22, NULL, 22, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 12, NULL, 22, NULL); - git_iterator_free(i); -} - -void test_repo_iterator__workdir_icase(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("icase"); - - /* auto expand with no tree entries */ - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 7, NULL, 7, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 5, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 1, NULL, 4, NULL); - git_iterator_free(i); - - /* auto expand with no tree entries */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 13, NULL, 13, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); - - /* auto expand with tree entries */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 14, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 6, NULL, 6, NULL); - git_iterator_free(i); - - /* no auto expand (implies trees included) */ - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 9, NULL, 14, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 1, NULL, 6, NULL); - git_iterator_free(i); -} - -static void build_workdir_tree(const char *root, int dirs, int subs) -{ - int i, j; - char buf[64], sub[64]; - - for (i = 0; i < dirs; ++i) { - if (i % 2 == 0) { - p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i); - cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH)); - - p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i); - cl_git_mkfile(buf, buf); - buf[strlen(buf) - 5] = '\0'; - } else { - p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i); - cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH)); - } - - for (j = 0; j < subs; ++j) { - switch (j % 4) { - case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break; - case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break; - case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break; - case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break; - } - cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH)); - - if (j % 2 == 0) { - size_t sublen = strlen(sub); - memcpy(&sub[sublen], "/file", sizeof("/file")); - cl_git_mkfile(sub, sub); - sub[sublen] = '\0'; - } - } - } -} - -void test_repo_iterator__workdir_depth(void) -{ - git_iterator *iter; - git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("icase"); - - build_workdir_tree("icase", 10, 10); - build_workdir_tree("icase/DIR01/sUB01", 50, 0); - build_workdir_tree("icase/dir02/sUB01", 50, 0); - - /* auto expand with no tree entries */ - cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts)); - expect_iterator_items(iter, 125, NULL, 125, NULL); - git_iterator_free(iter); - - /* auto expand with tree entries (empty dirs silently skipped) */ - iter_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts)); - expect_iterator_items(iter, 337, NULL, 337, NULL); - git_iterator_free(iter); -} - -void test_repo_iterator__fs(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - static const char *expect_base[] = { - "DIR01/Sub02/file", - "DIR01/sub00/file", - "current_file", - "dir00/Sub02/file", - "dir00/file", - "dir00/sub00/file", - "modified_file", - "new_file", - NULL, - }; - static const char *expect_trees[] = { - "DIR01/", - "DIR01/SUB03/", - "DIR01/Sub02/", - "DIR01/Sub02/file", - "DIR01/sUB01/", - "DIR01/sub00/", - "DIR01/sub00/file", - "current_file", - "dir00/", - "dir00/SUB03/", - "dir00/Sub02/", - "dir00/Sub02/file", - "dir00/file", - "dir00/sUB01/", - "dir00/sub00/", - "dir00/sub00/file", - "modified_file", - "new_file", - NULL, - }; - static const char *expect_noauto[] = { - "DIR01/", - "current_file", - "dir00/", - "modified_file", - "new_file", - NULL, - }; - - g_repo = cl_git_sandbox_init("status"); - - build_workdir_tree("status/subdir", 2, 4); - - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", NULL)); - expect_iterator_items(i, 8, expect_base, 8, expect_base); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); - expect_iterator_items(i, 18, expect_trees, 18, expect_trees); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); - expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); - git_iterator_free(i); - - git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp); - git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp); - git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); - expect_iterator_items(i, 8, expect_base, 8, expect_base); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); - expect_iterator_items(i, 18, expect_trees, 18, expect_trees); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND; - cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts)); - expect_iterator_items(i, 5, expect_noauto, 18, expect_trees); - git_iterator_free(i); -} - -void test_repo_iterator__fs2(void) -{ - git_iterator *i; - static const char *expect_base[] = { - "heads/br2", - "heads/dir", - "heads/ident", - "heads/long-file-name", - "heads/master", - "heads/packed-test", - "heads/subtrees", - "heads/test", - "tags/e90810b", - "tags/foo/bar", - "tags/foo/foo/bar", - "tags/point_to_blob", - "tags/test", - NULL, - }; - - g_repo = cl_git_sandbox_init("testrepo"); - - cl_git_pass(git_iterator_for_filesystem( - &i, "testrepo/.git/refs", NULL)); - expect_iterator_items(i, 13, expect_base, 13, expect_base); - git_iterator_free(i); -} - -void test_repo_iterator__fs_gunk(void) -{ - git_iterator *i; - git_buf parent = GIT_BUF_INIT; - int n; - - if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) - cl_skip(); - - g_repo = cl_git_sandbox_init("testrepo"); - - for (n = 0; n < 100000; n++) { - git_buf_clear(&parent); - git_buf_printf(&parent, "%s/refs/heads/foo/%d/subdir", - git_repository_path(g_repo), n); - cl_assert(!git_buf_oom(&parent)); - - cl_git_pass(git_futils_mkdir(parent.ptr, 0775, GIT_MKDIR_PATH)); - } - - cl_git_pass(git_iterator_for_filesystem(&i, "testrepo/.git/refs", NULL)); - - /* should only have 13 items, since we're not asking for trees to be - * returned. the goal of this test is simply to not crash. - */ - expect_iterator_items(i, 13, NULL, 13, NULL); - git_iterator_free(i); - git_buf_free(&parent); -} - -void test_repo_iterator__skips_unreadable_dirs(void) -{ - git_iterator *i; - const git_index_entry *e; - - if (!cl_is_chmod_supported()) - return; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_must_pass(p_mkdir("empty_standard_repo/r", 0777)); - cl_git_mkfile("empty_standard_repo/r/a", "hello"); - cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777)); - cl_git_mkfile("empty_standard_repo/r/b/problem", "not me"); - cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000)); - cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777)); - cl_git_mkfile("empty_standard_repo/r/c/foo", "aloha"); - cl_git_mkfile("empty_standard_repo/r/d", "final"); - - cl_git_pass(git_iterator_for_filesystem( - &i, "empty_standard_repo/r", NULL)); - - cl_git_pass(git_iterator_advance(&e, i)); /* a */ - cl_assert_equal_s("a", e->path); - - cl_git_pass(git_iterator_advance(&e, i)); /* c/foo */ - cl_assert_equal_s("c/foo", e->path); - - cl_git_pass(git_iterator_advance(&e, i)); /* d */ - cl_assert_equal_s("d", e->path); - - cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777)); - - git_iterator_free(i); -} - -void test_repo_iterator__skips_fifos_and_such(void) -{ -#ifndef GIT_WIN32 - git_iterator *i; - const git_index_entry *e; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("empty_standard_repo"); - - cl_must_pass(p_mkdir("empty_standard_repo/dir", 0777)); - cl_git_mkfile("empty_standard_repo/file", "not me"); - - cl_assert(!mkfifo("empty_standard_repo/fifo", 0777)); - cl_assert(!access("empty_standard_repo/fifo", F_OK)); - - i_opts.flags = GIT_ITERATOR_INCLUDE_TREES | - GIT_ITERATOR_DONT_AUTOEXPAND; - - cl_git_pass(git_iterator_for_filesystem( - &i, "empty_standard_repo", &i_opts)); - - cl_git_pass(git_iterator_advance(&e, i)); /* .git */ - cl_assert(S_ISDIR(e->mode)); - cl_git_pass(git_iterator_advance(&e, i)); /* dir */ - cl_assert(S_ISDIR(e->mode)); - /* skips fifo */ - cl_git_pass(git_iterator_advance(&e, i)); /* file */ - cl_assert(S_ISREG(e->mode)); - - cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i)); - - git_iterator_free(i); -#endif -} - -void test_repo_iterator__indexfilelist(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - git_vector filelist; - int default_icase; - int expect; - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - - /* In this test we DO NOT force a case setting on the index. */ - default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "c"; - i_opts.end = NULL; - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ - expect = ((default_icase) ? 6 : 4); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - i_opts.start = NULL; - i_opts.end = "e"; - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ - expect = ((default_icase) ? 5 : 6); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__indexfilelist_2(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - git_vector filelist = GIT_VECTOR_INIT; - int default_icase, expect; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "0")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - - /* In this test we DO NOT force a case setting on the index. */ - default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - i_opts.start = "b"; - i_opts.end = "k/D"; - - /* (c D e k/1 k/a ==> 5) vs (c e k/1 ==> 3) */ - expect = default_icase ? 5 : 3; - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__indexfilelist_3(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - git_vector filelist = GIT_VECTOR_INIT; - int default_icase, expect; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "0")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k/")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); - - /* In this test we DO NOT force a case setting on the index. */ - default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - i_opts.start = "b"; - i_opts.end = "k/D"; - - /* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */ - expect = default_icase ? 8 : 5; - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__indexfilelist_4(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - git_vector filelist = GIT_VECTOR_INIT; - int default_icase, expect; - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "0")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); - - /* In this test we DO NOT force a case setting on the index. */ - default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - i_opts.start = "b"; - i_opts.end = "k/D"; - - /* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */ - expect = default_icase ? 8 : 5; - - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__indexfilelist_icase(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_index *index; - int caps; - git_vector filelist; - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); - - g_repo = cl_git_sandbox_init("icase"); - - cl_git_pass(git_repository_index(&index, g_repo)); - caps = git_index_caps(index); - - /* force case sensitivity */ - cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE)); - - /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 1, NULL, 1, NULL); - git_iterator_free(i); - - /* force case insensitivity */ - cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE)); - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 2, NULL, 2, NULL); - git_iterator_free(i); - - cl_git_pass(git_index_set_caps(index, caps)); - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__indexfilelist_with_directory(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - git_index *index; - - g_repo = cl_git_sandbox_init("testrepo2"); - git_repository_head_tree(&tree, g_repo); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "subdir")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_repository_index(&index, g_repo)); - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - git_index_free(index); - git_vector_free(&filelist); -} - -void test_repo_iterator__workdir_pathlist(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - - cl_git_pass(git_vector_init(&filelist, 100, NULL)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); - - g_repo = cl_git_sandbox_init("icase"); - - /* Test iterators with default case sensitivity, without returning - * tree entries (but autoexpanding. - */ - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - /* Case sensitive */ - { - const char *expected[] = { - "B", "D", "L/1", "a", "c", "e", "k/1", "k/a" }; - size_t expected_len = 8; - - i_opts.start = NULL; - i_opts.end = NULL; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Case INsensitive */ - { - const char *expected[] = { - "a", "B", "c", "D", "e", "k/1", "k/a", "L/1" }; - size_t expected_len = 8; - - i_opts.start = NULL; - i_opts.end = NULL; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Set a start, but no end. Case sensitive. */ - { - const char *expected[] = { "c", "e", "k/1", "k/a" }; - size_t expected_len = 4; - - i_opts.start = "c"; - i_opts.end = NULL; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Set a start, but no end. Case INsensitive. */ - { - const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" }; - size_t expected_len = 6; - - i_opts.start = "c"; - i_opts.end = NULL; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Set no start, but an end. Case sensitive. */ - { - const char *expected[] = { "B", "D", "L/1", "a", "c", "e" }; - size_t expected_len = 6; - - i_opts.start = NULL; - i_opts.end = "e"; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Set no start, but an end. Case INsensitive. */ - { - const char *expected[] = { "a", "B", "c", "D", "e" }; - size_t expected_len = 5; - - i_opts.start = NULL; - i_opts.end = "e"; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Start and an end, case sensitive */ - { - const char *expected[] = { "c", "e", "k/1" }; - size_t expected_len = 3; - - i_opts.start = "c"; - i_opts.end = "k/D"; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Start and an end, case sensitive */ - { - const char *expected[] = { "k/1" }; - size_t expected_len = 1; - - i_opts.start = "k"; - i_opts.end = "k/D"; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Start and an end, case INsensitive */ - { - const char *expected[] = { "c", "D", "e", "k/1", "k/a" }; - size_t expected_len = 5; - - i_opts.start = "c"; - i_opts.end = "k/D"; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Start and an end, case INsensitive */ - { - const char *expected[] = { "k/1", "k/a" }; - size_t expected_len = 2; - - i_opts.start = "k"; - i_opts.end = "k/D"; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - git_vector_free(&filelist); -} - -void test_repo_iterator__workdir_pathlist_with_dirs(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - - cl_git_pass(git_vector_init(&filelist, 5, NULL)); - - g_repo = cl_git_sandbox_init("icase"); - - /* Test that a prefix `k` matches folders, even without trailing slash */ - { - const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; - size_t expected_len = 5; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "k")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Test that a `k/` matches a folder */ - { - const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; - size_t expected_len = 5; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "k/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* When the iterator is case sensitive, ensure we can't lookup the - * directory with the wrong case. - */ - { - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "K/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); - } - - /* Test that case insensitive matching works. */ - { - const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; - size_t expected_len = 5; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "K/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Test that case insensitive matching works without trailing slash. */ - { - const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; - size_t expected_len = 5; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "K")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - git_vector_free(&filelist); -} - -static void create_paths(const char *root, int depth) -{ - git_buf fullpath = GIT_BUF_INIT; - size_t root_len; - int i; - - cl_git_pass(git_buf_puts(&fullpath, root)); - cl_git_pass(git_buf_putc(&fullpath, '/')); - - root_len = fullpath.size; - - for (i = 0; i < 8; i++) { - bool file = (depth == 0 || (i % 2) == 0); - git_buf_truncate(&fullpath, root_len); - cl_git_pass(git_buf_printf(&fullpath, "item%d", i)); - - if (file) { - cl_git_rewritefile(fullpath.ptr, "This is a file!\n"); - } else { - cl_must_pass(p_mkdir(fullpath.ptr, 0777)); - - if (depth > 0) - create_paths(fullpath.ptr, (depth - 1)); - } - } -} - -void test_repo_iterator__workdir_pathlist_for_deeply_nested_item(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - - cl_git_pass(git_vector_init(&filelist, 5, NULL)); - - g_repo = cl_git_sandbox_init("icase"); - create_paths(git_repository_workdir(g_repo), 3); - - /* Ensure that we find the single path we're interested in, and we find - * it efficiently, and don't stat the entire world to get there. - */ - { - const char *expected[] = { "item1/item3/item5/item7" }; - size_t expected_len = 1; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - cl_assert_equal_i(4, i->stat_calls); - git_iterator_free(i); - } - - /* Ensure that we find the single path we're interested in, and we find - * it efficiently, and don't stat the entire world to get there. - */ - { - const char *expected[] = { - "item1/item3/item5/item0", "item1/item3/item5/item1", - "item1/item3/item5/item2", "item1/item3/item5/item3", - "item1/item3/item5/item4", "item1/item3/item5/item5", - "item1/item3/item5/item6", "item1/item3/item5/item7", - }; - size_t expected_len = 8; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - cl_assert_equal_i(11, i->stat_calls); - git_iterator_free(i); - } - - /* Ensure that we find the single path we're interested in, and we find - * it efficiently, and don't stat the entire world to get there. - */ - { - const char *expected[] = { - "item1/item3/item0", - "item1/item3/item1/item0", "item1/item3/item1/item1", - "item1/item3/item1/item2", "item1/item3/item1/item3", - "item1/item3/item1/item4", "item1/item3/item1/item5", - "item1/item3/item1/item6", "item1/item3/item1/item7", - "item1/item3/item2", - "item1/item3/item3/item0", "item1/item3/item3/item1", - "item1/item3/item3/item2", "item1/item3/item3/item3", - "item1/item3/item3/item4", "item1/item3/item3/item5", - "item1/item3/item3/item6", "item1/item3/item3/item7", - "item1/item3/item4", - "item1/item3/item5/item0", "item1/item3/item5/item1", - "item1/item3/item5/item2", "item1/item3/item5/item3", - "item1/item3/item5/item4", "item1/item3/item5/item5", - "item1/item3/item5/item6", "item1/item3/item5/item7", - "item1/item3/item6", - "item1/item3/item7/item0", "item1/item3/item7/item1", - "item1/item3/item7/item2", "item1/item3/item7/item3", - "item1/item3/item7/item4", "item1/item3/item7/item5", - "item1/item3/item7/item6", "item1/item3/item7/item7", - }; - size_t expected_len = 36; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "item1/item3/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - cl_assert_equal_i(42, i->stat_calls); - git_iterator_free(i); - } - - /* Ensure that we find the single path we're interested in, and we find - * it efficiently, and don't stat the entire world to get there. - */ - { - const char *expected[] = { - "item0", "item1/item2", "item5/item7/item4", "item6", - "item7/item3/item1/item6" }; - size_t expected_len = 5; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6")); - cl_git_pass(git_vector_insert(&filelist, "item6")); - cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4")); - cl_git_pass(git_vector_insert(&filelist, "item1/item2")); - cl_git_pass(git_vector_insert(&filelist, "item0")); - - /* also add some things that don't exist or don't match the right type */ - cl_git_pass(git_vector_insert(&filelist, "item2/")); - cl_git_pass(git_vector_insert(&filelist, "itemN")); - cl_git_pass(git_vector_insert(&filelist, "item1/itemA")); - cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - cl_assert_equal_i(14, i->stat_calls); - git_iterator_free(i); - } - - git_vector_free(&filelist); -} - -void test_repo_iterator__workdir_bounded_submodules(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_index *index; - git_tree *head; - - cl_git_pass(git_vector_init(&filelist, 5, NULL)); - - g_repo = setup_fixture_submod2(); - cl_git_pass(git_repository_index(&index, g_repo)); - cl_git_pass(git_repository_head_tree(&head, g_repo)); - - /* Test that a submodule matches */ - { - const char *expected[] = { "sm_changed_head" }; - size_t expected_len = 1; - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "sm_changed_head")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Test that a submodule never matches when suffixed with a '/' */ - { - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "sm_changed_head/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); - } - - /* Test that start/end work with a submodule */ - { - const char *expected[] = { "sm_changed_head", "sm_changed_index" }; - size_t expected_len = 2; - - i_opts.start = "sm_changed_head"; - i_opts.end = "sm_changed_index"; - i_opts.pathlist.strings = NULL; - i_opts.pathlist.count = 0; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); - expect_iterator_items(i, expected_len, expected, expected_len, expected); - git_iterator_free(i); - } - - /* Test that start and end do not allow '/' suffixes of submodules */ - { - i_opts.start = "sm_changed_head/"; - i_opts.end = "sm_changed_head/"; - i_opts.pathlist.strings = NULL; - i_opts.pathlist.count = 0; - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts)); - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); - } - - git_vector_free(&filelist); - git_index_free(index); - git_tree_free(head); -} - -static void expect_advance_over( - git_iterator *i, - const char *expected_path, - git_iterator_status_t expected_status) -{ - const git_index_entry *entry; - git_iterator_status_t status; - int error; - - cl_git_pass(git_iterator_current(&entry, i)); - cl_assert_equal_s(expected_path, entry->path); - - error = git_iterator_advance_over(&entry, &status, i); - cl_assert(!error || error == GIT_ITEROVER); - cl_assert_equal_i(expected_status, status); -} - -void test_repo_iterator__workdir_advance_over(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | - GIT_ITERATOR_DONT_AUTOEXPAND; - - g_repo = cl_git_sandbox_init("icase"); - - /* create an empty directory */ - cl_must_pass(p_mkdir("icase/empty", 0777)); - - /* create a directory in which all contents are ignored */ - cl_must_pass(p_mkdir("icase/all_ignored", 0777)); - cl_git_rewritefile("icase/all_ignored/one", "This is ignored\n"); - cl_git_rewritefile("icase/all_ignored/two", "This, too, is ignored\n"); - cl_git_rewritefile("icase/all_ignored/.gitignore", ".gitignore\none\ntwo\n"); - - /* create a directory in which not all contents are ignored */ - cl_must_pass(p_mkdir("icase/some_ignored", 0777)); - cl_git_rewritefile("icase/some_ignored/one", "This is ignored\n"); - cl_git_rewritefile("icase/some_ignored/two", "This is not ignored\n"); - cl_git_rewritefile("icase/some_ignored/.gitignore", ".gitignore\none\n"); - - /* create a directory which has some empty children */ - cl_must_pass(p_mkdir("icase/empty_children", 0777)); - cl_must_pass(p_mkdir("icase/empty_children/empty1", 0777)); - cl_must_pass(p_mkdir("icase/empty_children/empty2", 0777)); - cl_must_pass(p_mkdir("icase/empty_children/empty3", 0777)); - - /* create a directory which will disappear! */ - cl_must_pass(p_mkdir("icase/missing_directory", 0777)); - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - - cl_must_pass(p_rmdir("icase/missing_directory")); - - expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "all_ignored/", GIT_ITERATOR_STATUS_IGNORED); - expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "empty/", GIT_ITERATOR_STATUS_EMPTY); - expect_advance_over(i, "empty_children/", GIT_ITERATOR_STATUS_EMPTY); - expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "missing_directory/", GIT_ITERATOR_STATUS_EMPTY); - expect_advance_over(i, "some_ignored/", GIT_ITERATOR_STATUS_NORMAL); - - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); -} - -void test_repo_iterator__workdir_advance_over_with_pathlist(void) -{ - git_vector pathlist = GIT_VECTOR_INIT; - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - git_vector_insert(&pathlist, "dirA/subdir1/subdir2/file"); - git_vector_insert(&pathlist, "dirB/subdir1/subdir2"); - git_vector_insert(&pathlist, "dirC/subdir1/nonexistent"); - git_vector_insert(&pathlist, "dirD/subdir1/nonexistent"); - git_vector_insert(&pathlist, "dirD/subdir1/subdir2"); - git_vector_insert(&pathlist, "dirD/nonexistent"); - - i_opts.pathlist.strings = (char **)pathlist.contents; - i_opts.pathlist.count = pathlist.length; - i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | - GIT_ITERATOR_DONT_AUTOEXPAND; - - g_repo = cl_git_sandbox_init("icase"); - - /* Create a directory that has a file that is included in our pathlist */ - cl_must_pass(p_mkdir("icase/dirA", 0777)); - cl_must_pass(p_mkdir("icase/dirA/subdir1", 0777)); - cl_must_pass(p_mkdir("icase/dirA/subdir1/subdir2", 0777)); - cl_git_rewritefile("icase/dirA/subdir1/subdir2/file", "foo!"); - - /* Create a directory that has a directory that is included in our pathlist */ - cl_must_pass(p_mkdir("icase/dirB", 0777)); - cl_must_pass(p_mkdir("icase/dirB/subdir1", 0777)); - cl_must_pass(p_mkdir("icase/dirB/subdir1/subdir2", 0777)); - cl_git_rewritefile("icase/dirB/subdir1/subdir2/file", "foo!"); - - /* Create a directory that would contain an entry in our pathlist, but - * that entry does not actually exist. We don't know this until we - * advance_over it. We want to distinguish this from an actually empty - * or ignored directory. - */ - cl_must_pass(p_mkdir("icase/dirC", 0777)); - cl_must_pass(p_mkdir("icase/dirC/subdir1", 0777)); - cl_must_pass(p_mkdir("icase/dirC/subdir1/subdir2", 0777)); - cl_git_rewritefile("icase/dirC/subdir1/subdir2/file", "foo!"); - - /* Create a directory that has a mix of actual and nonexistent paths */ - cl_must_pass(p_mkdir("icase/dirD", 0777)); - cl_must_pass(p_mkdir("icase/dirD/subdir1", 0777)); - cl_must_pass(p_mkdir("icase/dirD/subdir1/subdir2", 0777)); - cl_git_rewritefile("icase/dirD/subdir1/subdir2/file", "foo!"); - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - - expect_advance_over(i, "dirA/", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "dirB/", GIT_ITERATOR_STATUS_NORMAL); - expect_advance_over(i, "dirC/", GIT_ITERATOR_STATUS_FILTERED); - expect_advance_over(i, "dirD/", GIT_ITERATOR_STATUS_NORMAL); - - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); - git_vector_free(&pathlist); -} - -static void expect_advance_into( - git_iterator *i, - const char *expected_path) -{ - const git_index_entry *entry; - int error; - - cl_git_pass(git_iterator_current(&entry, i)); - cl_assert_equal_s(expected_path, entry->path); - - if (S_ISDIR(entry->mode)) - error = git_iterator_advance_into(&entry, i); - else - error = git_iterator_advance(&entry, i); - - cl_assert(!error || error == GIT_ITEROVER); -} - -void test_repo_iterator__workdir_advance_into(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - - g_repo = cl_git_sandbox_init("icase"); - - i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | - GIT_ITERATOR_DONT_AUTOEXPAND; - - cl_must_pass(p_mkdir("icase/Empty", 0777)); - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_advance_into(i, "B"); - expect_advance_into(i, "D"); - expect_advance_into(i, "Empty/"); - expect_advance_into(i, "F"); - expect_advance_into(i, "H"); - expect_advance_into(i, "J"); - expect_advance_into(i, "L/"); - expect_advance_into(i, "L/1"); - expect_advance_into(i, "L/B"); - expect_advance_into(i, "L/D"); - expect_advance_into(i, "L/a"); - expect_advance_into(i, "L/c"); - expect_advance_into(i, "a"); - expect_advance_into(i, "c"); - expect_advance_into(i, "e"); - expect_advance_into(i, "g"); - expect_advance_into(i, "i"); - expect_advance_into(i, "k/"); - expect_advance_into(i, "k/1"); - expect_advance_into(i, "k/B"); - expect_advance_into(i, "k/D"); - expect_advance_into(i, "k/a"); - expect_advance_into(i, "k/c"); - - cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); - git_iterator_free(i); -} - -void test_repo_iterator__workdir_filelist_with_directory(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "subdir/")); - - g_repo = cl_git_sandbox_init("testrepo2"); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - git_vector_free(&filelist); -} - -void test_repo_iterator__treefilelist(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - bool default_icase; - int expect; - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); - - g_repo = cl_git_sandbox_init("icase"); - git_repository_head_tree(&tree, g_repo); - - /* All indexfilelist iterator tests are "autoexpand with no tree entries" */ - /* In this test we DO NOT force a case on the iterators and verify default behavior. */ - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); - - i_opts.start = "c"; - i_opts.end = NULL; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - default_icase = git_iterator_ignore_case(i); - /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ - expect = ((default_icase) ? 6 : 4); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - i_opts.start = NULL; - i_opts.end = "e"; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - default_icase = git_iterator_ignore_case(i); - /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ - expect = ((default_icase) ? 5 : 6); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); - - git_vector_free(&filelist); - git_tree_free(tree); -} - -void test_repo_iterator__treefilelist_icase(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "a")); - cl_git_pass(git_vector_insert(&filelist, "B")); - cl_git_pass(git_vector_insert(&filelist, "c")); - cl_git_pass(git_vector_insert(&filelist, "D")); - cl_git_pass(git_vector_insert(&filelist, "e")); - cl_git_pass(git_vector_insert(&filelist, "k.a")); - cl_git_pass(git_vector_insert(&filelist, "k.b")); - cl_git_pass(git_vector_insert(&filelist, "k/1")); - cl_git_pass(git_vector_insert(&filelist, "k/a")); - cl_git_pass(git_vector_insert(&filelist, "kZZZZ")); - cl_git_pass(git_vector_insert(&filelist, "L/1")); - - g_repo = cl_git_sandbox_init("icase"); - git_repository_head_tree(&tree, g_repo); - - i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 3, NULL, 3, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 1, NULL, 1, NULL); - git_iterator_free(i); - - i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - - i_opts.start = "c"; - i_opts.end = "k/D"; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 5, NULL, 5, NULL); - git_iterator_free(i); - - i_opts.start = "k"; - i_opts.end = "k/Z"; - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 2, NULL, 2, NULL); - git_iterator_free(i); - - git_vector_free(&filelist); - git_tree_free(tree); -} - -void test_repo_iterator__tree_filelist_with_directory(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - - g_repo = cl_git_sandbox_init("testrepo2"); - git_repository_head_tree(&tree, g_repo); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "subdir")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "subdir/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); - git_iterator_free(i); - - git_vector_clear(&filelist); - cl_git_pass(git_vector_insert(&filelist, "subdir/subdir2")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 2, NULL, 2, NULL); - git_iterator_free(i); - - git_vector_free(&filelist); -} - -void test_repo_iterator__tree_filelist_with_directory_include_tree_nodes(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - - g_repo = cl_git_sandbox_init("testrepo2"); - git_repository_head_tree(&tree, g_repo); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "subdir")); - - i_opts.flags |= GIT_ITERATOR_INCLUDE_TREES; - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 6, NULL, 6, NULL); - git_iterator_free(i); - - git_vector_free(&filelist); -} - -void test_repo_iterator__tree_filelist_no_match(void) -{ - git_iterator *i; - git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; - git_vector filelist; - git_tree *tree; - const git_index_entry *entry; - - g_repo = cl_git_sandbox_init("testrepo2"); - git_repository_head_tree(&tree, g_repo); - - cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); - cl_git_pass(git_vector_insert(&filelist, "nonexistent/")); - - i_opts.pathlist.strings = (char **)filelist.contents; - i_opts.pathlist.count = filelist.length; - - cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - cl_assert_equal_i(GIT_ITEROVER, git_iterator_current(&entry, i)); - - git_vector_free(&filelist); -} - From 82a1aab647c9a587e0b8959719a6ea507a68ea31 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Mar 2016 12:59:35 -0400 Subject: [PATCH 24/35] iterator: move the index into the iterator itself --- src/iterator.c | 41 ++++++++--------------------------------- src/iterator.h | 10 +++++++--- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index 88b7ed28a..188f0cf56 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 85444f11f..460f9475a 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 From ba6f86eb2e100ad6f39e70bd52d7144df1b43a1a Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Mar 2016 17:33:46 -0400 Subject: [PATCH 25/35] Introduce `git_path_common_dirlen` --- src/path.c | 14 ++++++++++++++ src/path.h | 12 ++++++++++++ tests/core/path.c | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/path.c b/src/path.c index 1fd14fcb9..4133985a4 100644 --- a/src/path.c +++ b/src/path.c @@ -810,6 +810,20 @@ int git_path_cmp( return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; } +size_t git_path_common_dirlen(const char *one, const char *two) +{ + const char *p, *q, *dirsep = NULL; + + for (p = one, q = two; *p && *q; p++, q++) { + if (*p == '/' && *q == '/') + dirsep = p; + else if (*p != *q) + break; + } + + return dirsep ? (dirsep - one) + 1 : 0; +} + int git_path_make_relative(git_buf *path, const char *parent) { const char *p, *q, *p_dirsep, *q_dirsep; diff --git a/src/path.h b/src/path.h index 875c8cb7e..f31cacc70 100644 --- a/src/path.h +++ b/src/path.h @@ -202,6 +202,18 @@ extern bool git_path_contains(git_buf *dir, const char *item); */ extern bool git_path_contains_dir(git_buf *parent, const char *subdir); +/** + * Determine the common directory length between two paths, including + * the final path separator. For example, given paths 'a/b/c/1.txt + * and 'a/b/c/d/2.txt', the common directory is 'a/b/c/', and this + * will return the length of the string 'a/b/c/', which is 6. + * + * @param one The first path + * @param two The second path + * @return The length of the common directory + */ +extern size_t git_path_common_dirlen(const char *one, const char *two); + /** * Make the path relative to the given parent path. * diff --git a/tests/core/path.c b/tests/core/path.c index c3e622f02..71c6eda58 100644 --- a/tests/core/path.c +++ b/tests/core/path.c @@ -652,3 +652,23 @@ void test_core_path__15_resolve_relative(void) git_buf_free(&buf); } + +#define assert_common_dirlen(i, p, q) \ + cl_assert_equal_i((i), git_path_common_dirlen((p), (q))); + +void test_core_path__16_resolve_relative(void) +{ + assert_common_dirlen(0, "", ""); + assert_common_dirlen(0, "", "bar.txt"); + assert_common_dirlen(0, "foo.txt", "bar.txt"); + assert_common_dirlen(0, "foo.txt", ""); + assert_common_dirlen(0, "foo/bar.txt", "bar/foo.txt"); + assert_common_dirlen(0, "foo/bar.txt", "../foo.txt"); + + assert_common_dirlen(1, "/one.txt", "/two.txt"); + assert_common_dirlen(4, "foo/one.txt", "foo/two.txt"); + assert_common_dirlen(5, "/foo/one.txt", "/foo/two.txt"); + + assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt"); + assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt"); +} From 0ef0b71ca5ce45a064dafe66462c7e9c143678ac Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 21 Mar 2016 12:54:47 -0400 Subject: [PATCH 26/35] iterator: refactor index iterator --- src/iterator.c | 428 ++++++++++--------- tests/iterator/index.c | 667 ++++++++++++++++++++++++++++-- tests/iterator/iterator_helpers.c | 36 ++ tests/iterator/iterator_helpers.h | 8 + tests/iterator/workdir.c | 35 -- 5 files changed, 899 insertions(+), 275 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index 188f0cf56..720a3d17a 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -345,6 +345,7 @@ static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist) static int iterator_init_common( git_iterator *iter, git_repository *repo, + git_index *index, git_iterator_options *given_opts) { static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT; @@ -354,6 +355,7 @@ static int iterator_init_common( int error; iter->repo = repo; + iter->index = index; iter->flags = options->flags; if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) { @@ -495,14 +497,10 @@ static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) */ if (p[cmp_len] == '/' && path[cmp_len] == '/') return true; - - /* examine the next character */ - cmp = (int)((const unsigned char)p[cmp_len]) - - (int)((const unsigned char)path[cmp_len]); } /* this pathlist entry sorts before the given path, try the next */ - if (cmp < 0) { + else if (cmp < 0) { iter->pathlist_walk_idx++; continue; } @@ -1164,7 +1162,7 @@ int git_iterator_for_tree( iter->base.cb = &callbacks; if ((error = iterator_init_common(&iter->base, - git_tree_owner(tree), options)) < 0 || + git_tree_owner(tree), NULL, options)) < 0 || (error = git_tree_dup(&iter->root, tree)) < 0 || (error = tree_iterator_init(iter)) < 0) goto on_error; @@ -2083,14 +2081,13 @@ static int iterator_for_filesystem( iter->base.type = type; iter->base.cb = &callbacks; - - if ((error = iterator_init_common(&iter->base, repo, options)) < 0) + if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0) goto on_error; if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0) goto on_error; - if ((iter->base.index = index) != NULL && + if (index && (error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0) goto on_error; @@ -2155,281 +2152,278 @@ int git_iterator_for_workdir_ext( typedef struct { git_iterator base; - git_iterator_callbacks cb; git_vector entries; - git_vector_cmp entry_srch; - size_t current; - /* when limiting with a pathlist, this is the current index into it */ - size_t pathlist_idx; - /* when not in autoexpand mode, use these to represent "tree" state */ - git_buf partial; - size_t partial_pos; - char restore_terminator; + size_t next_idx; + + /* the pseudotree entry */ git_index_entry tree_entry; + git_buf tree_buf; + bool skip_tree; + + const git_index_entry *entry; } index_iterator; -static const git_index_entry *index_iterator__index_entry(index_iterator *ii) +static int index_iterator_current( + const git_index_entry **out, git_iterator *i) { - const git_index_entry *ie = git_vector_get(&ii->entries, ii->current); + index_iterator *iter = (index_iterator *)i; - if (ie != NULL && iterator__past_end(ii, ie->path)) { - ii->current = git_vector_length(&ii->entries); - ie = NULL; + if (!iterator__has_been_accessed(i)) + return iter->base.cb->advance(out, i); + + if (iter->entry == NULL) { + *out = NULL; + return GIT_ITEROVER; } - return ie; + *out = iter->entry; + return 0; } -static const git_index_entry *index_iterator__advance_over_unwanted( - index_iterator *ii) +static bool index_iterator_create_pseudotree( + const git_index_entry **out, + index_iterator *iter, + const char *path) { - const git_index_entry *ie = index_iterator__index_entry(ii); - bool match; + const char *prev_path, *relative_path, *dirsep; + size_t common_len; - while (ie) { - if (!iterator__include_conflicts(ii) && - git_index_entry_is_conflict(ie)) { - ii->current++; - ie = index_iterator__index_entry(ii); + prev_path = iter->entry ? iter->entry->path : ""; + + /* determine if the new path is in a different directory from the old */ + common_len = git_path_common_dirlen(prev_path, path); + relative_path = path + common_len; + + if ((dirsep = strchr(relative_path, '/')) == NULL) + return false; + + git_buf_clear(&iter->tree_buf); + git_buf_put(&iter->tree_buf, path, (dirsep - path) + 1); + + iter->tree_entry.mode = GIT_FILEMODE_TREE; + iter->tree_entry.path = iter->tree_buf.ptr; + + *out = &iter->tree_entry; + return true; +} + +static int index_iterator_skip_pseudotree(index_iterator *iter) +{ + assert(iterator__has_been_accessed(&iter->base)); + assert(S_ISDIR(iter->entry->mode)); + + while (true) { + const git_index_entry *next_entry = NULL; + + if (++iter->next_idx >= iter->entries.length) + return GIT_ITEROVER; + + next_entry = iter->entries.contents[iter->next_idx]; + + if (iter->base.strncomp(iter->tree_buf.ptr, next_entry->path, + iter->tree_buf.size) != 0) + break; + } + + iter->skip_tree = false; + return 0; +} + +static int index_iterator_advance( + const git_index_entry **out, git_iterator *i) +{ + index_iterator *iter = (index_iterator *)i; + const git_index_entry *entry = NULL; + int error = 0; + + iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS; + + while (true) { + if (iter->next_idx >= iter->entries.length) { + error = GIT_ITEROVER; + break; + } + + /* we were not asked to expand this pseudotree. advance over it. */ + if (iter->skip_tree) { + index_iterator_skip_pseudotree(iter); continue; } - /* if we have a pathlist, this entry's path must be in it to be - * returned. walk the pathlist in unison with the index to - * compare paths. - */ - if (ii->base.pathlist.length) { - match = iterator_pathlist_walk__contains(&ii->base, ie->path); + entry = iter->entries.contents[iter->next_idx]; - if (!match) { - ii->current++; - ie = index_iterator__index_entry(ii); - continue; - } + if (!iterator_has_started(&iter->base, entry->path)) { + iter->next_idx++; + continue; } + if (iterator_has_ended(&iter->base, entry->path)) { + error = GIT_ITEROVER; + break; + } + + /* if we have a list of paths we're interested in, examine it */ + if (!iterator_pathlist_next_is(&iter->base, entry->path)) { + iter->next_idx++; + continue; + } + + /* if this is a conflict, skip it unless we're including conflicts */ + if (git_index_entry_is_conflict(entry) && + !iterator__include_conflicts(&iter->base)) { + iter->next_idx++; + continue; + } + + /* we've found what will be our next _file_ entry. but if we are + * returning trees entries, we may need to return a pseudotree + * entry that will contain this. don't advance over this entry, + * though, we still need to return it on the next `advance`. + */ + if (iterator__include_trees(&iter->base) && + index_iterator_create_pseudotree(&entry, iter, entry->path)) { + + /* Note whether this pseudo tree should be expanded or not */ + iter->skip_tree = iterator__dont_autoexpand(&iter->base); + break; + } + + iter->next_idx++; break; } - return ie; + iter->entry = (error == 0) ? entry : NULL; + + if (out) + *out = iter->entry; + + return error; } -static void index_iterator__next_prefix_tree(index_iterator *ii) +static int index_iterator_advance_into( + const git_index_entry **out, git_iterator *i) { - const char *slash; + index_iterator *iter = (index_iterator *)i; - 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 (! S_ISDIR(iter->tree_entry.mode)) { + *out = NULL; + return 0; } - if (index_iterator__index_entry(ii) == NULL) - ii->partial_pos = ii->partial.size; + iter->skip_tree = false; + return index_iterator_advance(out, i); } -static int index_iterator__first_prefix_tree(index_iterator *ii) +static int index_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) { - const git_index_entry *ie = index_iterator__advance_over_unwanted(ii); - const char *scan, *prior, *slash; + index_iterator *iter = (index_iterator *)i; + const git_index_entry *entry; + int error; - if (!ie || !iterator__include_trees(ii)) - return 0; + if ((error = index_iterator_current(&entry, i)) < 0) + return error; - /* 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 (S_ISDIR(entry->mode)) + index_iterator_skip_pseudotree(iter); - if (git_buf_sets(&ii->partial, ie->path) < 0) - return -1; + *status = GIT_ITERATOR_STATUS_NORMAL; + return index_iterator_advance(out, i); +} - ii->partial_pos = (slash - ie->path) + 1; - index_iterator__next_prefix_tree(ii); +static void index_iterator_clear(index_iterator *iter) +{ + iterator_clear(&iter->base); +} +static int index_iterator_init(index_iterator *iter) +{ + iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; + iter->next_idx = 0; + iter->skip_tree = false; return 0; } -#define index_iterator__at_tree(I) \ - (iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size) - -static int index_iterator__current( - const git_index_entry **entry, git_iterator *self) +static int index_iterator_reset(git_iterator *i) { - index_iterator *ii = (index_iterator *)self; - const git_index_entry *ie = git_vector_get(&ii->entries, ii->current); + index_iterator *iter = (index_iterator *)i; - if (ie != NULL && index_iterator__at_tree(ii)) { - ii->tree_entry.path = ii->partial.ptr; - ie = &ii->tree_entry; - } - - if (entry) - *entry = ie; - - ii->base.flags |= GIT_ITERATOR_FIRST_ACCESS; - - return (ie != NULL) ? 0 : GIT_ITEROVER; + index_iterator_clear(iter); + return index_iterator_init(iter); } -static int index_iterator__at_end(git_iterator *self) +static int index_iterator_reset_range( + git_iterator *i, const char *start, const char *end) { - index_iterator *ii = (index_iterator *)self; - return (ii->current >= git_vector_length(&ii->entries)); -} - -static int index_iterator__advance( - const git_index_entry **entry, git_iterator *self) -{ - index_iterator *ii = (index_iterator *)self; - size_t entrycount = git_vector_length(&ii->entries); - const git_index_entry *ie; - - 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_vector_get(&ii->entries, 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 (index_iterator__first_prefix_tree(ii) < 0) - return -1; - } - - return index_iterator__current(entry, self); -} - -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_vector_get(&ii->entries, ii->current); - - 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(entry, self); -} - -static int index_iterator__reset(git_iterator *self) -{ - index_iterator *ii = (index_iterator *)self; - const git_index_entry *ie; - - ii->current = 0; - ii->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS; - - iterator_pathlist_walk__reset(self); - - /* if we're given a start prefix, find it; if we're given a pathlist, find - * the first of those. start at the later of the two. - */ - if (ii->base.start) - git_index_snapshot_find( - &ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0); - - if ((ie = index_iterator__advance_over_unwanted(ii)) == NULL) - return 0; - - if (git_buf_sets(&ii->partial, ie->path) < 0) + if (iterator_range_reset(i, start, end) < 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; + return index_iterator_reset(i); } -static int index_iterator__reset_range( - git_iterator *self, const char *start, const char *end) +static int index_iterator_at_end(git_iterator *i) { - if (iterator__reset_range(self, start, end) < 0) - return -1; + index_iterator *iter = (index_iterator *)i; - return index_iterator__reset(self); + return (iter->entry == NULL); } -static void index_iterator__free(git_iterator *self) +static void index_iterator_free(git_iterator *i) { - index_iterator *ii = (index_iterator *)self; - git_index_snapshot_release(&ii->entries, ii->base.index); - ii->base.index = NULL; - git_buf_free(&ii->partial); + index_iterator *iter = (index_iterator *)i; + + git_index_snapshot_release(&iter->entries, iter->base.index); } int git_iterator_for_index( - git_iterator **iter, + git_iterator **out, git_repository *repo, git_index *index, git_iterator_options *options) { - int error = 0; - index_iterator *ii = git__calloc(1, sizeof(index_iterator)); - GITERR_CHECK_ALLOC(ii); + index_iterator *iter; + int error; - if ((error = git_index_snapshot_new(&ii->entries, index)) < 0) { - git__free(ii); - return error; - } - ii->base.index = index; + static git_iterator_callbacks callbacks = { + index_iterator_current, + index_iterator_advance, + index_iterator_advance_into, + index_iterator_advance_over, + index_iterator_reset, + index_iterator_reset_range, + index_iterator_at_end, + index_iterator_free + }; - ITERATOR_BASE_INIT(ii, index, INDEX, repo); + *out = NULL; - if ((error = iterator__update_ignore_case((git_iterator *)ii, options ? options->flags : 0)) < 0) { - git_iterator_free((git_iterator *)ii); - return error; - } + if (index == NULL) + return git_iterator_for_nothing(out, options); - ii->entry_srch = iterator__ignore_case(ii) ? - git_index_entry_isrch : git_index_entry_srch; + iter = git__calloc(1, sizeof(index_iterator)); + GITERR_CHECK_ALLOC(iter); - git_vector_set_cmp(&ii->entries, iterator__ignore_case(ii) ? + iter->base.type = GIT_ITERATOR_TYPE_INDEX; + iter->base.cb = &callbacks; + + if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0 || + (error = git_index_snapshot_new(&iter->entries, index)) < 0 || + (error = index_iterator_init(iter)) < 0) + goto on_error; + + /* TODO: make sure this keeps the entries sort if they were already */ + git_vector_set_cmp(&iter->entries, iterator__ignore_case(&iter->base) ? git_index_entry_icmp : git_index_entry_cmp); - git_vector_sort(&ii->entries); + git_vector_sort(&iter->entries); - git_buf_init(&ii->partial, 0); - ii->tree_entry.mode = GIT_FILEMODE_TREE; - - index_iterator__reset((git_iterator *)ii); - - *iter = (git_iterator *)ii; + *out = &iter->base; return 0; + +on_error: + git_iterator_free(&iter->base); + return error; } diff --git a/tests/iterator/index.c b/tests/iterator/index.c index 5524cdf8a..a48e07b4c 100644 --- a/tests/iterator/index.c +++ b/tests/iterator/index.c @@ -466,8 +466,6 @@ void test_iterator_index__pathlist(void) git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_index *index; git_vector filelist; - int default_icase; - int expect; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "a")); @@ -483,35 +481,251 @@ void test_iterator_index__pathlist(void) cl_git_pass(git_repository_index(&index, g_repo)); - /* In this test we DO NOT force a case setting on the index. */ - default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0); - i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; - /* All iterator tests are "autoexpand with no tree entries" */ + /* Case sensitive */ + { + const char *expected[] = { + "B", "D", "L/1", "a", "c", "e", "k/1", "k/a" }; + size_t expected_len = 8; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - expect_iterator_items(i, 8, NULL, 8, NULL); - git_iterator_free(i); + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; - i_opts.start = "c"; - i_opts.end = NULL; + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - /* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */ - expect = ((default_icase) ? 6 : 4); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); + /* Case INsensitive */ + { + const char *expected[] = { + "a", "B", "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 8; - i_opts.start = NULL; - i_opts.end = "e"; + i_opts.start = NULL; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; - cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); - /* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */ - expect = ((default_icase) ? 5 : 6); - expect_iterator_items(i, expect, NULL, expect, NULL); - git_iterator_free(i); + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case sensitive. */ + { + const char *expected[] = { "c", "e", "k/1", "k/a" }; + size_t expected_len = 4; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set a start, but no end. Case INsensitive. */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" }; + size_t expected_len = 6; + + i_opts.start = "c"; + i_opts.end = NULL; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case sensitive. */ + { + const char *expected[] = { "B", "D", "L/1", "a", "c", "e" }; + size_t expected_len = 6; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Set no start, but an end. Case INsensitive. */ + { + const char *expected[] = { "a", "B", "c", "D", "e" }; + size_t expected_len = 5; + + i_opts.start = NULL; + i_opts.end = "e"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "c", "e", "k/1" }; + size_t expected_len = 3; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case sensitive */ + { + const char *expected[] = { "k/1" }; + size_t expected_len = 1; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "c", "D", "e", "k/1", "k/a" }; + size_t expected_len = 5; + + i_opts.start = "c"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Start and an end, case INsensitive */ + { + const char *expected[] = { "k/1", "k/a" }; + size_t expected_len = 2; + + i_opts.start = "k"; + i_opts.end = "k/D"; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__pathlist_with_dirs(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + /* Test that a prefix `k` matches folders, even without trailing slash */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that a `k/` matches a folder */ + { + const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* When the iterator is case sensitive, ensure we can't lookup the + * directory with the wrong case. + */ + { + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + } + + /* Test that case insensitive matching works. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Test that case insensitive matching works without trailing slash. */ + { + const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "K")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } git_index_free(index); git_vector_free(&filelist); @@ -729,3 +943,410 @@ void test_iterator_index__pathlist_with_directory(void) git_vector_free(&filelist); } +static void create_paths(git_index *index, const char *root, int depth) +{ + git_buf fullpath = GIT_BUF_INIT; + git_index_entry entry; + size_t root_len; + int i; + + if (root) { + cl_git_pass(git_buf_puts(&fullpath, root)); + cl_git_pass(git_buf_putc(&fullpath, '/')); + } + + root_len = fullpath.size; + + for (i = 0; i < 8; i++) { + bool file = (depth == 0 || (i % 2) == 0); + git_buf_truncate(&fullpath, root_len); + cl_git_pass(git_buf_printf(&fullpath, "item%d", i)); + + if (file) { + memset(&entry, 0, sizeof(git_index_entry)); + entry.path = fullpath.ptr; + entry.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&entry.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6"); + + cl_git_pass(git_index_add(index, &entry)); + } else if (depth > 0) { + create_paths(index, fullpath.ptr, (depth - 1)); + } + } + + git_buf_free(&fullpath); +} + +void test_iterator_index__pathlist_for_deeply_nested_item(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + cl_git_pass(git_repository_index(&index, g_repo)); + + create_paths(index, NULL, 3); + + /* Ensure that we find the single path we're interested in */ + { + const char *expected[] = { "item1/item3/item5/item7" }; + size_t expected_len = 1; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + { + const char *expected[] = { + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + }; + size_t expected_len = 8; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + { + const char *expected[] = { + "item1/item3/item0", + "item1/item3/item1/item0", "item1/item3/item1/item1", + "item1/item3/item1/item2", "item1/item3/item1/item3", + "item1/item3/item1/item4", "item1/item3/item1/item5", + "item1/item3/item1/item6", "item1/item3/item1/item7", + "item1/item3/item2", + "item1/item3/item3/item0", "item1/item3/item3/item1", + "item1/item3/item3/item2", "item1/item3/item3/item3", + "item1/item3/item3/item4", "item1/item3/item3/item5", + "item1/item3/item3/item6", "item1/item3/item3/item7", + "item1/item3/item4", + "item1/item3/item5/item0", "item1/item3/item5/item1", + "item1/item3/item5/item2", "item1/item3/item5/item3", + "item1/item3/item5/item4", "item1/item3/item5/item5", + "item1/item3/item5/item6", "item1/item3/item5/item7", + "item1/item3/item6", + "item1/item3/item7/item0", "item1/item3/item7/item1", + "item1/item3/item7/item2", "item1/item3/item7/item3", + "item1/item3/item7/item4", "item1/item3/item7/item5", + "item1/item3/item7/item6", "item1/item3/item7/item7", + }; + size_t expected_len = 36; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item1/item3/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + /* Ensure that we find the single path we're interested in, and we find + * it efficiently, and don't stat the entire world to get there. + */ + { + const char *expected[] = { + "item0", "item1/item2", "item5/item7/item4", "item6", + "item7/item3/item1/item6" }; + size_t expected_len = 5; + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6")); + cl_git_pass(git_vector_insert(&filelist, "item6")); + cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4")); + cl_git_pass(git_vector_insert(&filelist, "item1/item2")); + cl_git_pass(git_vector_insert(&filelist, "item0")); + + /* also add some things that don't exist or don't match the right type */ + cl_git_pass(git_vector_insert(&filelist, "item2/")); + cl_git_pass(git_vector_insert(&filelist, "itemN")); + cl_git_pass(git_vector_insert(&filelist, "item1/itemA")); + cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + } + + git_index_free(index); + git_vector_free(&filelist); +} + +void test_iterator_index__advance_over(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + cl_git_pass(git_repository_index(&index, g_repo)); + + create_paths(index, NULL, 1); + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + + expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item0", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item1/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item2", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + git_index_free(index); +} + +void test_iterator_index__advance_into(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + + g_repo = cl_git_sandbox_init("icase"); + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_git_pass(git_repository_index(&index, g_repo)); + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_advance_into(i, "B"); + expect_advance_into(i, "D"); + expect_advance_into(i, "F"); + expect_advance_into(i, "H"); + expect_advance_into(i, "J"); + expect_advance_into(i, "L/"); + expect_advance_into(i, "L/1"); + expect_advance_into(i, "L/B"); + expect_advance_into(i, "L/D"); + expect_advance_into(i, "L/a"); + expect_advance_into(i, "L/c"); + expect_advance_into(i, "a"); + expect_advance_into(i, "c"); + expect_advance_into(i, "e"); + expect_advance_into(i, "g"); + expect_advance_into(i, "i"); + expect_advance_into(i, "k/"); + expect_advance_into(i, "k/1"); + expect_advance_into(i, "k/B"); + expect_advance_into(i, "k/D"); + expect_advance_into(i, "k/a"); + expect_advance_into(i, "k/c"); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + git_index_free(index); +} + +void test_iterator_index__advance_into_and_over(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + + g_repo = cl_git_sandbox_init("icase"); + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + cl_git_pass(git_repository_index(&index, g_repo)); + + create_paths(index, NULL, 2); + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_advance_into(i, "B"); + expect_advance_into(i, "D"); + expect_advance_into(i, "F"); + expect_advance_into(i, "H"); + expect_advance_into(i, "J"); + expect_advance_into(i, "L/"); + expect_advance_into(i, "L/1"); + expect_advance_into(i, "L/B"); + expect_advance_into(i, "L/D"); + expect_advance_into(i, "L/a"); + expect_advance_into(i, "L/c"); + expect_advance_into(i, "a"); + expect_advance_into(i, "c"); + expect_advance_into(i, "e"); + expect_advance_into(i, "g"); + expect_advance_into(i, "i"); + expect_advance_into(i, "item0"); + expect_advance_into(i, "item1/"); + expect_advance_into(i, "item1/item0"); + expect_advance_into(i, "item1/item1/"); + expect_advance_into(i, "item1/item1/item0"); + expect_advance_into(i, "item1/item1/item1"); + expect_advance_into(i, "item1/item1/item2"); + expect_advance_into(i, "item1/item1/item3"); + expect_advance_into(i, "item1/item1/item4"); + expect_advance_into(i, "item1/item1/item5"); + expect_advance_into(i, "item1/item1/item6"); + expect_advance_into(i, "item1/item1/item7"); + expect_advance_into(i, "item1/item2"); + expect_advance_over(i, "item1/item3/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item1/item4", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item1/item5/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item1/item6", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item1/item7/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_into(i, "item2"); + expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_into(i, "k/"); + expect_advance_into(i, "k/1"); + expect_advance_into(i, "k/B"); + expect_advance_into(i, "k/D"); + expect_advance_into(i, "k/a"); + expect_advance_into(i, "k/c"); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + git_index_free(index); +} + +static void add_conflict( + git_index *index, + const char *ancestor_path, + const char *our_path, + const char *their_path) +{ + git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}}; + + ancestor.path = ancestor_path; + ancestor.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&ancestor.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6"); + GIT_IDXENTRY_STAGE_SET(&ancestor, 1); + + ours.path = our_path; + ours.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&ours.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6"); + GIT_IDXENTRY_STAGE_SET(&ours, 2); + + theirs.path = their_path; + theirs.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&theirs.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6"); + GIT_IDXENTRY_STAGE_SET(&theirs, 3); + + cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs)); +} + +void test_iterator_index__include_conflicts(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | + GIT_ITERATOR_DONT_AUTOEXPAND; + + g_repo = cl_git_sandbox_init("icase"); + cl_git_pass(git_repository_index(&index, g_repo)); + + add_conflict(index, "CONFLICT1", "CONFLICT1" ,"CONFLICT1"); + add_conflict(index, "ZZZ-CONFLICT2.ancestor", "ZZZ-CONFLICT2.ours", "ZZZ-CONFLICT2.theirs"); + add_conflict(index, "ancestor.conflict3", "ours.conflict3", "theirs.conflict3"); + add_conflict(index, "zzz-conflict4", "zzz-conflict4", "zzz-conflict4"); + + /* Iterate the index, ensuring that conflicts are not included */ + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + + expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + + /* Try again, returning conflicts */ + i_opts.flags |= GIT_ITERATOR_INCLUDE_CONFLICTS; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + + expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "ZZZ-CONFLICT2.ancestor", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "ZZZ-CONFLICT2.ours", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "ZZZ-CONFLICT2.theirs", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "ancestor.conflict3", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "ours.conflict3", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "theirs.conflict3", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL); + expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL); + + cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i)); + git_iterator_free(i); + + git_index_free(index); +} diff --git a/tests/iterator/iterator_helpers.c b/tests/iterator/iterator_helpers.c index c04969f63..a3e803299 100644 --- a/tests/iterator/iterator_helpers.c +++ b/tests/iterator/iterator_helpers.c @@ -108,3 +108,39 @@ void expect_iterator_items( cl_assert_equal_i(expected_total, count); } + +void expect_advance_over( + git_iterator *i, + const char *expected_path, + git_iterator_status_t expected_status) +{ + const git_index_entry *entry; + git_iterator_status_t status; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + error = git_iterator_advance_over(&entry, &status, i); + cl_assert(!error || error == GIT_ITEROVER); + cl_assert_equal_i(expected_status, status); +} + +void expect_advance_into( + git_iterator *i, + const char *expected_path) +{ + const git_index_entry *entry; + int error; + + cl_git_pass(git_iterator_current(&entry, i)); + cl_assert_equal_s(expected_path, entry->path); + + if (S_ISDIR(entry->mode)) + error = git_iterator_advance_into(&entry, i); + else + error = git_iterator_advance(&entry, i); + + cl_assert(!error || error == GIT_ITEROVER); +} + diff --git a/tests/iterator/iterator_helpers.h b/tests/iterator/iterator_helpers.h index d92086e4a..8d0a17014 100644 --- a/tests/iterator/iterator_helpers.h +++ b/tests/iterator/iterator_helpers.h @@ -6,3 +6,11 @@ extern void expect_iterator_items( int expected_total, const char **expected_total_paths); +extern void expect_advance_over( + git_iterator *i, + const char *expected_path, + git_iterator_status_t expected_status); + +void expect_advance_into( + git_iterator *i, + const char *expected_path); diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c index 4daa32330..0dd4599a3 100644 --- a/tests/iterator/workdir.c +++ b/tests/iterator/workdir.c @@ -1243,23 +1243,6 @@ void test_iterator_workdir__bounded_submodules(void) git_tree_free(head); } -static void expect_advance_over( - git_iterator *i, - const char *expected_path, - git_iterator_status_t expected_status) -{ - const git_index_entry *entry; - git_iterator_status_t status; - int error; - - cl_git_pass(git_iterator_current(&entry, i)); - cl_assert_equal_s(expected_path, entry->path); - - error = git_iterator_advance_over(&entry, &status, i); - cl_assert(!error || error == GIT_ITEROVER); - cl_assert_equal_i(expected_status, status); -} - void test_iterator_workdir__advance_over(void) { git_iterator *i; @@ -1380,24 +1363,6 @@ void test_iterator_workdir__advance_over_with_pathlist(void) git_vector_free(&pathlist); } -static void expect_advance_into( - git_iterator *i, - const char *expected_path) -{ - const git_index_entry *entry; - int error; - - cl_git_pass(git_iterator_current(&entry, i)); - cl_assert_equal_s(expected_path, entry->path); - - if (S_ISDIR(entry->mode)) - error = git_iterator_advance_into(&entry, i); - else - error = git_iterator_advance(&entry, i); - - cl_assert(!error || error == GIT_ITEROVER); -} - void test_iterator_workdir__advance_into(void) { git_iterator *i; From 247e3b4305f317bede88a225788239df57a8aa6d Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 21 Mar 2016 16:51:45 -0400 Subject: [PATCH 27/35] iterator: mandate `advance_over` Since the three iterators implement `advance_over` differently, mandate it and implement each. --- src/iterator.c | 11 ++++++++++- src/iterator.h | 6 +----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index 720a3d17a..cf3e29b71 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -1070,6 +1070,15 @@ static int tree_iterator_advance_into( return tree_iterator_advance(out, i); } +static int tree_iterator_advance_over( + const git_index_entry **out, + git_iterator_status_t *status, + git_iterator *i) +{ + *status = GIT_ITERATOR_STATUS_NORMAL; + return git_iterator_advance(out, i); +} + static void tree_iterator_clear(tree_iterator *iter) { while (iter->frames.size) @@ -1143,7 +1152,7 @@ int git_iterator_for_tree( tree_iterator_current, tree_iterator_advance, tree_iterator_advance_into, - NULL, /* advance_over */ + tree_iterator_advance_over, tree_iterator_reset, tree_iterator_reset_range, tree_iterator_at_end, diff --git a/src/iterator.h b/src/iterator.h index 460f9475a..51ba3f777 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -217,11 +217,7 @@ GIT_INLINE(int) git_iterator_advance_over( git_iterator_status_t *status, git_iterator *iter) { - if (iter->cb->advance_over) - return iter->cb->advance_over(entry, status, iter); - - *status = GIT_ITERATOR_STATUS_NORMAL; - return git_iterator_advance(entry, iter); + return iter->cb->advance_over(entry, status, iter); } /** From 35877463fd5d91a75e97a0857ce6df669606e7c7 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 21 Mar 2016 17:03:00 -0400 Subject: [PATCH 28/35] iterator: refactor empty iterator to new style --- src/iterator.c | 51 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index cf3e29b71..37751446a 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -572,33 +572,44 @@ static iterator_pathlist_search_t iterator_pathlist_search( /* Empty iterator */ -static int empty_iterator__noop(const git_index_entry **e, git_iterator *i) +static int empty_iterator_noop(const git_index_entry **e, git_iterator *i) { GIT_UNUSED(i); iterator__clear_entry(e); return GIT_ITEROVER; } -static int empty_iterator__reset(git_iterator *i) +static int empty_iterator_advance_over( + const git_index_entry **e, + git_iterator_status_t *s, + git_iterator *i) +{ + GIT_UNUSED(i); + *s = GIT_ITERATOR_STATUS_EMPTY; + iterator__clear_entry(e); + return GIT_ITEROVER; +} + +static int empty_iterator_reset(git_iterator *i) { GIT_UNUSED(i); return 0; } -static int empty_iterator__reset_range( +static int empty_iterator_reset_range( git_iterator *i, const char *s, const char *e) { GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e); return 0; } -static int empty_iterator__at_end(git_iterator *i) +static int empty_iterator_at_end(git_iterator *i) { GIT_UNUSED(i); return 1; } -static void empty_iterator__free(git_iterator *i) +static void empty_iterator_free(git_iterator *i) { GIT_UNUSED(i); } @@ -609,22 +620,32 @@ typedef struct { } empty_iterator; int git_iterator_for_nothing( - git_iterator **iter, + git_iterator **out, git_iterator_options *options) { - empty_iterator *i = git__calloc(1, sizeof(empty_iterator)); - GITERR_CHECK_ALLOC(i); + empty_iterator *iter; -#define empty_iterator__current empty_iterator__noop -#define empty_iterator__advance empty_iterator__noop -#define empty_iterator__advance_into empty_iterator__noop + static git_iterator_callbacks callbacks = { + empty_iterator_noop, + empty_iterator_noop, + empty_iterator_noop, + empty_iterator_advance_over, + empty_iterator_reset, + empty_iterator_reset_range, + empty_iterator_at_end, + empty_iterator_free + }; - ITERATOR_BASE_INIT(i, empty, EMPTY, NULL); + *out = NULL; - if (options && (options->flags & GIT_ITERATOR_IGNORE_CASE) != 0) - i->base.flags |= GIT_ITERATOR_IGNORE_CASE; + iter = git__calloc(1, sizeof(empty_iterator)); + GITERR_CHECK_ALLOC(iter); - *iter = (git_iterator *)i; + iter->base.type = GIT_ITERATOR_TYPE_EMPTY; + iter->base.cb = &callbacks; + iter->base.flags = options->flags; + + *out = &iter->base; return 0; } From d712c2b27f5589364dd0b602a3abc1dff31d54a0 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 21 Mar 2016 18:30:21 -0400 Subject: [PATCH 29/35] iterator: don't run the gunk test by default on CI (It's slow!) --- tests/iterator/workdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c index 0dd4599a3..2df8ef53e 100644 --- a/tests/iterator/workdir.c +++ b/tests/iterator/workdir.c @@ -641,7 +641,7 @@ void test_iterator_workdir__filesystem_gunk(void) git_buf parent = GIT_BUF_INIT; int n; - if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) + if (!cl_is_env_set("GITTEST_INVASIVE_SPEED")) cl_skip(); g_repo = cl_git_sandbox_init("testrepo"); From 9eb9e5fa87667b823f73265c88a87f314d47aaf7 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 21 Mar 2016 17:19:24 -0400 Subject: [PATCH 30/35] iterator: cleanups Remove some unused functions, refactor some ugliness. --- src/diff.c | 5 +- src/iterator.c | 393 ++++++------------------------------------------- src/iterator.h | 22 +-- 3 files changed, 54 insertions(+), 366 deletions(-) diff --git a/src/diff.c b/src/diff.c index 5b70998f4..64641daab 100644 --- a/src/diff.c +++ b/src/diff.c @@ -1229,9 +1229,8 @@ int git_diff__from_iterators( /* make iterators have matching icase behavior */ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) { - if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 || - (error = git_iterator_set_ignore_case(new_iter, true)) < 0) - goto cleanup; + git_iterator_set_ignore_case(old_iter, true); + git_iterator_set_ignore_case(new_iter, true); } /* finish initialization */ diff --git a/src/iterator.c b/src/iterator.c index 37751446a..c78676ec6 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -13,42 +13,6 @@ #include "submodule.h" #include -#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.reset = NAME_LC ## _iterator__reset; \ - (P)->cb.reset_range = NAME_LC ## _iterator__reset_range; \ - (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 = options && options->start ? \ - git__strdup(options->start) : NULL; \ - (P)->base.end = options && options->end ? \ - git__strdup(options->end) : NULL; \ - if ((options && options->start && !(P)->base.start) || \ - (options && options->end && !(P)->base.end)) { \ - git__free(P); return -1; } \ - (P)->base.strcomp = git__strcmp; \ - (P)->base.strncomp = git__strncmp; \ - (P)->base.prefixcomp = git__prefixcmp; \ - (P)->base.flags = options ? options->flags & ~ITERATOR_CASE_FLAGS : 0; \ - if ((P)->base.flags & GIT_ITERATOR_DONT_AUTOEXPAND) \ - (P)->base.flags |= GIT_ITERATOR_INCLUDE_TREES; \ - if (options && options->pathlist.count && \ - iterator_pathlist__init(&P->base, &options->pathlist) < 0) { \ - git__free(P); return -1; } \ - } while (0) - #define GIT_ITERATOR_FIRST_ACCESS (1 << 15) #define GIT_ITERATOR_HONOR_IGNORES (1 << 16) #define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17) @@ -63,220 +27,22 @@ #define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES) #define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT) -#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) - -typedef enum { - ITERATOR_PATHLIST_NONE = 0, - ITERATOR_PATHLIST_MATCH = 1, - ITERATOR_PATHLIST_MATCH_DIRECTORY = 2, - ITERATOR_PATHLIST_MATCH_CHILD = 3, -} iterator_pathlist__match_t; - -static int iterator_pathlist__init(git_iterator *iter, git_strarray *pathspec) +static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case) { - size_t i; + if (ignore_case) + iter->flags |= GIT_ITERATOR_IGNORE_CASE; + else + iter->flags &= ~GIT_ITERATOR_IGNORE_CASE; - if (git_vector_init(&iter->pathlist, pathspec->count, - (git_vector_cmp)iter->strcomp) < 0) - return -1; + iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; + iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; + iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; + iter->entry_srch = ignore_case ? git_index_entry_srch : git_index_entry_isrch; - for (i = 0; i < pathspec->count; i++) { - if (!pathspec->strings[i]) - continue; - - if (git_vector_insert(&iter->pathlist, pathspec->strings[i]) < 0) - return -1; - } - - git_vector_sort(&iter->pathlist); - - return 0; -} - -static iterator_pathlist__match_t iterator_pathlist__match( - git_iterator *iter, const char *path, size_t path_len) -{ - const char *p; - size_t idx; - int error; - - error = git_vector_bsearch2(&idx, &iter->pathlist, - (git_vector_cmp)iter->strcomp, path); - - if (error == 0) - return ITERATOR_PATHLIST_MATCH; - - /* at this point, the path we're examining may be a directory (though we - * don't know that yet, since we're avoiding a stat unless it's necessary) - * so see if the pathlist contains a file beneath this directory. - */ - while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) { - if (iter->prefixcomp(p, path) != 0) - break; - - /* an exact match would have been matched by the bsearch above */ - assert(p[path_len]); - - /* is this a literal directory entry (eg `foo/`) or a file beneath */ - if (p[path_len] == '/') { - return (p[path_len+1] == '\0') ? - ITERATOR_PATHLIST_MATCH_DIRECTORY : - ITERATOR_PATHLIST_MATCH_CHILD; - } - - if (p[path_len] > '/') - break; - - idx++; - } - - return ITERATOR_PATHLIST_NONE; -} - -static void iterator_pathlist_walk__reset(git_iterator *iter) -{ - iter->pathlist_walk_idx = 0; -} - -/* walker for the index iterator that allows it to walk the sorted pathlist - * entries alongside the sorted index entries. the `iter->pathlist_walk_idx` - * stores the starting position for subsequent calls, the position is advanced - * along with the index iterator, with a special case for handling directories - * in the pathlist that are specified without trailing '/'. (eg, `foo`). - * we do not advance over these entries until we're certain that the index - * iterator will not ask us for a file beneath that directory (eg, `foo/bar`). - */ -static bool iterator_pathlist_walk__contains(git_iterator *iter, const char *path) -{ - size_t i; - char *p; - size_t p_len; - int cmp; - - for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) { - p = iter->pathlist.contents[i]; - p_len = strlen(p); - - /* see if the pathlist entry is a prefix of this path */ - cmp = iter->strncomp(p, path, p_len); - - /* this pathlist entry sorts before the given path, try the next */ - if (!p_len || cmp < 0) - iter->pathlist_walk_idx++; - - /* this pathlist sorts after the given path, no match. */ - else if (cmp > 0) - return false; - - /* match! an exact match (`foo` vs `foo`), the path is a child of an - * explicit directory in the pathlist (`foo/` vs `foo/bar`) or the path - * is a child of an entry in the pathlist (`foo` vs `foo/bar`) - */ - else if (path[p_len] == '\0' || p[p_len - 1] == '/' || path[p_len] == '/') - return true; - - /* only advance the start index for future callers if we know that we - * will not see a child of this path. eg, a pathlist entry `foo` is - * a prefix for `foo.txt` and `foo/bar`. don't advance the start - * pathlist index when we see `foo.txt` or we would miss a subsequent - * inspection of `foo/bar`. only advance when there are no more - * potential children. - */ - else if (path[p_len] > '/') - iter->pathlist_walk_idx++; - } - - return false; -} - -static void iterator_pathlist__update_ignore_case(git_iterator *iter) -{ git_vector_set_cmp(&iter->pathlist, (git_vector_cmp)iter->strcomp); - git_vector_sort(&iter->pathlist); - - iter->pathlist_walk_idx = 0; } - -static int iterator__reset_range( - git_iterator *iter, const char *start, const char *end) -{ - if (iter->start) - git__free(iter->start); - - if (start) { - iter->start = git__strdup(start); - GITERR_CHECK_ALLOC(iter->start); - } - - if (iter->end) - git__free(iter->end); - - if (end) { - iter->end = git__strdup(end); - GITERR_CHECK_ALLOC(iter->end); - } - - iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS; - - return 0; -} - -int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case) -{ - if (ignore_case) { - iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE); - - iter->strcomp = git__strcasecmp; - iter->strncomp = git__strncasecmp; - iter->prefixcomp = git__prefixcmp_icase; - iter->entry_srch = git_index_entry_isrch; - } else { - iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE); - - iter->strcomp = git__strcmp; - iter->strncomp = git__strncmp; - iter->prefixcomp = git__prefixcmp; - iter->entry_srch = git_index_entry_srch; - } - - iterator_pathlist__update_ignore_case(iter); - - return 0; -} - -static int iterator__update_ignore_case( - git_iterator *iter, - git_iterator_flag_t flags) -{ - bool ignore_case; - int error; - - 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)) < 0) - return error; - - ignore_case = (index->ignore_case == 1); - } - - return git_iterator_set_ignore_case(iter, ignore_case); -} - -GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry) -{ - if (entry) *entry = NULL; -} - - static int iterator_range_init( git_iterator *iter, const char *start, const char *end) { @@ -315,7 +81,7 @@ static void iterator_range_free(git_iterator *iter) } } -static int iterator_range_reset( +static int iterator_reset_range( git_iterator *iter, const char *start, const char *end) { iterator_range_free(iter); @@ -326,8 +92,7 @@ static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist) { size_t i; - if (git_vector_init(&iter->pathlist, pathlist->count, - (git_vector_cmp)iter->strcomp) < 0) + if (git_vector_init(&iter->pathlist, pathlist->count, NULL) < 0) return -1; for (i = 0; i < pathlist->count; i++) { @@ -338,7 +103,6 @@ static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist) return -1; } - git_vector_sort(&iter->pathlist); return 0; } @@ -392,15 +156,11 @@ static int iterator_init_common( if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND)) iter->flags |= GIT_ITERATOR_INCLUDE_TREES; - iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; - iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; - iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; - iter->entry_srch = ignore_case ? git_index_entry_srch : git_index_entry_isrch; - if ((error = iterator_range_init(iter, options->start, options->end)) < 0 || (error = iterator_pathlist_init(iter, &options->pathlist)) < 0) return error; + iterator_set_ignore_case(iter, ignore_case); return 0; } @@ -460,6 +220,8 @@ static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) if (iter->pathlist.length == 0) return true; + git_vector_sort(&iter->pathlist); + path_len = strlen(path); /* for comparison, drop the trailing slash on the current '/' */ @@ -515,7 +277,7 @@ static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) } typedef enum { - ITERATOR_PATHLIST_NOT_FOUND = 0, + ITERATOR_PATHLIST_NONE = 0, ITERATOR_PATHLIST_IS_FILE = 1, ITERATOR_PATHLIST_IS_DIR = 2, ITERATOR_PATHLIST_IS_PARENT = 3, @@ -529,6 +291,11 @@ static iterator_pathlist_search_t iterator_pathlist_search( size_t idx; int error; + if (iter->pathlist.length == 0) + return ITERATOR_PATHLIST_FULL; + + git_vector_sort(&iter->pathlist); + error = git_vector_bsearch2(&idx, &iter->pathlist, (git_vector_cmp)iter->strcomp, path); @@ -567,7 +334,7 @@ static iterator_pathlist_search_t iterator_pathlist_search( idx++; } - return ITERATOR_PATHLIST_NOT_FOUND; + return ITERATOR_PATHLIST_NONE; } /* Empty iterator */ @@ -575,7 +342,10 @@ static iterator_pathlist_search_t iterator_pathlist_search( static int empty_iterator_noop(const git_index_entry **e, git_iterator *i) { GIT_UNUSED(i); - iterator__clear_entry(e); + + if (e) + *e = NULL; + return GIT_ITEROVER; } @@ -584,10 +354,8 @@ static int empty_iterator_advance_over( git_iterator_status_t *s, git_iterator *i) { - GIT_UNUSED(i); *s = GIT_ITERATOR_STATUS_EMPTY; - iterator__clear_entry(e); - return GIT_ITEROVER; + return empty_iterator_noop(e, i); } static int empty_iterator_reset(git_iterator *i) @@ -596,19 +364,6 @@ static int empty_iterator_reset(git_iterator *i) return 0; } -static int empty_iterator_reset_range( - git_iterator *i, const char *s, const char *e) -{ - GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e); - return 0; -} - -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); @@ -631,8 +386,6 @@ int git_iterator_for_nothing( empty_iterator_noop, empty_iterator_advance_over, empty_iterator_reset, - empty_iterator_reset_range, - empty_iterator_at_end, empty_iterator_free }; @@ -1135,22 +888,6 @@ static int tree_iterator_reset(git_iterator *i) return tree_iterator_init(iter); } -static int tree_iterator_reset_range( - git_iterator *i, const char *start, const char *end) -{ - if (iterator_range_reset(i, start, end) < 0) - return -1; - - return tree_iterator_reset(i); -} - -static int tree_iterator_at_end(git_iterator *i) -{ - tree_iterator *iter = (tree_iterator *)i; - - return (iter->frames.size == 0); -} - static void tree_iterator_free(git_iterator *i) { tree_iterator *iter = (tree_iterator *)i; @@ -1175,8 +912,6 @@ int git_iterator_for_tree( tree_iterator_advance_into, tree_iterator_advance_over, tree_iterator_reset, - tree_iterator_reset_range, - tree_iterator_at_end, tree_iterator_free }; @@ -1326,7 +1061,7 @@ static int filesystem_iterator_entry_cmp_icase(const void *_a, const void *_b) * We consider it a submodule if the path is listed as a submodule in * either the tree or the index. */ -static int is_submodule( +static int filesystem_iterator_is_submodule( bool *out, filesystem_iterator *iter, const char *path, size_t path_len) { bool is_submodule = false; @@ -1368,18 +1103,6 @@ static int is_submodule( return 0; } -GIT_INLINE(git_dir_flag) filesystem_iterator_dir_flag(git_index_entry *entry) -{ -#if defined(GIT_WIN32) && !defined(__MINGW32__) - return (entry && entry->mode) ? - (S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) : - GIT_DIR_FLAG_UNKNOWN; -#else - GIT_UNUSED(entry); - return GIT_DIR_FLAG_UNKNOWN; -#endif -} - static void filesystem_iterator_frame_push_ignores( filesystem_iterator *iter, filesystem_iterator_entry *frame_entry, @@ -1433,7 +1156,7 @@ GIT_INLINE(bool) filesystem_iterator_examine_path( iterator_pathlist_search_t match = ITERATOR_PATHLIST_FULL; *is_dir_out = false; - *match_out = ITERATOR_PATHLIST_NOT_FOUND; + *match_out = ITERATOR_PATHLIST_NONE; if (iter->base.start_len) { int cmp = iter->base.strncomp(path, iter->base.start, path_len); @@ -1471,7 +1194,7 @@ GIT_INLINE(bool) filesystem_iterator_examine_path( else match = iterator_pathlist_search(&iter->base, path, path_len); - if (match == ITERATOR_PATHLIST_NOT_FOUND) + if (match == ITERATOR_PATHLIST_NONE) return false; /* Ensure that the pathlist entry lines up with what we expected */ @@ -1649,7 +1372,8 @@ static int filesystem_iterator_frame_push( if (S_ISDIR(statbuf.st_mode)) { bool submodule = false; - if ((error = is_submodule(&submodule, iter, path, path_len)) < 0) + if ((error = filesystem_iterator_is_submodule(&submodule, + iter, path, path_len)) < 0) goto done; if (submodule) @@ -2037,22 +1761,6 @@ static int filesystem_iterator_reset(git_iterator *i) return filesystem_iterator_init(iter); } -static int filesystem_iterator_reset_range( - git_iterator *i, const char *start, const char *end) -{ - if (iterator_range_reset(i, start, end) < 0) - return -1; - - return filesystem_iterator_reset(i); -} - -static int filesystem_iterator_at_end(git_iterator *i) -{ - filesystem_iterator *iter = (filesystem_iterator *)i; - - return (iter->frames.size == 0); -} - static void filesystem_iterator_free(git_iterator *i) { filesystem_iterator *iter = (filesystem_iterator *)i; @@ -2078,8 +1786,6 @@ static int iterator_for_filesystem( filesystem_iterator_advance_into, filesystem_iterator_advance_over, filesystem_iterator_reset, - filesystem_iterator_reset_range, - filesystem_iterator_at_end, filesystem_iterator_free }; @@ -2336,7 +2042,9 @@ static int index_iterator_advance_into( index_iterator *iter = (index_iterator *)i; if (! S_ISDIR(iter->tree_entry.mode)) { - *out = NULL; + if (out) + *out = NULL; + return 0; } @@ -2384,22 +2092,6 @@ static int index_iterator_reset(git_iterator *i) return index_iterator_init(iter); } -static int index_iterator_reset_range( - git_iterator *i, const char *start, const char *end) -{ - if (iterator_range_reset(i, start, end) < 0) - return -1; - - return index_iterator_reset(i); -} - -static int index_iterator_at_end(git_iterator *i) -{ - index_iterator *iter = (index_iterator *)i; - - return (iter->entry == NULL); -} - static void index_iterator_free(git_iterator *i) { index_iterator *iter = (index_iterator *)i; @@ -2422,8 +2114,6 @@ int git_iterator_for_index( index_iterator_advance_into, index_iterator_advance_over, index_iterator_reset, - index_iterator_reset_range, - index_iterator_at_end, index_iterator_free }; @@ -2443,7 +2133,6 @@ int git_iterator_for_index( (error = index_iterator_init(iter)) < 0) goto on_error; - /* TODO: make sure this keeps the entries sort if they were already */ git_vector_set_cmp(&iter->entries, iterator__ignore_case(&iter->base) ? git_index_entry_icmp : git_index_entry_cmp); git_vector_sort(&iter->entries); @@ -2459,6 +2148,20 @@ on_error: /* Iterator API */ +int git_iterator_reset_range( + git_iterator *i, const char *start, const char *end) +{ + if (iterator_reset_range(i, start, end) < 0) + return -1; + + return i->cb->reset(i); +} + +void git_iterator_set_ignore_case(git_iterator *i, bool ignore_case) +{ + assert(!iterator__has_been_accessed(i)); + iterator_set_ignore_case(i, ignore_case); +} void git_iterator_free(git_iterator *iter) { diff --git a/src/iterator.h b/src/iterator.h index 51ba3f777..0b239a5bd 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -69,8 +69,6 @@ typedef struct { int (*advance_over)( const git_index_entry **, git_iterator_status_t *, git_iterator *); int (*reset)(git_iterator *); - int (*reset_range)(git_iterator *, const char *start, const char *end); - int (*at_end)(git_iterator *); void (*free)(git_iterator *); } git_iterator_callbacks; @@ -232,21 +230,8 @@ GIT_INLINE(int) git_iterator_reset(git_iterator *iter) * Go back to the start of the iteration after updating the `start` and * `end` pathname boundaries of the iteration. */ -GIT_INLINE(int) git_iterator_reset_range( - git_iterator *iter, const char *start, const char *end) -{ - return iter->cb->reset_range(iter, start, end); -} - -/** - * Check if the iterator is at the end - * - * @return 0 if not at end, >0 if at end - */ -GIT_INLINE(int) git_iterator_at_end(git_iterator *iter) -{ - return iter->cb->at_end(iter); -} +extern int git_iterator_reset_range( + git_iterator *iter, const char *start, const char *end); GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter) { @@ -273,7 +258,8 @@ GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter) return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0); } -extern int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case); +extern void git_iterator_set_ignore_case( + git_iterator *iter, bool ignore_case); extern int git_iterator_current_tree_entry( const git_tree_entry **entry_out, git_iterator *iter); From 8152a748211f7a9c1a4ee8d28746ca20deb8928e Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Tue, 22 Mar 2016 10:27:50 +0100 Subject: [PATCH 31/35] iterator: more pathlist-related tests should test actual paths --- tests/iterator/tree.c | 22 +++++++++++++++++----- tests/iterator/workdir.c | 7 ++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/tests/iterator/tree.c b/tests/iterator/tree.c index 8e1130aab..b4d0f40f3 100644 --- a/tests/iterator/tree.c +++ b/tests/iterator/tree.c @@ -979,6 +979,13 @@ void test_iterator_tree__pathlist_with_directory(void) git_vector filelist; git_tree *tree; + const char *expected[] = { "subdir/README", "subdir/new.txt", + "subdir/subdir2/README", "subdir/subdir2/new.txt" }; + size_t expected_len = 4; + + const char *expected2[] = { "subdir/subdir2/README", "subdir/subdir2/new.txt" }; + size_t expected_len2 = 2; + g_repo = cl_git_sandbox_init("testrepo2"); git_repository_head_tree(&tree, g_repo); @@ -987,9 +994,10 @@ void test_iterator_tree__pathlist_with_directory(void) i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); + expect_iterator_items(i, expected_len, expected, expected_len, expected); git_iterator_free(i); git_vector_clear(&filelist); @@ -999,7 +1007,7 @@ void test_iterator_tree__pathlist_with_directory(void) i_opts.pathlist.count = filelist.length; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); + expect_iterator_items(i, expected_len, expected, expected_len, expected); git_iterator_free(i); git_vector_clear(&filelist); @@ -1009,7 +1017,7 @@ void test_iterator_tree__pathlist_with_directory(void) i_opts.pathlist.count = filelist.length; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 2, NULL, 2, NULL); + expect_iterator_items(i, expected_len2, expected2, expected_len2, expected2); git_iterator_free(i); git_vector_free(&filelist); @@ -1022,18 +1030,22 @@ void test_iterator_tree__pathlist_with_directory_include_tree_nodes(void) git_vector filelist; git_tree *tree; + const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt", + "subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt" }; + size_t expected_len = 6; + g_repo = cl_git_sandbox_init("testrepo2"); git_repository_head_tree(&tree, g_repo); cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "subdir")); - i_opts.flags |= GIT_ITERATOR_INCLUDE_TREES; i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts)); - expect_iterator_items(i, 6, NULL, 6, NULL); + expect_iterator_items(i, expected_len, expected, expected_len, expected); git_iterator_free(i); git_vector_free(&filelist); diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c index 2df8ef53e..389d8a1b6 100644 --- a/tests/iterator/workdir.c +++ b/tests/iterator/workdir.c @@ -1410,6 +1410,10 @@ void test_iterator_workdir__pathlist_with_directory(void) git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_vector filelist; + const char *expected[] = { "subdir/README", "subdir/new.txt", + "subdir/subdir2/README", "subdir/subdir2/new.txt" }; + size_t expected_len = 4; + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); cl_git_pass(git_vector_insert(&filelist, "subdir/")); @@ -1417,9 +1421,10 @@ void test_iterator_workdir__pathlist_with_directory(void) i_opts.pathlist.strings = (char **)filelist.contents; i_opts.pathlist.count = filelist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE; cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); - expect_iterator_items(i, 4, NULL, 4, NULL); + expect_iterator_items(i, expected_len, expected, expected_len, expected); git_iterator_free(i); git_vector_free(&filelist); From 09064f15c5c4efc0f4d9f4312e0aa4a11661ce4f Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Tue, 22 Mar 2016 10:28:50 +0100 Subject: [PATCH 32/35] iterator: new index-iterator test for pathlist + includings trees --- tests/iterator/index.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/iterator/index.c b/tests/iterator/index.c index a48e07b4c..64e7b14ba 100644 --- a/tests/iterator/index.c +++ b/tests/iterator/index.c @@ -731,6 +731,37 @@ void test_iterator_index__pathlist_with_dirs(void) git_vector_free(&filelist); } +void test_iterator_index__pathlist_with_dirs_include_trees(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_index *index; + git_vector filelist; + + const char *expected[] = { "k/", "k/1", "k/B", "k/D", "k/a", "k/c" }; + size_t expected_len = 6; + + cl_git_pass(git_vector_init(&filelist, 5, NULL)); + + g_repo = cl_git_sandbox_init("icase"); + + cl_git_pass(git_repository_index(&index, g_repo)); + + git_vector_clear(&filelist); + cl_git_pass(git_vector_insert(&filelist, "k")); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + + git_index_free(index); + git_vector_free(&filelist); +} + void test_iterator_index__pathlist_1(void) { git_iterator *i; From c017c183614e8b8fc84515b2ba802b80199920c6 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Tue, 22 Mar 2016 10:29:12 +0100 Subject: [PATCH 33/35] iterator: new workdir-iterator test for pathlist + includings trees --- tests/iterator/workdir.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c index 389d8a1b6..3abaee65c 100644 --- a/tests/iterator/workdir.c +++ b/tests/iterator/workdir.c @@ -1430,3 +1430,29 @@ void test_iterator_workdir__pathlist_with_directory(void) git_vector_free(&filelist); } +void test_iterator_workdir__pathlist_with_directory_include_trees(void) +{ + git_iterator *i; + git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; + git_vector filelist; + + const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt", + "subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt", }; + size_t expected_len = 6; + + cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb)); + cl_git_pass(git_vector_insert(&filelist, "subdir/")); + + g_repo = cl_git_sandbox_init("testrepo2"); + + i_opts.pathlist.strings = (char **)filelist.contents; + i_opts.pathlist.count = filelist.length; + i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES; + + cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts)); + expect_iterator_items(i, expected_len, expected, expected_len, expected); + git_iterator_free(i); + + git_vector_free(&filelist); +} + From f4777058d09937a3b7502745405655210c5b6298 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Tue, 22 Mar 2016 10:29:41 +0100 Subject: [PATCH 34/35] iterator: unused includes removed --- src/iterator.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index c78676ec6..dd9d1b299 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -8,10 +8,6 @@ #include "iterator.h" #include "tree.h" #include "index.h" -#include "ignore.h" -#include "buffer.h" -#include "submodule.h" -#include #define GIT_ITERATOR_FIRST_ACCESS (1 << 15) #define GIT_ITERATOR_HONOR_IGNORES (1 << 16) From d6713ec64e5249cd07d2a00faf8fd0cc2baa8845 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Tue, 22 Mar 2016 10:30:07 +0100 Subject: [PATCH 35/35] iterator: comment fixed --- src/iterator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iterator.c b/src/iterator.c index dd9d1b299..4202e00cd 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -204,8 +204,8 @@ GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path) return iter->ended; } -/* walker for the index iterator that allows it to walk the sorted pathlist - * entries alongside sorted iterator entries. +/* walker for the index and tree iterator that allows it to walk the sorted + * pathlist entries alongside sorted iterator entries. */ static bool iterator_pathlist_next_is(git_iterator *iter, const char *path) {