Commit 7b29be31 by Carlos Martín Nieto

Merge pull request #3691 from ethomson/iterators

Some FANTASTIC iterator refactoring
parents d4763c98 d6713ec6
...@@ -259,6 +259,15 @@ GIT_EXTERN(int) git_blob_create_frombuffer( ...@@ -259,6 +259,15 @@ GIT_EXTERN(int) git_blob_create_frombuffer(
*/ */
GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob); GIT_EXTERN(int) git_blob_is_binary(const git_blob *blob);
/**
* Create an in-memory copy of a blob. The copy must be explicitly
* free'd or it will leak.
*
* @param out Pointer to store the copy of the object
* @param source Original object to copy
*/
GIT_EXTERN(int) git_blob_dup(git_blob **out, git_blob *source);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -461,6 +461,15 @@ GIT_EXTERN(int) git_commit_create_with_signature( ...@@ -461,6 +461,15 @@ GIT_EXTERN(int) git_commit_create_with_signature(
const char *signature, const char *signature,
const char *signature_field); const char *signature_field);
/**
* Create an in-memory copy of a commit. The copy must be explicitly
* free'd or it will leak.
*
* @param out Pointer to store the copy of the commit
* @param source Original commit to copy
*/
GIT_EXTERN(int) git_commit_dup(git_commit **out, git_commit *source);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -347,6 +347,15 @@ GIT_EXTERN(int) git_tag_peel( ...@@ -347,6 +347,15 @@ GIT_EXTERN(int) git_tag_peel(
git_object **tag_target_out, git_object **tag_target_out,
const git_tag *tag); const git_tag *tag);
/**
* Create an in-memory copy of a tag. The copy must be explicitly
* free'd or it will leak.
*
* @param out Pointer to store the copy of the tag
* @param source Original tag to copy
*/
GIT_EXTERN(int) git_tag_dup(git_tag **out, git_tag *source);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -409,6 +409,15 @@ GIT_EXTERN(int) git_tree_walk( ...@@ -409,6 +409,15 @@ GIT_EXTERN(int) git_tree_walk(
git_treewalk_cb callback, git_treewalk_cb callback,
void *payload); void *payload);
/**
* Create an in-memory copy of a tree. The copy must be explicitly
* free'd or it will leak.
*
* @param out Pointer to store the copy of the tree
* @param source Original tree to copy
*/
GIT_EXTERN(int) git_tree_dup(git_tree **out, git_tree *source);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -66,8 +66,8 @@ typedef struct { ...@@ -66,8 +66,8 @@ typedef struct {
git_vector update_conflicts; git_vector update_conflicts;
git_vector *update_reuc; git_vector *update_reuc;
git_vector *update_names; git_vector *update_names;
git_buf path; git_buf target_path;
size_t workdir_len; size_t target_len;
git_buf tmp; git_buf tmp;
unsigned int strategy; unsigned int strategy;
int can_symlink; int can_symlink;
...@@ -294,14 +294,30 @@ static int checkout_action_no_wd( ...@@ -294,14 +294,30 @@ static int checkout_action_no_wd(
return checkout_action_common(action, data, delta, NULL); return checkout_action_common(action, data, delta, NULL);
} }
static bool wd_item_is_removable(git_iterator *iter, const git_index_entry *wd) static int checkout_target_fullpath(
git_buf **out, checkout_data *data, const char *path)
{ {
git_buf *full = NULL; git_buf_truncate(&data->target_path, data->target_len);
if (path && git_buf_puts(&data->target_path, path) < 0)
return -1;
*out = &data->target_path;
return 0;
}
static bool wd_item_is_removable(
checkout_data *data, const git_index_entry *wd)
{
git_buf *full;
if (wd->mode != GIT_FILEMODE_TREE) if (wd->mode != GIT_FILEMODE_TREE)
return true; return true;
if (git_iterator_current_workdir_path(&full, iter) < 0)
return true; if (checkout_target_fullpath(&full, data, wd->path) < 0)
return false;
return !full || !git_path_contains(full, DOT_GIT); return !full || !git_path_contains(full, DOT_GIT);
} }
...@@ -363,14 +379,14 @@ static int checkout_action_wd_only( ...@@ -363,14 +379,14 @@ static int checkout_action_wd_only(
if ((error = checkout_notify(data, notify, NULL, wd)) != 0) if ((error = checkout_notify(data, notify, NULL, wd)) != 0)
return error; return error;
if (remove && wd_item_is_removable(workdir, wd)) if (remove && wd_item_is_removable(data, wd))
error = checkout_queue_remove(data, wd->path); error = checkout_queue_remove(data, wd->path);
if (!error) if (!error)
error = git_iterator_advance(wditem, workdir); error = git_iterator_advance(wditem, workdir);
} else { } else {
/* untracked or ignored - can't know which until we advance through */ /* untracked or ignored - can't know which until we advance through */
bool over = false, removable = wd_item_is_removable(workdir, wd); bool over = false, removable = wd_item_is_removable(data, wd);
git_iterator_status_t untracked_state; git_iterator_status_t untracked_state;
/* copy the entry for issuing notification callback later */ /* copy the entry for issuing notification callback later */
...@@ -378,7 +394,7 @@ static int checkout_action_wd_only( ...@@ -378,7 +394,7 @@ static int checkout_action_wd_only(
git_buf_sets(&data->tmp, wd->path); git_buf_sets(&data->tmp, wd->path);
saved_wd.path = data->tmp.ptr; saved_wd.path = data->tmp.ptr;
error = git_iterator_advance_over_with_status( error = git_iterator_advance_over(
wditem, &untracked_state, workdir); wditem, &untracked_state, workdir);
if (error == GIT_ITEROVER) if (error == GIT_ITEROVER)
over = true; over = true;
...@@ -428,10 +444,12 @@ static bool submodule_is_config_only( ...@@ -428,10 +444,12 @@ static bool submodule_is_config_only(
static bool checkout_is_empty_dir(checkout_data *data, const char *path) static bool checkout_is_empty_dir(checkout_data *data, const char *path)
{ {
git_buf_truncate(&data->path, data->workdir_len); git_buf *fullpath;
if (git_buf_puts(&data->path, path) < 0)
if (checkout_target_fullpath(&fullpath, data, path) < 0)
return false; return false;
return git_path_is_empty_dir(data->path.ptr);
return git_path_is_empty_dir(fullpath->ptr);
} }
static int checkout_action_with_wd( static int checkout_action_with_wd(
...@@ -639,7 +657,7 @@ static int checkout_action( ...@@ -639,7 +657,7 @@ 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 */
error = git_iterator_advance_into_or_over(wditem, workdir); error = git_iterator_advance_into(wditem, workdir);
if (error < 0 && error != GIT_ITEROVER) if (error < 0 && error != GIT_ITEROVER)
goto done; goto done;
continue; continue;
...@@ -912,7 +930,7 @@ static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, g ...@@ -912,7 +930,7 @@ static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, g
git_index *index; git_index *index;
/* Only write conficts from sources that have them: indexes. */ /* Only write conficts from sources that have them: indexes. */
if ((index = git_iterator_get_index(data->target)) == NULL) if ((index = git_iterator_index(data->target)) == NULL)
return 0; return 0;
data->update_conflicts._cmp = checkout_conflictdata_cmp; data->update_conflicts._cmp = checkout_conflictdata_cmp;
...@@ -1062,7 +1080,7 @@ static int checkout_conflicts_coalesce_renames( ...@@ -1062,7 +1080,7 @@ static int checkout_conflicts_coalesce_renames(
size_t i, names; size_t i, names;
int error = 0; int error = 0;
if ((index = git_iterator_get_index(data->target)) == NULL) if ((index = git_iterator_index(data->target)) == NULL)
return 0; return 0;
/* Juggle entries based on renames */ /* Juggle entries based on renames */
...@@ -1120,7 +1138,7 @@ static int checkout_conflicts_mark_directoryfile( ...@@ -1120,7 +1138,7 @@ static int checkout_conflicts_mark_directoryfile(
const char *path; const char *path;
int prefixed, error = 0; int prefixed, error = 0;
if ((index = git_iterator_get_index(data->target)) == NULL) if ((index = git_iterator_index(data->target)) == NULL)
return 0; return 0;
len = git_index_entrycount(index); len = git_index_entrycount(index);
...@@ -1582,18 +1600,18 @@ static int checkout_submodule_update_index( ...@@ -1582,18 +1600,18 @@ static int checkout_submodule_update_index(
checkout_data *data, checkout_data *data,
const git_diff_file *file) const git_diff_file *file)
{ {
git_buf *fullpath;
struct stat st; struct stat st;
/* update the index unless prevented */ /* update the index unless prevented */
if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0) if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
return 0; return 0;
git_buf_truncate(&data->path, data->workdir_len); if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
if (git_buf_puts(&data->path, file->path) < 0)
return -1; return -1;
data->perfdata.stat_calls++; data->perfdata.stat_calls++;
if (p_stat(git_buf_cstr(&data->path), &st) < 0) { if (p_stat(fullpath->ptr, &st) < 0) {
giterr_set( giterr_set(
GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path); GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path);
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
...@@ -1718,22 +1736,23 @@ static int checkout_blob( ...@@ -1718,22 +1736,23 @@ static int checkout_blob(
checkout_data *data, checkout_data *data,
const git_diff_file *file) const git_diff_file *file)
{ {
int error = 0; git_buf *fullpath;
struct stat st; struct stat st;
int error = 0;
git_buf_truncate(&data->path, data->workdir_len); if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
if (git_buf_puts(&data->path, file->path) < 0)
return -1; return -1;
if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) { if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) {
int rval = checkout_safe_for_update_only( int rval = checkout_safe_for_update_only(
data, git_buf_cstr(&data->path), file->mode); data, fullpath->ptr, file->mode);
if (rval <= 0) if (rval <= 0)
return rval; return rval;
} }
error = checkout_write_content( error = checkout_write_content(
data, &file->id, git_buf_cstr(&data->path), NULL, file->mode, &st); data, &file->id, fullpath->ptr, NULL, file->mode, &st);
/* update the index unless prevented */ /* update the index unless prevented */
if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
...@@ -1754,18 +1773,21 @@ static int checkout_remove_the_old( ...@@ -1754,18 +1773,21 @@ static int checkout_remove_the_old(
git_diff_delta *delta; git_diff_delta *delta;
const char *str; const char *str;
size_t i; size_t i;
const char *workdir = git_buf_cstr(&data->path); git_buf *fullpath;
uint32_t flg = GIT_RMDIR_EMPTY_PARENTS | uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS; GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES) if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES)
flg |= GIT_RMDIR_SKIP_NONEMPTY; flg |= GIT_RMDIR_SKIP_NONEMPTY;
git_buf_truncate(&data->path, data->workdir_len); if (checkout_target_fullpath(&fullpath, data, NULL) < 0)
return -1;
git_vector_foreach(&data->diff->deltas, i, delta) { git_vector_foreach(&data->diff->deltas, i, delta) {
if (actions[i] & CHECKOUT_ACTION__REMOVE) { if (actions[i] & CHECKOUT_ACTION__REMOVE) {
error = git_futils_rmdir_r(delta->old_file.path, workdir, flg); error = git_futils_rmdir_r(
delta->old_file.path, fullpath->ptr, flg);
if (error < 0) if (error < 0)
return error; return error;
...@@ -1782,7 +1804,7 @@ static int checkout_remove_the_old( ...@@ -1782,7 +1804,7 @@ static int checkout_remove_the_old(
} }
git_vector_foreach(&data->removes, i, str) { git_vector_foreach(&data->removes, i, str) {
error = git_futils_rmdir_r(str, workdir, flg); error = git_futils_rmdir_r(str, fullpath->ptr, flg);
if (error < 0) if (error < 0)
return error; return error;
...@@ -1949,13 +1971,13 @@ static int checkout_write_entry( ...@@ -1949,13 +1971,13 @@ static int checkout_write_entry(
const git_index_entry *side) const git_index_entry *side)
{ {
const char *hint_path = NULL, *suffix; const char *hint_path = NULL, *suffix;
git_buf *fullpath;
struct stat st; struct stat st;
int error; int error;
assert (side == conflict->ours || side == conflict->theirs); assert (side == conflict->ours || side == conflict->theirs);
git_buf_truncate(&data->path, data->workdir_len); if (checkout_target_fullpath(&fullpath, data, side->path) < 0)
if (git_buf_puts(&data->path, side->path) < 0)
return -1; return -1;
if ((conflict->name_collision || conflict->directoryfile) && if ((conflict->name_collision || conflict->directoryfile) &&
...@@ -1969,18 +1991,18 @@ static int checkout_write_entry( ...@@ -1969,18 +1991,18 @@ static int checkout_write_entry(
suffix = data->opts.their_label ? data->opts.their_label : suffix = data->opts.their_label ? data->opts.their_label :
"theirs"; "theirs";
if (checkout_path_suffixed(&data->path, suffix) < 0) if (checkout_path_suffixed(fullpath, suffix) < 0)
return -1; return -1;
hint_path = side->path; hint_path = side->path;
} }
if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 && if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
(error = checkout_safe_for_update_only(data, git_buf_cstr(&data->path), side->mode)) <= 0) (error = checkout_safe_for_update_only(data, fullpath->ptr, side->mode)) <= 0)
return error; return error;
return checkout_write_content(data, return checkout_write_content(data,
&side->id, git_buf_cstr(&data->path), hint_path, side->mode, &st); &side->id, fullpath->ptr, hint_path, side->mode, &st);
} }
static int checkout_write_entries( static int checkout_write_entries(
...@@ -2293,7 +2315,7 @@ static void checkout_data_clear(checkout_data *data) ...@@ -2293,7 +2315,7 @@ static void checkout_data_clear(checkout_data *data)
git_strmap_free(data->mkdir_map); git_strmap_free(data->mkdir_map);
git_buf_free(&data->path); git_buf_free(&data->target_path);
git_buf_free(&data->tmp); git_buf_free(&data->tmp);
git_index_free(data->index); git_index_free(data->index);
...@@ -2356,7 +2378,7 @@ static int checkout_data_init( ...@@ -2356,7 +2378,7 @@ static int checkout_data_init(
if ((error = git_repository_index(&data->index, data->repo)) < 0) if ((error = git_repository_index(&data->index, data->repo)) < 0)
goto cleanup; goto cleanup;
if (data->index != git_iterator_get_index(target)) { if (data->index != git_iterator_index(target)) {
if ((error = git_index_read(data->index, true)) < 0) if ((error = git_index_read(data->index, true)) < 0)
goto cleanup; goto cleanup;
...@@ -2447,12 +2469,12 @@ static int checkout_data_init( ...@@ -2447,12 +2469,12 @@ static int checkout_data_init(
if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
(error = git_buf_puts(&data->path, data->opts.target_directory)) < 0 || (error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
(error = git_path_to_dir(&data->path)) < 0 || (error = git_path_to_dir(&data->target_path)) < 0 ||
(error = git_strmap_alloc(&data->mkdir_map)) < 0) (error = git_strmap_alloc(&data->mkdir_map)) < 0)
goto cleanup; goto cleanup;
data->workdir_len = git_buf_len(&data->path); data->target_len = git_buf_len(&data->target_path);
git_attr_session__init(&data->attr_session, data->repo); git_attr_session__init(&data->attr_session, data->repo);
...@@ -2508,7 +2530,7 @@ int git_checkout_iterator( ...@@ -2508,7 +2530,7 @@ int git_checkout_iterator(
workdir_opts.start = data.pfx; workdir_opts.start = data.pfx;
workdir_opts.end = data.pfx; workdir_opts.end = data.pfx;
if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 || if ((error = git_iterator_reset_range(target, data.pfx, data.pfx)) < 0 ||
(error = git_iterator_for_workdir_ext( (error = git_iterator_for_workdir_ext(
&workdir, data.repo, data.opts.target_directory, index, NULL, &workdir, data.repo, data.opts.target_directory, index, NULL,
&workdir_opts)) < 0) &workdir_opts)) < 0)
...@@ -2578,7 +2600,7 @@ int git_checkout_iterator( ...@@ -2578,7 +2600,7 @@ int git_checkout_iterator(
(error = checkout_create_conflicts(&data)) < 0) (error = checkout_create_conflicts(&data)) < 0)
goto cleanup; goto cleanup;
if (data.index != git_iterator_get_index(target) && if (data.index != git_iterator_index(target) &&
(error = checkout_extensions_update_index(&data)) < 0) (error = checkout_extensions_update_index(&data)) < 0)
goto cleanup; goto cleanup;
......
...@@ -615,7 +615,7 @@ int git_commit_nth_gen_ancestor( ...@@ -615,7 +615,7 @@ int git_commit_nth_gen_ancestor(
assert(ancestor && commit); assert(ancestor && commit);
if (git_object_dup((git_object **) &current, (git_object *) commit) < 0) if (git_commit_dup(&current, (git_commit *)commit) < 0)
return -1; return -1;
if (n == 0) { if (n == 0) {
......
...@@ -197,7 +197,7 @@ static int commit_name_dup(struct commit_name **out, struct commit_name *in) ...@@ -197,7 +197,7 @@ static int commit_name_dup(struct commit_name **out, struct commit_name *in)
name->tag = NULL; name->tag = NULL;
name->path = NULL; name->path = NULL;
if (in->tag && git_object_dup((git_object **) &name->tag, (git_object *) in->tag) < 0) if (in->tag && git_tag_dup(&name->tag, in->tag) < 0)
return -1; return -1;
name->path = git__strdup(in->path); name->path = git__strdup(in->path);
......
...@@ -826,8 +826,7 @@ static int maybe_modified( ...@@ -826,8 +826,7 @@ static int maybe_modified(
*/ */
} else if (git_oid_iszero(&nitem->id) && new_is_workdir) { } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0); bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
git_index *index; git_index *index = git_iterator_index(info->new_iter);
git_iterator_index(&index, info->new_iter);
status = GIT_DELTA_UNMODIFIED; status = GIT_DELTA_UNMODIFIED;
...@@ -980,15 +979,14 @@ static int iterator_advance_into( ...@@ -980,15 +979,14 @@ static int iterator_advance_into(
return error; return error;
} }
static int iterator_advance_over_with_status( static int iterator_advance_over(
const git_index_entry **entry, const git_index_entry **entry,
git_iterator_status_t *status, git_iterator_status_t *status,
git_iterator *iterator) git_iterator *iterator)
{ {
int error; int error = git_iterator_advance_over(entry, status, iterator);
if ((error = git_iterator_advance_over_with_status( if (error == GIT_ITEROVER) {
entry, status, iterator)) == GIT_ITEROVER) {
*entry = NULL; *entry = NULL;
error = 0; error = 0;
} }
...@@ -1056,7 +1054,7 @@ static int handle_unmatched_new_item( ...@@ -1056,7 +1054,7 @@ static int handle_unmatched_new_item(
return iterator_advance(&info->nitem, info->new_iter); return iterator_advance(&info->nitem, info->new_iter);
/* iterate into dir looking for an actual untracked file */ /* iterate into dir looking for an actual untracked file */
if ((error = iterator_advance_over_with_status( if ((error = iterator_advance_over(
&info->nitem, &untracked_state, info->new_iter)) < 0) &info->nitem, &untracked_state, info->new_iter)) < 0)
return error; return error;
...@@ -1093,7 +1091,7 @@ static int handle_unmatched_new_item( ...@@ -1093,7 +1091,7 @@ static int handle_unmatched_new_item(
/* if directory is empty, can't advance into it, so either skip /* if directory is empty, can't advance into it, so either skip
* it or ignore it * it or ignore it
*/ */
if (contains_oitem) if (error == GIT_ENOTFOUND || contains_oitem)
return iterator_advance(&info->nitem, info->new_iter); return iterator_advance(&info->nitem, info->new_iter);
delta_type = GIT_DELTA_IGNORED; delta_type = GIT_DELTA_IGNORED;
} }
...@@ -1231,9 +1229,8 @@ int git_diff__from_iterators( ...@@ -1231,9 +1229,8 @@ int git_diff__from_iterators(
/* make iterators have matching icase behavior */ /* make iterators have matching icase behavior */
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) { if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 || git_iterator_set_ignore_case(old_iter, true);
(error = git_iterator_set_ignore_case(new_iter, true)) < 0) git_iterator_set_ignore_case(new_iter, true);
goto cleanup;
} }
/* finish initialization */ /* finish initialization */
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -34,10 +34,19 @@ typedef enum { ...@@ -34,10 +34,19 @@ typedef enum {
GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3), GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3),
/** convert precomposed unicode to decomposed unicode */ /** convert precomposed unicode to decomposed unicode */
GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4), GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
/** never convert precomposed unicode to decomposed unicode */
GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
/** include conflicts */ /** include conflicts */
GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 5), GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
} git_iterator_flag_t; } git_iterator_flag_t;
typedef enum {
GIT_ITERATOR_STATUS_NORMAL = 0,
GIT_ITERATOR_STATUS_IGNORED = 1,
GIT_ITERATOR_STATUS_EMPTY = 2,
GIT_ITERATOR_STATUS_FILTERED = 3
} git_iterator_status_t;
typedef struct { typedef struct {
const char *start; const char *start;
const char *end; const char *end;
...@@ -57,23 +66,33 @@ typedef struct { ...@@ -57,23 +66,33 @@ typedef struct {
int (*current)(const git_index_entry **, git_iterator *); int (*current)(const git_index_entry **, git_iterator *);
int (*advance)(const git_index_entry **, git_iterator *); int (*advance)(const git_index_entry **, git_iterator *);
int (*advance_into)(const git_index_entry **, git_iterator *); int (*advance_into)(const git_index_entry **, git_iterator *);
int (*seek)(git_iterator *, const char *prefix); int (*advance_over)(
int (*reset)(git_iterator *, const char *start, const char *end); const git_index_entry **, git_iterator_status_t *, git_iterator *);
int (*at_end)(git_iterator *); int (*reset)(git_iterator *);
void (*free)(git_iterator *); void (*free)(git_iterator *);
} git_iterator_callbacks; } git_iterator_callbacks;
struct git_iterator { struct git_iterator {
git_iterator_type_t type; git_iterator_type_t type;
git_iterator_callbacks *cb; git_iterator_callbacks *cb;
git_repository *repo; git_repository *repo;
git_index *index;
char *start; char *start;
size_t start_len;
char *end; char *end;
size_t end_len;
bool started;
bool ended;
git_vector pathlist; git_vector pathlist;
size_t pathlist_walk_idx; size_t pathlist_walk_idx;
int (*strcomp)(const char *a, const char *b); int (*strcomp)(const char *a, const char *b);
int (*strncomp)(const char *a, const char *b, size_t n); int (*strncomp)(const char *a, const char *b, size_t n);
int (*prefixcomp)(const char *str, const char *prefix); int (*prefixcomp)(const char *str, const char *prefix);
int (*entry_srch)(const void *key, const void *array_member);
size_t stat_calls; size_t stat_calls;
unsigned int flags; unsigned int flags;
}; };
...@@ -181,54 +200,38 @@ GIT_INLINE(int) git_iterator_advance_into( ...@@ -181,54 +200,38 @@ GIT_INLINE(int) git_iterator_advance_into(
return iter->cb->advance_into(entry, iter); return iter->cb->advance_into(entry, iter);
} }
/** /* Advance over a directory and check if it contains no files or just
* Advance into a tree or skip over it if it is empty. * ignored files.
* *
* Because `git_iterator_advance_into` may return GIT_ENOTFOUND if the * In a tree or the index, all directories will contain files, but in the
* directory is empty (only with filesystem and working directory * working directory it is possible to have an empty directory tree or a
* iterators) and a common response is to just call `git_iterator_advance` * tree that only contains ignored files. Many Git operations treat these
* when that happens, this bundles the two into a single simple call. * cases specially. This advances over a directory (presumably an
* untracked directory) but checks during the scan if there are any files
* and any non-ignored files.
*/ */
GIT_INLINE(int) git_iterator_advance_into_or_over( GIT_INLINE(int) git_iterator_advance_over(
const git_index_entry **entry, git_iterator *iter) const git_index_entry **entry,
{ git_iterator_status_t *status,
int error = iter->cb->advance_into(entry, iter); git_iterator *iter)
if (error == GIT_ENOTFOUND) {
giterr_clear();
error = iter->cb->advance(entry, iter);
}
return error;
}
/* Seek is currently unimplemented */
GIT_INLINE(int) git_iterator_seek(
git_iterator *iter, const char *prefix)
{ {
return iter->cb->seek(iter, prefix); return iter->cb->advance_over(entry, status, iter);
} }
/** /**
* Go back to the start of the iteration. * Go back to the start of the iteration.
*
* This resets the iterator to the start of the iteration. It also allows
* you to reset the `start` and `end` pathname boundaries of the iteration
* when doing so.
*/ */
GIT_INLINE(int) git_iterator_reset( GIT_INLINE(int) git_iterator_reset(git_iterator *iter)
git_iterator *iter, const char *start, const char *end)
{ {
return iter->cb->reset(iter, start, end); return iter->cb->reset(iter);
} }
/** /**
* Check if the iterator is at the end * Go back to the start of the iteration after updating the `start` and
* * `end` pathname boundaries of the iteration.
* @return 0 if not at end, >0 if at end
*/ */
GIT_INLINE(int) git_iterator_at_end(git_iterator *iter) extern int git_iterator_reset_range(
{ git_iterator *iter, const char *start, const char *end);
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)
{ {
...@@ -240,6 +243,11 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter) ...@@ -240,6 +243,11 @@ GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
return iter->repo; return iter->repo;
} }
GIT_INLINE(git_index *) git_iterator_index(git_iterator *iter)
{
return iter->index;
}
GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter) GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
{ {
return iter->flags; return iter->flags;
...@@ -250,21 +258,19 @@ GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter) ...@@ -250,21 +258,19 @@ 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 void 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(
const git_tree_entry **entry_out, git_iterator *iter); const git_tree_entry **entry_out, git_iterator *iter);
extern int git_iterator_current_parent_tree( extern int git_iterator_current_parent_tree(
const git_tree **tree_out, git_iterator *iter, const char *parent_path); const git_tree **tree_out, git_iterator *iter, size_t depth);
extern bool git_iterator_current_is_ignored(git_iterator *iter); extern bool git_iterator_current_is_ignored(git_iterator *iter);
extern bool git_iterator_current_tree_is_ignored(git_iterator *iter); extern bool git_iterator_current_tree_is_ignored(git_iterator *iter);
extern int git_iterator_cmp(
git_iterator *iter, const char *path_prefix);
/** /**
* Get full path of the current item from a workdir iterator. This will * Get full path of the current item from a workdir iterator. This will
* return NULL for a non-workdir iterator. The git_buf is still owned by * return NULL for a non-workdir iterator. The git_buf is still owned by
...@@ -273,35 +279,12 @@ extern int git_iterator_cmp( ...@@ -273,35 +279,12 @@ extern int git_iterator_cmp(
extern int git_iterator_current_workdir_path( extern int git_iterator_current_workdir_path(
git_buf **path, git_iterator *iter); git_buf **path, git_iterator *iter);
/* Return index pointer if index iterator, else NULL */
extern git_index *git_iterator_get_index(git_iterator *iter);
typedef enum {
GIT_ITERATOR_STATUS_NORMAL = 0,
GIT_ITERATOR_STATUS_IGNORED = 1,
GIT_ITERATOR_STATUS_EMPTY = 2,
GIT_ITERATOR_STATUS_FILTERED = 3
} git_iterator_status_t;
/* Advance over a directory and check if it contains no files or just
* ignored files.
*
* In a tree or the index, all directories will contain files, but in the
* working directory it is possible to have an empty directory tree or a
* tree that only contains ignored files. Many Git operations treat these
* cases specially. This advances over a directory (presumably an
* untracked directory) but checks during the scan if there are any files
* and any non-ignored files.
*/
extern int git_iterator_advance_over_with_status(
const git_index_entry **entry, git_iterator_status_t *status, git_iterator *iter);
/** /**
* Retrieve the index stored in the iterator. * Retrieve the index stored in the iterator.
* *
* Only implemented for the workdir iterator * Only implemented for the workdir and index iterators.
*/ */
extern int git_iterator_index(git_index **out, git_iterator *iter); extern git_index *git_iterator_index(git_iterator *iter);
typedef int (*git_iterator_walk_cb)( typedef int (*git_iterator_walk_cb)(
const git_index_entry **entries, const git_index_entry **entries,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include "tag.h" #include "tag.h"
/** /**
* Blob * Commit
*/ */
int git_commit_lookup(git_commit **out, git_repository *repo, const git_oid *id) int git_commit_lookup(git_commit **out, git_repository *repo, const git_oid *id)
{ {
...@@ -42,6 +42,10 @@ git_repository *git_commit_owner(const git_commit *obj) ...@@ -42,6 +42,10 @@ git_repository *git_commit_owner(const git_commit *obj)
return git_object_owner((const git_object *)obj); return git_object_owner((const git_object *)obj);
} }
int git_commit_dup(git_commit **out, git_commit *obj)
{
return git_object_dup((git_object **)out, (git_object *)obj);
}
/** /**
* Tree * Tree
...@@ -71,6 +75,10 @@ git_repository *git_tree_owner(const git_tree *obj) ...@@ -71,6 +75,10 @@ git_repository *git_tree_owner(const git_tree *obj)
return git_object_owner((const git_object *)obj); return git_object_owner((const git_object *)obj);
} }
int git_tree_dup(git_tree **out, git_tree *obj)
{
return git_object_dup((git_object **)out, (git_object *)obj);
}
/** /**
* Tag * Tag
...@@ -100,6 +108,11 @@ git_repository *git_tag_owner(const git_tag *obj) ...@@ -100,6 +108,11 @@ git_repository *git_tag_owner(const git_tag *obj)
return git_object_owner((const git_object *)obj); return git_object_owner((const git_object *)obj);
} }
int git_tag_dup(git_tag **out, git_tag *obj)
{
return git_object_dup((git_object **)out, (git_object *)obj);
}
/** /**
* Blob * Blob
*/ */
...@@ -127,3 +140,8 @@ git_repository *git_blob_owner(const git_blob *obj) ...@@ -127,3 +140,8 @@ git_repository *git_blob_owner(const git_blob *obj)
{ {
return git_object_owner((const git_object *)obj); return git_object_owner((const git_object *)obj);
} }
int git_blob_dup(git_blob **out, git_blob *obj)
{
return git_object_dup((git_object **)out, (git_object *)obj);
}
...@@ -810,6 +810,20 @@ int git_path_cmp( ...@@ -810,6 +810,20 @@ int git_path_cmp(
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
} }
size_t git_path_common_dirlen(const char *one, const char *two)
{
const char *p, *q, *dirsep = NULL;
for (p = one, q = two; *p && *q; p++, q++) {
if (*p == '/' && *q == '/')
dirsep = p;
else if (*p != *q)
break;
}
return dirsep ? (dirsep - one) + 1 : 0;
}
int git_path_make_relative(git_buf *path, const char *parent) int git_path_make_relative(git_buf *path, const char *parent)
{ {
const char *p, *q, *p_dirsep, *q_dirsep; const char *p, *q, *p_dirsep, *q_dirsep;
......
...@@ -203,6 +203,18 @@ extern bool git_path_contains(git_buf *dir, const char *item); ...@@ -203,6 +203,18 @@ extern bool git_path_contains(git_buf *dir, const char *item);
extern bool git_path_contains_dir(git_buf *parent, const char *subdir); extern bool git_path_contains_dir(git_buf *parent, const char *subdir);
/** /**
* Determine the common directory length between two paths, including
* the final path separator. For example, given paths 'a/b/c/1.txt
* and 'a/b/c/d/2.txt', the common directory is 'a/b/c/', and this
* will return the length of the string 'a/b/c/', which is 6.
*
* @param one The first path
* @param two The second path
* @return The length of the common directory
*/
extern size_t git_path_common_dirlen(const char *one, const char *two);
/**
* Make the path relative to the given parent path. * Make the path relative to the given parent path.
* *
* @param path The path to make relative * @param path The path to make relative
......
...@@ -418,7 +418,7 @@ static int pathspec_match_from_iterator( ...@@ -418,7 +418,7 @@ static int pathspec_match_from_iterator(
GITERR_CHECK_ALLOC(m); GITERR_CHECK_ALLOC(m);
} }
if ((error = git_iterator_reset(iter, ps->prefix, ps->prefix)) < 0) if ((error = git_iterator_reset_range(iter, ps->prefix, ps->prefix)) < 0)
goto done; goto done;
if (git_iterator_type(iter) == GIT_ITERATOR_TYPE_WORKDIR && if (git_iterator_type(iter) == GIT_ITERATOR_TYPE_WORKDIR &&
......
...@@ -652,3 +652,23 @@ void test_core_path__15_resolve_relative(void) ...@@ -652,3 +652,23 @@ void test_core_path__15_resolve_relative(void)
git_buf_free(&buf); git_buf_free(&buf);
} }
#define assert_common_dirlen(i, p, q) \
cl_assert_equal_i((i), git_path_common_dirlen((p), (q)));
void test_core_path__16_resolve_relative(void)
{
assert_common_dirlen(0, "", "");
assert_common_dirlen(0, "", "bar.txt");
assert_common_dirlen(0, "foo.txt", "bar.txt");
assert_common_dirlen(0, "foo.txt", "");
assert_common_dirlen(0, "foo/bar.txt", "bar/foo.txt");
assert_common_dirlen(0, "foo/bar.txt", "../foo.txt");
assert_common_dirlen(1, "/one.txt", "/two.txt");
assert_common_dirlen(4, "foo/one.txt", "foo/two.txt");
assert_common_dirlen(5, "/foo/one.txt", "/foo/two.txt");
assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt");
assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt");
}
/* This test exercises the problem described in
** https://github.com/libgit2/libgit2/pull/3568
** where deleting a directory during a diff/status
** operation can cause an access violation.
**
** The "test_diff_racediffiter__basic() test confirms
** the normal operation of diff on the given repo.
**
** The "test_diff_racediffiter__racy_rmdir() test
** uses the new diff progress callback to delete
** a directory (after the initial readdir() and
** before the directory itself is visited) causing
** the recursion and iteration to fail.
*/
#include "clar_libgit2.h"
#include "diff_helpers.h"
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
void test_diff_racediffiter__initialize(void)
{
}
void test_diff_racediffiter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
typedef struct
{
const char *path;
git_delta_t t;
} basic_payload;
static int notify_cb__basic(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
basic_payload *exp = (basic_payload *)payload;
basic_payload *e;
GIT_UNUSED(diff_so_far);
GIT_UNUSED(matched_pathspec);
for (e = exp; e->path; e++) {
if (strcmp(e->path, delta_to_add->new_file.path) == 0) {
cl_assert_equal_i(e->t, delta_to_add->status);
return 0;
}
}
cl_assert(0);
return GIT_ENOTFOUND;
}
void test_diff_racediffiter__basic(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_repository *repo = cl_git_sandbox_init("diff");
git_diff *diff;
basic_payload exp_a[] = {
{ "another.txt", GIT_DELTA_MODIFIED },
{ "readme.txt", GIT_DELTA_MODIFIED },
{ "zzzzz/", GIT_DELTA_IGNORED },
{ NULL, 0 }
};
cl_must_pass(p_mkdir("diff/zzzzz", 0777));
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
opts.notify_cb = notify_cb__basic;
opts.payload = exp_a;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
git_diff_free(diff);
}
typedef struct {
bool first_time;
const char *dir;
basic_payload *basic_payload;
} racy_payload;
static int notify_cb__racy_rmdir(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
racy_payload *pay = (racy_payload *)payload;
if (pay->first_time) {
cl_must_pass(p_rmdir(pay->dir));
pay->first_time = false;
}
return notify_cb__basic(diff_so_far, delta_to_add, matched_pathspec, pay->basic_payload);
}
void test_diff_racediffiter__racy(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_repository *repo = cl_git_sandbox_init("diff");
git_diff *diff;
basic_payload exp_a[] = {
{ "another.txt", GIT_DELTA_MODIFIED },
{ "readme.txt", GIT_DELTA_MODIFIED },
{ NULL, 0 }
};
racy_payload pay = { true, "diff/zzzzz", exp_a };
cl_must_pass(p_mkdir("diff/zzzzz", 0777));
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
opts.notify_cb = notify_cb__racy_rmdir;
opts.payload = &pay;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
git_diff_free(diff);
}
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "fileops.h"
#include "iterator_helpers.h"
#include "../submodule/submodule_helpers.h"
#include <stdarg.h>
static git_repository *g_repo;
void test_iterator_index__initialize(void)
{
}
void test_iterator_index__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void index_iterator_test(
const char *sandbox,
const char *start,
const char *end,
git_iterator_flag_t flags,
int expected_count,
const char **expected_names,
const char **expected_oids)
{
git_index *index;
git_iterator *i;
const git_index_entry *entry;
int error, count = 0, caps;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init(sandbox);
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
iter_opts.flags = flags;
iter_opts.start = start;
iter_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &iter_opts));
while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry);
if (expected_names != NULL)
cl_assert_equal_s(expected_names[count], entry->path);
if (expected_oids != NULL) {
git_oid oid;
cl_git_pass(git_oid_fromstr(&oid, expected_oids[count]));
cl_assert_equal_oid(&oid, &entry->id);
}
count++;
}
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(expected_count, count);
git_iterator_free(i);
cl_assert(caps == git_index_caps(index));
git_index_free(index);
}
static const char *expected_index_0[] = {
"attr0",
"attr1",
"attr2",
"attr3",
"binfile",
"gitattributes",
"macro_bad",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"sub/abc",
"sub/file",
"sub/sub/file",
"sub/sub/subsub.txt",
"sub/subdir_test1",
"sub/subdir_test2.txt",
"subdir/.gitattributes",
"subdir/abc",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
};
static const char *expected_index_oids_0[] = {
"556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3",
"3b74db7ab381105dc0d28f8295a77f6a82989292",
"2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2",
"c485abe35abd4aa6fd83b076a78bbea9e2e7e06c",
"d800886d9c86731ae5c4a62b0b77c437015e00d2",
"2b40c5aca159b04ea8d20ffe36cdf8b09369b14a",
"5819a185d77b03325aaf87cafc771db36f6ddca7",
"ff69f8639ce2e6010b3f33a74160aad98b48da2b",
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"9e5bdc47d6a80f2be0ea3049ad74231b94609242",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"99eae476896f4907224978b88e5ecaa6c5bb67a9",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"dccada462d3df8ac6de596fb8c896aba9344f941"
};
void test_iterator_index__0(void)
{
index_iterator_test(
"attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0),
expected_index_0, expected_index_oids_0);
}
static const char *expected_index_1[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_new_file",
"staged_new_file_deleted_file",
"staged_new_file_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
};
static const char* expected_index_oids_1[] = {
"a0de7e0ac200c489c41c59dfa910154a70264e6e",
"5452d32f1dd538eb0405e8a83cc185f79e25e80f",
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
"55d316c9ba708999f1918e9677d01dfcae69c6b9",
"a6be623522ce87a1d862128ac42672604f7b468b",
"906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8",
"529a16e8e762d4acb7b9636ff540a00831f9155a",
"90b8c29d8ba39434d1c63e1b093daaa26e5bd972",
"ed062903b8f6f3dccb2fa81117ba6590944ef9bd",
"e8ee89e15bbe9b20137715232387b3de5b28972e",
"53ace0d1cc1145a5f4fe4f78a186a60263190733",
"1888c805345ba265b0ee9449b8877b6064592058",
"a6191982709b746d5650e93c2acf34ef74e11504"
};
void test_iterator_index__1(void)
{
index_iterator_test(
"status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1),
expected_index_1, expected_index_oids_1);
}
static const char *expected_index_range[] = {
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
};
static const char *expected_index_oids_range[] = {
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
};
void test_iterator_index__range(void)
{
index_iterator_test(
"attr", "root", "root", 0, ARRAY_SIZE(expected_index_range),
expected_index_range, expected_index_oids_range);
}
void test_iterator_index__range_empty_0(void)
{
index_iterator_test(
"attr", "empty", "empty", 0, 0, NULL, NULL);
}
void test_iterator_index__range_empty_1(void)
{
index_iterator_test(
"attr", "z_empty_after", NULL, 0, 0, NULL, NULL);
}
void test_iterator_index__range_empty_2(void)
{
index_iterator_test(
"attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL);
}
static void check_index_range(
git_repository *repo,
const char *start,
const char *end,
bool ignore_case,
int expected_count)
{
git_index *index;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count, caps;
bool is_ignoring_case;
cl_git_pass(git_repository_index(&index, repo));
caps = git_index_caps(index);
is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0);
if (ignore_case != is_ignoring_case)
cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE));
i_opts.flags = 0;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, repo, index, &i_opts));
cl_assert(git_iterator_ignore_case(i) == ignore_case);
for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count)
/* count em up */;
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert_equal_i(expected_count, count);
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__range_icase(void)
{
git_index *index;
git_tree *head;
g_repo = cl_git_sandbox_init("testrepo");
/* reset index to match HEAD */
cl_git_pass(git_repository_head_tree(&head, g_repo));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_read_tree(index, head));
cl_git_pass(git_index_write(index));
git_tree_free(head);
git_index_free(index);
/* do some ranged iterator checks toggling case sensitivity */
check_index_range(g_repo, "B", "C", false, 0);
check_index_range(g_repo, "B", "C", true, 1);
check_index_range(g_repo, "a", "z", false, 3);
check_index_range(g_repo, "a", "z", true, 4);
}
static const char *expected_index_cs[] = {
"B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c",
"a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c",
};
static const char *expected_index_ci[] = {
"a", "B", "c", "D", "e", "F", "g", "H", "i", "J",
"k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D",
};
void test_iterator_index__case_folding(void)
{
git_buf path = GIT_BUF_INIT;
int fs_is_ci = 0;
cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg"));
fs_is_ci = git_path_exists(path.ptr);
git_buf_free(&path);
index_iterator_test(
"icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs),
fs_is_ci ? expected_index_ci : expected_index_cs, NULL);
cl_git_sandbox_cleanup();
index_iterator_test(
"icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE,
ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL);
cl_git_sandbox_cleanup();
index_iterator_test(
"icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE,
ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL);
}
/* Index contents (including pseudotrees):
*
* 0: a 5: F 10: k/ 16: L/
* 1: B 6: g 11: k/1 17: L/1
* 2: c 7: H 12: k/a 18: L/a
* 3: D 8: i 13: k/B 19: L/B
* 4: e 9: J 14: k/c 20: L/c
* 15: k/D 21: L/D
*
* 0: B 5: L/ 11: a 16: k/
* 1: D 6: L/1 12: c 17: k/1
* 2: F 7: L/B 13: e 18: k/B
* 3: H 8: L/D 14: g 19: k/D
* 4: J 9: L/a 15: i 20: k/a
* 10: L/c 21: k/c
*/
void test_iterator_index__icase_0(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* autoexpand with no tree entries for index */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__icase_1(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.flags = 0;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
}
void test_iterator_index__pathlist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
/* Case sensitive */
{
const char *expected[] = {
"B", "D", "L/1", "a", "c", "e", "k/1", "k/a" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Case INsensitive */
{
const char *expected[] = {
"a", "B", "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case sensitive. */
{
const char *expected[] = { "c", "e", "k/1", "k/a" };
size_t expected_len = 4;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case INsensitive. */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 6;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case sensitive. */
{
const char *expected[] = { "B", "D", "L/1", "a", "c", "e" };
size_t expected_len = 6;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case INsensitive. */
{
const char *expected[] = { "a", "B", "c", "D", "e" };
size_t expected_len = 5;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "c", "e", "k/1" };
size_t expected_len = 3;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "k/1" };
size_t expected_len = 1;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a" };
size_t expected_len = 5;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "k/1", "k/a" };
size_t expected_len = 2;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_dirs(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* Test that a prefix `k` matches folders, even without trailing slash */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a `k/` matches a folder */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* When the iterator is case sensitive, ensure we can't lookup the
* directory with the wrong case.
*/
{
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
/* Test that case insensitive matching works. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that case insensitive matching works without trailing slash. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_dirs_include_trees(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
const char *expected[] = { "k/", "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 6;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_1(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a ==> 5) vs (c e k/1 ==> 3) */
expect = default_icase ? 5 : 3;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_2(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_four(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE));
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_directory(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
git_index *index;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
static void create_paths(git_index *index, const char *root, int depth)
{
git_buf fullpath = GIT_BUF_INIT;
git_index_entry entry;
size_t root_len;
int i;
if (root) {
cl_git_pass(git_buf_puts(&fullpath, root));
cl_git_pass(git_buf_putc(&fullpath, '/'));
}
root_len = fullpath.size;
for (i = 0; i < 8; i++) {
bool file = (depth == 0 || (i % 2) == 0);
git_buf_truncate(&fullpath, root_len);
cl_git_pass(git_buf_printf(&fullpath, "item%d", i));
if (file) {
memset(&entry, 0, sizeof(git_index_entry));
entry.path = fullpath.ptr;
entry.mode = GIT_FILEMODE_BLOB;
git_oid_fromstr(&entry.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6");
cl_git_pass(git_index_add(index, &entry));
} else if (depth > 0) {
create_paths(index, fullpath.ptr, (depth - 1));
}
}
git_buf_free(&fullpath);
}
void test_iterator_index__pathlist_for_deeply_nested_item(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 3);
/* Ensure that we find the single path we're interested in */
{
const char *expected[] = { "item1/item3/item5/item7" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
{
const char *expected[] = {
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
};
size_t expected_len = 8;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
{
const char *expected[] = {
"item1/item3/item0",
"item1/item3/item1/item0", "item1/item3/item1/item1",
"item1/item3/item1/item2", "item1/item3/item1/item3",
"item1/item3/item1/item4", "item1/item3/item1/item5",
"item1/item3/item1/item6", "item1/item3/item1/item7",
"item1/item3/item2",
"item1/item3/item3/item0", "item1/item3/item3/item1",
"item1/item3/item3/item2", "item1/item3/item3/item3",
"item1/item3/item3/item4", "item1/item3/item3/item5",
"item1/item3/item3/item6", "item1/item3/item3/item7",
"item1/item3/item4",
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
"item1/item3/item6",
"item1/item3/item7/item0", "item1/item3/item7/item1",
"item1/item3/item7/item2", "item1/item3/item7/item3",
"item1/item3/item7/item4", "item1/item3/item7/item5",
"item1/item3/item7/item6", "item1/item3/item7/item7",
};
size_t expected_len = 36;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item0", "item1/item2", "item5/item7/item4", "item6",
"item7/item3/item1/item6" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6"));
cl_git_pass(git_vector_insert(&filelist, "item6"));
cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4"));
cl_git_pass(git_vector_insert(&filelist, "item1/item2"));
cl_git_pass(git_vector_insert(&filelist, "item0"));
/* also add some things that don't exist or don't match the right type */
cl_git_pass(git_vector_insert(&filelist, "item2/"));
cl_git_pass(git_vector_insert(&filelist, "itemN"));
cl_git_pass(git_vector_insert(&filelist, "item1/itemA"));
cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__advance_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 1);
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item0", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item2", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__advance_into(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__advance_into_and_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 2);
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "item0");
expect_advance_into(i, "item1/");
expect_advance_into(i, "item1/item0");
expect_advance_into(i, "item1/item1/");
expect_advance_into(i, "item1/item1/item0");
expect_advance_into(i, "item1/item1/item1");
expect_advance_into(i, "item1/item1/item2");
expect_advance_into(i, "item1/item1/item3");
expect_advance_into(i, "item1/item1/item4");
expect_advance_into(i, "item1/item1/item5");
expect_advance_into(i, "item1/item1/item6");
expect_advance_into(i, "item1/item1/item7");
expect_advance_into(i, "item1/item2");
expect_advance_over(i, "item1/item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_into(i, "item2");
expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
static void add_conflict(
git_index *index,
const char *ancestor_path,
const char *our_path,
const char *their_path)
{
git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}};
ancestor.path = ancestor_path;
ancestor.mode = GIT_FILEMODE_BLOB;
git_oid_fromstr(&ancestor.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6");
GIT_IDXENTRY_STAGE_SET(&ancestor, 1);
ours.path = our_path;
ours.mode = GIT_FILEMODE_BLOB;
git_oid_fromstr(&ours.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6");
GIT_IDXENTRY_STAGE_SET(&ours, 2);
theirs.path = their_path;
theirs.mode = GIT_FILEMODE_BLOB;
git_oid_fromstr(&theirs.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6");
GIT_IDXENTRY_STAGE_SET(&theirs, 3);
cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
}
void test_iterator_index__include_conflicts(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "CONFLICT1", "CONFLICT1" ,"CONFLICT1");
add_conflict(index, "ZZZ-CONFLICT2.ancestor", "ZZZ-CONFLICT2.ours", "ZZZ-CONFLICT2.theirs");
add_conflict(index, "ancestor.conflict3", "ours.conflict3", "theirs.conflict3");
add_conflict(index, "zzz-conflict4", "zzz-conflict4", "zzz-conflict4");
/* Iterate the index, ensuring that conflicts are not included */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
/* Try again, returning conflicts */
i_opts.flags |= GIT_ITERATOR_INCLUDE_CONFLICTS;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.ancestor", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.ours", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.theirs", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ancestor.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ours.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "theirs.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "fileops.h"
#include "iterator_helpers.h"
#include <stdarg.h>
static void assert_at_end(git_iterator *i, bool verbose)
{
const git_index_entry *end;
int error = git_iterator_advance(&end, i);
if (verbose && error != GIT_ITEROVER)
fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path);
cl_git_fail_with(GIT_ITEROVER, error);
}
void expect_iterator_items(
git_iterator *i,
int expected_flat,
const char **expected_flat_paths,
int expected_total,
const char **expected_total_paths)
{
const git_index_entry *entry;
int count, error;
int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES);
bool v = false;
if (expected_flat < 0) { v = true; expected_flat = -expected_flat; }
if (expected_total < 0) { v = true; expected_total = -expected_total; }
if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees");
count = 0;
while (!git_iterator_advance(&entry, i)) {
if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
if (no_trees)
cl_assert(entry->mode != GIT_FILEMODE_TREE);
if (expected_flat_paths) {
const char *expect_path = expected_flat_paths[count];
size_t expect_len = strlen(expect_path);
cl_assert_equal_s(expect_path, entry->path);
if (expect_path[expect_len - 1] == '/')
cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
else
cl_assert(entry->mode != GIT_FILEMODE_TREE);
}
if (++count >= expected_flat)
break;
}
assert_at_end(i, v);
cl_assert_equal_i(expected_flat, count);
cl_git_pass(git_iterator_reset(i));
count = 0;
cl_git_pass(git_iterator_current(&entry, i));
if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees");
while (entry != NULL) {
if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
if (no_trees)
cl_assert(entry->mode != GIT_FILEMODE_TREE);
if (expected_total_paths) {
const char *expect_path = expected_total_paths[count];
size_t expect_len = strlen(expect_path);
cl_assert_equal_s(expect_path, entry->path);
if (expect_path[expect_len - 1] == '/')
cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
else
cl_assert(entry->mode != GIT_FILEMODE_TREE);
}
if (entry->mode == GIT_FILEMODE_TREE) {
error = git_iterator_advance_into(&entry, i);
/* could return NOTFOUND if directory is empty */
cl_assert(!error || error == GIT_ENOTFOUND);
if (error == GIT_ENOTFOUND) {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
} else {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
if (++count >= expected_total)
break;
}
assert_at_end(i, v);
cl_assert_equal_i(expected_total, count);
}
void expect_advance_over(
git_iterator *i,
const char *expected_path,
git_iterator_status_t expected_status)
{
const git_index_entry *entry;
git_iterator_status_t status;
int error;
cl_git_pass(git_iterator_current(&entry, i));
cl_assert_equal_s(expected_path, entry->path);
error = git_iterator_advance_over(&entry, &status, i);
cl_assert(!error || error == GIT_ITEROVER);
cl_assert_equal_i(expected_status, status);
}
void expect_advance_into(
git_iterator *i,
const char *expected_path)
{
const git_index_entry *entry;
int error;
cl_git_pass(git_iterator_current(&entry, i));
cl_assert_equal_s(expected_path, entry->path);
if (S_ISDIR(entry->mode))
error = git_iterator_advance_into(&entry, i);
else
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
extern void expect_iterator_items(
git_iterator *i,
int expected_flat,
const char **expected_flat_paths,
int expected_total,
const char **expected_total_paths);
extern void expect_advance_over(
git_iterator *i,
const char *expected_path,
git_iterator_status_t expected_status);
void expect_advance_into(
git_iterator *i,
const char *expected_path);
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "diff_helpers.h"
#include "iterator.h" #include "iterator.h"
#include "repository.h"
#include "fileops.h"
#include "tree.h" #include "tree.h"
#include "../submodule/submodule_helpers.h"
#include "../diff/diff_helpers.h"
#include "iterator_helpers.h"
#include <stdarg.h>
void test_diff_iterator__initialize(void) static git_repository *g_repo;
void test_iterator_tree__initialize(void)
{ {
/* since we are doing tests with different sandboxes, defer setup
* to the actual tests. cleanup will still be done in the global
* cleanup function so that assertion failures don't result in a
* missed cleanup.
*/
} }
void test_diff_iterator__cleanup(void) void test_iterator_tree__cleanup(void)
{ {
cl_git_sandbox_cleanup(); cl_git_sandbox_cleanup();
g_repo = NULL;
} }
/* -- TREE ITERATOR TESTS -- */
static void tree_iterator_test( static void tree_iterator_test(
const char *sandbox, const char *sandbox,
const char *treeish, const char *treeish,
...@@ -33,13 +33,14 @@ static void tree_iterator_test( ...@@ -33,13 +33,14 @@ static void tree_iterator_test(
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry; const git_index_entry *entry;
int error, count = 0, count_post_reset = 0; int error, count = 0, count_post_reset = 0;
git_repository *repo = cl_git_sandbox_init(sandbox);
g_repo = cl_git_sandbox_init(sandbox);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = start; i_opts.start = start;
i_opts.end = end; i_opts.end = end;
cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish));
cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); cl_git_pass(git_iterator_for_tree(&i, t, &i_opts));
/* test loop */ /* test loop */
...@@ -54,7 +55,7 @@ static void tree_iterator_test( ...@@ -54,7 +55,7 @@ static void tree_iterator_test(
cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count, count);
/* test reset */ /* test reset */
cl_git_pass(git_iterator_reset(i, NULL, NULL)); cl_git_pass(git_iterator_reset(i));
while (!(error = git_iterator_advance(&entry, i))) { while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry); cl_assert(entry);
...@@ -91,7 +92,7 @@ const char *expected_tree_0[] = { ...@@ -91,7 +92,7 @@ const char *expected_tree_0[] = {
NULL NULL
}; };
void test_diff_iterator__tree_0(void) void test_iterator_tree__0(void)
{ {
tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0); tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0);
} }
...@@ -114,7 +115,7 @@ const char *expected_tree_1[] = { ...@@ -114,7 +115,7 @@ const char *expected_tree_1[] = {
NULL NULL
}; };
void test_diff_iterator__tree_1(void) void test_iterator_tree__1(void)
{ {
tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1); tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1);
} }
...@@ -136,7 +137,7 @@ const char *expected_tree_2[] = { ...@@ -136,7 +137,7 @@ const char *expected_tree_2[] = {
NULL NULL
}; };
void test_diff_iterator__tree_2(void) void test_iterator_tree__2(void)
{ {
tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2); tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2);
} }
...@@ -153,7 +154,7 @@ const char *expected_tree_3[] = { ...@@ -153,7 +154,7 @@ const char *expected_tree_3[] = {
"staged_delete_modified_file" "staged_delete_modified_file"
}; };
void test_diff_iterator__tree_3(void) void test_iterator_tree__3(void)
{ {
tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3); tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3);
} }
...@@ -186,14 +187,14 @@ const char *expected_tree_4[] = { ...@@ -186,14 +187,14 @@ const char *expected_tree_4[] = {
NULL NULL
}; };
void test_diff_iterator__tree_4(void) void test_iterator_tree__4(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL, "attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL,
23, expected_tree_4); 23, expected_tree_4);
} }
void test_diff_iterator__tree_4_ranged(void) void test_iterator_tree__4_ranged(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
...@@ -212,7 +213,7 @@ const char *expected_tree_ranged_0[] = { ...@@ -212,7 +213,7 @@ const char *expected_tree_ranged_0[] = {
NULL NULL
}; };
void test_diff_iterator__tree_ranged_0(void) void test_iterator_tree__ranged_0(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
...@@ -225,7 +226,7 @@ const char *expected_tree_ranged_1[] = { ...@@ -225,7 +226,7 @@ const char *expected_tree_ranged_1[] = {
NULL NULL
}; };
void test_diff_iterator__tree_ranged_1(void) void test_iterator_tree__ranged_1(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
...@@ -233,21 +234,21 @@ void test_diff_iterator__tree_ranged_1(void) ...@@ -233,21 +234,21 @@ void test_diff_iterator__tree_ranged_1(void)
1, expected_tree_ranged_1); 1, expected_tree_ranged_1);
} }
void test_diff_iterator__tree_range_empty_0(void) void test_iterator_tree__range_empty_0(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"empty", "empty", 0, NULL); "empty", "empty", 0, NULL);
} }
void test_diff_iterator__tree_range_empty_1(void) void test_iterator_tree__range_empty_1(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"z_empty_after", NULL, 0, NULL); "z_empty_after", NULL, 0, NULL);
} }
void test_diff_iterator__tree_range_empty_2(void) void test_iterator_tree__range_empty_2(void)
{ {
tree_iterator_test( tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", "attr", "24fa9a9fc4e202313e24b648087495441dab432b",
...@@ -264,51 +265,45 @@ static void check_tree_entry( ...@@ -264,51 +265,45 @@ static void check_tree_entry(
const git_index_entry *ie; const git_index_entry *ie;
const git_tree_entry *te; const git_tree_entry *te;
const git_tree *tree; const git_tree *tree;
git_buf path = GIT_BUF_INIT;
cl_git_pass(git_iterator_current_tree_entry(&te, i)); 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(&ie, i)); cl_git_pass(git_iterator_current(&ie, i));
cl_git_pass(git_buf_sets(&path, ie->path));
if (oid_p) { if (oid_p) {
git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, 0));
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, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, 1));
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, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, 2));
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);
} }
git_buf_free(&path);
} }
void test_diff_iterator__tree_special_functions(void) void test_iterator_tree__special_functions(void)
{ {
git_tree *t; git_tree *t;
git_iterator *i; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry; const git_index_entry *entry;
git_repository *repo = cl_git_sandbox_init("attr");
int error, cases = 0; int error, cases = 0;
const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e";
g_repo = cl_git_sandbox_init("attr");
t = resolve_commit_oid_to_tree( t = resolve_commit_oid_to_tree(
repo, "24fa9a9fc4e202313e24b648087495441dab432b"); g_repo, "24fa9a9fc4e202313e24b648087495441dab432b");
cl_assert(t != NULL); cl_assert(t != NULL);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
...@@ -355,658 +350,727 @@ void test_diff_iterator__tree_special_functions(void) ...@@ -355,658 +350,727 @@ void test_diff_iterator__tree_special_functions(void)
git_tree_free(t); git_tree_free(t);
} }
/* -- INDEX ITERATOR TESTS -- */ static void check_tree_range(
git_repository *repo,
static void index_iterator_test(
const char *sandbox,
const char *start, const char *start,
const char *end, const char *end,
git_iterator_flag_t flags, bool ignore_case,
int expected_count, int expected_count)
const char **expected_names,
const char **expected_oids)
{ {
git_index *index; git_tree *head;
git_iterator *i; git_iterator *i;
const git_index_entry *entry; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count = 0, caps; int error, count;
git_repository *repo = cl_git_sandbox_init(sandbox);
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
cl_git_pass(git_repository_index(&index, repo));
caps = git_index_caps(index);
iter_opts.flags = flags;
iter_opts.start = start;
iter_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, repo, index, &iter_opts));
while (!(error = git_iterator_advance(&entry, i))) { i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
cl_assert(entry); i_opts.start = start;
i_opts.end = end;
if (expected_names != NULL) cl_git_pass(git_repository_head_tree(&head, repo));
cl_assert_equal_s(expected_names[count], entry->path);
if (expected_oids != NULL) { cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
git_oid oid;
cl_git_pass(git_oid_fromstr(&oid, expected_oids[count]));
cl_assert_equal_oid(&oid, &entry->id);
}
count++; for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count)
} /* count em up */;
cl_assert_equal_i(GIT_ITEROVER, error); cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count, count);
git_iterator_free(i); git_iterator_free(i);
git_tree_free(head);
cl_assert(caps == git_index_caps(index));
git_index_free(index);
}
static const char *expected_index_0[] = {
"attr0",
"attr1",
"attr2",
"attr3",
"binfile",
"gitattributes",
"macro_bad",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"sub/abc",
"sub/file",
"sub/sub/file",
"sub/sub/subsub.txt",
"sub/subdir_test1",
"sub/subdir_test2.txt",
"subdir/.gitattributes",
"subdir/abc",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
};
static const char *expected_index_oids_0[] = {
"556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3",
"3b74db7ab381105dc0d28f8295a77f6a82989292",
"2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2",
"c485abe35abd4aa6fd83b076a78bbea9e2e7e06c",
"d800886d9c86731ae5c4a62b0b77c437015e00d2",
"2b40c5aca159b04ea8d20ffe36cdf8b09369b14a",
"5819a185d77b03325aaf87cafc771db36f6ddca7",
"ff69f8639ce2e6010b3f33a74160aad98b48da2b",
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"9e5bdc47d6a80f2be0ea3049ad74231b94609242",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"99eae476896f4907224978b88e5ecaa6c5bb67a9",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"dccada462d3df8ac6de596fb8c896aba9344f941"
};
void test_diff_iterator__index_0(void)
{
index_iterator_test(
"attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0),
expected_index_0, expected_index_oids_0);
}
static const char *expected_index_range[] = {
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
};
static const char *expected_index_oids_range[] = {
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
};
void test_diff_iterator__index_range(void)
{
index_iterator_test(
"attr", "root", "root", 0, ARRAY_SIZE(expected_index_range),
expected_index_range, expected_index_oids_range);
}
void test_diff_iterator__index_range_empty_0(void)
{
index_iterator_test(
"attr", "empty", "empty", 0, 0, NULL, NULL);
}
void test_diff_iterator__index_range_empty_1(void)
{
index_iterator_test(
"attr", "z_empty_after", NULL, 0, 0, NULL, NULL);
} }
void test_diff_iterator__index_range_empty_2(void) void test_iterator_tree__range_icase(void)
{ {
index_iterator_test( g_repo = cl_git_sandbox_init("testrepo");
"attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL);
check_tree_range(g_repo, "B", "C", false, 0);
check_tree_range(g_repo, "B", "C", true, 1);
check_tree_range(g_repo, "b", "c", false, 1);
check_tree_range(g_repo, "b", "c", true, 1);
check_tree_range(g_repo, "a", "z", false, 3);
check_tree_range(g_repo, "a", "z", true, 4);
check_tree_range(g_repo, "A", "Z", false, 1);
check_tree_range(g_repo, "A", "Z", true, 4);
check_tree_range(g_repo, "a", "Z", false, 0);
check_tree_range(g_repo, "a", "Z", true, 4);
check_tree_range(g_repo, "A", "z", false, 4);
check_tree_range(g_repo, "A", "z", true, 4);
check_tree_range(g_repo, "new.txt", "new.txt", true, 1);
check_tree_range(g_repo, "new.txt", "new.txt", false, 1);
check_tree_range(g_repo, "README", "README", true, 1);
check_tree_range(g_repo, "README", "README", false, 1);
} }
static const char *expected_index_1[] = { void test_iterator_tree__icase_0(void)
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_new_file",
"staged_new_file_deleted_file",
"staged_new_file_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
};
static const char* expected_index_oids_1[] = {
"a0de7e0ac200c489c41c59dfa910154a70264e6e",
"5452d32f1dd538eb0405e8a83cc185f79e25e80f",
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
"55d316c9ba708999f1918e9677d01dfcae69c6b9",
"a6be623522ce87a1d862128ac42672604f7b468b",
"906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8",
"529a16e8e762d4acb7b9636ff540a00831f9155a",
"90b8c29d8ba39434d1c63e1b093daaa26e5bd972",
"ed062903b8f6f3dccb2fa81117ba6590944ef9bd",
"e8ee89e15bbe9b20137715232387b3de5b28972e",
"53ace0d1cc1145a5f4fe4f78a186a60263190733",
"1888c805345ba265b0ee9449b8877b6064592058",
"a6191982709b746d5650e93c2acf34ef74e11504"
};
void test_diff_iterator__index_1(void)
{ {
index_iterator_test( git_iterator *i;
"status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1), git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
expected_index_1, expected_index_oids_1); git_tree *head;
}
static const char *expected_index_cs[] = {
"B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c",
"a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c",
};
static const char *expected_index_ci[] = { g_repo = cl_git_sandbox_init("icase");
"a", "B", "c", "D", "e", "F", "g", "H", "i", "J",
"k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D",
};
void test_diff_iterator__index_case_folding(void) cl_git_pass(git_repository_head_tree(&head, g_repo));
{
git_buf path = GIT_BUF_INIT;
int fs_is_ci = 0;
cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg")); /* auto expand with no tree entries */
fs_is_ci = git_path_exists(path.ptr); cl_git_pass(git_iterator_for_tree(&i, head, NULL));
git_buf_free(&path); expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
index_iterator_test( /* auto expand with tree entries */
"icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs), i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
fs_is_ci ? expected_index_ci : expected_index_cs, NULL);
cl_git_sandbox_cleanup(); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
index_iterator_test( /* no auto expand (implies trees included) */
"icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE, i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL);
cl_git_sandbox_cleanup(); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
index_iterator_test( git_tree_free(head);
"icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE,
ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL);
} }
/* -- WORKDIR ITERATOR TESTS -- */ void test_iterator_tree__icase_1(void)
static void workdir_iterator_test(
const char *sandbox,
const char *start,
const char *end,
int expected_count,
int expected_ignores,
const char **expected_names,
const char *an_ignored_name)
{ {
git_iterator *i; git_iterator *i;
git_tree *head;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, count = 0, count_all = 0, count_all_post_reset = 0;
git_repository *repo = cl_git_sandbox_init(sandbox);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; g_repo = cl_git_sandbox_init("icase");
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_workdir(&i, repo, NULL, NULL, &i_opts)); cl_git_pass(git_repository_head_tree(&head, g_repo));
error = git_iterator_current(&entry, i); i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
while (entry != NULL) { /* auto expand with no tree entries */
int ignored = git_iterator_current_is_ignored(i); i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
if (S_ISDIR(entry->mode)) { i_opts.start = "k";
cl_git_pass(git_iterator_advance_into(&entry, i)); i_opts.end = "k/Z";
continue; cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
} expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
if (expected_names != NULL) i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_assert_equal_s(expected_names[count_all], entry->path);
if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0) /* auto expand with tree entries */
cl_assert(ignored); i_opts.start = "c";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
if (!ignored) i_opts.start = "k";
count++; i_opts.end = "k/Z";
count_all++; cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
error = git_iterator_advance(&entry, i); /* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
cl_assert((error == 0 && entry != NULL) || i_opts.start = "k";
(error == GIT_ITEROVER && entry == NULL)); i_opts.end = "k/D";
} cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
cl_assert_equal_i(expected_count, count); /* auto expand with no tree entries */
cl_assert_equal_i(expected_count + expected_ignores, count_all); i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_reset(i, NULL, NULL)); i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
error = git_iterator_current(&entry, i); i_opts.start = "k";
cl_assert((error == 0 && entry != NULL) || i_opts.end = "k/Z";
(error == GIT_ITEROVER && entry == NULL)); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
while (entry != NULL) { /* auto expand with tree entries */
if (S_ISDIR(entry->mode)) { i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_advance_into(&entry, i));
continue;
}
if (expected_names != NULL) i_opts.start = "c";
cl_assert_equal_s( i_opts.end = "k/D";
expected_names[count_all_post_reset], entry->path); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
count_all_post_reset++; expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
error = git_iterator_advance(&entry, i); i_opts.start = "k";
cl_assert(error == 0 || error == GIT_ITEROVER); i_opts.end = "k/Z";
} cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
cl_assert_equal_i(count_all, count_all_post_reset); /* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i); git_iterator_free(i);
}
void test_diff_iterator__workdir_0(void) i_opts.start = "k";
{ i_opts.end = "k/Z";
workdir_iterator_test("attr", NULL, NULL, 23, 5, NULL, "ign"); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
git_tree_free(head);
} }
static const char *status_paths[] = { void test_iterator_tree__icase_2(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head;
static const char *expect_basic[] = {
"current_file", "current_file",
"ignored_file", "file_deleted",
"modified_file", "modified_file",
"new_file",
"staged_changes", "staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file", "staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file", "staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
"subdir.txt", "subdir.txt",
"subdir/current_file", "subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file", "subdir/modified_file",
"subdir/new_file", NULL,
"\xe8\xbf\x99", };
NULL static const char *expect_trees[] = {
}; "current_file",
"file_deleted",
void test_diff_iterator__workdir_1(void) "modified_file",
{
workdir_iterator_test(
"status", NULL, NULL, 13, 1, status_paths, "ignored_file");
}
static const char *status_paths_range_0[] = {
"staged_changes", "staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file", "staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file", "staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
NULL
};
void test_diff_iterator__workdir_1_ranged_0(void)
{
workdir_iterator_test(
"status", "staged", "staged", 5, 0, status_paths_range_0, NULL);
}
static const char *status_paths_range_1[] = {
"modified_file", NULL
};
void test_diff_iterator__workdir_1_ranged_1(void)
{
workdir_iterator_test(
"status", "modified_file", "modified_file",
1, 0, status_paths_range_1, NULL);
}
static const char *status_paths_range_3[] = {
"subdir.txt", "subdir.txt",
"subdir/",
"subdir/current_file", "subdir/current_file",
"subdir/deleted_file",
"subdir/modified_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 NULL
}; };
void test_diff_iterator__workdir_1_ranged_3(void) g_repo = cl_git_sandbox_init("status");
{
workdir_iterator_test(
"status", "subdir", "subdir/modified_file",
3, 0, status_paths_range_3, NULL);
}
static const char *status_paths_range_4[] = { cl_git_pass(git_repository_head_tree(&head, g_repo));
"subdir/current_file",
"subdir/modified_file",
"subdir/new_file",
"\xe8\xbf\x99",
NULL
};
void test_diff_iterator__workdir_1_ranged_4(void) /* auto expand with no tree entries */
{ cl_git_pass(git_iterator_for_tree(&i, head, NULL));
workdir_iterator_test( expect_iterator_items(i, 12, expect_basic, 12, expect_basic);
"status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL); git_iterator_free(i);
}
static const char *status_paths_range_5[] = { /* auto expand with tree entries */
"subdir/modified_file", i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
NULL
};
void test_diff_iterator__workdir_1_ranged_5(void) cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
{ expect_iterator_items(i, 13, expect_trees, 13, expect_trees);
workdir_iterator_test( git_iterator_free(i);
"status", "subdir/modified_file", "subdir/modified_file",
1, 0, status_paths_range_5, NULL);
}
void test_diff_iterator__workdir_1_ranged_empty_0(void) /* no auto expand (implies trees included) */
{ i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
workdir_iterator_test(
"status", "\xff_does_not_exist", NULL,
0, 0, NULL, NULL);
}
void test_diff_iterator__workdir_1_ranged_empty_1(void) cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
{ expect_iterator_items(i, 10, expect_noauto, 13, expect_trees);
workdir_iterator_test( git_iterator_free(i);
"status", "empty", "empty",
0, 0, NULL, NULL); git_tree_free(head);
} }
void test_diff_iterator__workdir_1_ranged_empty_2(void) /* "b=name,t=name", blob_id, tree_id */
static void build_test_tree(
git_oid *out, git_repository *repo, const char *fmt, ...)
{ {
workdir_iterator_test( git_oid *id;
"status", NULL, "aaaa_empty_before", git_treebuilder *builder;
0, 0, NULL, NULL); const char *scan = fmt, *next;
char type, delimiter;
git_filemode_t mode = GIT_FILEMODE_BLOB;
git_buf name = GIT_BUF_INIT;
va_list arglist;
cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); /* start builder */
va_start(arglist, fmt);
while (*scan) {
switch (type = *scan++) {
case 't': case 'T': mode = GIT_FILEMODE_TREE; break;
case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break;
default:
cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B');
}
delimiter = *scan++; /* read and skip delimiter */
for (next = scan; *next && *next != delimiter; ++next)
/* seek end */;
cl_git_pass(git_buf_set(&name, scan, (size_t)(next - scan)));
for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan)
/* skip delimiter and optional comma */;
id = va_arg(arglist, git_oid *);
cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode));
}
va_end(arglist);
cl_git_pass(git_treebuilder_write(out, builder));
git_treebuilder_free(builder);
git_buf_free(&name);
} }
void test_diff_iterator__workdir_builtin_ignores(void) void test_iterator_tree__case_conflicts_0(void)
{ {
git_repository *repo = cl_git_sandbox_init("attr"); const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, biga_id, littlea_id, tree_id;
git_iterator *i; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int idx;
static struct {
const char *path;
bool ignored;
} expected[] = {
{ "dir/", true },
{ "file", false },
{ "ign", true },
{ "macro_bad", false },
{ "macro_test", false },
{ "root_test1", false },
{ "root_test2", false },
{ "root_test3", false },
{ "root_test4.txt", false },
{ "sub/", false },
{ "sub/.gitattributes", false },
{ "sub/abc", false },
{ "sub/dir/", true },
{ "sub/file", false },
{ "sub/ign/", true },
{ "sub/sub/", false },
{ "sub/sub/.gitattributes", false },
{ "sub/sub/dir", false }, /* file is not actually a dir */
{ "sub/sub/file", false },
{ NULL, false }
};
cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); const char *expect_cs[] = {
cl_git_mkfile("attr/sub/.git", "whatever"); "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" };
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; g_repo = cl_git_sandbox_init("icase");
i_opts.start = "dir";
i_opts.end = "sub/sub/file"; cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
cl_git_pass(git_iterator_for_workdir( /* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */
&i, repo, NULL, NULL, &i_opts)); build_test_tree(
cl_git_pass(git_iterator_current(&entry, i)); &biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id);
build_test_tree(
for (idx = 0; entry != NULL; ++idx) { &littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id);
int ignored = git_iterator_current_is_ignored(i); build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_assert_equal_s(expected[idx].path, entry->path);
cl_assert_(ignored == expected[idx].ignored, expected[idx].path);
if (!ignored &&
(entry->mode == GIT_FILEMODE_TREE ||
entry->mode == GIT_FILEMODE_COMMIT))
{
/* it is possible to advance "into" a submodule */
cl_git_pass(git_iterator_advance_into(&entry, i));
} else {
int error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
}
cl_assert(expected[idx].path == NULL); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_cs, 4, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees);
git_iterator_free(i); git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
} }
static void check_wd_first_through_third_range( void test_iterator_tree__case_conflicts_1(void)
git_repository *repo, const char *start, const char *end)
{ {
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id;
git_iterator *i; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, idx; const char *expect_cs[] = {
static const char *expected[] = { "FIRST", "second", "THIRD", NULL }; "A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" };
const char *expect_ci[] = {
"A/a", "a/b", "A/b/1", "A/c" };
const char *expect_cs_trees[] = {
"A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" };
const char *expect_ci_trees[] = {
"A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
/* create: A/a A/b/1 A/c a/a a/b a/C */
build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id);
build_test_tree(
&biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id);
build_test_tree(
&littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id);
build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs, 6, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE; i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = start; cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
i_opts.end = end; expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
cl_git_pass(git_iterator_for_workdir( i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
&i, repo, NULL, NULL, &i_opts)); cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
cl_git_pass(git_iterator_current(&entry, i)); expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees);
git_iterator_free(i);
for (idx = 0; entry != NULL; ++idx) { i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_assert_equal_s(expected[idx], entry->path); cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees);
git_iterator_free(i);
error = git_iterator_advance(&entry, i); git_tree_free(tree);
cl_assert(!error || error == GIT_ITEROVER); }
}
void test_iterator_tree__case_conflicts_2(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO",
"A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO",
"A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO",
"A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO",
"a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO",
"a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO",
"a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO",
"a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", };
const char *expect_ci[] = {
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
const char *expect_ci_trees[] = {
"A/", "A/B/", "A/B/C/", "A/B/C/D/",
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 32, expect_cs, 32, expect_cs);
git_iterator_free(i);
cl_assert(expected[idx] == NULL); i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 17, expect_ci, 17, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees);
git_iterator_free(i); git_iterator_free(i);
git_tree_free(tree);
} }
void test_diff_iterator__workdir_handles_icase_range(void) void test_iterator_tree__pathlist(void)
{ {
git_repository *repo; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
repo = cl_git_sandbox_init("empty_standard_repo"); git_vector filelist;
cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt"); git_tree *tree;
bool default_icase;
cl_git_mkfile("empty_standard_repo/before", "whatever\n"); int expect;
cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n");
cl_git_mkfile("empty_standard_repo/second", "whatever\n"); cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n"); cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_mkfile("empty_standard_repo/zafter", "whatever\n"); cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n"); cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
check_wd_first_through_third_range(repo, "first", "third"); cl_git_pass(git_vector_insert(&filelist, "e"));
check_wd_first_through_third_range(repo, "FIRST", "THIRD"); cl_git_pass(git_vector_insert(&filelist, "k.a"));
check_wd_first_through_third_range(repo, "first", "THIRD"); cl_git_pass(git_vector_insert(&filelist, "k.b"));
check_wd_first_through_third_range(repo, "FIRST", "third"); cl_git_pass(git_vector_insert(&filelist, "k/1"));
check_wd_first_through_third_range(repo, "FirSt", "tHiRd"); cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
/* In this test we DO NOT force a case on the iterators and verify default behavior. */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "c";
i_opts.end = NULL;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */
expect = ((default_icase) ? 6 : 4);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
i_opts.start = NULL;
i_opts.end = "e";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */
expect = ((default_icase) ? 5 : 6);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
git_tree_free(tree);
} }
static void check_tree_range( void test_iterator_tree__pathlist_icase(void)
git_repository *repo,
const char *start,
const char *end,
bool ignore_case,
int expected_count)
{ {
git_tree *head;
git_iterator *i; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count; git_vector filelist;
git_tree *tree;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = start; i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.end = end; i_opts.pathlist.count = filelist.length;
cl_git_pass(git_repository_head_tree(&head, repo)); i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
/* count em up */;
cl_assert_equal_i(GIT_ITEROVER, error); i_opts.start = "c";
cl_assert_equal_i(expected_count, count); i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i); git_iterator_free(i);
git_tree_free(head);
}
void test_diff_iterator__tree_handles_icase_range(void) git_vector_free(&filelist);
{ git_tree_free(tree);
git_repository *repo;
repo = cl_git_sandbox_init("testrepo");
check_tree_range(repo, "B", "C", false, 0);
check_tree_range(repo, "B", "C", true, 1);
check_tree_range(repo, "b", "c", false, 1);
check_tree_range(repo, "b", "c", true, 1);
check_tree_range(repo, "a", "z", false, 3);
check_tree_range(repo, "a", "z", true, 4);
check_tree_range(repo, "A", "Z", false, 1);
check_tree_range(repo, "A", "Z", true, 4);
check_tree_range(repo, "a", "Z", false, 0);
check_tree_range(repo, "a", "Z", true, 4);
check_tree_range(repo, "A", "z", false, 4);
check_tree_range(repo, "A", "z", true, 4);
check_tree_range(repo, "new.txt", "new.txt", true, 1);
check_tree_range(repo, "new.txt", "new.txt", false, 1);
check_tree_range(repo, "README", "README", true, 1);
check_tree_range(repo, "README", "README", false, 1);
} }
static void check_index_range( void test_iterator_tree__pathlist_with_directory(void)
git_repository *repo,
const char *start,
const char *end,
bool ignore_case,
int expected_count)
{ {
git_index *index;
git_iterator *i; git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count, caps; git_vector filelist;
bool is_ignoring_case; git_tree *tree;
cl_git_pass(git_repository_index(&index, repo)); const char *expected[] = { "subdir/README", "subdir/new.txt",
"subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 4;
caps = git_index_caps(index); const char *expected2[] = { "subdir/subdir2/README", "subdir/subdir2/new.txt" };
is_ignoring_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); size_t expected_len2 = 2;
if (ignore_case != is_ignoring_case) g_repo = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEXCAP_IGNORE_CASE)); git_repository_head_tree(&tree, g_repo);
i_opts.flags = 0; cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
i_opts.start = start; cl_git_pass(git_vector_insert(&filelist, "subdir"));
i_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, repo, index, &i_opts)); i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
cl_assert(git_iterator_ignore_case(i) == ignore_case); cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) git_vector_clear(&filelist);
/* count em up */; cl_git_pass(git_vector_insert(&filelist, "subdir/"));
cl_assert_equal_i(GIT_ITEROVER, error); i_opts.pathlist.strings = (char **)filelist.contents;
cl_assert_equal_i(expected_count, count); i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i); git_iterator_free(i);
git_index_free(index);
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "subdir/subdir2"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len2, expected2, expected_len2, expected2);
git_iterator_free(i);
git_vector_free(&filelist);
} }
void test_diff_iterator__index_handles_icase_range(void) void test_iterator_tree__pathlist_with_directory_include_tree_nodes(void)
{ {
git_repository *repo; git_iterator *i;
git_index *index; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head; git_vector filelist;
git_tree *tree;
repo = cl_git_sandbox_init("testrepo"); const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt",
"subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 6;
/* reset index to match HEAD */ g_repo = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_repository_head_tree(&head, repo)); git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_read_tree(index, head)); cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_index_write(index)); cl_git_pass(git_vector_insert(&filelist, "subdir"));
git_tree_free(head);
git_index_free(index);
/* do some ranged iterator checks toggling case sensitivity */ i_opts.pathlist.strings = (char **)filelist.contents;
check_index_range(repo, "B", "C", false, 0); i_opts.pathlist.count = filelist.length;
check_index_range(repo, "B", "C", true, 1); i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
check_index_range(repo, "a", "z", false, 3);
check_index_range(repo, "a", "z", true, 4); cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_free(&filelist);
} }
void test_iterator_tree__pathlist_no_match(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
const git_index_entry *entry;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "nonexistent/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_current(&entry, i));
git_vector_free(&filelist);
}
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "fileops.h"
#include "../submodule/submodule_helpers.h"
#include "iterator_helpers.h"
#include <stdarg.h>
static git_repository *g_repo;
void test_iterator_workdir__initialize(void)
{
}
void test_iterator_workdir__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void workdir_iterator_test(
const char *sandbox,
const char *start,
const char *end,
int expected_count,
int expected_ignores,
const char **expected_names,
const char *an_ignored_name)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, count = 0, count_all = 0, count_all_post_reset = 0;
g_repo = cl_git_sandbox_init(sandbox);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
error = git_iterator_current(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
while (entry != NULL) {
int ignored = git_iterator_current_is_ignored(i);
if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into(&entry, i));
continue;
}
if (expected_names != NULL)
cl_assert_equal_s(expected_names[count_all], entry->path);
if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0)
cl_assert(ignored);
if (!ignored)
count++;
count_all++;
error = git_iterator_advance(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
}
cl_assert_equal_i(expected_count, count);
cl_assert_equal_i(expected_count + expected_ignores, count_all);
cl_git_pass(git_iterator_reset(i));
error = git_iterator_current(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
while (entry != NULL) {
if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into(&entry, i));
continue;
}
if (expected_names != NULL)
cl_assert_equal_s(
expected_names[count_all_post_reset], entry->path);
count_all_post_reset++;
error = git_iterator_advance(&entry, i);
cl_assert(error == 0 || error == GIT_ITEROVER);
}
cl_assert_equal_i(count_all, count_all_post_reset);
git_iterator_free(i);
}
void test_iterator_workdir__0(void)
{
workdir_iterator_test("attr", NULL, NULL, 23, 5, NULL, "ign");
}
static const char *status_paths[] = {
"current_file",
"ignored_file",
"modified_file",
"new_file",
"staged_changes",
"staged_changes_modified_file",
"staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/modified_file",
"subdir/new_file",
"\xe8\xbf\x99",
NULL
};
void test_iterator_workdir__1(void)
{
workdir_iterator_test(
"status", NULL, NULL, 13, 1, status_paths, "ignored_file");
}
static const char *status_paths_range_0[] = {
"staged_changes",
"staged_changes_modified_file",
"staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
NULL
};
void test_iterator_workdir__1_ranged_0(void)
{
workdir_iterator_test(
"status", "staged", "staged", 5, 0, status_paths_range_0, NULL);
}
static const char *status_paths_range_1[] = {
"modified_file", NULL
};
void test_iterator_workdir__1_ranged_1(void)
{
workdir_iterator_test(
"status", "modified_file", "modified_file",
1, 0, status_paths_range_1, NULL);
}
static const char *status_paths_range_3[] = {
"subdir.txt",
"subdir/current_file",
"subdir/modified_file",
NULL
};
void test_iterator_workdir__1_ranged_3(void)
{
workdir_iterator_test(
"status", "subdir", "subdir/modified_file",
3, 0, status_paths_range_3, NULL);
}
static const char *status_paths_range_4[] = {
"subdir/current_file",
"subdir/modified_file",
"subdir/new_file",
"\xe8\xbf\x99",
NULL
};
void test_iterator_workdir__1_ranged_4(void)
{
workdir_iterator_test(
"status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL);
}
static const char *status_paths_range_5[] = {
"subdir/modified_file",
NULL
};
void test_iterator_workdir__1_ranged_5(void)
{
workdir_iterator_test(
"status", "subdir/modified_file", "subdir/modified_file",
1, 0, status_paths_range_5, NULL);
}
void test_iterator_workdir__1_ranged_5_1_ranged_empty_0(void)
{
workdir_iterator_test(
"status", "\xff_does_not_exist", NULL,
0, 0, NULL, NULL);
}
void test_iterator_workdir__1_ranged_empty_1(void)
{
workdir_iterator_test(
"status", "empty", "empty",
0, 0, NULL, NULL);
}
void test_iterator_workdir__1_ranged_empty_2(void)
{
workdir_iterator_test(
"status", NULL, "aaaa_empty_before",
0, 0, NULL, NULL);
}
void test_iterator_workdir__builtin_ignores(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int idx;
static struct {
const char *path;
bool ignored;
} expected[] = {
{ "dir/", true },
{ "file", false },
{ "ign", true },
{ "macro_bad", false },
{ "macro_test", false },
{ "root_test1", false },
{ "root_test2", false },
{ "root_test3", false },
{ "root_test4.txt", false },
{ "sub/", false },
{ "sub/.gitattributes", false },
{ "sub/abc", false },
{ "sub/dir/", true },
{ "sub/file", false },
{ "sub/ign/", true },
{ "sub/sub/", false },
{ "sub/sub/.gitattributes", false },
{ "sub/sub/dir", false }, /* file is not actually a dir */
{ "sub/sub/file", false },
{ NULL, false }
};
g_repo = cl_git_sandbox_init("attr");
cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777));
cl_git_mkfile("attr/sub/.git", "whatever");
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "dir";
i_opts.end = "sub/sub/file";
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, NULL, NULL, &i_opts));
cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) {
int ignored = git_iterator_current_is_ignored(i);
cl_assert_equal_s(expected[idx].path, entry->path);
cl_assert_(ignored == expected[idx].ignored, expected[idx].path);
if (!ignored &&
(entry->mode == GIT_FILEMODE_TREE ||
entry->mode == GIT_FILEMODE_COMMIT))
{
/* it is possible to advance "into" a submodule */
cl_git_pass(git_iterator_advance_into(&entry, i));
} else {
int error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
}
cl_assert(expected[idx].path == NULL);
git_iterator_free(i);
}
static void check_wd_first_through_third_range(
git_repository *repo, const char *start, const char *end)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, idx;
static const char *expected[] = { "FIRST", "second", "THIRD", NULL };
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_workdir(
&i, repo, NULL, NULL, &i_opts));
cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) {
cl_assert_equal_s(expected[idx], entry->path);
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
cl_assert(expected[idx] == NULL);
git_iterator_free(i);
}
void test_iterator_workdir__handles_icase_range(void)
{
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
cl_git_mkfile("empty_standard_repo/before", "whatever\n");
cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n");
cl_git_mkfile("empty_standard_repo/second", "whatever\n");
cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n");
cl_git_mkfile("empty_standard_repo/zafter", "whatever\n");
cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n");
check_wd_first_through_third_range(g_repo, "first", "third");
check_wd_first_through_third_range(g_repo, "FIRST", "THIRD");
check_wd_first_through_third_range(g_repo, "first", "THIRD");
check_wd_first_through_third_range(g_repo, "FIRST", "third");
check_wd_first_through_third_range(g_repo, "FirSt", "tHiRd");
}
/*
* The workdir iterator is like the filesystem iterator, but honors
* special git type constructs (ignores, submodules, etc).
*/
void test_iterator_workdir__icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
}
void test_iterator_workdir__icase_starts_and_ends(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
}
static void build_workdir_tree(const char *root, int dirs, int subs)
{
int i, j;
char buf[64], sub[64];
for (i = 0; i < dirs; ++i) {
if (i % 2 == 0) {
p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i);
cl_git_mkfile(buf, buf);
buf[strlen(buf) - 5] = '\0';
} else {
p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
}
for (j = 0; j < subs; ++j) {
switch (j % 4) {
case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break;
case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break;
case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break;
case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break;
}
cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH));
if (j % 2 == 0) {
size_t sublen = strlen(sub);
memcpy(&sub[sublen], "/file", sizeof("/file"));
cl_git_mkfile(sub, sub);
sub[sublen] = '\0';
}
}
}
}
void test_iterator_workdir__depth(void)
{
git_iterator *iter;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
build_workdir_tree("icase", 10, 10);
build_workdir_tree("icase/DIR01/sUB01", 50, 0);
build_workdir_tree("icase/dir02/sUB01", 50, 0);
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 125, NULL, 125, NULL);
git_iterator_free(iter);
/* auto expand with tree entries (empty dirs silently skipped) */
iter_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 337, NULL, 337, NULL);
git_iterator_free(iter);
}
/* The filesystem iterator is a workdir iterator without any special
* workdir handling capabilities (ignores, submodules, etc).
*/
void test_iterator_workdir__filesystem(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
static const char *expect_base[] = {
"DIR01/Sub02/file",
"DIR01/sub00/file",
"current_file",
"dir00/Sub02/file",
"dir00/file",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_trees[] = {
"DIR01/",
"DIR01/SUB03/",
"DIR01/Sub02/",
"DIR01/Sub02/file",
"DIR01/sUB01/",
"DIR01/sub00/",
"DIR01/sub00/file",
"current_file",
"dir00/",
"dir00/SUB03/",
"dir00/Sub02/",
"dir00/Sub02/file",
"dir00/file",
"dir00/sUB01/",
"dir00/sub00/",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_noauto[] = {
"DIR01/",
"current_file",
"dir00/",
"modified_file",
"new_file",
NULL,
};
g_repo = cl_git_sandbox_init("status");
build_workdir_tree("status/subdir", 2, 4);
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", NULL));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
}
void test_iterator_workdir__filesystem2(void)
{
git_iterator *i;
static const char *expect_base[] = {
"heads/br2",
"heads/dir",
"heads/ident",
"heads/long-file-name",
"heads/master",
"heads/packed-test",
"heads/subtrees",
"heads/test",
"tags/e90810b",
"tags/foo/bar",
"tags/foo/foo/bar",
"tags/point_to_blob",
"tags/test",
NULL,
};
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_iterator_for_filesystem(
&i, "testrepo/.git/refs", NULL));
expect_iterator_items(i, 13, expect_base, 13, expect_base);
git_iterator_free(i);
}
/* Lots of empty dirs, or nearly empty ones, make the old workdir
* iterator cry. Also, segfault.
*/
void test_iterator_workdir__filesystem_gunk(void)
{
git_iterator *i;
git_buf parent = GIT_BUF_INIT;
int n;
if (!cl_is_env_set("GITTEST_INVASIVE_SPEED"))
cl_skip();
g_repo = cl_git_sandbox_init("testrepo");
for (n = 0; n < 100000; n++) {
git_buf_clear(&parent);
git_buf_printf(&parent, "%s/refs/heads/foo/%d/subdir",
git_repository_path(g_repo), n);
cl_assert(!git_buf_oom(&parent));
cl_git_pass(git_futils_mkdir(parent.ptr, 0775, GIT_MKDIR_PATH));
}
cl_git_pass(git_iterator_for_filesystem(&i, "testrepo/.git/refs", NULL));
/* should only have 13 items, since we're not asking for trees to be
* returned. the goal of this test is simply to not crash.
*/
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
git_buf_free(&parent);
}
void test_iterator_workdir__skips_unreadable_dirs(void)
{
git_iterator *i;
const git_index_entry *e;
if (!cl_is_chmod_supported())
return;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/r", 0777));
cl_git_mkfile("empty_standard_repo/r/a", "hello");
cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777));
cl_git_mkfile("empty_standard_repo/r/b/problem", "not me");
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000));
cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777));
cl_git_mkfile("empty_standard_repo/r/c/foo", "aloha");
cl_git_mkfile("empty_standard_repo/r/d", "final");
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo/r", NULL));
cl_git_pass(git_iterator_advance(&e, i)); /* a */
cl_assert_equal_s("a", e->path);
cl_git_pass(git_iterator_advance(&e, i)); /* c/foo */
cl_assert_equal_s("c/foo", e->path);
cl_git_pass(git_iterator_advance(&e, i)); /* d */
cl_assert_equal_s("d", e->path);
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
git_iterator_free(i);
}
void test_iterator_workdir__skips_fifos_and_special_files(void)
{
#ifndef GIT_WIN32
git_iterator *i;
const git_index_entry *e;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/dir", 0777));
cl_git_mkfile("empty_standard_repo/file", "not me");
cl_assert(!mkfifo("empty_standard_repo/fifo", 0777));
cl_assert(!access("empty_standard_repo/fifo", F_OK));
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo", &i_opts));
cl_git_pass(git_iterator_advance(&e, i)); /* .git */
cl_assert(S_ISDIR(e->mode));
cl_git_pass(git_iterator_advance(&e, i)); /* dir */
cl_assert(S_ISDIR(e->mode));
/* skips fifo */
cl_git_pass(git_iterator_advance(&e, i)); /* file */
cl_assert(S_ISREG(e->mode));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
git_iterator_free(i);
#endif
}
void test_iterator_workdir__pathlist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, NULL));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
/* Test iterators without returning tree entries (but autoexpanding.) */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
/* Case sensitive */
{
const char *expected[] = {
"B", "D", "L/1", "a", "c", "e", "k/1", "k/a" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Case INsensitive */
{
const char *expected[] = {
"a", "B", "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case sensitive. */
{
const char *expected[] = { "c", "e", "k/1", "k/a" };
size_t expected_len = 4;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case INsensitive. */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 6;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case sensitive. */
{
const char *expected[] = { "B", "D", "L/1", "a", "c", "e" };
size_t expected_len = 6;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case INsensitive. */
{
const char *expected[] = { "a", "B", "c", "D", "e" };
size_t expected_len = 5;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "c", "e", "k/1" };
size_t expected_len = 3;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "k/1" };
size_t expected_len = 1;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a" };
size_t expected_len = 5;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "k/1", "k/a" };
size_t expected_len = 2;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
void test_iterator_workdir__pathlist_with_dirs(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
/* Test that a prefix `k` matches folders, even without trailing slash */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a `k/` matches a folder */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* When the iterator is case sensitive, ensure we can't lookup the
* directory with the wrong case.
*/
{
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
/* Test that case insensitive matching works. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that case insensitive matching works without trailing slash. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
static void create_paths(const char *root, int depth)
{
git_buf fullpath = GIT_BUF_INIT;
size_t root_len;
int i;
cl_git_pass(git_buf_puts(&fullpath, root));
cl_git_pass(git_buf_putc(&fullpath, '/'));
root_len = fullpath.size;
for (i = 0; i < 8; i++) {
bool file = (depth == 0 || (i % 2) == 0);
git_buf_truncate(&fullpath, root_len);
cl_git_pass(git_buf_printf(&fullpath, "item%d", i));
if (file) {
cl_git_rewritefile(fullpath.ptr, "This is a file!\n");
} else {
cl_must_pass(p_mkdir(fullpath.ptr, 0777));
if (depth > 0)
create_paths(fullpath.ptr, (depth - 1));
}
}
}
void test_iterator_workdir__pathlist_for_deeply_nested_item(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
create_paths(git_repository_workdir(g_repo), 3);
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = { "item1/item3/item5/item7" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(4, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
};
size_t expected_len = 8;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(11, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item1/item3/item0",
"item1/item3/item1/item0", "item1/item3/item1/item1",
"item1/item3/item1/item2", "item1/item3/item1/item3",
"item1/item3/item1/item4", "item1/item3/item1/item5",
"item1/item3/item1/item6", "item1/item3/item1/item7",
"item1/item3/item2",
"item1/item3/item3/item0", "item1/item3/item3/item1",
"item1/item3/item3/item2", "item1/item3/item3/item3",
"item1/item3/item3/item4", "item1/item3/item3/item5",
"item1/item3/item3/item6", "item1/item3/item3/item7",
"item1/item3/item4",
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
"item1/item3/item6",
"item1/item3/item7/item0", "item1/item3/item7/item1",
"item1/item3/item7/item2", "item1/item3/item7/item3",
"item1/item3/item7/item4", "item1/item3/item7/item5",
"item1/item3/item7/item6", "item1/item3/item7/item7",
};
size_t expected_len = 36;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(42, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item0", "item1/item2", "item5/item7/item4", "item6",
"item7/item3/item1/item6" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6"));
cl_git_pass(git_vector_insert(&filelist, "item6"));
cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4"));
cl_git_pass(git_vector_insert(&filelist, "item1/item2"));
cl_git_pass(git_vector_insert(&filelist, "item0"));
/* also add some things that don't exist or don't match the right type */
cl_git_pass(git_vector_insert(&filelist, "item2/"));
cl_git_pass(git_vector_insert(&filelist, "itemN"));
cl_git_pass(git_vector_insert(&filelist, "item1/itemA"));
cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(14, i->stat_calls);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
void test_iterator_workdir__bounded_submodules(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_index *index;
git_tree *head;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = setup_fixture_submod2();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* Test that a submodule matches */
{
const char *expected[] = { "sm_changed_head" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "sm_changed_head"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a submodule never matches when suffixed with a '/' */
{
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "sm_changed_head/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
/* Test that start/end work with a submodule */
{
const char *expected[] = { "sm_changed_head", "sm_changed_index" };
size_t expected_len = 2;
i_opts.start = "sm_changed_head";
i_opts.end = "sm_changed_index";
i_opts.pathlist.strings = NULL;
i_opts.pathlist.count = 0;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that start and end do not allow '/' suffixes of submodules */
{
i_opts.start = "sm_changed_head/";
i_opts.end = "sm_changed_head/";
i_opts.pathlist.strings = NULL;
i_opts.pathlist.count = 0;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
git_vector_free(&filelist);
git_index_free(index);
git_tree_free(head);
}
void test_iterator_workdir__advance_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
/* create an empty directory */
cl_must_pass(p_mkdir("icase/empty", 0777));
/* create a directory in which all contents are ignored */
cl_must_pass(p_mkdir("icase/all_ignored", 0777));
cl_git_rewritefile("icase/all_ignored/one", "This is ignored\n");
cl_git_rewritefile("icase/all_ignored/two", "This, too, is ignored\n");
cl_git_rewritefile("icase/all_ignored/.gitignore", ".gitignore\none\ntwo\n");
/* create a directory in which not all contents are ignored */
cl_must_pass(p_mkdir("icase/some_ignored", 0777));
cl_git_rewritefile("icase/some_ignored/one", "This is ignored\n");
cl_git_rewritefile("icase/some_ignored/two", "This is not ignored\n");
cl_git_rewritefile("icase/some_ignored/.gitignore", ".gitignore\none\n");
/* create a directory which has some empty children */
cl_must_pass(p_mkdir("icase/empty_children", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty1", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty2", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty3", 0777));
/* create a directory which will disappear! */
cl_must_pass(p_mkdir("icase/missing_directory", 0777));
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
cl_must_pass(p_rmdir("icase/missing_directory"));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "all_ignored/", GIT_ITERATOR_STATUS_IGNORED);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "empty/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "empty_children/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "missing_directory/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "some_ignored/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
void test_iterator_workdir__advance_over_with_pathlist(void)
{
git_vector pathlist = GIT_VECTOR_INIT;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector_insert(&pathlist, "dirA/subdir1/subdir2/file");
git_vector_insert(&pathlist, "dirB/subdir1/subdir2");
git_vector_insert(&pathlist, "dirC/subdir1/nonexistent");
git_vector_insert(&pathlist, "dirD/subdir1/nonexistent");
git_vector_insert(&pathlist, "dirD/subdir1/subdir2");
git_vector_insert(&pathlist, "dirD/nonexistent");
i_opts.pathlist.strings = (char **)pathlist.contents;
i_opts.pathlist.count = pathlist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
/* Create a directory that has a file that is included in our pathlist */
cl_must_pass(p_mkdir("icase/dirA", 0777));
cl_must_pass(p_mkdir("icase/dirA/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirA/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirA/subdir1/subdir2/file", "foo!");
/* Create a directory that has a directory that is included in our pathlist */
cl_must_pass(p_mkdir("icase/dirB", 0777));
cl_must_pass(p_mkdir("icase/dirB/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirB/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirB/subdir1/subdir2/file", "foo!");
/* Create a directory that would contain an entry in our pathlist, but
* that entry does not actually exist. We don't know this until we
* advance_over it. We want to distinguish this from an actually empty
* or ignored directory.
*/
cl_must_pass(p_mkdir("icase/dirC", 0777));
cl_must_pass(p_mkdir("icase/dirC/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirC/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirC/subdir1/subdir2/file", "foo!");
/* Create a directory that has a mix of actual and nonexistent paths */
cl_must_pass(p_mkdir("icase/dirD", 0777));
cl_must_pass(p_mkdir("icase/dirD/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirD/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirD/subdir1/subdir2/file", "foo!");
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_advance_over(i, "dirA/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "dirB/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "dirC/", GIT_ITERATOR_STATUS_FILTERED);
expect_advance_over(i, "dirD/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_vector_free(&pathlist);
}
void test_iterator_workdir__advance_into(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_must_pass(p_mkdir("icase/Empty", 0777));
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "Empty/");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
void test_iterator_workdir__pathlist_with_directory(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
const char *expected[] = { "subdir/README", "subdir/new.txt",
"subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 4;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir/"));
g_repo = cl_git_sandbox_init("testrepo2");
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_free(&filelist);
}
void test_iterator_workdir__pathlist_with_directory_include_trees(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt",
"subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt", };
size_t expected_len = 6;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir/"));
g_repo = cl_git_sandbox_init("testrepo2");
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_free(&filelist);
}
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "fileops.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, error;
int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES);
bool v = false;
if (expected_flat < 0) { v = true; expected_flat = -expected_flat; }
if (expected_total < 0) { v = true; expected_total = -expected_total; }
if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees");
count = 0;
while (!git_iterator_advance(&entry, i)) {
if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
if (no_trees)
cl_assert(entry->mode != GIT_FILEMODE_TREE);
if (expected_flat_paths) {
const char *expect_path = expected_flat_paths[count];
size_t expect_len = strlen(expect_path);
cl_assert_equal_s(expect_path, entry->path);
if (expect_path[expect_len - 1] == '/')
cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
else
cl_assert(entry->mode != GIT_FILEMODE_TREE);
}
if (++count > expected_flat)
break;
}
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) {
error = git_iterator_advance_into(&entry, i);
/* could return NOTFOUND if directory is empty */
cl_assert(!error || error == GIT_ENOTFOUND);
if (error == GIT_ENOTFOUND) {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
} else {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
if (++count > expected_total)
break;
}
cl_assert_equal_i(expected_total, count);
}
/* Index contents (including pseudotrees):
*
* 0: a 5: F 10: k/ 16: L/
* 1: B 6: g 11: k/1 17: L/1
* 2: c 7: H 12: k/a 18: L/a
* 3: D 8: i 13: k/B 19: L/B
* 4: e 9: J 14: k/c 20: L/c
* 15: k/D 21: L/D
*
* 0: B 5: L/ 11: a 16: k/
* 1: D 6: L/1 12: c 17: k/1
* 2: F 7: L/B 13: e 18: k/B
* 3: H 8: L/D 14: g 19: k/D
* 4: J 9: L/a 15: i 20: k/a
* 10: L/c 21: k/c
*/
void test_repo_iterator__index(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* autoexpand with no tree entries for index */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
git_index_free(index);
}
void test_repo_iterator__index_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.flags = 0;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
}
void test_repo_iterator__tree(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
git_tree_free(head);
}
void test_repo_iterator__tree_icase(void)
{
git_iterator *i;
git_tree *head;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_head_tree(&head, g_repo));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
/* auto expand with no tree entries */
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
/* auto expand with tree entries */
i_opts.start = "c";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
git_tree_free(head);
}
void test_repo_iterator__tree_more(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head;
static const char *expect_basic[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
NULL,
};
static const char *expect_trees[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
NULL,
};
static const char *expect_noauto[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/",
NULL
};
g_repo = cl_git_sandbox_init("status");
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, NULL));
expect_iterator_items(i, 12, expect_basic, 12, expect_basic);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 13, expect_trees, 13, expect_trees);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 10, expect_noauto, 13, expect_trees);
git_iterator_free(i);
git_tree_free(head);
}
/* "b=name,t=name", blob_id, tree_id */
static void build_test_tree(
git_oid *out, git_repository *repo, const char *fmt, ...)
{
git_oid *id;
git_treebuilder *builder;
const char *scan = fmt, *next;
char type, delimiter;
git_filemode_t mode = GIT_FILEMODE_BLOB;
git_buf name = GIT_BUF_INIT;
va_list arglist;
cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); /* start builder */
va_start(arglist, fmt);
while (*scan) {
switch (type = *scan++) {
case 't': case 'T': mode = GIT_FILEMODE_TREE; break;
case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break;
default:
cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B');
}
delimiter = *scan++; /* read and skip delimiter */
for (next = scan; *next && *next != delimiter; ++next)
/* seek end */;
cl_git_pass(git_buf_set(&name, scan, (size_t)(next - scan)));
for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan)
/* skip delimiter and optional comma */;
id = va_arg(arglist, git_oid *);
cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode));
}
va_end(arglist);
cl_git_pass(git_treebuilder_write(out, builder));
git_treebuilder_free(builder);
git_buf_free(&name);
}
void test_repo_iterator__tree_case_conflicts_0(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, biga_id, littlea_id, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/1.file", "A/3.file", "a/2.file", "a/4.file" };
const char *expect_ci[] = {
"A/1.file", "a/2.file", "A/3.file", "a/4.file" };
const char *expect_cs_trees[] = {
"A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" };
const char *expect_ci_trees[] = {
"A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
/* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */
build_test_tree(
&biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id);
build_test_tree(
&littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id);
build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_cs, 4, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_repo_iterator__tree_case_conflicts_1(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" };
const char *expect_ci[] = {
"A/a", "a/b", "A/b/1", "A/c" };
const char *expect_cs_trees[] = {
"A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" };
const char *expect_ci_trees[] = {
"A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
/* create: A/a A/b/1 A/c a/a a/b a/C */
build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id);
build_test_tree(
&biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id);
build_test_tree(
&littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id);
build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs, 6, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_repo_iterator__tree_case_conflicts_2(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO",
"A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO",
"A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO",
"A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO",
"a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO",
"a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO",
"a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO",
"a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", };
const char *expect_ci[] = {
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
const char *expect_ci_trees[] = {
"A/", "A/B/", "A/B/C/", "A/B/C/D/",
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid_fromstr(&blob_id, blob_sha)); /* lookup blob */
build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 32, expect_cs, 32, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 17, expect_ci, 17, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_repo_iterator__workdir(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
}
void test_repo_iterator__workdir_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
}
static void build_workdir_tree(const char *root, int dirs, int subs)
{
int i, j;
char buf[64], sub[64];
for (i = 0; i < dirs; ++i) {
if (i % 2 == 0) {
p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i);
cl_git_mkfile(buf, buf);
buf[strlen(buf) - 5] = '\0';
} else {
p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
}
for (j = 0; j < subs; ++j) {
switch (j % 4) {
case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break;
case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break;
case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break;
case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break;
}
cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH));
if (j % 2 == 0) {
size_t sublen = strlen(sub);
memcpy(&sub[sublen], "/file", sizeof("/file"));
cl_git_mkfile(sub, sub);
sub[sublen] = '\0';
}
}
}
}
void test_repo_iterator__workdir_depth(void)
{
git_iterator *iter;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
build_workdir_tree("icase", 10, 10);
build_workdir_tree("icase/DIR01/sUB01", 50, 0);
build_workdir_tree("icase/dir02/sUB01", 50, 0);
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 125, NULL, 125, NULL);
git_iterator_free(iter);
/* auto expand with tree entries (empty dirs silently skipped) */
iter_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 337, NULL, 337, NULL);
git_iterator_free(iter);
}
void test_repo_iterator__fs(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
static const char *expect_base[] = {
"DIR01/Sub02/file",
"DIR01/sub00/file",
"current_file",
"dir00/Sub02/file",
"dir00/file",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_trees[] = {
"DIR01/",
"DIR01/SUB03/",
"DIR01/Sub02/",
"DIR01/Sub02/file",
"DIR01/sUB01/",
"DIR01/sub00/",
"DIR01/sub00/file",
"current_file",
"dir00/",
"dir00/SUB03/",
"dir00/Sub02/",
"dir00/Sub02/file",
"dir00/file",
"dir00/sUB01/",
"dir00/sub00/",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_noauto[] = {
"DIR01/",
"current_file",
"dir00/",
"modified_file",
"new_file",
NULL,
};
g_repo = cl_git_sandbox_init("status");
build_workdir_tree("status/subdir", 2, 4);
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", NULL));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
}
void test_repo_iterator__fs2(void)
{
git_iterator *i;
static const char *expect_base[] = {
"heads/br2",
"heads/dir",
"heads/ident",
"heads/long-file-name",
"heads/master",
"heads/packed-test",
"heads/subtrees",
"heads/test",
"tags/e90810b",
"tags/foo/bar",
"tags/foo/foo/bar",
"tags/point_to_blob",
"tags/test",
NULL,
};
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_iterator_for_filesystem(
&i, "testrepo/.git/refs", NULL));
expect_iterator_items(i, 13, expect_base, 13, expect_base);
git_iterator_free(i);
}
void test_repo_iterator__unreadable_dir(void)
{
git_iterator *i;
const git_index_entry *e;
if (!cl_is_chmod_supported())
return;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/r", 0777));
cl_git_mkfile("empty_standard_repo/r/a", "hello");
cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777));
cl_git_mkfile("empty_standard_repo/r/b/problem", "not me");
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000));
cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777));
cl_git_mkfile("empty_standard_repo/r/d", "final");
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo/r", NULL));
cl_git_pass(git_iterator_advance(&e, i)); /* a */
cl_git_fail(git_iterator_advance(&e, i)); /* b */
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777));
git_iterator_free(i);
}
void test_repo_iterator__skips_fifos_and_such(void)
{
#ifndef GIT_WIN32
git_iterator *i;
const git_index_entry *e;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/dir", 0777));
cl_git_mkfile("empty_standard_repo/file", "not me");
cl_assert(!mkfifo("empty_standard_repo/fifo", 0777));
cl_assert(!access("empty_standard_repo/fifo", F_OK));
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo", &i_opts));
cl_git_pass(git_iterator_advance(&e, i)); /* .git */
cl_assert(S_ISDIR(e->mode));
cl_git_pass(git_iterator_advance(&e, i)); /* dir */
cl_assert(S_ISDIR(e->mode));
/* skips fifo */
cl_git_pass(git_iterator_advance(&e, i)); /* file */
cl_assert(S_ISREG(e->mode));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
git_iterator_free(i);
#endif
}
void test_repo_iterator__indexfilelist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
int default_icase;
int expect;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "c";
i_opts.end = NULL;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
/* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */
expect = ((default_icase) ? 6 : 4);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
i_opts.start = NULL;
i_opts.end = "e";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
/* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */
expect = ((default_icase) ? 5 : 6);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_repo_iterator__indexfilelist_2(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a ==> 5) vs (c e k/1 ==> 3) */
expect = default_icase ? 5 : 3;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_repo_iterator__indexfilelist_3(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_repo_iterator__indexfilelist_4(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_repo_iterator__indexfilelist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEXCAP_IGNORE_CASE));
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
git_vector_free(&filelist);
}
void test_repo_iterator__workdirfilelist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
bool default_icase;
int expect;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
/* In this test we DO NOT force a case on the iteratords and verify default behavior. */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "c";
i_opts.end = NULL;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */
expect = ((default_icase) ? 6 : 4);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
i_opts.start = NULL;
i_opts.end = "e";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */
expect = ((default_icase) ? 5 : 6);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
}
void test_repo_iterator__workdirfilelist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
}
void test_repo_iterator__treefilelist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
bool default_icase;
int expect;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
/* In this test we DO NOT force a case on the iteratords and verify default behavior. */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "c";
i_opts.end = NULL;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */
expect = ((default_icase) ? 6 : 4);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
i_opts.start = NULL;
i_opts.end = "e";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */
expect = ((default_icase) ? 5 : 6);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
git_tree_free(tree);
}
void test_repo_iterator__treefilelist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
git_tree_free(tree);
}
...@@ -218,6 +218,58 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void) ...@@ -218,6 +218,58 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void)
cl_assert_equal_i(0, counts.wrong_sorted_path); cl_assert_equal_i(0, counts.wrong_sorted_path);
} }
static void stage_and_commit(git_repository *repo, const char *path)
{
git_index *index;
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, path));
cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n");
git_index_free(index);
}
void test_status_worktree__within_subdir(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
char *paths[] = { "zzz_new_dir" };
git_strarray pathsArray;
/* first alter the contents of the worktree */
cl_git_mkfile("status/.new_file", "dummy");
cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", 0777));
cl_git_mkfile("status/zzz_new_dir/new_file", "dummy");
cl_git_mkfile("status/zzz_new_file", "dummy");
cl_git_mkfile("status/wut", "dummy");
stage_and_commit(repo, "zzz_new_dir/new_file");
/* now get status */
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count4;
counts.expected_paths = entry_paths4;
counts.expected_statuses = entry_statuses4;
counts.debug = true;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
pathsArray.count = 1;
pathsArray.strings = paths;
opts.pathspec = pathsArray;
// We committed zzz_new_dir/new_file above. It shouldn't be reported.
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(0, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
/* this test is equivalent to t18-status.c:singlestatus0 */ /* this test is equivalent to t18-status.c:singlestatus0 */
void test_status_worktree__single_file(void) void test_status_worktree__single_file(void)
{ {
...@@ -657,7 +709,7 @@ void test_status_worktree__conflict_has_no_oid(void) ...@@ -657,7 +709,7 @@ void test_status_worktree__conflict_has_no_oid(void)
entry.mode = 0100644; entry.mode = 0100644;
entry.path = "modified_file"; entry.path = "modified_file";
git_oid_fromstr(&entry.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); git_oid_fromstr(&entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_conflict_add(index, &entry, &entry, &entry)); cl_git_pass(git_index_conflict_add(index, &entry, &entry, &entry));
...@@ -692,16 +744,6 @@ void test_status_worktree__conflict_has_no_oid(void) ...@@ -692,16 +744,6 @@ void test_status_worktree__conflict_has_no_oid(void)
git_status_list_free(statuslist); git_status_list_free(statuslist);
} }
static void stage_and_commit(git_repository *repo, const char *path)
{
git_index *index;
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, path));
cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n");
git_index_free(index);
}
static void assert_ignore_case( static void assert_ignore_case(
bool should_ignore_case, bool should_ignore_case,
int expected_lower_cased_file_status, int expected_lower_cased_file_status,
...@@ -1146,3 +1188,86 @@ void test_status_worktree__update_index_with_symlink_doesnt_change_mode(void) ...@@ -1146,3 +1188,86 @@ void test_status_worktree__update_index_with_symlink_doesnt_change_mode(void)
git_reference_free(head); git_reference_free(head);
} }
static const char *testrepo2_subdir_paths[] = {
"subdir/README",
"subdir/new.txt",
"subdir/subdir2/README",
"subdir/subdir2/new.txt",
};
static const char *testrepo2_subdir_paths_icase[] = {
"subdir/new.txt",
"subdir/README",
"subdir/subdir2/new.txt",
"subdir/subdir2/README"
};
void test_status_worktree__with_directory_in_pathlist(void)
{
git_repository *repo = cl_git_sandbox_init("testrepo2");
git_index *index;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuslist;
const git_status_entry *status;
size_t i, entrycount;
bool native_ignore_case;
cl_git_pass(git_repository_index(&index, repo));
native_ignore_case =
(git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0;
git_index_free(index);
opts.pathspec.count = 1;
opts.pathspec.strings = malloc(opts.pathspec.count * sizeof(char *));
opts.pathspec.strings[0] = "subdir";
opts.flags =
GIT_STATUS_OPT_DEFAULTS |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->index_to_workdir->old_file.path);
}
opts.show = GIT_STATUS_SHOW_INDEX_ONLY;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->head_to_index->old_file.path);
}
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->index_to_workdir->old_file.path);
}
}
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