static void *run_workdir_iterator(void *arg) { int error = 0; git_iterator *iter; const git_index_entry *entry = NULL; cl_git_pass(git_iterator_for_workdir( &iter, _repo, NULL, NULL, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL)); while (!error) { if (entry && entry->mode == GIT_FILEMODE_TREE) { error = git_iterator_advance_into(&entry, iter); if (error == GIT_ENOTFOUND) error = git_iterator_advance(&entry, iter); } else { error = git_iterator_advance(&entry, iter); } if (!error) (void)git_iterator_current_is_ignored(iter); } cl_assert_equal_i(GIT_ITEROVER, error); git_iterator_free(iter); giterr_clear(); return arg; }
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__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); }
static void tree_iterator_test( const char *sandbox, const char *treeish, const char *start, const char *end, int expected_count, const char **expected_values) { git_tree *t; git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; const git_index_entry *entry; int error, count = 0, count_post_reset = 0; git_repository *repo = cl_git_sandbox_init(sandbox); i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.start = start; i_opts.end = end; cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); /* test loop */ while (!(error = git_iterator_advance(&entry, i))) { cl_assert(entry); if (expected_values != NULL) cl_assert_equal_s(expected_values[count], entry->path); count++; } cl_assert_equal_i(GIT_ITEROVER, error); cl_assert(!entry); cl_assert_equal_i(expected_count, count); /* test reset */ cl_git_pass(git_iterator_reset(i, NULL, NULL)); while (!(error = git_iterator_advance(&entry, i))) { cl_assert(entry); if (expected_values != NULL) cl_assert_equal_s(expected_values[count_post_reset], entry->path); count_post_reset++; } cl_assert_equal_i(GIT_ITEROVER, error); cl_assert(!entry); cl_assert_equal_i(count, count_post_reset); git_iterator_free(i); git_tree_free(t); }
static void check_tree_range( git_repository *repo, const char *start, const char *end, bool ignore_case, int expected_count) { git_tree *head; git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; int error, count; i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; i_opts.start = start; i_opts.end = end; cl_git_pass(git_repository_head_tree(&head, repo)); cl_git_pass(git_iterator_for_tree(&i, head, &i_opts)); for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count) /* count em up */; cl_assert_equal_i(GIT_ITEROVER, error); cl_assert_equal_i(expected_count, count); git_iterator_free(i); git_tree_free(head); }
static void tree_iterator_test( const char *sandbox, const char *treeish, const char *start, const char *end, int expected_count, const char **expected_values) { git_tree *t; git_iterator *i; const git_index_entry *entry; int count = 0; git_repository *repo = cl_git_sandbox_init(sandbox); cl_assert(t = resolve_commit_oid_to_tree(repo, treeish)); cl_git_pass(git_iterator_for_tree_range(&i, repo, t, start, end)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { if (expected_values != NULL) cl_assert_equal_s(expected_values[count], entry->path); count++; cl_git_pass(git_iterator_advance(i, &entry)); } git_iterator_free(i); cl_assert(expected_count == count); git_tree_free(t); }
static void index_iterator_test( const char *sandbox, const char *start, const char *end, int expected_count, const char **expected_names, const char **expected_oids) { git_iterator *i; const git_index_entry *entry; int count = 0; git_repository *repo = cl_git_sandbox_init(sandbox); cl_git_pass(git_iterator_for_index_range(&i, repo, start, end)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { 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_i(git_oid_cmp(&oid, &entry->oid), 0); } count++; cl_git_pass(git_iterator_advance(i, &entry)); } git_iterator_free(i); cl_assert_equal_i(expected_count, count); }
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; 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)); cl_git_pass(git_iterator_for_index(&i, index, 0, start, end)); 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); }
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); }
int git_note_foreach( git_repository *repo, const char *notes_ref, git_note_foreach_cb note_cb, void *payload) { int error; git_iterator *iter = NULL; git_tree *tree = NULL; git_commit *commit = NULL; const git_index_entry *item; if (!(error = retrieve_note_tree_and_commit( &tree, &commit, repo, ¬es_ref)) && !(error = git_iterator_for_tree(&iter, tree))) error = git_iterator_current(iter, &item); while (!error && item) { error = process_entry_path(item->path, &item->oid, note_cb, payload); if (!error) error = git_iterator_advance(iter, &item); } git_iterator_free(iter); git_tree_free(tree); git_commit_free(commit); return error; }
static int submodules_from_index(git_strmap *map, git_index *idx) { int error; git_iterator *i; const git_index_entry *entry; if ((error = git_iterator_for_index(&i, idx, 0, NULL, NULL)) < 0) return error; while (!(error = git_iterator_advance(&entry, i))) { khiter_t pos = git_strmap_lookup_index(map, entry->path); git_submodule *sm; if (git_strmap_valid_index(map, pos)) { sm = git_strmap_value_at(map, pos); if (S_ISGITLINK(entry->mode)) submodule_update_from_index_entry(sm, entry); else sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; } else if (S_ISGITLINK(entry->mode)) { if (!submodule_get_or_create(&sm, git_index_owner(idx), map, entry->path)) { submodule_update_from_index_entry(sm, entry); git_submodule_free(sm); } } } if (error == GIT_ITEROVER) error = 0; git_iterator_free(i); return error; }
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_submodule_status__iterator(void) { git_iterator *iter; const git_index_entry *entry; size_t i; static const char *expected[] = { ".gitmodules", "just_a_dir/", "just_a_dir/contents", "just_a_file", "not", "not-submodule", "README.txt", "sm_added_and_uncommited", "sm_changed_file", "sm_changed_head", "sm_changed_index", "sm_changed_untracked_file", "sm_missing_commits", "sm_unchanged", NULL }; static int expected_flags[] = { GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED, /* ".gitmodules" */ 0, /* "just_a_dir/" will be skipped */ GIT_STATUS_CURRENT, /* "just_a_dir/contents" */ GIT_STATUS_CURRENT, /* "just_a_file" */ GIT_STATUS_IGNORED, /* "not" (contains .git) */ GIT_STATUS_IGNORED, /* "not-submodule" (contains .git) */ GIT_STATUS_CURRENT, /* "README.txt */ GIT_STATUS_INDEX_NEW, /* "sm_added_and_uncommited" */ GIT_STATUS_WT_MODIFIED, /* "sm_changed_file" */ GIT_STATUS_WT_MODIFIED, /* "sm_changed_head" */ GIT_STATUS_WT_MODIFIED, /* "sm_changed_index" */ GIT_STATUS_WT_MODIFIED, /* "sm_changed_untracked_file" */ GIT_STATUS_WT_MODIFIED, /* "sm_missing_commits" */ GIT_STATUS_CURRENT, /* "sm_unchanged" */ 0 }; submodule_expectations exp = { 0, expected, expected_flags }; git_status_options opts = GIT_STATUS_OPTIONS_INIT; cl_git_pass(git_iterator_for_workdir(&iter, g_repo, GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES, NULL, NULL)); for (i = 0; !git_iterator_advance(&entry, iter); ++i) cl_assert_equal_s(expected[i], entry->path); git_iterator_free(iter); opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_INCLUDE_UNMODIFIED | GIT_STATUS_OPT_INCLUDE_IGNORED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, confirm_submodule_status, &exp)); }
void test_diff_iterator__tree_special_functions(void) { git_tree *t; git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; const git_index_entry *entry; git_repository *repo = cl_git_sandbox_init("attr"); int error, cases = 0; const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; t = resolve_commit_oid_to_tree( repo, "24fa9a9fc4e202313e24b648087495441dab432b"); cl_assert(t != NULL); i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; cl_git_pass(git_iterator_for_tree(&i, t, &i_opts)); while (!(error = git_iterator_advance(&entry, i))) { cl_assert(entry); if (strcmp(entry->path, "sub/file") == 0) { cases++; check_tree_entry( i, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid, NULL); } else if (strcmp(entry->path, "sub/sub/subsub.txt") == 0) { cases++; check_tree_entry( i, "9e5bdc47d6a80f2be0ea3049ad74231b94609242", "4e49ba8c5b6c32ff28cd9dcb60be34df50fcc485", "ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid); } else if (strcmp(entry->path, "subdir/.gitattributes") == 0) { cases++; check_tree_entry( i, "99eae476896f4907224978b88e5ecaa6c5bb67a9", "9fb40b6675dde60b5697afceae91b66d908c02d9", rootoid, NULL); } else if (strcmp(entry->path, "subdir2/subdir2_test1") == 0) { cases++; check_tree_entry( i, "dccada462d3df8ac6de596fb8c896aba9344f941", "2929de282ce999e95183aedac6451d3384559c4b", rootoid, NULL); } } cl_assert_equal_i(GIT_ITEROVER, error); cl_assert(!entry); cl_assert_equal_i(4, cases); git_iterator_free(i); git_tree_free(t); }
void test_iterator_workdir__skips_unreadable_dirs(void) { git_iterator *i; const git_index_entry *e; if (!cl_is_chmod_supported()) return; #ifndef GIT_WIN32 if (geteuid() == 0) cl_skip(); #endif 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); }
static void index_iterator_test( const char *sandbox, const char *start, const char *end, git_iterator_flag_t flags, int expected_count, const char **expected_names, const char **expected_oids) { git_index *index; git_iterator *i; const git_index_entry *entry; int error, count = 0, caps; git_repository *repo = cl_git_sandbox_init(sandbox); git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, repo)); caps = git_index_caps(index); iter_opts.flags = flags; iter_opts.start = start; iter_opts.end = end; cl_git_pass(git_iterator_for_index(&i, repo, index, &iter_opts)); while (!(error = git_iterator_advance(&entry, i))) { cl_assert(entry); if (expected_names != NULL) cl_assert_equal_s(expected_names[count], entry->path); if (expected_oids != NULL) { git_oid oid; cl_git_pass(git_oid_fromstr(&oid, expected_oids[count])); cl_assert_equal_oid(&oid, &entry->id); } count++; } cl_assert_equal_i(GIT_ITEROVER, error); cl_assert(!entry); cl_assert_equal_i(expected_count, count); git_iterator_free(i); cl_assert(caps == git_index_caps(index)); git_index_free(index); }
int git_note_next( git_oid* note_id, git_oid* annotated_id, git_note_iterator *it) { int error; const git_index_entry *item; if ((error = git_iterator_current(&item, it)) < 0) return error; git_oid_cpy(note_id, &item->oid); if (!(error = process_entry_path(item->path, annotated_id))) git_iterator_advance(NULL, it); return error; }
static int checkout_remaining_wd_items( checkout_data *data, git_iterator *workdir, const git_index_entry *wd, git_vector *spec) { int error = 0; while (wd && !error) { if (!(error = checkout_action_wd_only(data, workdir, wd, spec))) error = git_iterator_advance(&wd, workdir); } if (error == GIT_ITEROVER) error = 0; return error; }
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; const git_index_entry *entry; int count = 0, count_all = 0; git_repository *repo = cl_git_sandbox_init(sandbox); cl_git_pass(git_iterator_for_workdir_range(&i, repo, start, end)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { int ignored = git_iterator_current_is_ignored(i); if (S_ISDIR(entry->mode)) { cl_git_pass(git_iterator_advance_into_directory(i, &entry)); 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++; cl_git_pass(git_iterator_advance(i, &entry)); } git_iterator_free(i); cl_assert_equal_i(expected_count,count); cl_assert_equal_i(expected_count + expected_ignores, count_all); }
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_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); }
void test_diff_iterator__workdir_builtin_ignores(void) { git_repository *repo = cl_git_sandbox_init("attr"); git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; const git_index_entry *entry; int idx; static struct { const char *path; bool ignored; } expected[] = { { "dir/", true }, { "file", false }, { "ign", true }, { "macro_bad", false }, { "macro_test", false }, { "root_test1", false }, { "root_test2", false }, { "root_test3", false }, { "root_test4.txt", false }, { "sub/", false }, { "sub/.gitattributes", false }, { "sub/abc", false }, { "sub/dir/", true }, { "sub/file", false }, { "sub/ign/", true }, { "sub/sub/", false }, { "sub/sub/.gitattributes", false }, { "sub/sub/dir", false }, /* file is not actually a dir */ { "sub/sub/file", false }, { NULL, false } }; cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777)); cl_git_mkfile("attr/sub/.git", "whatever"); i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; i_opts.start = "dir"; i_opts.end = "sub/sub/file"; cl_git_pass(git_iterator_for_workdir( &i, repo, NULL, NULL, &i_opts)); cl_git_pass(git_iterator_current(&entry, i)); for (idx = 0; entry != NULL; ++idx) { int ignored = git_iterator_current_is_ignored(i); cl_assert_equal_s(expected[idx].path, entry->path); cl_assert_(ignored == expected[idx].ignored, expected[idx].path); if (!ignored && (entry->mode == GIT_FILEMODE_TREE || entry->mode == GIT_FILEMODE_COMMIT)) { /* it is possible to advance "into" a submodule */ cl_git_pass(git_iterator_advance_into(&entry, i)); } else { int error = git_iterator_advance(&entry, i); cl_assert(!error || error == GIT_ITEROVER); } } cl_assert(expected[idx].path == NULL); git_iterator_free(i); }
static void workdir_iterator_test( const char *sandbox, const char *start, const char *end, int expected_count, int expected_ignores, const char **expected_names, const char *an_ignored_name) { git_iterator *i; git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT; const git_index_entry *entry; int error, count = 0, count_all = 0, count_all_post_reset = 0; git_repository *repo = cl_git_sandbox_init(sandbox); i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND; i_opts.start = start; i_opts.end = end; cl_git_pass(git_iterator_for_workdir(&i, repo, NULL, NULL, &i_opts)); error = git_iterator_current(&entry, i); cl_assert((error == 0 && entry != NULL) || (error == GIT_ITEROVER && entry == NULL)); while (entry != NULL) { int ignored = git_iterator_current_is_ignored(i); if (S_ISDIR(entry->mode)) { cl_git_pass(git_iterator_advance_into(&entry, i)); continue; } if (expected_names != NULL) cl_assert_equal_s(expected_names[count_all], entry->path); if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0) cl_assert(ignored); if (!ignored) count++; count_all++; error = git_iterator_advance(&entry, i); cl_assert((error == 0 && entry != NULL) || (error == GIT_ITEROVER && entry == NULL)); } cl_assert_equal_i(expected_count, count); cl_assert_equal_i(expected_count + expected_ignores, count_all); cl_git_pass(git_iterator_reset(i, NULL, NULL)); 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); }
static int checkout_action( checkout_data *data, git_diff_delta *delta, git_iterator *workdir, const git_index_entry **wditem_ptr, git_vector *pathspec) { const git_index_entry *wd = *wditem_ptr; int cmp = -1, act; int (*strcomp)(const char *, const char *) = data->diff->strcomp; int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp; int error; /* move workdir iterator to follow along with deltas */ while (1) { if (!wd) return checkout_action_no_wd(data, delta); cmp = strcomp(wd->path, delta->old_file.path); /* 1. wd before delta ("a/a" before "a/b") * 2. wd prefixes delta & should expand ("a/" before "a/b") * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c") * 4. wd equals delta ("a/b" and "a/b") * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b") * 6. wd after delta ("a/c" after "a/b") */ if (cmp < 0) { cmp = pfxcomp(delta->old_file.path, wd->path); if (cmp == 0) { if (wd->mode == GIT_FILEMODE_TREE) { /* case 2 - entry prefixed by workdir tree */ error = git_iterator_advance_into_or_over(&wd, workdir); if (error && error != GIT_ITEROVER) goto fail; *wditem_ptr = wd; continue; } /* case 3 maybe - wd contains non-dir where dir expected */ if (delta->old_file.path[strlen(wd->path)] == '/') { act = checkout_action_with_wd_blocker(data, delta, wd); *wditem_ptr = git_iterator_advance(&wd, workdir) ? NULL : wd; return act; } } /* case 1 - handle wd item (if it matches pathspec) */ if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0) goto fail; if ((error = git_iterator_advance(&wd, workdir)) < 0 && error != GIT_ITEROVER) goto fail; *wditem_ptr = wd; continue; } if (cmp == 0) { /* case 4 */ act = checkout_action_with_wd(data, delta, wd); *wditem_ptr = git_iterator_advance(&wd, workdir) ? NULL : wd; return act; } cmp = pfxcomp(wd->path, delta->old_file.path); if (cmp == 0) { /* case 5 */ if (wd->path[strlen(delta->old_file.path)] != '/') return checkout_action_no_wd(data, delta); if (delta->status == GIT_DELTA_TYPECHANGE) { if (delta->old_file.mode == GIT_FILEMODE_TREE) { act = checkout_action_with_wd(data, delta, wd); if ((error = git_iterator_advance_into(&wd, workdir)) < 0 && error != GIT_ENOTFOUND) goto fail; *wditem_ptr = wd; return act; } if (delta->new_file.mode == GIT_FILEMODE_TREE || delta->new_file.mode == GIT_FILEMODE_COMMIT || delta->old_file.mode == GIT_FILEMODE_COMMIT) { act = checkout_action_with_wd(data, delta, wd); if ((error = git_iterator_advance(&wd, workdir)) < 0 && error != GIT_ITEROVER) goto fail; *wditem_ptr = wd; return act; } } return checkout_action_with_wd_dir(data, delta, wd); } /* case 6 - wd is after delta */ return checkout_action_no_wd(data, delta); } fail: *wditem_ptr = NULL; return -1; }
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); }