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 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 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); }
void test_diff_iterator__tree_special_functions(void) { git_tree *t; git_iterator *i; const git_index_entry *entry; git_repository *repo = cl_git_sandbox_init("attr"); int cases = 0; const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e"; t = resolve_commit_oid_to_tree( repo, "24fa9a9fc4e202313e24b648087495441dab432b"); cl_assert(t != NULL); cl_git_pass(git_iterator_for_tree_range(&i, repo, t, NULL, NULL)); cl_git_pass(git_iterator_current(i, &entry)); while (entry != NULL) { 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_git_pass(git_iterator_advance(i, &entry)); } cl_assert_equal_i(4, cases); git_iterator_free(i); git_tree_free(t); }
static int checkout_get_actions( uint32_t **actions_ptr, size_t **counts_ptr, checkout_data *data, git_iterator *workdir) { int error = 0; const git_index_entry *wditem; git_vector pathspec = GIT_VECTOR_INIT, *deltas; git_pool pathpool = GIT_POOL_INIT_STRINGPOOL; git_diff_delta *delta; size_t i, *counts = NULL; uint32_t *actions = NULL; if (data->opts.paths.count > 0 && git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0) return -1; if ((error = git_iterator_current(&wditem, workdir)) < 0 && error != GIT_ITEROVER) goto fail; deltas = &data->diff->deltas; *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t)); *actions_ptr = actions = git__calloc( deltas->length ? deltas->length : 1, sizeof(uint32_t)); if (!counts || !actions) { error = -1; goto fail; } git_vector_foreach(deltas, i, delta) { int act = checkout_action(data, delta, workdir, &wditem, &pathspec); if (act < 0) { error = act; goto fail; } actions[i] = act; if (act & CHECKOUT_ACTION__REMOVE) counts[CHECKOUT_ACTION__REMOVE]++; if (act & CHECKOUT_ACTION__UPDATE_BLOB) counts[CHECKOUT_ACTION__UPDATE_BLOB]++; if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE) counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++; if (act & CHECKOUT_ACTION__CONFLICT) counts[CHECKOUT_ACTION__CONFLICT]++; }
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 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); }
static void check_tree_entry( git_iterator *i, const char *oid, const char *oid_p, const char *oid_pp, const char *oid_ppp) { const git_index_entry *ie; const git_tree_entry *te; const git_tree *tree; git_buf path = GIT_BUF_INIT; cl_git_pass(git_iterator_current_tree_entry(&te, i)); cl_assert(te); cl_assert(git_oid_streq(te->oid, oid) == 0); cl_git_pass(git_iterator_current(&ie, i)); cl_git_pass(git_buf_sets(&path, ie->path)); if (oid_p) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0); } if (oid_pp) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0); } if (oid_ppp) { git_buf_rtruncate_at_char(&path, '/'); cl_git_pass(git_iterator_current_parent_tree(&tree, i, path.ptr)); cl_assert(tree); cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0); } git_buf_free(&path); }
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 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); }