Commit 134d8c91 by Russell Belfer

Update iterator API with flags for ignore_case

This changes the iterator API so that flags can be passed in to
the constructor functions to control the ignore_case behavior.
At this point, the flags are not supported on tree iterators (i.e.
there is no functional change over the old API), but the API
changes are all made to accomodate this.

By the way, I went with a flags parameter because in the future
I have a couple of other ideas for iterator flags that will make
it easier to fix some diff/status/checkout bugs.
parent 5c8bb98c
...@@ -224,7 +224,7 @@ static int checkout_action_wd_only( ...@@ -224,7 +224,7 @@ static int checkout_action_wd_only(
if (!git_pathspec_match_path( if (!git_pathspec_match_path(
pathspec, wd->path, pathspec, wd->path,
(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0, (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
workdir->ignore_case)) git_iterator_ignore_case(workdir)))
return 0; return 0;
/* check if item is tracked in the index but not in the checkout diff */ /* check if item is tracked in the index but not in the checkout diff */
...@@ -1130,7 +1130,7 @@ static int checkout_data_init( ...@@ -1130,7 +1130,7 @@ 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_INDEX) { if (git_iterator_inner_type(target) == GIT_ITERATOR_TYPE_INDEX) {
/* if we are iterating over the index, don't reload */ /* if we are iterating over the index, don't reload */
data->index = git_iterator_index_get_index(target); data->index = git_iterator_index_get_index(target);
GIT_REFCOUNT_INC(data->index); GIT_REFCOUNT_INC(data->index);
...@@ -1208,6 +1208,7 @@ int git_checkout_iterator( ...@@ -1208,6 +1208,7 @@ int git_checkout_iterator(
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
uint32_t *actions = NULL; uint32_t *actions = NULL;
size_t *counts = NULL; size_t *counts = NULL;
git_iterator_flag_t iterflags = 0;
/* initialize structures and options */ /* initialize structures and options */
error = checkout_data_init(&data, target, opts); error = checkout_data_init(&data, target, opts);
...@@ -1228,18 +1229,21 @@ int git_checkout_iterator( ...@@ -1228,18 +1229,21 @@ int git_checkout_iterator(
diff_opts.pathspec = data.opts.paths; diff_opts.pathspec = data.opts.paths;
/* set up iterators */ /* set up iterators */
iterflags = git_iterator_ignore_case(target) ?
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_range(
&workdir, data.repo, data.pfx, data.pfx)) < 0 || &workdir, data.repo, iterflags, data.pfx, data.pfx)) < 0 ||
(error = git_iterator_for_tree_range( (error = git_iterator_for_tree_range(
&baseline, data.opts.baseline, 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 */ /* Handle case insensitivity for baseline if necessary */
if (workdir->ignore_case && !baseline->ignore_case) { if (git_iterator_ignore_case(workdir) != git_iterator_ignore_case(baseline))
if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0) if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0)
goto cleanup; 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.
......
...@@ -418,7 +418,7 @@ static int maybe_modified( ...@@ -418,7 +418,7 @@ static int maybe_modified(
git_delta_t status = GIT_DELTA_MODIFIED; git_delta_t status = GIT_DELTA_MODIFIED;
unsigned int omode = oitem->mode; unsigned int omode = oitem->mode;
unsigned int nmode = nitem->mode; unsigned int nmode = nitem->mode;
bool new_is_workdir = (new_iter->type == GIT_ITERATOR_WORKDIR); bool new_is_workdir = (new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
GIT_UNUSED(old_iter); GIT_UNUSED(old_iter);
...@@ -556,7 +556,9 @@ static int diff_list_init_from_iterators( ...@@ -556,7 +556,9 @@ static int diff_list_init_from_iterators(
/* Use case-insensitive compare if either iterator has /* Use case-insensitive compare if either iterator has
* the ignore_case bit set */ * the ignore_case bit set */
if (!old_iter->ignore_case && !new_iter->ignore_case) { if (!git_iterator_ignore_case(old_iter) &&
!git_iterator_ignore_case(new_iter))
{
diff->opts.flags &= ~GIT_DIFF_DELTAS_ARE_ICASE; diff->opts.flags &= ~GIT_DIFF_DELTAS_ARE_ICASE;
diff->strcomp = git__strcmp; diff->strcomp = git__strcmp;
...@@ -714,7 +716,7 @@ int git_diff__from_iterators( ...@@ -714,7 +716,7 @@ int git_diff__from_iterators(
else if (git_iterator_current_is_ignored(new_iter)) else if (git_iterator_current_is_ignored(new_iter))
delta_type = GIT_DELTA_IGNORED; delta_type = GIT_DELTA_IGNORED;
else if (new_iter->type != GIT_ITERATOR_WORKDIR) else if (new_iter->type != GIT_ITERATOR_TYPE_WORKDIR)
delta_type = GIT_DELTA_ADDED; delta_type = GIT_DELTA_ADDED;
if (diff_delta__from_one(diff, delta_type, nitem) < 0) if (diff_delta__from_one(diff, delta_type, nitem) < 0)
...@@ -786,8 +788,8 @@ int git_diff_tree_to_tree( ...@@ -786,8 +788,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, pfx, pfx), git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
git_iterator_for_tree_range(&b, new_tree, pfx, pfx) git_iterator_for_tree_range(&b, new_tree, 0, pfx, pfx)
); );
return error; return error;
...@@ -808,8 +810,8 @@ int git_diff_tree_to_index( ...@@ -808,8 +810,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, pfx, pfx), git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
git_iterator_for_index_range(&b, index, pfx, pfx) git_iterator_for_index_range(&b, index, 0, pfx, pfx)
); );
return error; return error;
...@@ -829,8 +831,8 @@ int git_diff_index_to_workdir( ...@@ -829,8 +831,8 @@ int git_diff_index_to_workdir(
return error; return error;
DIFF_FROM_ITERATORS( DIFF_FROM_ITERATORS(
git_iterator_for_index_range(&a, index, pfx, pfx), git_iterator_for_index_range(&a, index, 0, pfx, pfx),
git_iterator_for_workdir_range(&b, repo, pfx, pfx) git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx)
); );
return error; return error;
...@@ -848,8 +850,8 @@ int git_diff_tree_to_workdir( ...@@ -848,8 +850,8 @@ 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, pfx, pfx), git_iterator_for_tree_range(&a, old_tree, 0, pfx, pfx),
git_iterator_for_workdir_range(&b, repo, pfx, pfx) git_iterator_for_workdir_range(&b, repo, 0, pfx, pfx)
); );
return error; return error;
......
...@@ -495,7 +495,7 @@ static void diff_patch_init( ...@@ -495,7 +495,7 @@ static void diff_patch_init(
patch->old_src = patch->diff->old_src; patch->old_src = patch->diff->old_src;
patch->new_src = patch->diff->new_src; patch->new_src = patch->diff->new_src;
} else { } else {
patch->old_src = patch->new_src = GIT_ITERATOR_TREE; patch->old_src = patch->new_src = GIT_ITERATOR_TYPE_TREE;
} }
} }
...@@ -578,7 +578,7 @@ static int diff_patch_load( ...@@ -578,7 +578,7 @@ static int diff_patch_load(
*/ */
if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
patch->old_src == GIT_ITERATOR_WORKDIR) { patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_workdir_content( if ((error = get_workdir_content(
ctxt, delta, &delta->old_file, &patch->old_data)) < 0) ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
goto cleanup; goto cleanup;
...@@ -587,7 +587,7 @@ static int diff_patch_load( ...@@ -587,7 +587,7 @@ static int diff_patch_load(
} }
if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
patch->new_src == GIT_ITERATOR_WORKDIR) { patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_workdir_content( if ((error = get_workdir_content(
ctxt, delta, &delta->new_file, &patch->new_data)) < 0) ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
goto cleanup; goto cleanup;
...@@ -596,7 +596,7 @@ static int diff_patch_load( ...@@ -596,7 +596,7 @@ static int diff_patch_load(
} }
if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
patch->old_src != GIT_ITERATOR_WORKDIR) { patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_blob_content( if ((error = get_blob_content(
ctxt, delta, &delta->old_file, ctxt, delta, &delta->old_file,
&patch->old_data, &patch->old_blob)) < 0) &patch->old_data, &patch->old_blob)) < 0)
...@@ -606,7 +606,7 @@ static int diff_patch_load( ...@@ -606,7 +606,7 @@ static int diff_patch_load(
} }
if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 && if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
patch->new_src != GIT_ITERATOR_WORKDIR) { patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_blob_content( if ((error = get_blob_content(
ctxt, delta, &delta->new_file, ctxt, delta, &delta->new_file,
&patch->new_data, &patch->new_blob)) < 0) &patch->new_data, &patch->new_blob)) < 0)
......
...@@ -24,12 +24,11 @@ ...@@ -24,12 +24,11 @@
#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \ #define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) 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_ ## 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.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; \
(P)->base.ignore_case = false; \
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; } \
} while (0) } while (0)
...@@ -54,6 +53,31 @@ static int iterator__reset_range( ...@@ -54,6 +53,31 @@ static int iterator__reset_range(
return 0; return 0;
} }
static int iterator_update_ignore_case(
git_iterator *iter,
git_iterator_flag_t flags)
{
int error = 0, ignore_case = -1;
if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
ignore_case = true;
else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0)
ignore_case = false;
else {
git_index *index;
if (!(error = git_repository_index__weakptr(&index, iter->repo)))
ignore_case = (index->ignore_case != false);
}
if (ignore_case > 0)
iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
else if (ignore_case == 0)
iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);
return error;
}
static int empty_iterator__no_item( static int empty_iterator__no_item(
git_iterator *iter, const git_index_entry **entry) git_iterator *iter, const git_index_entry **entry)
{ {
...@@ -91,13 +115,14 @@ typedef struct { ...@@ -91,13 +115,14 @@ typedef struct {
git_iterator_callbacks cb; git_iterator_callbacks cb;
} empty_iterator; } empty_iterator;
int git_iterator_for_nothing(git_iterator **iter) int git_iterator_for_nothing(git_iterator **iter, git_iterator_flag_t flags)
{ {
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_EMPTY; i->base.type = GIT_ITERATOR_TYPE_EMPTY;
i->base.cb = &i->cb; i->base.cb = &i->cb;
i->base.flags = flags;
i->cb.current = empty_iterator__no_item; i->cb.current = empty_iterator__no_item;
i->cb.at_end = empty_iterator__at_end; i->cb.at_end = empty_iterator__at_end;
i->cb.advance = empty_iterator__no_item; i->cb.advance = empty_iterator__no_item;
...@@ -349,6 +374,7 @@ static int tree_iterator__reset( ...@@ -349,6 +374,7 @@ static int tree_iterator__reset(
int git_iterator_for_tree_range( int git_iterator_for_tree_range(
git_iterator **iter, git_iterator **iter,
git_tree *tree, git_tree *tree,
git_iterator_flag_t flags,
const char *start, const char *start,
const char *end) const char *end)
{ {
...@@ -356,7 +382,7 @@ int git_iterator_for_tree_range( ...@@ -356,7 +382,7 @@ int git_iterator_for_tree_range(
tree_iterator *ti; tree_iterator *ti;
if (tree == NULL) if (tree == NULL)
return git_iterator_for_nothing(iter); return git_iterator_for_nothing(iter, flags);
if ((error = git_tree__dup(&tree, tree)) < 0) if ((error = git_tree__dup(&tree, tree)) < 0)
return error; return error;
...@@ -364,13 +390,23 @@ int git_iterator_for_tree_range( ...@@ -364,13 +390,23 @@ int git_iterator_for_tree_range(
ITERATOR_BASE_INIT(ti, tree, TREE); ITERATOR_BASE_INIT(ti, tree, TREE);
ti->base.repo = git_tree_owner(tree); ti->base.repo = git_tree_owner(tree);
if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0)
goto fail;
/* TODO: implement icase support natively in tree iterators */
ti->base.flags = (ti->base.flags & ~GIT_ITERATOR_IGNORE_CASE);
ti->stack = ti->tail = tree_iterator__alloc_frame(tree, ti->base.start); ti->stack = ti->tail = tree_iterator__alloc_frame(tree, ti->base.start);
if ((error = tree_iterator__expand_tree(ti)) < 0) if ((error = tree_iterator__expand_tree(ti)) < 0)
git_iterator_free((git_iterator *)ti); goto fail;
else
*iter = (git_iterator *)ti; *iter = (git_iterator *)ti;
return 0;
fail:
git_iterator_free((git_iterator *)ti);
return error; return error;
} }
...@@ -466,15 +502,19 @@ static void index_iterator__free(git_iterator *self) ...@@ -466,15 +502,19 @@ static void index_iterator__free(git_iterator *self)
int git_iterator_for_index_range( int git_iterator_for_index_range(
git_iterator **iter, git_iterator **iter,
git_index *index, git_index *index,
git_iterator_flag_t flags,
const char *start, const char *start,
const char *end) const char *end)
{ {
index_iterator *ii; index_iterator *ii;
GIT_UNUSED(flags);
ITERATOR_BASE_INIT(ii, index, INDEX); ITERATOR_BASE_INIT(ii, index, INDEX);
ii->base.repo = git_index_owner(index); ii->base.repo = git_index_owner(index);
ii->base.ignore_case = index->ignore_case; if (index->ignore_case)
ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
ii->index = index; ii->index = index;
GIT_REFCOUNT_INC(index); GIT_REFCOUNT_INC(index);
...@@ -530,7 +570,8 @@ static workdir_iterator_frame *workdir_iterator__alloc_frame( ...@@ -530,7 +570,8 @@ static workdir_iterator_frame *workdir_iterator__alloc_frame(
workdir_iterator *wi) workdir_iterator *wi)
{ {
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(wi->base.ignore_case, git_vector_cmp entry_compare = CASESELECT(
(wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0,
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)
...@@ -592,7 +633,8 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi) ...@@ -592,7 +633,8 @@ static int workdir_iterator__expand_dir(workdir_iterator *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->base.ignore_case, wi->path.ptr, wi->root_len,
(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) {
...@@ -775,12 +817,12 @@ static int workdir_iterator__update_entry(workdir_iterator *wi) ...@@ -775,12 +817,12 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
int git_iterator_for_workdir_range( int git_iterator_for_workdir_range(
git_iterator **iter, git_iterator **iter,
git_repository *repo, git_repository *repo,
git_iterator_flag_t flags,
const char *start, const char *start,
const char *end) const char *end)
{ {
int error; int error;
workdir_iterator *wi; workdir_iterator *wi;
git_index *index;
assert(iter && repo); assert(iter && repo);
...@@ -791,13 +833,8 @@ int git_iterator_for_workdir_range( ...@@ -791,13 +833,8 @@ int git_iterator_for_workdir_range(
ITERATOR_BASE_INIT(wi, workdir, WORKDIR); ITERATOR_BASE_INIT(wi, workdir, WORKDIR);
wi->base.repo = repo; wi->base.repo = repo;
if ((error = git_repository_index__weakptr(&index, repo)) < 0) { if ((error = iterator_update_ignore_case((git_iterator *)wi, flags)) < 0)
git_iterator_free((git_iterator *)wi); goto fail;
return error;
}
/* Match ignore_case flag for iterator to that of the index */
wi->base.ignore_case = index->ignore_case;
if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 || if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 ||
git_path_to_dir(&wi->path) < 0 || git_path_to_dir(&wi->path) < 0 ||
...@@ -808,23 +845,24 @@ int git_iterator_for_workdir_range( ...@@ -808,23 +845,24 @@ int git_iterator_for_workdir_range(
} }
wi->root_len = wi->path.size; wi->root_len = wi->path.size;
wi->entrycmp = wi->base.ignore_case ? wi->entrycmp = (wi->base.flags & GIT_ITERATOR_IGNORE_CASE) != 0 ?
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) {
if (error == GIT_ENOTFOUND) if (error != GIT_ENOTFOUND)
error = 0; goto fail;
else { giterr_clear();
git_iterator_free((git_iterator *)wi);
wi = NULL;
}
} }
*iter = (git_iterator *)wi; *iter = (git_iterator *)wi;
return 0;
fail:
git_iterator_free((git_iterator *)wi);
return error; return error;
} }
typedef struct { typedef struct {
/* replacement callbacks */ /* replacement callbacks */
git_iterator_callbacks cb; git_iterator_callbacks cb;
...@@ -899,12 +937,12 @@ void git_iterator_spoolandsort_pop(git_iterator *self) ...@@ -899,12 +937,12 @@ void git_iterator_spoolandsort_pop(git_iterator *self)
{ {
spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb; spoolandsort_callbacks *scb = (spoolandsort_callbacks *)self->cb;
if (self->type != GIT_ITERATOR_SPOOLANDSORT) if (self->type != GIT_ITERATOR_TYPE_SPOOLANDSORT)
return; return;
self->cb = scb->orig; self->cb = scb->orig;
self->type = scb->orig_type; self->type = scb->orig_type;
self->ignore_case = !self->ignore_case; self->flags ^= GIT_ITERATOR_IGNORE_CASE;
spoolandsort_iterator__free_callbacks(scb); spoolandsort_iterator__free_callbacks(scb);
} }
...@@ -921,7 +959,7 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case) ...@@ -921,7 +959,7 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case)
spoolandsort_callbacks *scb; spoolandsort_callbacks *scb;
int (*entrycomp)(const void *a, const void *b); int (*entrycomp)(const void *a, const void *b);
if (iter->ignore_case == ignore_case) if (((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) == (ignore_case != 0))
return 0; return 0;
scb = git__calloc(1, sizeof(spoolandsort_callbacks)); scb = git__calloc(1, sizeof(spoolandsort_callbacks));
...@@ -964,8 +1002,8 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case) ...@@ -964,8 +1002,8 @@ int git_iterator_spoolandsort_push(git_iterator *iter, bool ignore_case)
git_vector_sort(&scb->entries); git_vector_sort(&scb->entries);
iter->cb = (git_iterator_callbacks *)scb; iter->cb = (git_iterator_callbacks *)scb;
iter->type = GIT_ITERATOR_SPOOLANDSORT; iter->type = GIT_ITERATOR_TYPE_SPOOLANDSORT;
iter->ignore_case = !iter->ignore_case; iter->flags ^= GIT_ITERATOR_IGNORE_CASE;
return 0; return 0;
...@@ -992,11 +1030,11 @@ void git_iterator_free(git_iterator *iter) ...@@ -992,11 +1030,11 @@ void git_iterator_free(git_iterator *iter)
git_index *git_iterator_index_get_index(git_iterator *iter) git_index *git_iterator_index_get_index(git_iterator *iter)
{ {
if (iter->type == GIT_ITERATOR_INDEX) if (iter->type == GIT_ITERATOR_TYPE_INDEX)
return ((index_iterator *)iter)->index; return ((index_iterator *)iter)->index;
if (iter->type == GIT_ITERATOR_SPOOLANDSORT && if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT &&
((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_INDEX) ((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_TYPE_INDEX)
return ((index_iterator *)iter)->index; return ((index_iterator *)iter)->index;
return NULL; return NULL;
...@@ -1004,7 +1042,7 @@ git_index *git_iterator_index_get_index(git_iterator *iter) ...@@ -1004,7 +1042,7 @@ git_index *git_iterator_index_get_index(git_iterator *iter)
git_iterator_type_t git_iterator_inner_type(git_iterator *iter) git_iterator_type_t git_iterator_inner_type(git_iterator *iter)
{ {
if (iter->type == GIT_ITERATOR_SPOOLANDSORT) if (iter->type == GIT_ITERATOR_TYPE_SPOOLANDSORT)
return ((spoolandsort_callbacks *)iter->cb)->orig_type; return ((spoolandsort_callbacks *)iter->cb)->orig_type;
return iter->type; return iter->type;
...@@ -1013,7 +1051,7 @@ git_iterator_type_t git_iterator_inner_type(git_iterator *iter) ...@@ -1013,7 +1051,7 @@ git_iterator_type_t git_iterator_inner_type(git_iterator *iter)
int git_iterator_current_tree_entry( int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry) git_iterator *iter, const git_tree_entry **tree_entry)
{ {
*tree_entry = (iter->type != GIT_ITERATOR_TREE) ? NULL : *tree_entry = (iter->type != GIT_ITERATOR_TYPE_TREE) ? NULL :
tree_iterator__tree_entry((tree_iterator *)iter); tree_iterator__tree_entry((tree_iterator *)iter);
return 0; return 0;
} }
...@@ -1027,7 +1065,7 @@ int git_iterator_current_parent_tree( ...@@ -1027,7 +1065,7 @@ int git_iterator_current_parent_tree(
tree_iterator_frame *tf; tree_iterator_frame *tf;
const char *scan = parent_path; const char *scan = parent_path;
if (iter->type != GIT_ITERATOR_TREE || ti->stack == NULL) if (iter->type != GIT_ITERATOR_TYPE_TREE || ti->stack == NULL)
goto notfound; goto notfound;
for (tf = ti->tail; tf != NULL; tf = tf->prev) { for (tf = ti->tail; tf != NULL; tf = tf->prev) {
...@@ -1061,7 +1099,7 @@ int git_iterator_current_is_ignored(git_iterator *iter) ...@@ -1061,7 +1099,7 @@ int 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_WORKDIR) if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
return 0; return 0;
if (wi->is_ignored != -1) if (wi->is_ignored != -1)
...@@ -1078,7 +1116,7 @@ int git_iterator_advance_into_directory( ...@@ -1078,7 +1116,7 @@ int git_iterator_advance_into_directory(
{ {
workdir_iterator *wi = (workdir_iterator *)iter; workdir_iterator *wi = (workdir_iterator *)iter;
if (iter->type == GIT_ITERATOR_WORKDIR && if (iter->type == GIT_ITERATOR_TYPE_WORKDIR &&
wi->entry.path && wi->entry.path &&
(wi->entry.mode == GIT_FILEMODE_TREE || (wi->entry.mode == GIT_FILEMODE_TREE ||
wi->entry.mode == GIT_FILEMODE_COMMIT)) wi->entry.mode == GIT_FILEMODE_COMMIT))
...@@ -1112,7 +1150,7 @@ int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path) ...@@ -1112,7 +1150,7 @@ int git_iterator_current_workdir_path(git_iterator *iter, git_buf **path)
{ {
workdir_iterator *wi = (workdir_iterator *)iter; workdir_iterator *wi = (workdir_iterator *)iter;
if (iter->type != GIT_ITERATOR_WORKDIR || !wi->entry.path) if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->entry.path)
*path = NULL; *path = NULL;
else else
*path = &wi->path; *path = &wi->path;
......
...@@ -12,20 +12,26 @@ ...@@ -12,20 +12,26 @@
#include "vector.h" #include "vector.h"
#include "buffer.h" #include "buffer.h"
#define ITERATOR_PREFIXCMP(ITER, STR, PREFIX) (((ITER).ignore_case) ? \ #define ITERATOR_PREFIXCMP(ITER, STR, PREFIX) \
(((ITER).flags & GIT_ITERATOR_IGNORE_CASE) != 0 ? \
git__prefixcmp_icase((STR), (PREFIX)) : \ git__prefixcmp_icase((STR), (PREFIX)) : \
git__prefixcmp((STR), (PREFIX))) git__prefixcmp((STR), (PREFIX)))
typedef struct git_iterator git_iterator; typedef struct git_iterator git_iterator;
typedef enum { typedef enum {
GIT_ITERATOR_EMPTY = 0, GIT_ITERATOR_TYPE_EMPTY = 0,
GIT_ITERATOR_TREE = 1, GIT_ITERATOR_TYPE_TREE = 1,
GIT_ITERATOR_INDEX = 2, GIT_ITERATOR_TYPE_INDEX = 2,
GIT_ITERATOR_WORKDIR = 3, GIT_ITERATOR_TYPE_WORKDIR = 3,
GIT_ITERATOR_SPOOLANDSORT = 4 GIT_ITERATOR_TYPE_SPOOLANDSORT = 4
} git_iterator_type_t; } git_iterator_type_t;
typedef enum {
GIT_ITERATOR_IGNORE_CASE = (1 << 0), /* ignore_case */
GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1), /* force ignore_case off */
} git_iterator_flag_t;
typedef struct { typedef struct {
int (*current)(git_iterator *, const git_index_entry **); int (*current)(git_iterator *, const git_index_entry **);
int (*at_end)(git_iterator *); int (*at_end)(git_iterator *);
...@@ -41,33 +47,55 @@ struct git_iterator { ...@@ -41,33 +47,55 @@ struct git_iterator {
git_repository *repo; git_repository *repo;
char *start; char *start;
char *end; char *end;
bool ignore_case; unsigned int flags;
}; };
extern int git_iterator_for_nothing(git_iterator **out); extern int git_iterator_for_nothing(
git_iterator **out, git_iterator_flag_t flags);
/* tree iterators will match the ignore_case value from the index of the
* repository, unless you override with a non-zero flag value
*/
extern int git_iterator_for_tree_range( extern int git_iterator_for_tree_range(
git_iterator **out, git_tree *tree, const char *start, const char *end); git_iterator **out,
git_tree *tree,
git_iterator_flag_t flags,
const char *start,
const char *end);
GIT_INLINE(int) git_iterator_for_tree(git_iterator **out, git_tree *tree) GIT_INLINE(int) git_iterator_for_tree(git_iterator **out, git_tree *tree)
{ {
return git_iterator_for_tree_range(out, tree, NULL, NULL); return git_iterator_for_tree_range(out, tree, 0, NULL, NULL);
} }
/* index iterators will take the ignore_case value from the index; the
* ignore_case flags are not used
*/
extern int git_iterator_for_index_range( extern int git_iterator_for_index_range(
git_iterator **out, git_index *index, const char *start, const char *end); git_iterator **out,
git_index *index,
git_iterator_flag_t flags,
const char *start,
const char *end);
GIT_INLINE(int) git_iterator_for_index(git_iterator **out, git_index *index) GIT_INLINE(int) git_iterator_for_index(git_iterator **out, git_index *index)
{ {
return git_iterator_for_index_range(out, index, NULL, NULL); return git_iterator_for_index_range(out, index, 0, NULL, NULL);
} }
/* workdir iterators will match the ignore_case value from the index of the
* repository, unless you override with a non-zero flag value
*/
extern int git_iterator_for_workdir_range( extern int git_iterator_for_workdir_range(
git_iterator **out, git_repository *repo, const char *start, const char *end); git_iterator **out,
git_repository *repo,
git_iterator_flag_t flags,
const char *start,
const char *end);
GIT_INLINE(int) git_iterator_for_workdir(git_iterator **out, git_repository *repo) GIT_INLINE(int) git_iterator_for_workdir(git_iterator **out, git_repository *repo)
{ {
return git_iterator_for_workdir_range(out, repo, NULL, NULL); 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);
...@@ -127,6 +155,16 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter) ...@@ -127,6 +155,16 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
return iter->repo; return iter->repo;
} }
GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
{
return iter->flags;
}
GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter)
{
return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0);
}
extern int git_iterator_current_tree_entry( extern int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry); git_iterator *iter, const git_tree_entry **tree_entry);
......
...@@ -35,7 +35,8 @@ static void tree_iterator_test( ...@@ -35,7 +35,8 @@ 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(&i, t, start, end)); cl_git_pass(git_iterator_for_tree_range(
&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(i, &entry));
...@@ -304,7 +305,8 @@ void test_diff_iterator__tree_special_functions(void) ...@@ -304,7 +305,8 @@ 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(&i, t, NULL, NULL)); cl_git_pass(git_iterator_for_tree_range(
&i, t, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
...@@ -362,7 +364,7 @@ static void index_iterator_test( ...@@ -362,7 +364,7 @@ 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, start, end)); cl_git_pass(git_iterator_for_index_range(&i, index, 0, start, end));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
...@@ -536,7 +538,7 @@ static void workdir_iterator_test( ...@@ -536,7 +538,7 @@ 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, start, end)); cl_git_pass(git_iterator_for_workdir_range(&i, repo, 0, start, end));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
...@@ -734,7 +736,7 @@ void test_diff_iterator__workdir_builtin_ignores(void) ...@@ -734,7 +736,7 @@ void test_diff_iterator__workdir_builtin_ignores(void)
cl_git_mkfile("attr/sub/.git", "whatever"); cl_git_mkfile("attr/sub/.git", "whatever");
cl_git_pass( cl_git_pass(
git_iterator_for_workdir_range(&i, repo, "dir", "sub/sub/file")); git_iterator_for_workdir_range(&i, repo, 0, "dir", "sub/sub/file"));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
for (idx = 0; entry != NULL; ++idx) { for (idx = 0; entry != NULL; ++idx) {
......
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