void git_index_clear(git_index *index) { unsigned int i; assert(index); for (i = 0; i < index->entries.length; ++i) { git_index_entry *e; e = git_vector_get(&index->entries, i); free((char *)e->path); free(e); } for (i = 0; i < index->unmerged.length; ++i) { git_index_entry_unmerged *e; e = git_vector_get(&index->unmerged, i); free((char *)e->path); free(e); } git_vector_clear(&index->entries); git_vector_clear(&index->unmerged); index->last_modified = 0; free_tree(index->tree); index->tree = NULL; }
static int filter_wants(git_remote *remote) { struct filter_payload p; git_refspec tagspec; int error = -1; git_vector_clear(&remote->refs); if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) return error; /* * The fetch refspec can be NULL, and what this means is that the * user didn't specify one. This is fine, as it means that we're * not interested in any particular branch but just the remote's * HEAD, which will be stored in FETCH_HEAD after the fetch. */ p.tagspec = &tagspec; p.found_head = 0; p.remote = remote; if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0) goto cleanup; error = git_remote_ls(remote, filter_ref__cb, &p); cleanup: git_refspec__free(&tagspec); return error; }
static void clear_parents(git_commit *commit) { unsigned int i; for (i = 0; i < commit->parent_oids.length; ++i) { git_oid *parent = git_vector_get(&commit->parent_oids, i); free(parent); } git_vector_clear(&commit->parent_oids); }
void git_treebuilder_clear(git_treebuilder *bld) { size_t i; assert(bld); for (i = 0; i < bld->entries.length; ++i) { git_tree_entry *e = bld->entries.contents[i]; free(e->filename); free(e); } git_vector_clear(&bld->entries); }
static void clear_entries(git_tree *tree) { unsigned int i; if (tree == NULL) return; for (i = 0; i < tree->entries.length; ++i) { git_tree_entry *e; e = git_vector_get(&tree->entries, i); free(e->filename); free(e); } git_vector_clear(&tree->entries); }
static int filter_wants(git_remote *remote) { git_remote_head **heads; git_refspec tagspec, head; int error = 0; git_odb *odb; size_t i, heads_len; git_vector_clear(&remote->refs); if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0) return error; /* * The fetch refspec can be NULL, and what this means is that the * user didn't specify one. This is fine, as it means that we're * not interested in any particular branch but just the remote's * HEAD, which will be stored in FETCH_HEAD after the fetch. */ if (remote->active_refspecs.length == 0) { if ((error = git_refspec__parse(&head, "HEAD", true)) < 0) goto cleanup; error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs); git_refspec__free(&head); if (error < 0) goto cleanup; } if (git_repository_odb__weakptr(&odb, remote->repo) < 0) goto cleanup; if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0) goto cleanup; for (i = 0; i < heads_len; i++) { if ((error = maybe_want(remote, heads[i], odb, &tagspec)) < 0) break; } cleanup: git_refspec__free(&tagspec); return error; }
static int filter_wants(git_remote *remote) { int error; struct filter_payload p; git_vector_clear(&remote->refs); /* * The fetch refspec can be NULL, and what this means is that the * user didn't specify one. This is fine, as it means that we're * not interested in any particular branch but just the remote's * HEAD, which will be stored in FETCH_HEAD after the fetch. */ p.spec = git_remote_fetchspec(remote); p.found_head = 0; p.remote = remote; error = git_repository_odb__weakptr(&p.odb, remote->repo); if (error < GIT_SUCCESS) return error; return remote->transport->ls(remote->transport, &filter_ref__cb, &p); }
static void clear_parents(git_commit *commit) { git_vector_clear(&commit->parents); }
static int parse_index(git_index *index, const char *buffer, size_t buffer_size) { unsigned int i; struct index_header header; git_oid checksum_calculated, checksum_expected; #define seek_forward(_increase) { \ if (_increase >= buffer_size) \ return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \ buffer += _increase; \ buffer_size -= _increase;\ } if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer too small"); /* Precalculate the SHA1 of the files's contents -- we'll match it to * the provided SHA1 in the footer */ git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE); /* Parse header */ if (read_header(&header, buffer) < GIT_SUCCESS) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header is corrupted"); seek_forward(INDEX_HEADER_SIZE); git_vector_clear(&index->entries); /* Parse all the entries */ for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) { size_t entry_size; git_index_entry *entry; entry = git__malloc(sizeof(git_index_entry)); if (entry == NULL) return GIT_ENOMEM; entry_size = read_entry(entry, buffer, buffer_size); /* 0 bytes read means an object corruption */ if (entry_size == 0) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Entry size is zero"); if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS) return GIT_ENOMEM; seek_forward(entry_size); } if (i != header.entry_count) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header entries changed while parsing"); /* There's still space for some extensions! */ while (buffer_size > INDEX_FOOTER_SIZE) { size_t extension_size; extension_size = read_extension(index, buffer, buffer_size); /* see if we have read any bytes from the extension */ if (extension_size == 0) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero"); seek_forward(extension_size); } if (buffer_size != INDEX_FOOTER_SIZE) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer size does not match index footer size"); /* 160-bit SHA-1 over the content of the index file before this checksum. */ git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer); if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0) return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Calculated checksum does not match expected checksum"); #undef seek_forward /* force sorting in the vector: the entries are * assured to be sorted on the index */ index->entries.sorted = 1; return GIT_SUCCESS; }
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_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 still matches when suffixed with a '/' */ { 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 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 allow '/' suffixes of submodules */ { 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); } git_vector_free(&filelist); git_index_free(index); git_tree_free(head); }
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); }