Commit 1ac10aae by Vicent Martí

Merge pull request #1408 from arrbee/refactor-iterators

Refactor iterators
parents b70bf922 62beacd3
...@@ -119,7 +119,7 @@ void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf); ...@@ -119,7 +119,7 @@ void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf);
#define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1) #define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1)
GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch) GIT_INLINE(ssize_t) git_buf_rfind_next(const git_buf *buf, char ch)
{ {
ssize_t idx = (ssize_t)buf->size - 1; ssize_t idx = (ssize_t)buf->size - 1;
while (idx >= 0 && buf->ptr[idx] == ch) idx--; while (idx >= 0 && buf->ptr[idx] == ch) idx--;
...@@ -127,18 +127,17 @@ GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch) ...@@ -127,18 +127,17 @@ GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch)
return idx; return idx;
} }
GIT_INLINE(ssize_t) git_buf_rfind(git_buf *buf, char ch) GIT_INLINE(ssize_t) git_buf_rfind(const git_buf *buf, char ch)
{ {
ssize_t idx = (ssize_t)buf->size - 1; ssize_t idx = (ssize_t)buf->size - 1;
while (idx >= 0 && buf->ptr[idx] != ch) idx--; while (idx >= 0 && buf->ptr[idx] != ch) idx--;
return idx; return idx;
} }
GIT_INLINE(ssize_t) git_buf_find(git_buf *buf, char ch) GIT_INLINE(ssize_t) git_buf_find(const git_buf *buf, char ch)
{ {
size_t idx = 0; void *found = memchr(buf->ptr, ch, buf->size);
while (idx < buf->size && buf->ptr[idx] != ch) idx++; return found ? (ssize_t)((const char *)found - buf->ptr) : -1;
return (idx == buf->size) ? -1 : (ssize_t)idx;
} }
/* Remove whitespace from the end of the buffer */ /* Remove whitespace from the end of the buffer */
......
...@@ -456,7 +456,7 @@ static int checkout_action( ...@@ -456,7 +456,7 @@ static int checkout_action(
while (1) { while (1) {
if (!wd) if (!wd)
return checkout_action_no_wd(data, delta); return checkout_action_no_wd(data, delta);
cmp = strcomp(wd->path, delta->old_file.path); cmp = strcomp(wd->path, delta->old_file.path);
/* 1. wd before delta ("a/a" before "a/b") /* 1. wd before delta ("a/a" before "a/b")
...@@ -473,9 +473,9 @@ static int checkout_action( ...@@ -473,9 +473,9 @@ static int checkout_action(
if (cmp == 0) { if (cmp == 0) {
if (wd->mode == GIT_FILEMODE_TREE) { if (wd->mode == GIT_FILEMODE_TREE) {
/* case 2 - entry prefixed by workdir tree */ /* case 2 - entry prefixed by workdir tree */
if (git_iterator_advance_into_directory(workdir, &wd) < 0) if (git_iterator_advance_into(&wd, workdir) < 0)
goto fail; goto fail;
*wditem_ptr = wd; *wditem_ptr = wd;
continue; continue;
} }
...@@ -484,14 +484,14 @@ static int checkout_action( ...@@ -484,14 +484,14 @@ static int checkout_action(
if (delta->old_file.path[strlen(wd->path)] == '/') { if (delta->old_file.path[strlen(wd->path)] == '/') {
act = checkout_action_with_wd_blocker(data, delta, wd); act = checkout_action_with_wd_blocker(data, delta, wd);
*wditem_ptr = *wditem_ptr =
git_iterator_advance(workdir, &wd) ? NULL : wd; git_iterator_advance(&wd, workdir) ? NULL : wd;
return act; return act;
} }
} }
/* case 1 - handle wd item (if it matches pathspec) */ /* case 1 - handle wd item (if it matches pathspec) */
if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 || if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 ||
git_iterator_advance(workdir, &wd) < 0) git_iterator_advance(&wd, workdir) < 0)
goto fail; goto fail;
*wditem_ptr = wd; *wditem_ptr = wd;
...@@ -501,7 +501,7 @@ static int checkout_action( ...@@ -501,7 +501,7 @@ static int checkout_action(
if (cmp == 0) { if (cmp == 0) {
/* case 4 */ /* case 4 */
act = checkout_action_with_wd(data, delta, wd); act = checkout_action_with_wd(data, delta, wd);
*wditem_ptr = git_iterator_advance(workdir, &wd) ? NULL : wd; *wditem_ptr = git_iterator_advance(&wd, workdir) ? NULL : wd;
return act; return act;
} }
...@@ -514,7 +514,7 @@ static int checkout_action( ...@@ -514,7 +514,7 @@ static int checkout_action(
if (delta->status == GIT_DELTA_TYPECHANGE) { if (delta->status == GIT_DELTA_TYPECHANGE) {
if (delta->old_file.mode == GIT_FILEMODE_TREE) { if (delta->old_file.mode == GIT_FILEMODE_TREE) {
act = checkout_action_with_wd(data, delta, wd); act = checkout_action_with_wd(data, delta, wd);
if (git_iterator_advance_into_directory(workdir, &wd) < 0) if (git_iterator_advance_into(&wd, workdir) < 0)
wd = NULL; wd = NULL;
*wditem_ptr = wd; *wditem_ptr = wd;
return act; return act;
...@@ -525,7 +525,7 @@ static int checkout_action( ...@@ -525,7 +525,7 @@ static int checkout_action(
delta->old_file.mode == GIT_FILEMODE_COMMIT) delta->old_file.mode == GIT_FILEMODE_COMMIT)
{ {
act = checkout_action_with_wd(data, delta, wd); act = checkout_action_with_wd(data, delta, wd);
if (git_iterator_advance(workdir, &wd) < 0) if (git_iterator_advance(&wd, workdir) < 0)
wd = NULL; wd = NULL;
*wditem_ptr = wd; *wditem_ptr = wd;
return act; return act;
...@@ -554,7 +554,7 @@ static int checkout_remaining_wd_items( ...@@ -554,7 +554,7 @@ static int checkout_remaining_wd_items(
while (wd && !error) { while (wd && !error) {
if (!(error = checkout_action_wd_only(data, workdir, wd, spec))) if (!(error = checkout_action_wd_only(data, workdir, wd, spec)))
error = git_iterator_advance(workdir, &wd); error = git_iterator_advance(&wd, workdir);
} }
return error; return error;
...@@ -578,7 +578,7 @@ static int checkout_get_actions( ...@@ -578,7 +578,7 @@ static int checkout_get_actions(
git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0) git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0)
return -1; return -1;
if ((error = git_iterator_current(workdir, &wditem)) < 0) if ((error = git_iterator_current(&wditem, workdir)) < 0)
goto fail; goto fail;
deltas = &data->diff->deltas; deltas = &data->diff->deltas;
...@@ -1134,16 +1134,17 @@ static int checkout_data_init( ...@@ -1134,16 +1134,17 @@ static int checkout_data_init(
if ((error = git_config_refresh(cfg)) < 0) if ((error = git_config_refresh(cfg)) < 0)
goto cleanup; goto cleanup;
if (git_iterator_inner_type(target) == GIT_ITERATOR_TYPE_INDEX) { /* if we are checking out the index, don't reload,
/* if we are iterating over the index, don't reload */ * otherwise get index and force reload
data->index = git_iterator_index_get_index(target); */
if ((data->index = git_iterator_get_index(target)) != NULL) {
GIT_REFCOUNT_INC(data->index); GIT_REFCOUNT_INC(data->index);
} else { } else {
/* otherwise, grab and reload the index */ /* otherwise, grab and reload the index */
if ((error = git_repository_index(&data->index, data->repo)) < 0 || if ((error = git_repository_index(&data->index, data->repo)) < 0 ||
(error = git_index_read(data->index)) < 0) (error = git_index_read(data->index)) < 0)
goto cleanup; goto cleanup;
/* clear the REUC when doing a tree or commit checkout */ /* clear the REUC when doing a tree or commit checkout */
git_index_reuc_clear(data->index); git_index_reuc_clear(data->index);
} }
...@@ -1241,16 +1242,15 @@ int git_checkout_iterator( ...@@ -1241,16 +1242,15 @@ int git_checkout_iterator(
GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 || if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 ||
(error = git_iterator_for_workdir_range( (error = git_iterator_for_workdir(
&workdir, data.repo, iterflags, data.pfx, data.pfx)) < 0 || &workdir, data.repo, iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
(error = git_iterator_for_tree_range( data.pfx, data.pfx)) < 0 ||
(error = git_iterator_for_tree(
&baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0) &baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0)
goto cleanup; goto cleanup;
/* Handle case insensitivity for baseline if necessary */ /* Should not have case insensitivity mismatch */
if (git_iterator_ignore_case(workdir) != git_iterator_ignore_case(baseline)) assert(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline));
if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0)
goto cleanup;
/* Generate baseline-to-target diff which will include an entry for /* Generate baseline-to-target diff which will include an entry for
* every possible update that might need to be made. * every possible update that might need to be made.
...@@ -1321,7 +1321,7 @@ int git_checkout_index( ...@@ -1321,7 +1321,7 @@ int git_checkout_index(
return error; return error;
GIT_REFCOUNT_INC(index); GIT_REFCOUNT_INC(index);
if (!(error = git_iterator_for_index(&index_i, index))) if (!(error = git_iterator_for_index(&index_i, index, 0, NULL, NULL)))
error = git_checkout_iterator(index_i, opts); error = git_checkout_iterator(index_i, opts);
git_iterator_free(index_i); git_iterator_free(index_i);
...@@ -1348,7 +1348,7 @@ int git_checkout_tree( ...@@ -1348,7 +1348,7 @@ int git_checkout_tree(
return -1; return -1;
} }
if (!(error = git_iterator_for_tree(&tree_i, tree))) if (!(error = git_iterator_for_tree(&tree_i, tree, 0, NULL, NULL)))
error = git_checkout_iterator(tree_i, opts); error = git_checkout_iterator(tree_i, opts);
git_iterator_free(tree_i); git_iterator_free(tree_i);
...@@ -1369,7 +1369,7 @@ int git_checkout_head( ...@@ -1369,7 +1369,7 @@ int git_checkout_head(
return error; return error;
if (!(error = checkout_lookup_head_tree(&head, repo)) && if (!(error = checkout_lookup_head_tree(&head, repo)) &&
!(error = git_iterator_for_tree(&head_i, head))) !(error = git_iterator_for_tree(&head_i, head, 0, NULL, NULL)))
error = git_checkout_iterator(head_i, opts); error = git_checkout_iterator(head_i, opts);
git_iterator_free(head_i); git_iterator_free(head_i);
......
...@@ -623,18 +623,13 @@ int git_diff__from_iterators( ...@@ -623,18 +623,13 @@ int git_diff__from_iterators(
goto fail; goto fail;
if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) { if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) {
/* If either iterator does not have ignore_case set, then we will if (git_iterator_set_ignore_case(old_iter, true) < 0 ||
* spool its data, sort it icase, and use that for the merge join git_iterator_set_ignore_case(new_iter, true) < 0)
* with the other iterator which was icase sorted. This call is
* a no-op on an iterator that already matches "ignore_case".
*/
if (git_iterator_spoolandsort_push(old_iter, true) < 0 ||
git_iterator_spoolandsort_push(new_iter, true) < 0)
goto fail; goto fail;
} }
if (git_iterator_current(old_iter, &oitem) < 0 || if (git_iterator_current(&oitem, old_iter) < 0 ||
git_iterator_current(new_iter, &nitem) < 0) git_iterator_current(&nitem, new_iter) < 0)
goto fail; goto fail;
/* run iterators building diffs */ /* run iterators building diffs */
...@@ -666,12 +661,12 @@ int git_diff__from_iterators( ...@@ -666,12 +661,12 @@ int git_diff__from_iterators(
if (S_ISDIR(nitem->mode) && if (S_ISDIR(nitem->mode) &&
!(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS)) !(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS))
{ {
if (git_iterator_advance(new_iter, &nitem) < 0) if (git_iterator_advance(&nitem, new_iter) < 0)
goto fail; goto fail;
} }
} }
if (git_iterator_advance(old_iter, &oitem) < 0) if (git_iterator_advance(&oitem, old_iter) < 0)
goto fail; goto fail;
} }
...@@ -699,7 +694,7 @@ int git_diff__from_iterators( ...@@ -699,7 +694,7 @@ int git_diff__from_iterators(
/* do not advance into directories that contain a .git file */ /* do not advance into directories that contain a .git file */
if (!contains_oitem && recurse_untracked) { if (!contains_oitem && recurse_untracked) {
git_buf *full = NULL; git_buf *full = NULL;
if (git_iterator_current_workdir_path(new_iter, &full) < 0) if (git_iterator_current_workdir_path(&full, new_iter) < 0)
goto fail; goto fail;
if (git_path_contains_dir(full, DOT_GIT)) if (git_path_contains_dir(full, DOT_GIT))
recurse_untracked = false; recurse_untracked = false;
...@@ -713,9 +708,19 @@ int git_diff__from_iterators( ...@@ -713,9 +708,19 @@ int git_diff__from_iterators(
git_iterator_current_is_ignored(new_iter)) git_iterator_current_is_ignored(new_iter))
git_buf_sets(&ignore_prefix, nitem->path); git_buf_sets(&ignore_prefix, nitem->path);
if (git_iterator_advance_into_directory(new_iter, &nitem) < 0) /* advance into directory */
goto fail; error = git_iterator_advance_into(&nitem, new_iter);
/* if directory is empty, can't advance into it, so skip */
if (error == GIT_ENOTFOUND) {
giterr_clear();
error = git_iterator_advance(&nitem, new_iter);
git_buf_clear(&ignore_prefix);
}
if (error < 0)
goto fail;
continue; continue;
} }
} }
...@@ -736,7 +741,7 @@ int git_diff__from_iterators( ...@@ -736,7 +741,7 @@ int git_diff__from_iterators(
* skip the file. * skip the file.
*/ */
else if (delta_type == GIT_DELTA_IGNORED) { else if (delta_type == GIT_DELTA_IGNORED) {
if (git_iterator_advance(new_iter, &nitem) < 0) if (git_iterator_advance(&nitem, new_iter) < 0)
goto fail; goto fail;
continue; /* ignored parent directory, so skip completely */ continue; /* ignored parent directory, so skip completely */
} }
...@@ -765,7 +770,7 @@ int git_diff__from_iterators( ...@@ -765,7 +770,7 @@ int git_diff__from_iterators(
} }
} }
if (git_iterator_advance(new_iter, &nitem) < 0) if (git_iterator_advance(&nitem, new_iter) < 0)
goto fail; goto fail;
} }
...@@ -775,11 +780,10 @@ int git_diff__from_iterators( ...@@ -775,11 +780,10 @@ int git_diff__from_iterators(
else { else {
assert(oitem && nitem && cmp == 0); assert(oitem && nitem && cmp == 0);
if (maybe_modified( if (maybe_modified(old_iter, oitem, new_iter, nitem, diff) < 0 ||
old_iter, oitem, new_iter, nitem, diff) < 0 || git_iterator_advance(&oitem, old_iter) < 0 ||
git_iterator_advance(old_iter, &oitem) < 0 || git_iterator_advance(&nitem, new_iter) < 0)
git_iterator_advance(new_iter, &nitem) < 0) goto fail;
goto fail;
} }
} }
...@@ -800,7 +804,7 @@ fail: ...@@ -800,7 +804,7 @@ fail:
git_iterator *a = NULL, *b = NULL; \ git_iterator *a = NULL, *b = NULL; \
char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \ char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
error = git_diff__from_iterators(diff, repo, a, b, opts); \ error = git_diff__from_iterators(diff, repo, a, b, opts); \
git__free(pfx); git_iterator_free(a); git_iterator_free(b); \ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
} while (0) } while (0)
...@@ -817,8 +821,8 @@ int git_diff_tree_to_tree( ...@@ -817,8 +821,8 @@ int git_diff_tree_to_tree(
assert(diff && repo); assert(diff && repo);
DIFF_FROM_ITERATORS( DIFF_FROM_ITERATORS(
git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), git_iterator_for_tree(&a, old_tree, 0, pfx, pfx),
git_iterator_for_tree_range(&b, new_tree, 0, pfx, pfx) git_iterator_for_tree(&b, new_tree, 0, pfx, pfx)
); );
return error; return error;
...@@ -839,8 +843,8 @@ int git_diff_tree_to_index( ...@@ -839,8 +843,8 @@ int git_diff_tree_to_index(
return error; return error;
DIFF_FROM_ITERATORS( DIFF_FROM_ITERATORS(
git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), git_iterator_for_tree(&a, old_tree, 0, pfx, pfx),
git_iterator_for_index_range(&b, index, 0, pfx, pfx) git_iterator_for_index(&b, index, 0, pfx, pfx)
); );
return error; return error;
...@@ -860,8 +864,9 @@ int git_diff_index_to_workdir( ...@@ -860,8 +864,9 @@ int git_diff_index_to_workdir(
return error; return error;
DIFF_FROM_ITERATORS( DIFF_FROM_ITERATORS(
git_iterator_for_index_range(&a, index, 0, pfx, pfx), git_iterator_for_index(&a, index, 0, pfx, pfx),
git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx) git_iterator_for_workdir(
&b, repo, GIT_ITERATOR_DONT_AUTOEXPAND, pfx, pfx)
); );
return error; return error;
...@@ -879,8 +884,9 @@ int git_diff_tree_to_workdir( ...@@ -879,8 +884,9 @@ int git_diff_tree_to_workdir(
assert(diff && repo); assert(diff && repo);
DIFF_FROM_ITERATORS( DIFF_FROM_ITERATORS(
git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx), git_iterator_for_tree(&a, old_tree, 0, pfx, pfx),
git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx) git_iterator_for_workdir(
&b, repo, GIT_ITERATOR_DONT_AUTOEXPAND, pfx, pfx)
); );
return error; return error;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
#include "hashsig.h" #include "hashsig.h"
#include "fileops.h" #include "fileops.h"
#include "util.h"
typedef uint32_t hashsig_t; typedef uint32_t hashsig_t;
typedef uint64_t hashsig_state; typedef uint64_t hashsig_state;
...@@ -19,7 +20,7 @@ typedef uint64_t hashsig_state; ...@@ -19,7 +20,7 @@ typedef uint64_t hashsig_state;
#define HASHSIG_HEAP_SIZE ((1 << 7) - 1) #define HASHSIG_HEAP_SIZE ((1 << 7) - 1)
typedef int (GIT_STDLIB_CALL *hashsig_cmp)(const void *a, const void *b); typedef int (*hashsig_cmp)(const void *a, const void *b, void *);
typedef struct { typedef struct {
int size, asize; int size, asize;
...@@ -53,15 +54,17 @@ static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp) ...@@ -53,15 +54,17 @@ static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp)
h->cmp = cmp; h->cmp = cmp;
} }
static int GIT_STDLIB_CALL hashsig_cmp_max(const void *a, const void *b) static int hashsig_cmp_max(const void *a, const void *b, void *payload)
{ {
hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b; hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
GIT_UNUSED(payload);
return (av < bv) ? -1 : (av > bv) ? 1 : 0; return (av < bv) ? -1 : (av > bv) ? 1 : 0;
} }
static int GIT_STDLIB_CALL hashsig_cmp_min(const void *a, const void *b) static int hashsig_cmp_min(const void *a, const void *b, void *payload)
{ {
hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b; hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
GIT_UNUSED(payload);
return (av > bv) ? -1 : (av < bv) ? 1 : 0; return (av > bv) ? -1 : (av < bv) ? 1 : 0;
} }
...@@ -69,7 +72,7 @@ static void hashsig_heap_up(hashsig_heap *h, int el) ...@@ -69,7 +72,7 @@ static void hashsig_heap_up(hashsig_heap *h, int el)
{ {
int parent_el = HEAP_PARENT_OF(el); int parent_el = HEAP_PARENT_OF(el);
while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el]) > 0) { while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el], NULL) > 0) {
hashsig_t t = h->values[el]; hashsig_t t = h->values[el];
h->values[el] = h->values[parent_el]; h->values[el] = h->values[parent_el];
h->values[parent_el] = t; h->values[parent_el] = t;
...@@ -92,10 +95,10 @@ static void hashsig_heap_down(hashsig_heap *h, int el) ...@@ -92,10 +95,10 @@ static void hashsig_heap_down(hashsig_heap *h, int el)
lv = h->values[lel]; lv = h->values[lel];
rv = h->values[rel]; rv = h->values[rel];
if (h->cmp(&v, &lv) < 0 && h->cmp(&v, &rv) < 0) if (h->cmp(&v, &lv, NULL) < 0 && h->cmp(&v, &rv, NULL) < 0)
break; break;
swapel = (h->cmp(&lv, &rv) < 0) ? lel : rel; swapel = (h->cmp(&lv, &rv, NULL) < 0) ? lel : rel;
h->values[el] = h->values[swapel]; h->values[el] = h->values[swapel];
h->values[swapel] = v; h->values[swapel] = v;
...@@ -107,13 +110,13 @@ static void hashsig_heap_down(hashsig_heap *h, int el) ...@@ -107,13 +110,13 @@ static void hashsig_heap_down(hashsig_heap *h, int el)
static void hashsig_heap_sort(hashsig_heap *h) static void hashsig_heap_sort(hashsig_heap *h)
{ {
/* only need to do this at the end for signature comparison */ /* only need to do this at the end for signature comparison */
qsort(h->values, h->size, sizeof(hashsig_t), h->cmp); git__qsort_r(h->values, h->size, sizeof(hashsig_t), h->cmp, NULL);
} }
static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val) static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val)
{ {
/* if heap is full, pop top if new element should replace it */ /* if heap is full, pop top if new element should replace it */
if (h->size == h->asize && h->cmp(&val, &h->values[0]) > 0) { if (h->size == h->asize && h->cmp(&val, &h->values[0], NULL) > 0) {
h->size--; h->size--;
h->values[0] = h->values[h->size]; h->values[0] = h->values[h->size];
hashsig_heap_down(h, 0); hashsig_heap_down(h, 0);
...@@ -343,7 +346,7 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b) ...@@ -343,7 +346,7 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
/* hash heaps are sorted - just look for overlap vs total */ /* hash heaps are sorted - just look for overlap vs total */
for (i = 0, j = 0; i < a->size && j < b->size; ) { for (i = 0, j = 0; i < a->size && j < b->size; ) {
cmp = a->cmp(&a->values[i], &b->values[j]); cmp = a->cmp(&a->values[i], &b->values[j], NULL);
if (cmp < 0) if (cmp < 0)
++i; ++i;
......
...@@ -1679,54 +1679,3 @@ git_repository *git_index_owner(const git_index *index) ...@@ -1679,54 +1679,3 @@ git_repository *git_index_owner(const git_index *index)
{ {
return INDEX_OWNER(index); return INDEX_OWNER(index);
} }
int git_index_read_tree_match(
git_index *index, git_tree *tree, git_strarray *strspec)
{
#if 0
git_iterator *iter = NULL;
const git_index_entry *entry;
char *pfx = NULL;
git_vector pathspec = GIT_VECTOR_INIT;
git_pool pathpool = GIT_POOL_INIT_STRINGPOOL;
#endif
if (!git_pathspec_is_interesting(strspec))
return git_index_read_tree(index, tree);
return git_index_read_tree(index, tree);
#if 0
/* The following loads the matches into the index, but doesn't
* erase obsoleted entries (e.g. you load a blob at "a/b" which
* should obsolete a blob at "a/b/c/d" since b is no longer a tree)
*/
if (git_pathspec_init(&pathspec, strspec, &pathpool) < 0)
return -1;
pfx = git_pathspec_prefix(strspec);
if ((error = git_iterator_for_tree_range(&iter, tree, pfx, pfx)) < 0 ||
(error = git_iterator_current(iter, &entry)) < 0)
goto cleanup;
while (entry != NULL) {
if (git_pathspec_match_path(
&pathspec, entry->path, false, false, NULL) &&
(error = git_index_add(index, entry)) < 0)
goto cleanup;
if ((error = git_iterator_advance(iter, &entry)) < 0)
goto cleanup;
}
cleanup:
git_iterator_free(iter);
git_pathspec_free(&pathspec);
git_pool_clear(&pathpool);
git__free(pfx);
return error;
#endif
}
...@@ -50,7 +50,4 @@ extern int git_index_entry__cmp_icase(const void *a, const void *b); ...@@ -50,7 +50,4 @@ extern int git_index_entry__cmp_icase(const void *a, const void *b);
extern void git_index__set_ignore_case(git_index *index, bool ignore_case); extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
extern int git_index_read_tree_match(
git_index *index, git_tree *tree, git_strarray *strspec);
#endif #endif
...@@ -14,26 +14,45 @@ ...@@ -14,26 +14,45 @@
#define ITERATOR_SET_CB(P,NAME_LC) do { \ #define ITERATOR_SET_CB(P,NAME_LC) do { \
(P)->cb.current = NAME_LC ## _iterator__current; \ (P)->cb.current = NAME_LC ## _iterator__current; \
(P)->cb.at_end = NAME_LC ## _iterator__at_end; \
(P)->cb.advance = NAME_LC ## _iterator__advance; \ (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.seek = NAME_LC ## _iterator__seek; \
(P)->cb.reset = NAME_LC ## _iterator__reset; \ (P)->cb.reset = NAME_LC ## _iterator__reset; \
(P)->cb.at_end = NAME_LC ## _iterator__at_end; \
(P)->cb.free = NAME_LC ## _iterator__free; \ (P)->cb.free = NAME_LC ## _iterator__free; \
} while (0) } while (0)
#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \ #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) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \ (P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \
GITERR_CHECK_ALLOC(P); \ GITERR_CHECK_ALLOC(P); \
(P)->base.type = GIT_ITERATOR_TYPE_ ## NAME_UC; \ (P)->base.type = GIT_ITERATOR_TYPE_ ## NAME_UC; \
(P)->base.cb = &(P)->cb; \ (P)->base.cb = &(P)->cb; \
ITERATOR_SET_CB(P,NAME_LC); \ ITERATOR_SET_CB(P,NAME_LC); \
(P)->base.repo = (REPO); \
(P)->base.start = start ? git__strdup(start) : NULL; \ (P)->base.start = start ? git__strdup(start) : NULL; \
(P)->base.end = end ? git__strdup(end) : NULL; \ (P)->base.end = end ? git__strdup(end) : NULL; \
if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \ if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
git__free(P); return -1; } \ git__free(P); return -1; } \
(P)->base.prefixcomp = git__prefixcmp; \ (P)->base.prefixcomp = git__prefixcmp; \
(P)->base.flags = flags & ~ITERATOR_CASE_FLAGS; \
if ((P)->base.flags & GIT_ITERATOR_DONT_AUTOEXPAND) \
(P)->base.flags |= GIT_ITERATOR_INCLUDE_TREES; \
} while (0) } 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__end(I) ((git_iterator *)(I))->end
#define iterator__past_end(I,PATH) \
(iterator__end(I) && ((git_iterator *)(I))->prefixcomp((PATH),iterator__end(I)) > 0)
static int iterator__reset_range( static int iterator__reset_range(
git_iterator *iter, const char *start, const char *end) git_iterator *iter, const char *start, const char *end)
{ {
...@@ -54,7 +73,7 @@ static int iterator__reset_range( ...@@ -54,7 +73,7 @@ static int iterator__reset_range(
return 0; return 0;
} }
static int iterator_update_ignore_case( static int iterator__update_ignore_case(
git_iterator *iter, git_iterator *iter,
git_iterator_flag_t flags) git_iterator_flag_t flags)
{ {
...@@ -76,42 +95,46 @@ static int iterator_update_ignore_case( ...@@ -76,42 +95,46 @@ static int iterator_update_ignore_case(
else if (ignore_case == 0) else if (ignore_case == 0)
iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE); iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
iter->prefixcomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ? iter->prefixcomp = iterator__ignore_case(iter) ?
git__prefixcmp_icase : git__prefixcmp; git__prefixcmp_icase : git__prefixcmp;
return error; return error;
} }
static int empty_iterator__no_item( GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
git_iterator *iter, const git_index_entry **entry) {
if (entry) *entry = NULL;
}
static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
{ {
GIT_UNUSED(iter); GIT_UNUSED(i);
*entry = NULL; iterator__clear_entry(e);
return 0; return 0;
} }
static int empty_iterator__at_end(git_iterator *iter) static int empty_iterator__seek(git_iterator *i, const char *p)
{ {
GIT_UNUSED(iter); GIT_UNUSED(i); GIT_UNUSED(p);
return 1; return -1;
} }
static int empty_iterator__reset( static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
git_iterator *iter, const char *start, const char *end)
{ {
GIT_UNUSED(iter); GIT_UNUSED(start); GIT_UNUSED(end); GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
return 0; return 0;
} }
static int empty_iterator__seek(git_iterator *iter, const char *prefix) static int empty_iterator__at_end(git_iterator *i)
{ {
GIT_UNUSED(iter); GIT_UNUSED(prefix); GIT_UNUSED(i);
return -1; return 1;
} }
static void empty_iterator__free(git_iterator *iter) static void empty_iterator__free(git_iterator *i)
{ {
GIT_UNUSED(iter); GIT_UNUSED(i);
} }
typedef struct { typedef struct {
...@@ -119,56 +142,82 @@ typedef struct { ...@@ -119,56 +142,82 @@ typedef struct {
git_iterator_callbacks cb; git_iterator_callbacks cb;
} empty_iterator; } empty_iterator;
int git_iterator_for_nothing(git_iterator **iter, git_iterator_flag_t flags) int git_iterator_for_nothing(
git_iterator **iter,
git_iterator_flag_t flags,
const char *start,
const char *end)
{ {
empty_iterator *i = git__calloc(1, sizeof(empty_iterator)); empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
GITERR_CHECK_ALLOC(i); GITERR_CHECK_ALLOC(i);
i->base.type = GIT_ITERATOR_TYPE_EMPTY; #define empty_iterator__current empty_iterator__noop
i->base.cb = &i->cb; #define empty_iterator__advance empty_iterator__noop
i->base.flags = flags; #define empty_iterator__advance_into empty_iterator__noop
i->cb.current = empty_iterator__no_item;
i->cb.at_end = empty_iterator__at_end;
i->cb.advance = empty_iterator__no_item;
i->cb.seek = empty_iterator__seek;
i->cb.reset = empty_iterator__reset;
i->cb.free = empty_iterator__free;
*iter = (git_iterator *)i; ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
i->base.flags |= GIT_ITERATOR_IGNORE_CASE;
*iter = (git_iterator *)i;
return 0; return 0;
} }
typedef struct {
size_t parent_entry_index; /* index in parent entries array */
size_t parent_tree_index; /* index in parent entry tree */
git_tree *tree; /* this tree if this is tree (only valid while current) */
} tree_iterator_entry;
typedef struct tree_iterator_frame tree_iterator_frame; typedef struct tree_iterator_frame tree_iterator_frame;
struct tree_iterator_frame { struct tree_iterator_frame {
tree_iterator_frame *next, *prev; tree_iterator_frame *parent, *child;
git_tree *tree;
char *start; size_t n_entries; /* items in this frame */
size_t current; /* start of currently active range in frame */
size_t next; /* start of next range in frame */
const char *start;
size_t startlen; size_t startlen;
size_t index;
void **icase_map; tree_iterator_entry entries[GIT_FLEX_ARRAY];
void *icase_data[GIT_FLEX_ARRAY];
}; };
typedef struct { typedef struct {
git_iterator base; git_iterator base;
git_iterator_callbacks cb; git_iterator_callbacks cb;
tree_iterator_frame *stack, *tail; tree_iterator_frame *head, *top;
git_index_entry entry; git_index_entry entry;
git_buf path; git_buf path;
int path_ambiguities;
bool path_has_filename; bool path_has_filename;
int (*strncomp)(const char *a, const char *b, size_t sz);
} tree_iterator; } tree_iterator;
GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti) static const git_tree_entry *tree_iterator__tree_entry(
tree_iterator_frame *tf, const tree_iterator_entry *entry)
{ {
tree_iterator_frame *tf = ti->stack; git_tree *tree = tf->parent->entries[entry->parent_entry_index].tree;
if (!tree)
return NULL;
return git_tree_entry_byindex(tree, entry->parent_tree_index);
}
if (tf->index >= git_tree_entrycount(tf->tree)) static const git_tree_entry *tree_iterator__tree_entry_by_index(
tree_iterator_frame *tf, size_t i)
{
git_tree *tree;
if (i >= tf->n_entries)
return NULL;
tree = tf->parent->entries[tf->entries[i].parent_entry_index].tree;
if (!tree)
return NULL; return NULL;
return git_tree_entry_byindex( return git_tree_entry_byindex(tree, tf->entries[i].parent_tree_index);
tf->tree, tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index);
} }
static char *tree_iterator__current_filename( static char *tree_iterator__current_filename(
...@@ -177,274 +226,379 @@ static char *tree_iterator__current_filename( ...@@ -177,274 +226,379 @@ static char *tree_iterator__current_filename(
if (!ti->path_has_filename) { if (!ti->path_has_filename) {
if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0) if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
return NULL; return NULL;
if (git_tree_entry__is_tree(te) && git_buf_putc(&ti->path, '/') < 0)
return NULL;
ti->path_has_filename = true; ti->path_has_filename = true;
} }
return ti->path.ptr; return ti->path.ptr;
} }
static void tree_iterator__free_frame(tree_iterator_frame *tf) static void tree_iterator__rewrite_filename(tree_iterator *ti)
{ {
if (!tf) tree_iterator_frame *scan = ti->head;
return; size_t current = scan->current;
ssize_t strpos = ti->path.size;
const git_tree_entry *te;
git_tree_free(tf->tree); if (strpos && ti->path.ptr[strpos - 1] == '/')
tf->tree = NULL; strpos--;
git__free(tf); while (scan && scan->parent) {
tree_iterator_entry *entry = &scan->entries[current];
if (!(te = tree_iterator__tree_entry(scan, entry)))
break;
strpos -= te->filename_len;
memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
strpos -= 1; /* separator */
current = entry->parent_entry_index;
scan = scan->parent;
}
} }
static bool tree_iterator__pop_frame(tree_iterator *ti) static int tree_iterator__tree_entry_cmp(
const git_tree_entry *a,
const git_tree_entry *b,
int (*strncomp)(const char *, const char *, size_t))
{ {
tree_iterator_frame *tf = ti->stack; size_t common = min(a->filename_len, b->filename_len);
int cmp = strncomp(a->filename, b->filename, common);
/* don't free the initial tree/frame */ if (!cmp) {
if (!tf->next) char a_next = a->filename[common], b_next = b->filename[common];
return false;
ti->stack = tf->next; if (!a_next && a->attr == GIT_FILEMODE_TREE)
ti->stack->prev = NULL; a_next = '/';
if (!b_next && b->attr == GIT_FILEMODE_TREE)
b_next = '/';
tree_iterator__free_frame(tf); cmp = (int)a_next - (int)b_next;
}
return true; return cmp;
} }
static int tree_iterator__to_end(tree_iterator *ti) static int tree_iterator__entry_cmp(const void *a, const void *b, void *p)
{ {
while (tree_iterator__pop_frame(ti)) /* pop all */; const tree_iterator_entry *ae = a, *be = b;
ti->stack->index = git_tree_entrycount(ti->stack->tree); const git_tree_entry *ate = tree_iterator__tree_entry(p, ae);
return 0; const git_tree_entry *bte = tree_iterator__tree_entry(p, be);
int cmp = tree_iterator__tree_entry_cmp(ate, bte, git__strncasecmp);
/* stabilize sort order among equivalent names */
if (!cmp) {
cmp = (ae->parent_entry_index < be->parent_entry_index) ? -1 :
(ae->parent_entry_index > be->parent_entry_index) ? 1 : 0;
if (!cmp)
cmp = (ae->parent_tree_index < be->parent_tree_index) ? -1 :
(ae->parent_tree_index > be->parent_tree_index) ? 1 : 0;
}
return cmp;
} }
static int tree_iterator__current( static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
git_iterator *self, const git_index_entry **entry)
{ {
tree_iterator *ti = (tree_iterator *)self; /* find next and load trees for current range */
const git_tree_entry *te = tree_iterator__tree_entry(ti); int error = 0;
const git_tree_entry *te, *last = NULL;
if (entry) tf->next = tf->current;
*entry = NULL;
if (te == NULL) while (tf->next < tf->n_entries) {
return 0; if (!(te = tree_iterator__tree_entry_by_index(tf, tf->next)) ||
(last && tree_iterator__tree_entry_cmp(last, te, ti->strncomp)))
break;
ti->entry.mode = te->attr; if (git_tree_entry__is_tree(te) &&
git_oid_cpy(&ti->entry.oid, &te->oid); (error = git_tree_lookup(
&tf->entries[tf->next].tree, ti->base.repo, &te->oid)) < 0)
break;
ti->entry.path = tree_iterator__current_filename(ti, te); tf->next++;
if (ti->entry.path == NULL) last = te;
return -1; }
if (ti->base.end && ti->base.prefixcomp(ti->entry.path, ti->base.end) > 0) if (tf->next > tf->current + 1)
return tree_iterator__to_end(ti); ti->path_ambiguities++;
if (entry) if (last && !tree_iterator__current_filename(ti, last))
*entry = &ti->entry; return -1;
return 0; return error;
} }
static int tree_iterator__at_end(git_iterator *self) GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
{ {
return (tree_iterator__tree_entry((tree_iterator *)self) == NULL); return (ti->head->current < ti->head->n_entries &&
ti->head->entries[ti->head->current].tree != NULL);
} }
static int tree_iterator__icase_map_cmp(const void *a, const void *b, void *data) static int tree_iterator__push_frame(tree_iterator *ti)
{ {
git_tree *tree = data; int error = 0;
const git_tree_entry *te1 = git_tree_entry_byindex(tree, (size_t)a); tree_iterator_frame *tf = ti->head, *new_tf = NULL;
const git_tree_entry *te2 = git_tree_entry_byindex(tree, (size_t)b); size_t i, n_entries = 0, sz = sizeof(tree_iterator_frame);
const git_tree_entry *te;
return te1 ? (te2 ? git_tree_entry_icmp(te1, te2) : 1) : -1; /* if current item in head is not a tree, do nothing */
} if (tf->current >= tf->n_entries || !tf->entries[tf->current].tree)
return 0;
static int tree_iterator__frame_start_icmp(const void *key, const void *el) /* build frame - sum tree entries from parent range */
{ for (i = tf->current; i < tf->next; ++i)
const tree_iterator_frame *tf = (const tree_iterator_frame *)key; n_entries += git_tree_entrycount(tf->entries[i].tree);
const git_tree_entry *te = git_tree_entry_byindex(tf->tree, (size_t)el); sz += n_entries * sizeof(tree_iterator_entry);
size_t minlen = min(tf->startlen, te->filename_len); new_tf = git__calloc(sz, sizeof(char));
GITERR_CHECK_ALLOC(new_tf);
/* populate frame and entries */
new_tf->parent = tf;
new_tf->n_entries = n_entries;
for (i = tf->current, n_entries = 0; i < tf->next; ++i) {
git_tree *tree = tf->entries[i].tree;
size_t j, max_j = git_tree_entrycount(tree);
for (j = 0; j < max_j; ++j) {
new_tf->entries[n_entries].parent_entry_index = i;
new_tf->entries[n_entries].parent_tree_index = j;
n_entries++;
}
}
return git__strncasecmp(tf->start, te->filename, minlen); /* if ignore_case, sort entries case insensitively */
} if (iterator__ignore_case(ti))
git__qsort_r(
new_tf->entries, new_tf->n_entries, sizeof(tree_iterator_entry),
tree_iterator__entry_cmp, new_tf);
/* pick new_tf->current based on "start" (or start at zero) */
if (tf->startlen > 0) {
/* find first item >= start */
for (i = 0; i < new_tf->n_entries; ++i) {
if (!(te = tree_iterator__tree_entry_by_index(new_tf, i)))
break;
sz = min(tf->startlen, te->filename_len);
if (ti->strncomp(tf->start, te->filename, sz) <= 0 &&
(tf->startlen <= te->filename_len ||
tf->start[te->filename_len] == '/'))
break;
}
new_tf->current = i;
static void tree_iterator__frame_seek_start(tree_iterator_frame *tf) if ((new_tf->start = strchr(tf->start, '/')) != NULL) {
{ new_tf->start++;
if (!tf->start) new_tf->startlen = strlen(new_tf->start);
tf->index = 0;
else if (!tf->icase_map)
tf->index = git_tree__prefix_position(tf->tree, tf->start);
else {
if (!git__bsearch(
tf->icase_map, git_tree_entrycount(tf->tree),
tf, tree_iterator__frame_start_icmp, &tf->index))
{
while (tf->index > 0) {
/* move back while previous entry is still prefixed */
if (tree_iterator__frame_start_icmp(
tf, (const void *)(tf->index - 1)))
break;
tf->index--;
}
} }
} }
ti->path_has_filename = false;
/* find next and load trees for current range */
if ((error = tree_iterator__set_next(ti, new_tf)) < 0)
return error;
tf->child = new_tf;
ti->head = new_tf;
if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
return tree_iterator__push_frame(ti);
return 0;
} }
static tree_iterator_frame *tree_iterator__alloc_frame( GIT_INLINE(void) tree_iterator__free_tree(tree_iterator_entry *entry)
tree_iterator *ti, git_tree *tree, char *start)
{ {
size_t i, max_i = git_tree_entrycount(tree); if (entry->tree) {
tree_iterator_frame *tf = git_tree_free(entry->tree);
git__calloc(1, sizeof(tree_iterator_frame) + max_i * sizeof(void *)); entry->tree = NULL;
if (!tf) }
return NULL; }
tf->tree = tree; static bool tree_iterator__move_to_next(
tree_iterator *ti, tree_iterator_frame *tf)
{
if (tf->next > tf->current + 1)
ti->path_ambiguities--;
if (start && *start) { for (; tf->current < tf->next; tf->current++) {
tf->start = start; if (tf->parent)
tf->startlen = strlen(start); tree_iterator__free_tree(&tf->entries[tf->current]);
} }
if (!max_i) return (tf->current < tf->n_entries);
return tf; }
if ((ti->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0) { static bool tree_iterator__pop_frame(tree_iterator *ti)
tf->icase_map = tf->icase_data; {
tree_iterator_frame *tf = ti->head;
for (i = 0; i < max_i; ++i) if (!tf->parent)
tf->icase_map[i] = (void *)i; return false;
git__tsort_r( tree_iterator__move_to_next(ti, tf);
tf->icase_map, max_i, tree_iterator__icase_map_cmp, tf->tree);
}
tree_iterator__frame_seek_start(tf); ti->head = tf->parent;
ti->head->child = NULL;
git__free(tf);
git_buf_rtruncate_at_char(&ti->path, '/');
return tf; return true;
} }
static int tree_iterator__expand_tree(tree_iterator *ti) static int tree_iterator__current(
const git_index_entry **entry, git_iterator *self)
{ {
int error; tree_iterator *ti = (tree_iterator *)self;
git_tree *subtree; tree_iterator_frame *tf = ti->head;
const git_tree_entry *te = tree_iterator__tree_entry(ti); const git_tree_entry *te;
tree_iterator_frame *tf;
char *relpath;
while (te != NULL && git_tree_entry__is_tree(te)) {
if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
return -1;
/* check that we have not passed the range end */ iterator__clear_entry(entry);
if (ti->base.end != NULL &&
ti->base.prefixcomp(ti->path.ptr, ti->base.end) > 0)
return tree_iterator__to_end(ti);
if ((error = git_tree_lookup(&subtree, ti->base.repo, &te->oid)) < 0) if (!(te = tree_iterator__tree_entry_by_index(tf, tf->current)))
return error; return 0;
relpath = NULL;
/* apply range start to new frame if relevant */ ti->entry.mode = te->attr;
if (ti->stack->start && git_oid_cpy(&ti->entry.oid, &te->oid);
ti->base.prefixcomp(ti->stack->start, te->filename) == 0)
{
if (ti->stack->start[te->filename_len] == '/')
relpath = ti->stack->start + te->filename_len + 1;
}
if ((tf = tree_iterator__alloc_frame(ti, subtree, relpath)) == NULL) ti->entry.path = tree_iterator__current_filename(ti, te);
return -1; if (ti->entry.path == NULL)
return -1;
tf->next = ti->stack; if (ti->path_ambiguities > 0)
ti->stack = tf; tree_iterator__rewrite_filename(ti);
tf->next->prev = tf;
te = tree_iterator__tree_entry(ti); if (iterator__past_end(ti, ti->entry.path)) {
while (tree_iterator__pop_frame(ti)) /* pop to top */;
ti->head->current = ti->head->n_entries;
return 0;
} }
if (entry)
*entry = &ti->entry;
return 0; return 0;
} }
static int tree_iterator__advance( static int tree_iterator__advance_into(
git_iterator *self, const git_index_entry **entry) const git_index_entry **entry, git_iterator *self)
{ {
int error = 0; int error = 0;
tree_iterator *ti = (tree_iterator *)self; tree_iterator *ti = (tree_iterator *)self;
const git_tree_entry *te = NULL;
if (entry != NULL) iterator__clear_entry(entry);
*entry = NULL;
if (ti->path_has_filename) { if (tree_iterator__at_tree(ti) &&
git_buf_rtruncate_at_char(&ti->path, '/'); !(error = tree_iterator__push_frame(ti)))
ti->path_has_filename = false; error = tree_iterator__current(entry, self);
}
while (1) { return error;
++ti->stack->index; }
if ((te = tree_iterator__tree_entry(ti)) != NULL) static int tree_iterator__advance(
break; 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 0;
if (!tree_iterator__pop_frame(ti)) if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
break; /* no frames left to pop */ tree_iterator__at_tree(ti))
return tree_iterator__advance_into(entry, self);
if (ti->path_has_filename) {
git_buf_rtruncate_at_char(&ti->path, '/'); git_buf_rtruncate_at_char(&ti->path, '/');
ti->path_has_filename = false;
} }
if (te && git_tree_entry__is_tree(te)) /* scan forward and up, advancing in frame or popping frame when done */
error = tree_iterator__expand_tree(ti); while (!tree_iterator__move_to_next(ti, tf) && tree_iterator__pop_frame(ti))
tf = ti->head;
if (!error) /* find next and load trees */
error = tree_iterator__current(self, entry); if ((error = tree_iterator__set_next(ti, tf)) < 0)
return error;
return error; /* deal with include_trees / auto_expand as needed */
if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
return tree_iterator__advance_into(entry, self);
return tree_iterator__current(entry, self);
} }
static int tree_iterator__seek(git_iterator *self, const char *prefix) static int tree_iterator__seek(git_iterator *self, const char *prefix)
{ {
GIT_UNUSED(self); GIT_UNUSED(self); GIT_UNUSED(prefix);
GIT_UNUSED(prefix);
/* pop stack until matches prefix */
/* seek item in current frame matching prefix */
/* push stack which matches prefix */
return -1; return -1;
} }
static void tree_iterator__free(git_iterator *self) static int tree_iterator__reset(
git_iterator *self, const char *start, const char *end)
{ {
tree_iterator *ti = (tree_iterator *)self; tree_iterator *ti = (tree_iterator *)self;
while (tree_iterator__pop_frame(ti)) /* pop all */; while (tree_iterator__pop_frame(ti)) /* pop to top */;
ti->top->current = 0;
tree_iterator__free_frame(ti->stack); if (iterator__reset_range(self, start, end) < 0)
ti->stack = ti->tail = NULL; return -1;
git_buf_clear(&ti->path);
ti->path_ambiguities = 0;
git_buf_free(&ti->path); return tree_iterator__push_frame(ti); /* re-expand top tree */
} }
static int tree_iterator__reset( static int tree_iterator__at_end(git_iterator *self)
git_iterator *self, const char *start, const char *end)
{ {
tree_iterator *ti = (tree_iterator *)self; tree_iterator *ti = (tree_iterator *)self;
return (ti->head->current >= ti->head->n_entries);
}
while (tree_iterator__pop_frame(ti)) /* pop all */; static void tree_iterator__free(git_iterator *self)
{
tree_iterator *ti = (tree_iterator *)self;
if (iterator__reset_range(self, start, end) < 0) while (tree_iterator__pop_frame(ti)) /* pop to top */;
return -1;
/* reset start position */ if (ti->head) {
tree_iterator__frame_seek_start(ti->stack); tree_iterator__free_tree(&ti->head->entries[0]);
git__free(ti->head);
}
ti->head = ti->top = NULL;
git_buf_clear(&ti->path); git_buf_free(&ti->path);
ti->path_has_filename = false; }
static int tree_iterator__create_top_frame(tree_iterator *ti, git_tree *tree)
{
size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
tree_iterator_frame *top = git__calloc(sz, sizeof(char));
GITERR_CHECK_ALLOC(top);
top->n_entries = 1;
top->next = 1;
top->start = ti->base.start;
top->startlen = top->start ? strlen(top->start) : 0;
top->entries[0].tree = tree;
ti->head = ti->top = top;
return tree_iterator__expand_tree(ti); return 0;
} }
int git_iterator_for_tree_range( int git_iterator_for_tree(
git_iterator **iter, git_iterator **iter,
git_tree *tree, git_tree *tree,
git_iterator_flag_t flags, git_iterator_flag_t flags,
...@@ -455,21 +609,19 @@ int git_iterator_for_tree_range( ...@@ -455,21 +609,19 @@ int git_iterator_for_tree_range(
tree_iterator *ti; tree_iterator *ti;
if (tree == NULL) if (tree == NULL)
return git_iterator_for_nothing(iter, flags); return git_iterator_for_nothing(iter, flags, start, end);
if ((error = git_tree__dup(&tree, tree)) < 0) if ((error = git_tree__dup(&tree, tree)) < 0)
return error; return error;
ITERATOR_BASE_INIT(ti, tree, TREE); ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
ti->base.repo = git_tree_owner(tree);
if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0) if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
goto fail; goto fail;
ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
ti->stack = ti->tail = tree_iterator__alloc_frame(ti, tree, ti->base.start); if ((error = tree_iterator__create_top_frame(ti, tree)) < 0 ||
(error = tree_iterator__push_frame(ti)) < 0) /* expand top right now */
if ((error = tree_iterator__expand_tree(ti)) < 0)
goto fail; goto fail;
*iter = (git_iterator *)ti; *iter = (git_iterator *)ti;
...@@ -486,14 +638,94 @@ typedef struct { ...@@ -486,14 +638,94 @@ typedef struct {
git_iterator_callbacks cb; git_iterator_callbacks cb;
git_index *index; git_index *index;
size_t current; size_t current;
/* when not in autoexpand mode, use these to represent "tree" state */
git_buf partial;
size_t partial_pos;
char restore_terminator;
git_index_entry tree_entry;
} index_iterator; } index_iterator;
static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
{
const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
if (ie != NULL && iterator__past_end(ii, ie->path)) {
ii->current = git_index_entrycount(ii->index);
ie = NULL;
}
return ie;
}
static const git_index_entry *index_iterator__skip_conflicts(index_iterator *ii)
{
const git_index_entry *ie;
while ((ie = index_iterator__index_entry(ii)) != NULL &&
git_index_entry_stage(ie) != 0)
ii->current++;
return ie;
}
static void index_iterator__next_prefix_tree(index_iterator *ii)
{
const char *slash;
if (!iterator__include_trees(ii))
return;
slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');
if (slash != NULL) {
ii->partial_pos = (slash - ii->partial.ptr) + 1;
ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
ii->partial.ptr[ii->partial_pos] = '\0';
} else {
ii->partial_pos = ii->partial.size;
}
if (index_iterator__index_entry(ii) == NULL)
ii->partial_pos = ii->partial.size;
}
static int index_iterator__first_prefix_tree(index_iterator *ii)
{
const git_index_entry *ie = index_iterator__skip_conflicts(ii);
const char *scan, *prior, *slash;
if (!ie || !iterator__include_trees(ii))
return 0;
/* find longest common prefix with prior index entry */
for (scan = slash = ie->path, prior = ii->partial.ptr;
*scan && *scan == *prior; ++scan, ++prior)
if (*scan == '/')
slash = scan;
if (git_buf_sets(&ii->partial, ie->path) < 0)
return -1;
ii->partial_pos = (slash - ie->path) + 1;
index_iterator__next_prefix_tree(ii);
return 0;
}
#define index_iterator__at_tree(I) \
(iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)
static int index_iterator__current( static int index_iterator__current(
git_iterator *self, const git_index_entry **entry) const git_index_entry **entry, git_iterator *self)
{ {
index_iterator *ii = (index_iterator *)self; index_iterator *ii = (index_iterator *)self;
const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current); const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
if (ie != NULL && index_iterator__at_tree(ii)) {
ii->tree_entry.path = ii->partial.ptr;
ie = &ii->tree_entry;
}
if (entry) if (entry)
*entry = ie; *entry = ie;
...@@ -506,47 +738,59 @@ static int index_iterator__at_end(git_iterator *self) ...@@ -506,47 +738,59 @@ static int index_iterator__at_end(git_iterator *self)
return (ii->current >= git_index_entrycount(ii->index)); return (ii->current >= git_index_entrycount(ii->index));
} }
static void index_iterator__skip_conflicts( static int index_iterator__advance(
index_iterator *ii) const git_index_entry **entry, git_iterator *self)
{ {
index_iterator *ii = (index_iterator *)self;
size_t entrycount = git_index_entrycount(ii->index); size_t entrycount = git_index_entrycount(ii->index);
const git_index_entry *ie; const git_index_entry *ie;
while (ii->current < entrycount) { if (index_iterator__at_tree(ii)) {
ie = git_index_get_byindex(ii->index, ii->current); if (iterator__do_autoexpand(ii)) {
ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
index_iterator__next_prefix_tree(ii);
} else {
/* advance to sibling tree (i.e. find entry with new prefix) */
while (ii->current < entrycount) {
ii->current++;
if (!(ie = git_index_get_byindex(ii->index, ii->current)) ||
ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
break;
}
if (ie == NULL || if (index_iterator__first_prefix_tree(ii) < 0)
(ii->base.end != NULL && return -1;
ii->base.prefixcomp(ie->path, ii->base.end) > 0)) {
ii->current = entrycount;
break;
} }
} else {
if (ii->current < entrycount)
ii->current++;
if (git_index_entry_stage(ie) == 0) if (index_iterator__first_prefix_tree(ii) < 0)
break; return -1;
ii->current++;
} }
return index_iterator__current(entry, self);
} }
static int index_iterator__advance( static int index_iterator__advance_into(
git_iterator *self, const git_index_entry **entry) const git_index_entry **entry, git_iterator *self)
{ {
index_iterator *ii = (index_iterator *)self; index_iterator *ii = (index_iterator *)self;
const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
if (ii->current < git_index_entrycount(ii->index)) if (ie != NULL && index_iterator__at_tree(ii)) {
ii->current++; if (ii->restore_terminator)
ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
index_iterator__skip_conflicts(ii); index_iterator__next_prefix_tree(ii);
}
return index_iterator__current(self, entry); return index_iterator__current(entry, self);
} }
static int index_iterator__seek(git_iterator *self, const char *prefix) static int index_iterator__seek(git_iterator *self, const char *prefix)
{ {
GIT_UNUSED(self); GIT_UNUSED(self); GIT_UNUSED(prefix);
GIT_UNUSED(prefix);
/* find last item before prefix */
return -1; return -1;
} }
...@@ -554,11 +798,31 @@ static int index_iterator__reset( ...@@ -554,11 +798,31 @@ static int index_iterator__reset(
git_iterator *self, const char *start, const char *end) git_iterator *self, const char *start, const char *end)
{ {
index_iterator *ii = (index_iterator *)self; index_iterator *ii = (index_iterator *)self;
const git_index_entry *ie;
if (iterator__reset_range(self, start, end) < 0) if (iterator__reset_range(self, start, end) < 0)
return -1; return -1;
ii->current = ii->base.start ? ii->current = ii->base.start ?
git_index__prefix_position(ii->index, ii->base.start) : 0; git_index__prefix_position(ii->index, ii->base.start) : 0;
index_iterator__skip_conflicts(ii);
if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
return 0;
if (git_buf_sets(&ii->partial, ie->path) < 0)
return -1;
ii->partial_pos = 0;
if (ii->base.start) {
size_t startlen = strlen(ii->base.start);
ii->partial_pos = (startlen > ii->partial.size) ?
ii->partial.size : startlen;
}
index_iterator__next_prefix_tree(ii);
return 0; return 0;
} }
...@@ -567,9 +831,11 @@ static void index_iterator__free(git_iterator *self) ...@@ -567,9 +831,11 @@ static void index_iterator__free(git_iterator *self)
index_iterator *ii = (index_iterator *)self; index_iterator *ii = (index_iterator *)self;
git_index_free(ii->index); git_index_free(ii->index);
ii->index = NULL; ii->index = NULL;
git_buf_free(&ii->partial);
} }
int git_iterator_for_index_range( int git_iterator_for_index(
git_iterator **iter, git_iterator **iter,
git_index *index, git_index *index,
git_iterator_flag_t flags, git_iterator_flag_t flags,
...@@ -578,18 +844,19 @@ int git_iterator_for_index_range( ...@@ -578,18 +844,19 @@ int git_iterator_for_index_range(
{ {
index_iterator *ii; index_iterator *ii;
GIT_UNUSED(flags); ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
ITERATOR_BASE_INIT(ii, index, INDEX);
ii->base.repo = git_index_owner(index);
if (index->ignore_case) { if (index->ignore_case) {
ii->base.flags |= GIT_ITERATOR_IGNORE_CASE; ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
ii->base.prefixcomp = git__prefixcmp_icase; ii->base.prefixcomp = git__prefixcmp_icase;
} }
ii->index = index; ii->index = index;
GIT_REFCOUNT_INC(index); GIT_REFCOUNT_INC(index);
git_buf_init(&ii->partial, 0);
ii->tree_entry.mode = GIT_FILEMODE_TREE;
index_iterator__reset((git_iterator *)ii, NULL, NULL); index_iterator__reset((git_iterator *)ii, NULL, NULL);
*iter = (git_iterator *)ii; *iter = (git_iterator *)ii;
...@@ -598,6 +865,8 @@ int git_iterator_for_index_range( ...@@ -598,6 +865,8 @@ int git_iterator_for_index_range(
} }
#define WORKDIR_MAX_DEPTH 100
typedef struct workdir_iterator_frame workdir_iterator_frame; typedef struct workdir_iterator_frame workdir_iterator_frame;
struct workdir_iterator_frame { struct workdir_iterator_frame {
workdir_iterator_frame *next; workdir_iterator_frame *next;
...@@ -615,6 +884,7 @@ typedef struct { ...@@ -615,6 +884,7 @@ typedef struct {
git_buf path; git_buf path;
size_t root_len; size_t root_len;
int is_ignored; int is_ignored;
int depth;
} workdir_iterator; } workdir_iterator;
GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps) GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps)
...@@ -643,7 +913,7 @@ static workdir_iterator_frame *workdir_iterator__alloc_frame( ...@@ -643,7 +913,7 @@ static workdir_iterator_frame *workdir_iterator__alloc_frame(
{ {
workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame)); workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
git_vector_cmp entry_compare = CASESELECT( git_vector_cmp entry_compare = CASESELECT(
(wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0, iterator__ignore_case(wi),
git_path_with_stat_cmp_icase, git_path_with_stat_cmp); git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
if (wf == NULL) if (wf == NULL)
...@@ -701,12 +971,18 @@ static void workdir_iterator__seek_frame_start( ...@@ -701,12 +971,18 @@ static void workdir_iterator__seek_frame_start(
static int workdir_iterator__expand_dir(workdir_iterator *wi) static int workdir_iterator__expand_dir(workdir_iterator *wi)
{ {
int error; int error;
workdir_iterator_frame *wf = workdir_iterator__alloc_frame(wi); workdir_iterator_frame *wf;
if (++(wi->depth) > WORKDIR_MAX_DEPTH) {
giterr_set(GITERR_REPOSITORY, "Working directory is too deep");
return -1;
}
wf = workdir_iterator__alloc_frame(wi);
GITERR_CHECK_ALLOC(wf); GITERR_CHECK_ALLOC(wf);
error = git_path_dirload_with_stat( error = git_path_dirload_with_stat(
wi->path.ptr, wi->root_len, wi->path.ptr, wi->root_len, iterator__ignore_case(wi),
(wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0,
wi->base.start, wi->base.end, &wf->entries); wi->base.start, wi->base.end, &wf->entries);
if (error < 0 || wf->entries.length == 0) { if (error < 0 || wf->entries.length == 0) {
...@@ -729,10 +1005,11 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi) ...@@ -729,10 +1005,11 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi)
} }
static int workdir_iterator__current( static int workdir_iterator__current(
git_iterator *self, const git_index_entry **entry) const git_index_entry **entry, git_iterator *self)
{ {
workdir_iterator *wi = (workdir_iterator *)self; workdir_iterator *wi = (workdir_iterator *)self;
*entry = (wi->entry.path == NULL) ? NULL : &wi->entry; if (entry)
*entry = (wi->entry.path == NULL) ? NULL : &wi->entry;
return 0; return 0;
} }
...@@ -741,21 +1018,56 @@ static int workdir_iterator__at_end(git_iterator *self) ...@@ -741,21 +1018,56 @@ static int workdir_iterator__at_end(git_iterator *self)
return (((workdir_iterator *)self)->entry.path == NULL); return (((workdir_iterator *)self)->entry.path == NULL);
} }
static int workdir_iterator__advance_into(
const git_index_entry **entry, git_iterator *iter)
{
int error = 0;
workdir_iterator *wi = (workdir_iterator *)iter;
iterator__clear_entry(entry);
/* workdir iterator will allow you to explicitly advance into a
* commit/submodule (as well as a tree) to avoid some cases where an
* entry is mislabeled as a submodule in the working directory
*/
if (wi->entry.path != NULL &&
(wi->entry.mode == GIT_FILEMODE_TREE ||
wi->entry.mode == GIT_FILEMODE_COMMIT))
/* returns GIT_ENOTFOUND if the directory is empty */
error = workdir_iterator__expand_dir(wi);
if (!error && entry)
error = workdir_iterator__current(entry, iter);
return error;
}
static int workdir_iterator__advance( static int workdir_iterator__advance(
git_iterator *self, const git_index_entry **entry) const git_index_entry **entry, git_iterator *self)
{ {
int error; int error = 0;
workdir_iterator *wi = (workdir_iterator *)self; workdir_iterator *wi = (workdir_iterator *)self;
workdir_iterator_frame *wf; workdir_iterator_frame *wf;
git_path_with_stat *next; git_path_with_stat *next;
/* given include_trees & autoexpand, we might have to go into a tree */
if (iterator__do_autoexpand(wi) &&
wi->entry.path != NULL &&
wi->entry.mode == GIT_FILEMODE_TREE)
{
error = workdir_iterator__advance_into(entry, self);
/* continue silently past empty directories if autoexpanding */
if (error != GIT_ENOTFOUND)
return error;
giterr_clear();
error = 0;
}
if (entry != NULL) if (entry != NULL)
*entry = NULL; *entry = NULL;
if (wi->entry.path == NULL) while (wi->entry.path != NULL) {
return 0;
while (1) {
wf = wi->stack; wf = wi->stack;
next = git_vector_get(&wf->entries, ++wf->index); next = git_vector_get(&wf->entries, ++wf->index);
...@@ -781,7 +1093,7 @@ static int workdir_iterator__advance( ...@@ -781,7 +1093,7 @@ static int workdir_iterator__advance(
error = workdir_iterator__update_entry(wi); error = workdir_iterator__update_entry(wi);
if (!error && entry != NULL) if (!error && entry != NULL)
error = workdir_iterator__current(self, entry); error = workdir_iterator__current(entry, self);
return error; return error;
} }
...@@ -832,6 +1144,7 @@ static void workdir_iterator__free(git_iterator *self) ...@@ -832,6 +1144,7 @@ static void workdir_iterator__free(git_iterator *self)
static int workdir_iterator__update_entry(workdir_iterator *wi) static int workdir_iterator__update_entry(workdir_iterator *wi)
{ {
int error = 0;
git_path_with_stat *ps = git_path_with_stat *ps =
git_vector_get(&wi->stack->entries, wi->stack->index); git_vector_get(&wi->stack->entries, wi->stack->index);
...@@ -841,19 +1154,18 @@ static int workdir_iterator__update_entry(workdir_iterator *wi) ...@@ -841,19 +1154,18 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
if (!ps) if (!ps)
return 0; return 0;
/* skip over .git entries */
if (path_is_dotgit(ps))
return workdir_iterator__advance(NULL, (git_iterator *)wi);
if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0) if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0)
return -1; return -1;
if (wi->base.end && if (iterator__past_end(wi, wi->path.ptr + wi->root_len))
wi->base.prefixcomp(wi->path.ptr + wi->root_len, wi->base.end) > 0)
return 0; return 0;
wi->entry.path = ps->path; wi->entry.path = ps->path;
/* skip over .git entries */
if (path_is_dotgit(ps))
return workdir_iterator__advance((git_iterator *)wi, NULL);
wi->is_ignored = -1; wi->is_ignored = -1;
git_index_entry__init_from_stat(&wi->entry, &ps->st); git_index_entry__init_from_stat(&wi->entry, &ps->st);
...@@ -867,26 +1179,32 @@ static int workdir_iterator__update_entry(workdir_iterator *wi) ...@@ -867,26 +1179,32 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
return 0; return 0;
} }
/* if this isn't a tree, then we're done */
if (wi->entry.mode != GIT_FILEMODE_TREE)
return 0;
/* detect submodules */ /* detect submodules */
if (S_ISDIR(wi->entry.mode)) {
int res = git_submodule_lookup(NULL, wi->base.repo, wi->entry.path); error = git_submodule_lookup(NULL, wi->base.repo, wi->entry.path);
bool is_submodule = (res == 0); if (error == GIT_ENOTFOUND)
if (res == GIT_ENOTFOUND) giterr_clear();
giterr_clear();
/* if submodule, mark as GITLINK and remove trailing slash */
/* if submodule, mark as GITLINK and remove trailing slash */ if (!error) {
if (is_submodule) { size_t len = strlen(wi->entry.path);
size_t len = strlen(wi->entry.path); assert(wi->entry.path[len - 1] == '/');
assert(wi->entry.path[len - 1] == '/'); wi->entry.path[len - 1] = '\0';
wi->entry.path[len - 1] = '\0'; wi->entry.mode = S_IFGITLINK;
wi->entry.mode = S_IFGITLINK; return 0;
}
} }
return 0; if (iterator__include_trees(wi))
return 0;
return workdir_iterator__advance_into(NULL, (git_iterator *)wi);
} }
int git_iterator_for_workdir_range( int git_iterator_for_workdir(
git_iterator **iter, git_iterator **iter,
git_repository *repo, git_repository *repo,
git_iterator_flag_t flags, git_iterator_flag_t flags,
...@@ -899,13 +1217,12 @@ int git_iterator_for_workdir_range( ...@@ -899,13 +1217,12 @@ int git_iterator_for_workdir_range(
assert(iter && repo); assert(iter && repo);
if ((error = git_repository__ensure_not_bare( if ((error = git_repository__ensure_not_bare(
repo, "scan working directory")) < 0) repo, "scan working directory")) < 0)
return error; return error;
ITERATOR_BASE_INIT(wi, workdir, WORKDIR); ITERATOR_BASE_INIT(wi, workdir, WORKDIR, repo);
wi->base.repo = repo;
if ((error = iterator_update_ignore_case((git_iterator *)wi, flags)) < 0) if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0)
goto fail; goto fail;
if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 || if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 ||
...@@ -917,7 +1234,7 @@ int git_iterator_for_workdir_range( ...@@ -917,7 +1234,7 @@ int git_iterator_for_workdir_range(
} }
wi->root_len = wi->path.size; wi->root_len = wi->path.size;
wi->entrycmp = (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? wi->entrycmp = iterator__ignore_case(wi) ?
workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case; workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case;
if ((error = workdir_iterator__expand_dir(wi)) < 0) { if ((error = workdir_iterator__expand_dir(wi)) < 0) {
...@@ -935,161 +1252,6 @@ fail: ...@@ -935,161 +1252,6 @@ fail:
} }
typedef struct {
/* replacement callbacks */
git_iterator_callbacks cb;
/* original iterator values */
git_iterator_callbacks *orig;
git_iterator_type_t orig_type;
/* spoolandsort data */
git_vector entries;
git_pool entry_pool;
git_pool string_pool;
size_t position;
} spoolandsort_callbacks;
static int spoolandsort_iterator__current(
git_iterator *self, const git_index_entry **entry)
{
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
*entry = (const git_index_entry *)
git_vector_get(&scb->entries, scb->position);
return 0;
}
static int spoolandsort_iterator__at_end(git_iterator *self)
{
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
return 0 == scb->entries.length || scb->entries.length - 1 <= scb->position;
}
static int spoolandsort_iterator__advance(
git_iterator *self, const git_index_entry **entry)
{
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
*entry = (const git_index_entry *)
git_vector_get(&scb->entries, ++scb->position);
return 0;
}
static int spoolandsort_iterator__seek(git_iterator *self, const char *prefix)
{
GIT_UNUSED(self);
GIT_UNUSED(prefix);
return -1;
}
static int spoolandsort_iterator__reset(
git_iterator *self, const char *start, const char *end)
{
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
GIT_UNUSED(start); GIT_UNUSED(end);
scb->position = 0;
return 0;
}
static void spoolandsort_iterator__free_callbacks(spoolandsort_callbacks *scb)
{
git_pool_clear(&scb->string_pool);
git_pool_clear(&scb->entry_pool);
git_vector_free(&scb->entries);
git__free(scb);
}
void git_iterator_spoolandsort_pop(git_iterator *self)
{
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
if (self->type != GIT_ITERATOR_TYPE_SPOOLANDSORT)
return;
self->cb = scb->orig;
self->type = scb->orig_type;
self->flags ^= GIT_ITERATOR_IGNORE_CASE;
spoolandsort_iterator__free_callbacks(scb);
}
static void spoolandsort_iterator__free(git_iterator *self)
{
git_iterator_spoolandsort_pop(self);
self->cb->free(self);
}
int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case)
{
const git_index_entry *item;
spoolandsort_callbacks *scb;
int (*entrycomp)(const void *a, const void *b);
if (((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) == (ignore_case != 0))
return 0;
if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
iter->flags = (iter->flags ^ GIT_ITERATOR_IGNORE_CASE);
return 0;
}
scb = git__calloc(1, sizeof(spoolandsort_callbacks));
GITERR_CHECK_ALLOC(scb);
ITERATOR_SET_CB(scb,spoolandsort);
scb->orig = iter->cb;
scb->orig_type = iter->type;
scb->position = 0;
entrycomp = ignore_case ? git_index_entry__cmp_icase : git_index_entry__cmp;
if (git_vector_init(&scb->entries, 16, entrycomp) < 0 ||
git_pool_init(&scb->entry_pool, sizeof(git_index_entry), 0) < 0 ||
git_pool_init(&scb->string_pool, 1, 0) < 0 ||
git_iterator_current(iter, &item) < 0)
goto fail;
while (item) {
git_index_entry *clone = git_pool_malloc(&scb->entry_pool, 1);
if (!clone)
goto fail;
memcpy(clone, item, sizeof(git_index_entry));
if (item->path) {
clone->path = git_pool_strdup(&scb->string_pool, item->path);
if (!clone->path)
goto fail;
}
if (git_vector_insert(&scb->entries, clone) < 0)
goto fail;
if (git_iterator_advance(iter, &item) < 0)
goto fail;
}
git_vector_sort(&scb->entries);
iter->cb = (git_iterator_callbacks *)scb;
iter->type = GIT_ITERATOR_TYPE_SPOOLANDSORT;
iter->flags ^= GIT_ITERATOR_IGNORE_CASE;
return 0;
fail:
spoolandsort_iterator__free_callbacks(scb);
return -1;
}
void git_iterator_free(git_iterator *iter) void git_iterator_free(git_iterator *iter)
{ {
if (iter == NULL) if (iter == NULL)
...@@ -1105,110 +1267,94 @@ void git_iterator_free(git_iterator *iter) ...@@ -1105,110 +1267,94 @@ void git_iterator_free(git_iterator *iter)
git__free(iter); git__free(iter);
} }
git_index *git_iterator_index_get_index(git_iterator *iter) int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
{ {
if (iter->type == GIT_ITERATOR_TYPE_INDEX) bool desire_ignore_case = (ignore_case != 0);
return ((index_iterator *)iter)->index;
if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT && if (iterator__ignore_case(iter) == desire_ignore_case)
((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_TYPE_INDEX) return 0;
return ((index_iterator *)iter)->index;
return NULL; 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_iterator_type_t git_iterator_inner_type(git_iterator *iter) git_index *git_iterator_get_index(git_iterator *iter)
{ {
if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT) if (iter->type == GIT_ITERATOR_TYPE_INDEX)
return ((spoolandsort_callbacks *)iter->cb)->orig_type; return ((index_iterator *)iter)->index;
return NULL;
return iter->type;
} }
int git_iterator_current_tree_entry( int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry) const git_tree_entry **tree_entry, git_iterator *iter)
{ {
*tree_entry = (iter->type != GIT_ITERATOR_TYPE_TREE) ? NULL : if (iter->type != GIT_ITERATOR_TYPE_TREE)
tree_iterator__tree_entry((tree_iterator *)iter); *tree_entry = NULL;
else {
tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
*tree_entry = tree_iterator__tree_entry_by_index(tf, tf->current);
}
return 0; return 0;
} }
int git_iterator_current_parent_tree( int git_iterator_current_parent_tree(
const git_tree **tree_ptr,
git_iterator *iter, git_iterator *iter,
const char *parent_path, const char *parent_path)
const git_tree **tree_ptr)
{ {
tree_iterator *ti = (tree_iterator *)iter; tree_iterator *ti = (tree_iterator *)iter;
tree_iterator_frame *tf; tree_iterator_frame *tf;
const char *scan = parent_path; const char *scan = parent_path;
int (*strncomp)(const char *a, const char *b, size_t sz); const git_tree_entry *te;
if (iter->type != GIT_ITERATOR_TYPE_TREE || ti->stack == NULL) *tree_ptr = NULL;
goto notfound;
strncomp = ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) ? if (iter->type != GIT_ITERATOR_TYPE_TREE)
git__strncasecmp : git__strncmp; return 0;
for (tf = ti->tail; tf != NULL; tf = tf->prev) { tf = ti->top;
const git_tree_entry *te;
if (!*scan) { while (*scan) {
*tree_ptr = tf->tree; /* get entry of this parent that child is currently on */
if (!(tf = tf->child) ||
!(te = tree_iterator__tree_entry_by_index(tf, tf->current)) ||
ti->strncomp(scan, te->filename, te->filename_len) != 0)
return 0; return 0;
}
te = git_tree_entry_byindex(tf->tree,
tf->icase_map ? (size_t)tf->icase_map[tf->index] : tf->index);
if (strncomp(scan, te->filename, te->filename_len) != 0)
goto notfound;
scan += te->filename_len; scan += te->filename_len;
if (*scan == '/')
if (*scan) {
if (*scan != '/')
goto notfound;
scan++; scan++;
}
} }
notfound: *tree_ptr = tf->entries[tf->current].tree;
*tree_ptr = NULL;
return 0; return 0;
} }
int git_iterator_current_is_ignored(git_iterator *iter) bool git_iterator_current_is_ignored(git_iterator *iter)
{ {
workdir_iterator *wi = (workdir_iterator *)iter; workdir_iterator *wi = (workdir_iterator *)iter;
if (iter->type != GIT_ITERATOR_TYPE_WORKDIR) if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
return 0; return false;
if (wi->is_ignored != -1) if (wi->is_ignored != -1)
return wi->is_ignored; return (bool)(wi->is_ignored != 0);
if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0) if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0)
wi->is_ignored = 1; wi->is_ignored = true;
return wi->is_ignored; return (bool)wi->is_ignored;
}
int git_iterator_advance_into_directory(
git_iterator *iter, const git_index_entry **entry)
{
workdir_iterator *wi = (workdir_iterator *)iter;
if (iter->type == GIT_ITERATOR_TYPE_WORKDIR &&
wi->entry.path &&
(wi->entry.mode == GIT_FILEMODE_TREE ||
wi->entry.mode == GIT_FILEMODE_COMMIT))
{
if (workdir_iterator__expand_dir(wi) < 0)
/* if error loading or if empty, skip the directory. */
return workdir_iterator__advance(iter, entry);
}
return entry ? git_iterator_current(iter, entry) : 0;
} }
int git_iterator_cmp(git_iterator *iter, const char *path_prefix) int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
...@@ -1216,8 +1362,7 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix) ...@@ -1216,8 +1362,7 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
const git_index_entry *entry; const git_index_entry *entry;
/* a "done" iterator is after every prefix */ /* a "done" iterator is after every prefix */
if (git_iterator_current(iter, &entry) < 0 || if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
entry == NULL)
return 1; return 1;
/* a NULL prefix is after any valid iterator */ /* a NULL prefix is after any valid iterator */
...@@ -1227,7 +1372,7 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix) ...@@ -1227,7 +1372,7 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
return iter->prefixcomp(entry->path, path_prefix); return iter->prefixcomp(entry->path, path_prefix);
} }
int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path) int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
{ {
workdir_iterator *wi = (workdir_iterator *)iter; workdir_iterator *wi = (workdir_iterator *)iter;
...@@ -1238,4 +1383,3 @@ int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path) ...@@ -1238,4 +1383,3 @@ int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path)
return 0; return 0;
} }
...@@ -12,11 +12,6 @@ ...@@ -12,11 +12,6 @@
#include "vector.h" #include "vector.h"
#include "buffer.h" #include "buffer.h"
#define ITERATOR_PREFIXCMP(ITER, STR, PREFIX) \
(((ITER).flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? \
git__prefixcmp_icase((STR), (PREFIX)) : \
git__prefixcmp((STR), (PREFIX)))
typedef struct git_iterator git_iterator; typedef struct git_iterator git_iterator;
typedef enum { typedef enum {
...@@ -24,20 +19,26 @@ typedef enum { ...@@ -24,20 +19,26 @@ typedef enum {
GIT_ITERATOR_TYPE_TREE = 1, GIT_ITERATOR_TYPE_TREE = 1,
GIT_ITERATOR_TYPE_INDEX = 2, GIT_ITERATOR_TYPE_INDEX = 2,
GIT_ITERATOR_TYPE_WORKDIR = 3, GIT_ITERATOR_TYPE_WORKDIR = 3,
GIT_ITERATOR_TYPE_SPOOLANDSORT = 4
} git_iterator_type_t; } git_iterator_type_t;
typedef enum { typedef enum {
GIT_ITERATOR_IGNORE_CASE = (1 << 0), /* ignore_case */ /** ignore case for entry sort order */
GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1), /* force ignore_case off */ GIT_ITERATOR_IGNORE_CASE = (1 << 0),
/** force case sensitivity for entry sort order */
GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1),
/** return tree items in addition to blob items */
GIT_ITERATOR_INCLUDE_TREES = (1 << 2),
/** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
GIT_ITERATOR_DONT_AUTOEXPAND = (1 << 3),
} git_iterator_flag_t; } git_iterator_flag_t;
typedef struct { typedef struct {
int (*current)(git_iterator *, const git_index_entry **); int (*current)(const git_index_entry **, git_iterator *);
int (*at_end)(git_iterator *); int (*advance)(const git_index_entry **, git_iterator *);
int (*advance)(git_iterator *, const git_index_entry **); int (*advance_into)(const git_index_entry **, git_iterator *);
int (*seek)(git_iterator *, const char *prefix); int (*seek)(git_iterator *, const char *prefix);
int (*reset)(git_iterator *, const char *start, const char *end); int (*reset)(git_iterator *, const char *start, const char *end);
int (*at_end)(git_iterator *);
void (*free)(git_iterator *); void (*free)(git_iterator *);
} git_iterator_callbacks; } git_iterator_callbacks;
...@@ -52,86 +53,92 @@ struct git_iterator { ...@@ -52,86 +53,92 @@ struct git_iterator {
}; };
extern int git_iterator_for_nothing( extern int git_iterator_for_nothing(
git_iterator **out, git_iterator_flag_t flags); git_iterator **out,
git_iterator_flag_t flags,
const char *start,
const char *end);
/* tree iterators will match the ignore_case value from the index of the /* tree iterators will match the ignore_case value from the index of the
* repository, unless you override with a non-zero flag value * repository, unless you override with a non-zero flag value
*/ */
extern int git_iterator_for_tree_range( extern int git_iterator_for_tree(
git_iterator **out, git_iterator **out,
git_tree *tree, git_tree *tree,
git_iterator_flag_t flags, git_iterator_flag_t flags,
const char *start, const char *start,
const char *end); const char *end);
GIT_INLINE(int) git_iterator_for_tree(git_iterator **out, git_tree *tree)
{
return git_iterator_for_tree_range(out, tree, 0, NULL, NULL);
}
/* index iterators will take the ignore_case value from the index; the /* index iterators will take the ignore_case value from the index; the
* ignore_case flags are not used * ignore_case flags are not used
*/ */
extern int git_iterator_for_index_range( extern int git_iterator_for_index(
git_iterator **out, git_iterator **out,
git_index *index, git_index *index,
git_iterator_flag_t flags, git_iterator_flag_t flags,
const char *start, const char *start,
const char *end); const char *end);
GIT_INLINE(int) git_iterator_for_index(git_iterator **out, git_index *index)
{
return git_iterator_for_index_range(out, index, 0, NULL, NULL);
}
/* workdir iterators will match the ignore_case value from the index of the /* workdir iterators will match the ignore_case value from the index of the
* repository, unless you override with a non-zero flag value * repository, unless you override with a non-zero flag value
*/ */
extern int git_iterator_for_workdir_range( extern int git_iterator_for_workdir(
git_iterator **out, git_iterator **out,
git_repository *repo, git_repository *repo,
git_iterator_flag_t flags, git_iterator_flag_t flags,
const char *start, const char *start,
const char *end); const char *end);
GIT_INLINE(int) git_iterator_for_workdir(git_iterator **out, git_repository *repo)
{
return git_iterator_for_workdir_range(out, repo, 0, NULL, NULL);
}
extern void git_iterator_free(git_iterator *iter); extern void git_iterator_free(git_iterator *iter);
/* Spool all iterator values, resort with alternative ignore_case value /* Return a git_index_entry structure for the current value the iterator
* and replace callbacks with spoolandsort alternates. * is looking at or NULL if the iterator is at the end.
*/ *
extern int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case); * The entry may noy be fully populated. Tree iterators will only have a
* value mode, OID, and path. Workdir iterators will not have an OID (but
/* Restore original callbacks - not required in most circumstances */ * you can use `git_iterator_current_oid()` to calculate it on demand).
extern void git_iterator_spoolandsort_pop(git_iterator *iter);
/* Entry is not guaranteed to be fully populated. For a tree iterator,
* we will only populate the mode, oid and path, for example. For a workdir
* iterator, we will not populate the oid.
* *
* You do not need to free the entry. It is still "owned" by the iterator. * You do not need to free the entry. It is still "owned" by the iterator.
* Once you call `git_iterator_advance`, then content of the old entry is * Once you call `git_iterator_advance()` then the old entry is no longer
* no longer guaranteed to be valid. * guaranteed to be valid - it may be freed or just overwritten in place.
*/ */
GIT_INLINE(int) git_iterator_current( GIT_INLINE(int) git_iterator_current(
git_iterator *iter, const git_index_entry **entry) const git_index_entry **entry, git_iterator *iter)
{ {
return iter->cb->current(iter, entry); return iter->cb->current(entry, iter);
} }
GIT_INLINE(int) git_iterator_at_end(git_iterator *iter) /**
* Advance to the next item for the iterator.
*
* If GIT_ITERATOR_INCLUDE_TREES is set, this may be a tree item. If
* GIT_ITERATOR_DONT_AUTOEXPAND is set, calling this again when on a tree
* item will skip over all the items under that tree.
*/
GIT_INLINE(int) git_iterator_advance(
const git_index_entry **entry, git_iterator *iter)
{ {
return iter->cb->at_end(iter); return iter->cb->advance(entry, iter);
} }
GIT_INLINE(int) git_iterator_advance( /**
git_iterator *iter, const git_index_entry **entry) * Iterate into a tree item (when GIT_ITERATOR_DONT_AUTOEXPAND is set).
*
* git_iterator_advance() steps through all items being iterated over
* (either with or without trees, depending on GIT_ITERATOR_INCLUDE_TREES),
* but if GIT_ITERATOR_DONT_AUTOEXPAND is set, it will skip to the next
* sibling of a tree instead of going to the first child of the tree. In
* that case, use this function to advance to the first child of the tree.
*
* If the current item is not a tree, this is a no-op.
*
* For working directory iterators only, a tree (i.e. directory) can be
* empty. In that case, this function returns GIT_ENOTFOUND and does not
* advance. That can't happen for tree and index iterators.
*/
GIT_INLINE(int) git_iterator_advance_into(
const git_index_entry **entry, git_iterator *iter)
{ {
return iter->cb->advance(iter, entry); return iter->cb->advance_into(entry, iter);
} }
GIT_INLINE(int) git_iterator_seek( GIT_INLINE(int) git_iterator_seek(
...@@ -146,6 +153,11 @@ GIT_INLINE(int) git_iterator_reset( ...@@ -146,6 +153,11 @@ GIT_INLINE(int) git_iterator_reset(
return iter->cb->reset(iter, start, end); return iter->cb->reset(iter, start, end);
} }
GIT_INLINE(int) git_iterator_at_end(git_iterator *iter)
{
return iter->cb->at_end(iter);
}
GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter) GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter)
{ {
return iter->type; return iter->type;
...@@ -166,47 +178,28 @@ GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter) ...@@ -166,47 +178,28 @@ GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter)
return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0); return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0);
} }
extern int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case);
extern int git_iterator_current_tree_entry( extern int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry); const git_tree_entry **entry_out, git_iterator *iter);
extern int git_iterator_current_parent_tree( extern int git_iterator_current_parent_tree(
git_iterator *iter, const char *parent_path, const git_tree **tree_ptr); const git_tree **tree_out, git_iterator *iter, const char *parent_path);
extern int git_iterator_current_is_ignored(git_iterator *iter); extern bool git_iterator_current_is_ignored(git_iterator *iter);
/**
* Iterate into a workdir directory.
*
* Workdir iterators do not automatically descend into directories (so that
* when comparing two iterator entries you can detect a newly created
* directory in the workdir). As a result, you may get S_ISDIR items from
* a workdir iterator. If you wish to iterate over the contents of the
* directories you encounter, then call this function when you encounter
* a directory.
*
* If there are no files in the directory, this will end up acting like a
* regular advance and will skip past the directory, so you should be
* prepared for that case.
*
* On non-workdir iterators or if not pointing at a directory, this is a
* no-op and will not advance the iterator.
*/
extern int git_iterator_advance_into_directory(
git_iterator *iter, const git_index_entry **entry);
extern int git_iterator_cmp( extern int git_iterator_cmp(
git_iterator *iter, const char *path_prefix); git_iterator *iter, const char *path_prefix);
/** /**
* Get the full path of the current item from a workdir iterator. * Get full path of the current item from a workdir iterator. This will
* This will return NULL for a non-workdir iterator. * return NULL for a non-workdir iterator. The git_buf is still owned by
* the iterator; this is exposed just for efficiency.
*/ */
extern int git_iterator_current_workdir_path( extern int git_iterator_current_workdir_path(
git_iterator *iter, git_buf **path); git_buf **path, git_iterator *iter);
extern git_index *git_iterator_index_get_index(git_iterator *iter);
extern git_iterator_type_t git_iterator_inner_type(git_iterator *iter); /* Return index pointer if index iterator, else NULL */
extern git_index *git_iterator_get_index(git_iterator *iter);
#endif #endif
...@@ -625,7 +625,7 @@ int git_note_iterator_new( ...@@ -625,7 +625,7 @@ int git_note_iterator_new(
if (error < 0) if (error < 0)
goto cleanup; goto cleanup;
if ((error = git_iterator_for_tree(it, tree)) < 0) if ((error = git_iterator_for_tree(it, tree, 0, NULL, NULL)) < 0)
git_iterator_free(*it); git_iterator_free(*it);
cleanup: cleanup:
...@@ -643,7 +643,7 @@ int git_note_next( ...@@ -643,7 +643,7 @@ int git_note_next(
int error; int error;
const git_index_entry *item; const git_index_entry *item;
if ((error = git_iterator_current(it, &item)) < 0) if ((error = git_iterator_current(&item, it)) < 0)
goto exit; goto exit;
if (item != NULL) { if (item != NULL) {
...@@ -651,7 +651,7 @@ int git_note_next( ...@@ -651,7 +651,7 @@ int git_note_next(
error = process_entry_path(item->path, annotated_id); error = process_entry_path(item->path, annotated_id);
if (error >= 0) if (error >= 0)
error = git_iterator_advance(it, NULL); error = git_iterator_advance(NULL, it);
} else { } else {
error = GIT_ITEROVER; error = GIT_ITEROVER;
} }
......
...@@ -32,14 +32,13 @@ typedef int git_file; ...@@ -32,14 +32,13 @@ typedef int git_file;
* Standard POSIX Methods * Standard POSIX Methods
* *
* All the methods starting with the `p_` prefix are * All the methods starting with the `p_` prefix are
* direct ports of the standard POSIX methods. * direct ports of the standard POSIX methods.
* *
* Some of the methods are slightly wrapped to provide * Some of the methods are slightly wrapped to provide
* saner defaults. Some of these methods are emulated * saner defaults. Some of these methods are emulated
* in Windows platforns. * in Windows platforns.
* *
* Use your manpages to check the docs on these. * Use your manpages to check the docs on these.
* Straightforward
*/ */
extern int p_read(git_file fd, void *buf, size_t cnt); extern int p_read(git_file fd, void *buf, size_t cnt);
......
...@@ -1135,10 +1135,10 @@ static int load_submodule_config_from_index( ...@@ -1135,10 +1135,10 @@ static int load_submodule_config_from_index(
const git_index_entry *entry; const git_index_entry *entry;
if ((error = git_repository_index__weakptr(&index, repo)) < 0 || if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
(error = git_iterator_for_index(&i, index)) < 0) (error = git_iterator_for_index(&i, index, 0, NULL, NULL)) < 0)
return error; return error;
error = git_iterator_current(i, &entry); error = git_iterator_current(&entry, i);
while (!error && entry != NULL) { while (!error && entry != NULL) {
...@@ -1154,7 +1154,7 @@ static int load_submodule_config_from_index( ...@@ -1154,7 +1154,7 @@ static int load_submodule_config_from_index(
git_oid_cpy(gitmodules_oid, &entry->oid); git_oid_cpy(gitmodules_oid, &entry->oid);
} }
error = git_iterator_advance(i, &entry); error = git_iterator_advance(&entry, i);
} }
git_iterator_free(i); git_iterator_free(i);
...@@ -1173,12 +1173,12 @@ static int load_submodule_config_from_head( ...@@ -1173,12 +1173,12 @@ static int load_submodule_config_from_head(
if ((error = git_repository_head_tree(&head, repo)) < 0) if ((error = git_repository_head_tree(&head, repo)) < 0)
return error; return error;
if ((error = git_iterator_for_tree(&i, head)) < 0) { if ((error = git_iterator_for_tree(&i, head, 0, NULL, NULL)) < 0) {
git_tree_free(head); git_tree_free(head);
return error; return error;
} }
error = git_iterator_current(i, &entry); error = git_iterator_current(&entry, i);
while (!error && entry != NULL) { while (!error && entry != NULL) {
...@@ -1195,7 +1195,7 @@ static int load_submodule_config_from_head( ...@@ -1195,7 +1195,7 @@ static int load_submodule_config_from_head(
git_oid_cpy(gitmodules_oid, &entry->oid); git_oid_cpy(gitmodules_oid, &entry->oid);
} }
error = git_iterator_advance(i, &entry); error = git_iterator_advance(&entry, i);
} }
git_iterator_free(i); git_iterator_free(i);
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#endif #endif
static int binsearch( static int binsearch(
void **dst, const void *x, size_t size, git__tsort_r_cmp cmp, void *payload) void **dst, const void *x, size_t size, git__sort_r_cmp cmp, void *payload)
{ {
int l, c, r; int l, c, r;
void *lx, *cx; void *lx, *cx;
...@@ -71,7 +71,7 @@ static int binsearch( ...@@ -71,7 +71,7 @@ static int binsearch(
/* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */ /* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
static void bisort( static void bisort(
void **dst, size_t start, size_t size, git__tsort_r_cmp cmp, void *payload) void **dst, size_t start, size_t size, git__sort_r_cmp cmp, void *payload)
{ {
size_t i; size_t i;
void *x; void *x;
...@@ -102,7 +102,7 @@ struct tsort_run { ...@@ -102,7 +102,7 @@ struct tsort_run {
struct tsort_store { struct tsort_store {
size_t alloc; size_t alloc;
git__tsort_r_cmp cmp; git__sort_r_cmp cmp;
void *payload; void *payload;
void **storage; void **storage;
}; };
...@@ -334,7 +334,7 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr, ...@@ -334,7 +334,7 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr,
while (0) while (0)
void git__tsort_r( void git__tsort_r(
void **dst, size_t size, git__tsort_r_cmp cmp, void *payload) void **dst, size_t size, git__sort_r_cmp cmp, void *payload)
{ {
struct tsort_store _store, *store = &_store; struct tsort_store _store, *store = &_store;
struct tsort_run run_stack[128]; struct tsort_run run_stack[128];
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define INCLUDE_posix__w32_h__ #define INCLUDE_posix__w32_h__
#include <stdio.h> #include <stdio.h>
#include <sys/param.h>
#define p_lstat(p,b) lstat(p,b) #define p_lstat(p,b) lstat(p,b)
#define p_readlink(a, b, c) readlink(a, b, c) #define p_readlink(a, b, c) readlink(a, b, c)
......
...@@ -607,3 +607,55 @@ size_t git__unescape(char *str) ...@@ -607,3 +607,55 @@ size_t git__unescape(char *str)
return (pos - str); return (pos - str);
} }
#if defined(GIT_WIN32) || defined(BSD)
typedef struct {
git__sort_r_cmp cmp;
void *payload;
} git__qsort_r_glue;
static int GIT_STDLIB_CALL git__qsort_r_glue_cmp(
void *payload, const void *a, const void *b)
{
git__qsort_r_glue *glue = payload;
return glue->cmp(a, b, glue->payload);
}
#endif
void git__qsort_r(
void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload)
{
#if defined(__MINGW32__)
git__insertsort_r(els, nel, elsize, NULL, cmp, payload);
#elif defined(GIT_WIN32)
git__qsort_r_glue glue = { cmp, payload };
qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
#elif defined(BSD)
git__qsort_r_glue glue = { cmp, payload };
qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
#else
qsort_r(els, nel, elsize, cmp, payload);
#endif
}
void git__insertsort_r(
void *els, size_t nel, size_t elsize, void *swapel,
git__sort_r_cmp cmp, void *payload)
{
uint8_t *base = els, *end = els + nel * elsize;
uint8_t *i, *j;
bool freeswap = !swapel;
if (freeswap)
swapel = git__malloc(elsize);
for (i = base + elsize; i < end; i += elsize)
for (j = i; j > base && cmp(j, j - elsize, payload) < 0; j -= elsize) {
memcpy(swapel, j, elsize);
memcpy(j, j - elsize, elsize);
memcpy(j - elsize, swapel, elsize);
}
if (freeswap)
git__free(swapel);
}
...@@ -146,11 +146,17 @@ typedef int (*git__tsort_cmp)(const void *a, const void *b); ...@@ -146,11 +146,17 @@ typedef int (*git__tsort_cmp)(const void *a, const void *b);
extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp); extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
typedef int (*git__tsort_r_cmp)(const void *a, const void *b, void *payload); typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload);
extern void git__tsort_r( extern void git__tsort_r(
void **dst, size_t size, git__tsort_r_cmp cmp, void *payload); void **dst, size_t size, git__sort_r_cmp cmp, void *payload);
extern void git__qsort_r(
void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload);
extern void git__insertsort_r(
void *els, size_t nel, size_t elsize, void *swapel,
git__sort_r_cmp cmp, void *payload);
/** /**
* @param position If non-NULL, this will be set to the position where the * @param position If non-NULL, this will be set to the position where the
......
...@@ -35,26 +35,26 @@ static void tree_iterator_test( ...@@ -35,26 +35,26 @@ static void tree_iterator_test(
git_repository *repo = cl_git_sandbox_init(sandbox); git_repository *repo = cl_git_sandbox_init(sandbox);
cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); cl_assert(t = resolve_commit_oid_to_tree(repo, treeish));
cl_git_pass(git_iterator_for_tree_range( cl_git_pass(git_iterator_for_tree(
&i, t, GIT_ITERATOR_DONT_IGNORE_CASE, start, end)); &i, t, GIT_ITERATOR_DONT_IGNORE_CASE, start, end));
/* test loop */ /* test loop */
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
if (expected_values != NULL) if (expected_values != NULL)
cl_assert_equal_s(expected_values[count], entry->path); cl_assert_equal_s(expected_values[count], entry->path);
count++; count++;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
/* test reset */ /* test reset */
cl_git_pass(git_iterator_reset(i, NULL, NULL)); cl_git_pass(git_iterator_reset(i, NULL, NULL));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
if (expected_values != NULL) if (expected_values != NULL)
cl_assert_equal_s(expected_values[count_post_reset], entry->path); cl_assert_equal_s(expected_values[count_post_reset], entry->path);
count_post_reset++; count_post_reset++;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
git_iterator_free(i); git_iterator_free(i);
...@@ -261,30 +261,30 @@ static void check_tree_entry( ...@@ -261,30 +261,30 @@ static void check_tree_entry(
const git_tree *tree; const git_tree *tree;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
cl_git_pass(git_iterator_current_tree_entry(i, &te)); cl_git_pass(git_iterator_current_tree_entry(&te, i));
cl_assert(te); cl_assert(te);
cl_assert(git_oid_streq(&te->oid, oid) == 0); cl_assert(git_oid_streq(&te->oid, oid) == 0);
cl_git_pass(git_iterator_current(i, &ie)); cl_git_pass(git_iterator_current(&ie, i));
cl_git_pass(git_buf_sets(&path, ie->path)); cl_git_pass(git_buf_sets(&path, ie->path));
if (oid_p) { if (oid_p) {
git_buf_rtruncate_at_char(&path, '/'); git_buf_rtruncate_at_char(&path, '/');
cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr));
cl_assert(tree); cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0);
} }
if (oid_pp) { if (oid_pp) {
git_buf_rtruncate_at_char(&path, '/'); git_buf_rtruncate_at_char(&path, '/');
cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr));
cl_assert(tree); cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0);
} }
if (oid_ppp) { if (oid_ppp) {
git_buf_rtruncate_at_char(&path, '/'); git_buf_rtruncate_at_char(&path, '/');
cl_git_pass(git_iterator_current_parent_tree(i, path.ptr, &tree)); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr));
cl_assert(tree); cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0);
} }
...@@ -305,9 +305,9 @@ void test_diff_iterator__tree_special_functions(void) ...@@ -305,9 +305,9 @@ void test_diff_iterator__tree_special_functions(void)
repo, "24fa9a9fc4e202313e24b648087495441dab432b"); repo, "24fa9a9fc4e202313e24b648087495441dab432b");
cl_assert(t != NULL); cl_assert(t != NULL);
cl_git_pass(git_iterator_for_tree_range( cl_git_pass(git_iterator_for_tree(
&i, t, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)); &i, t, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
if (strcmp(entry->path, "sub/file") == 0) { if (strcmp(entry->path, "sub/file") == 0) {
...@@ -339,7 +339,7 @@ void test_diff_iterator__tree_special_functions(void) ...@@ -339,7 +339,7 @@ void test_diff_iterator__tree_special_functions(void)
rootoid, NULL); rootoid, NULL);
} }
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
cl_assert_equal_i(4, cases); cl_assert_equal_i(4, cases);
...@@ -364,8 +364,8 @@ static void index_iterator_test( ...@@ -364,8 +364,8 @@ static void index_iterator_test(
git_repository *repo = cl_git_sandbox_init(sandbox); git_repository *repo = cl_git_sandbox_init(sandbox);
cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end)); cl_git_pass(git_iterator_for_index(&i, index, 0, start, end));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
if (expected_names != NULL) if (expected_names != NULL)
...@@ -378,7 +378,7 @@ static void index_iterator_test( ...@@ -378,7 +378,7 @@ static void index_iterator_test(
} }
count++; count++;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
git_iterator_free(i); git_iterator_free(i);
...@@ -538,14 +538,15 @@ static void workdir_iterator_test( ...@@ -538,14 +538,15 @@ static void workdir_iterator_test(
int count = 0, count_all = 0, count_all_post_reset = 0; int count = 0, count_all = 0, count_all_post_reset = 0;
git_repository *repo = cl_git_sandbox_init(sandbox); git_repository *repo = cl_git_sandbox_init(sandbox);
cl_git_pass(git_iterator_for_workdir_range(&i, repo, 0, start, end)); cl_git_pass(git_iterator_for_workdir(
cl_git_pass(git_iterator_current(i, &entry)); &i, repo, GIT_ITERATOR_DONT_AUTOEXPAND, start, end));
cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
int ignored = git_iterator_current_is_ignored(i); int ignored = git_iterator_current_is_ignored(i);
if (S_ISDIR(entry->mode)) { if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into_directory(i, &entry)); cl_git_pass(git_iterator_advance_into(&entry, i));
continue; continue;
} }
...@@ -559,22 +560,22 @@ static void workdir_iterator_test( ...@@ -559,22 +560,22 @@ static void workdir_iterator_test(
count++; count++;
count_all++; count_all++;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
cl_git_pass(git_iterator_reset(i, NULL, NULL)); cl_git_pass(git_iterator_reset(i, NULL, NULL));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
while (entry != NULL) { while (entry != NULL) {
if (S_ISDIR(entry->mode)) { if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into_directory(i, &entry)); cl_git_pass(git_iterator_advance_into(&entry, i));
continue; continue;
} }
if (expected_names != NULL) if (expected_names != NULL)
cl_assert_equal_s( cl_assert_equal_s(
expected_names[count_all_post_reset], entry->path); expected_names[count_all_post_reset], entry->path);
count_all_post_reset++; count_all_post_reset++;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
git_iterator_free(i); git_iterator_free(i);
...@@ -735,9 +736,9 @@ void test_diff_iterator__workdir_builtin_ignores(void) ...@@ -735,9 +736,9 @@ void test_diff_iterator__workdir_builtin_ignores(void)
cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777));
cl_git_mkfile("attr/sub/.git", "whatever"); cl_git_mkfile("attr/sub/.git", "whatever");
cl_git_pass( cl_git_pass(git_iterator_for_workdir(
git_iterator_for_workdir_range(&i, repo, 0, "dir", "sub/sub/file")); &i, repo, GIT_ITERATOR_DONT_AUTOEXPAND, "dir", "sub/sub/file"));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) { for (idx = 0; entry != NULL; ++idx) {
int ignored = git_iterator_current_is_ignored(i); int ignored = git_iterator_current_is_ignored(i);
...@@ -746,9 +747,9 @@ void test_diff_iterator__workdir_builtin_ignores(void) ...@@ -746,9 +747,9 @@ void test_diff_iterator__workdir_builtin_ignores(void)
cl_assert_(ignored == expected[idx].ignored, expected[idx].path); cl_assert_(ignored == expected[idx].ignored, expected[idx].path);
if (!ignored && S_ISDIR(entry->mode)) if (!ignored && S_ISDIR(entry->mode))
cl_git_pass(git_iterator_advance_into_directory(i, &entry)); cl_git_pass(git_iterator_advance_into(&entry, i));
else else
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
cl_assert(expected[idx].path == NULL); cl_assert(expected[idx].path == NULL);
...@@ -764,17 +765,14 @@ static void check_wd_first_through_third_range( ...@@ -764,17 +765,14 @@ static void check_wd_first_through_third_range(
int idx; int idx;
static const char *expected[] = { "FIRST", "second", "THIRD", NULL }; static const char *expected[] = { "FIRST", "second", "THIRD", NULL };
cl_git_pass(git_iterator_for_workdir_range( cl_git_pass(git_iterator_for_workdir(
&i, repo, GIT_ITERATOR_IGNORE_CASE, start, end)); &i, repo, GIT_ITERATOR_IGNORE_CASE, start, end));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) { for (idx = 0; entry != NULL; ++idx) {
cl_assert_equal_s(expected[idx], entry->path); cl_assert_equal_s(expected[idx], entry->path);
if (S_ISDIR(entry->mode)) cl_git_pass(git_iterator_advance(&entry, i));
cl_git_pass(git_iterator_advance_into_directory(i, &entry));
else
cl_git_pass(git_iterator_advance(i, &entry));
} }
cl_assert(expected[idx] == NULL); cl_assert(expected[idx] == NULL);
...@@ -817,16 +815,16 @@ static void check_tree_range( ...@@ -817,16 +815,16 @@ static void check_tree_range(
cl_git_pass(git_repository_head_tree(&head, repo)); cl_git_pass(git_repository_head_tree(&head, repo));
cl_git_pass(git_iterator_for_tree_range( cl_git_pass(git_iterator_for_tree(
&i, head, &i, head,
ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE, ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE,
start, end)); start, end));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
for (count = 0; entry != NULL; ) { for (count = 0; entry != NULL; ) {
++count; ++count;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count, count);
...@@ -882,15 +880,15 @@ static void check_index_range( ...@@ -882,15 +880,15 @@ static void check_index_range(
if (ignore_case != is_ignoring_case) if (ignore_case != is_ignoring_case)
cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE)); cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE));
cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end)); cl_git_pass(git_iterator_for_index(&i, index, 0, start, end));
cl_assert(git_iterator_ignore_case(i) == ignore_case); cl_assert(git_iterator_ignore_case(i) == ignore_case);
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(&entry, i));
for (count = 0; entry != NULL; ) { for (count = 0; entry != NULL; ) {
++count; ++count;
cl_git_pass(git_iterator_advance(i, &entry)); cl_git_pass(git_iterator_advance(&entry, i));
} }
cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count, count);
......
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include <stdarg.h>
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 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;
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; }
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_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);
}
cl_git_pass(git_iterator_advance(&entry, i));
if (++count > expected_flat)
break;
}
cl_assert_equal_i(expected_flat, count);
cl_git_pass(git_iterator_reset(i, NULL, NULL));
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)
cl_git_pass(git_iterator_advance_into(&entry, i));
else
cl_git_pass(git_iterator_advance(&entry, i));
if (++count > expected_total)
break;
}
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_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, index, 0, NULL, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
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_index *index;
unsigned 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 */
cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D"));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z"));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
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 */
cl_git_pass(git_iterator_for_index(&i, index, 0, "c", "k/D"));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(&i, index, 0, "k", "k/Z"));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_index(
&i, index, GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
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_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, 0, NULL, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_tree(
&i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_tree(
&i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
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_flag_t flag;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_head_tree(&head, g_repo));
flag = GIT_ITERATOR_DONT_IGNORE_CASE;
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D"));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z"));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
flag = GIT_ITERATOR_IGNORE_CASE;
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, flag, "c", "k/D"));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(&i, head, flag, "k", "k/Z"));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
}
void test_repo_iterator__tree_more(void)
{
git_iterator *i;
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, 0, NULL, NULL));
expect_iterator_items(i, 12, expect_basic, 12, expect_basic);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_tree(
&i, head, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 13, expect_trees, 13, expect_trees);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_tree(
&i, head, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
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_buf name = GIT_BUF_INIT;
va_list arglist;
cl_git_pass(git_treebuilder_create(&builder, 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, repo, 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;
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));
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 4, expect_cs, 4, expect_cs);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
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;
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));
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 6, expect_cs, 6, expect_cs);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
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;
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));
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 32, expect_cs, 32, expect_cs);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 17, expect_ci, 17, expect_ci);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(
&i, tree, GIT_ITERATOR_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
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;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, 0, NULL, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
}
void test_repo_iterator__workdir_icase(void)
{
git_iterator *i;
git_iterator_flag_t flag;
g_repo = cl_git_sandbox_init("icase");
flag = GIT_ITERATOR_DONT_IGNORE_CASE;
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D"));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z"));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
flag = GIT_ITERATOR_IGNORE_CASE;
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "c", "k/D"));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(&i, g_repo, flag, "k", "k/Z"));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "c", "k/D"));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_INCLUDE_TREES, "k", "k/Z"));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "c", "k/D"));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
}
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = false
Unnamed repository; edit this file 'description' to name the repository.
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
0000000000000000000000000000000000000000 76d6e1d231b1085fcce151427e9899335de74be6 Russell Belfer <rb@github.com> 1359157123 -0800 commit (initial): initial commit
0000000000000000000000000000000000000000 76d6e1d231b1085fcce151427e9899335de74be6 Russell Belfer <rb@github.com> 1359157123 -0800 commit (initial): initial commit
76d6e1d231b1085fcce151427e9899335de74be6
...@@ -274,6 +274,7 @@ void test_status_worktree__issue_592(void) ...@@ -274,6 +274,7 @@ void test_status_worktree__issue_592(void)
repo = cl_git_sandbox_init("issue_592"); repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "l.txt")); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "l.txt"));
cl_git_pass(p_unlink(git_buf_cstr(&path))); cl_git_pass(p_unlink(git_buf_cstr(&path)));
cl_assert(!git_path_exists("issue_592/l.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "l.txt")); cl_git_pass(git_status_foreach(repo, cb_status__check_592, "l.txt"));
...@@ -288,6 +289,7 @@ void test_status_worktree__issue_592_2(void) ...@@ -288,6 +289,7 @@ void test_status_worktree__issue_592_2(void)
repo = cl_git_sandbox_init("issue_592"); repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c/a.txt")); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c/a.txt"));
cl_git_pass(p_unlink(git_buf_cstr(&path))); cl_git_pass(p_unlink(git_buf_cstr(&path)));
cl_assert(!git_path_exists("issue_592/c/a.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt")); cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt"));
...@@ -303,6 +305,7 @@ void test_status_worktree__issue_592_3(void) ...@@ -303,6 +305,7 @@ void test_status_worktree__issue_592_3(void)
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c")); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c"));
cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES));
cl_assert(!git_path_exists("issue_592/c/a.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt")); cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment