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, 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_checkout_index__can_get_repo_from_index(void) { git_index *index; git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; cl_assert_equal_i(false, git_path_isfile("./testrepo/README")); cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt")); cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt")); opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_checkout_index(NULL, index, &opts)); check_file_contents("./testrepo/README", "hey there\n"); check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n"); check_file_contents("./testrepo/new.txt", "my new file\n"); git_index_free(index); }
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void) { git_index *index; const git_index_entry *entry; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_repo_set_bool(g_repo, "core.autocrlf", false); git_checkout_head(g_repo, &opts); git_repository_index(&index, g_repo); cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL); cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW)); cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL); cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW)); git_index_free(index); }
void git_repository_free(git_repository *repo) { if (repo == NULL) return; git_cache_free(&repo->objects); git_repository__refcache_free(&repo->references); free(repo->path_workdir); free(repo->path_index); free(repo->path_repository); free(repo->path_odb); if (repo->db != NULL) git_odb_close(repo->db); if (repo->index != NULL) git_index_free(repo->index); free(repo); }
void GitWrapper::commitCreateBasket() { GIT_RETURN_IF_DISABLED() QMutexLocker l(&gitMutex); git_repository* repo = openRepository(); if(repo==0) return; const QDateTime gitdate = getLastCommitDate(repo); const QString basketxml = Global::savesFolder() + "baskets/baskets.xml"; const QFileInfo basketxmlinfo(basketxml); if(gitdate <= basketxmlinfo.lastModified()) { git_index *index = NULL; int error = git_repository_index(&index, repo); if(error < 0) { gitErrorHandling(); return; } //this is kind of hacky because somebody could come in between and we still have stuff open //change basket.xml const QString basketxml("baskets/baskets.xml"); QByteArray basketxmlba = basketxml.toUtf8(); char *basketxmlCString = basketxmlba.data(); error = git_index_add_bypath(index, basketxmlCString); if(error < 0) { gitErrorHandling(); return; } bool result = commitIndex(repo,index); git_index_free(index); } git_repository_free(repo); }
void test_merge_trees_automerge__automerge(void) { git_index *index; const git_index_entry *entry; git_merge_tree_opts opts = GIT_MERGE_TREE_OPTS_INIT; git_blob *blob; struct merge_index_entry merge_index_entries[] = { ADDED_IN_MASTER_INDEX_ENTRY, AUTOMERGEABLE_INDEX_ENTRY, CHANGED_IN_BRANCH_INDEX_ENTRY, CHANGED_IN_MASTER_INDEX_ENTRY, { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 1, "conflicting.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 3, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY }; cl_git_pass(merge_trees_from_branches(&index, repo, "master", THEIRS_AUTOMERGE_BRANCH, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); cl_assert(merge_test_reuc(index, merge_reuc_entries, 3)); cl_assert((entry = git_index_get_bypath(index, "automergeable.txt", 0)) != NULL); cl_assert(entry->file_size == strlen(AUTOMERGEABLE_MERGED_FILE)); cl_git_pass(git_object_lookup((git_object **)&blob, repo, &entry->oid, GIT_OBJ_BLOB)); cl_assert(memcmp(git_blob_rawcontent(blob), AUTOMERGEABLE_MERGED_FILE, (size_t)entry->file_size) == 0); git_index_free(index); git_blob_free(blob); }
void test_checkout_tree__donot_update_deleted_file_by_default(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid old_id, new_id; git_commit *old_commit = NULL, *new_commit = NULL; git_index *index = NULL; checkout_counts ct; opts.checkout_strategy = GIT_CHECKOUT_SAFE; memset(&ct, 0, sizeof(ct)); opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL; opts.notify_cb = checkout_count_callback; opts.notify_payload = &ct; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_oid_fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id)); cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL)); cl_git_pass(p_unlink("testrepo/branch_file.txt")); cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt")); cl_git_pass(git_index_write(index)); cl_assert(!git_path_exists("testrepo/branch_file.txt")); cl_git_pass(git_oid_fromstr(&new_id, "099fabac3a9ea935598528c27f866e34089c2eff")); cl_git_pass(git_commit_lookup(&new_commit, g_repo, &new_id)); cl_git_fail(git_checkout_tree(g_repo, (git_object *)new_commit, &opts)); cl_assert_equal_i(1, ct.n_conflicts); cl_assert_equal_i(1, ct.n_updates); git_commit_free(old_commit); git_commit_free(new_commit); git_index_free(index); }
void test_index_tests__add(void) { git_index *index; git_filebuf file = GIT_FILEBUF_INIT; git_repository *repo; git_index_entry *entry; git_oid id1; /* Intialize a new repository */ cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); /* Ensure we're the only guy in the room */ cl_git_pass(git_repository_index(&index, repo)); cl_assert(git_index_entrycount(index) == 0); /* Create a new file in the working directory */ cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); cl_git_pass(git_filebuf_commit(&file, 0666)); /* Store the expected hash of the file/blob * This has been generated by executing the following * $ echo "hey there" | git hash-object --stdin */ cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); /* Add the new file to the index */ cl_git_pass(git_index_add(index, "test.txt", 0)); /* Wow... it worked! */ cl_assert(git_index_entrycount(index) == 1); entry = git_index_get(index, 0); /* And the built-in hashing mechanism worked as expected */ cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); git_index_free(index); git_repository_free(repo); }
void test_merge_workdir_renames__ours(void) { git_index *index; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 0, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 0, "0c-rewritten-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 0, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 0, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 0, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 0, "5b-renamed-in-theirs-added-in-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "7-both-renamed-side-2.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "7-both-renamed.txt" }, }; merge_opts.flags |= GIT_MERGE_TREE_FIND_RENAMES; merge_opts.rename_threshold = 50; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR BRANCH_RENAME_OURS, GIT_REFS_HEADS_DIR BRANCH_RENAME_THEIRS, &merge_opts, &checkout_opts)); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_write(index)); cl_assert(merge_test_workdir(repo, merge_index_entries, 20)); git_index_free(index); }
void test_repo_pathspec__index0(void) { git_index *idx; git_strarray s; git_pathspec *ps; git_pathspec_match_list *m; cl_git_pass(git_repository_index(&idx, g_repo)); /* { "*_file", "new_file", "garbage" } */ s.strings = str0; s.count = ARRAY_SIZE(str0); cl_git_pass(git_pathspec_new(&ps, &s)); cl_git_pass(git_pathspec_match_index(&m, idx, 0, ps)); cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); cl_assert_equal_s("staged_new_file", git_pathspec_match_list_entry(m, 3)); cl_assert_equal_s("staged_new_file_deleted_file", git_pathspec_match_list_entry(m, 4)); cl_assert_equal_s("staged_new_file_modified_file", git_pathspec_match_list_entry(m, 5)); cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6)); cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 7)); cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 8)); cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 9)); git_pathspec_match_list_free(m); cl_git_pass(git_pathspec_match_index(&m, idx, GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); git_pathspec_match_list_free(m); git_pathspec_free(ps); git_index_free(idx); }
void test_checkout_tree__removes_conflicts(void) { git_oid commit_id; git_commit *commit; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_index *index; cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_remove(index, "executable.txt", 0)); create_conflict("executable.txt"); cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n"); create_conflict("other.txt"); cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n"); git_index_write(index); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2)); cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3)); cl_assert(!git_path_exists("testrepo/other.txt")); git_commit_free(commit); git_index_free(index); }
void test_status_worktree__conflict_with_diff3(void) { git_repository *repo = cl_git_sandbox_init("status"); git_index *index; unsigned int status; git_index_entry ancestor_entry, our_entry, their_entry; memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); memset(&our_entry, 0x0, sizeof(git_index_entry)); memset(&their_entry, 0x0, sizeof(git_index_entry)); ancestor_entry.path = "modified_file"; ancestor_entry.mode = 0100644; git_oid_fromstr(&ancestor_entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); our_entry.path = "modified_file"; our_entry.mode = 0100644; git_oid_fromstr(&our_entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); their_entry.path = "modified_file"; their_entry.mode = 0100644; git_oid_fromstr(&their_entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a"); cl_git_pass(git_status_file(&status, repo, "modified_file")); cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_remove(index, "modified_file", 0)); cl_git_pass(git_index_conflict_add( index, &ancestor_entry, &our_entry, &their_entry)); cl_git_pass(git_index_write(index)); git_index_free(index); cl_git_pass(git_status_file(&status, repo, "modified_file")); cl_assert_equal_i(GIT_STATUS_CONFLICTED, status); }
int main (int argc, char** argv) { git_index_matched_path_cb matched_cb = NULL; git_repository *repo = NULL; git_index *index; git_strarray array = {0}; int options = 0, count = 0; struct print_payload payload = {0}; git_threads_init(); parse_opts(&options, &count, argc, argv); init_array(&array, argc-count, argv+count); check_lg2(git_repository_open(&repo, "."), "No git repository", NULL); check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL); if (options&VERBOSE || options&SKIP) { matched_cb = &print_matched_cb; } payload.options = options; payload.repo = repo; if (options&UPDATE) { git_index_update_all(index, &array, matched_cb, &payload); } else { git_index_add_all(index, &array, 0, matched_cb, &payload); } git_index_write(index); git_index_free(index); git_repository_free(repo); git_threads_shutdown(); return 0; }
void test_checkout_tree__filemode_preserved_in_index(void) { git_oid executable_oid; git_commit *commit; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_index *index; const git_index_entry *entry; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(entry = git_index_get_bypath(index, "executable.txt", 0)); cl_assert_equal_i(0100755, entry->mode); git_commit_free(commit); git_index_free(index); }
void test_index_version__can_read_v4(void) { const char *paths[] = { "file.tx", "file.txt", "file.txz", "foo", "zzz", }; git_index *index; size_t i; g_repo = cl_git_sandbox_init("indexv4"); cl_git_pass(git_repository_index(&index, g_repo)); cl_assert_equal_sz(git_index_entrycount(index), 5); for (i = 0; i < ARRAY_SIZE(paths); i++) { const git_index_entry *entry = git_index_get_bypath(index, paths[i], GIT_INDEX_STAGE_NORMAL); cl_assert(entry != NULL); } git_index_free(index); }
void test_status_worktree__file_status_honors_case_ignorecase_regarding_untracked_files(void) { git_repository *repo = cl_git_sandbox_init("status"); unsigned int status; git_index *index; cl_repo_set_bool(repo, "core.ignorecase", false); repo = cl_git_sandbox_reopen(); /* Actually returns GIT_STATUS_IGNORED on Windows */ cl_git_fail_with(git_status_file(&status, repo, "NEW_FILE"), GIT_ENOTFOUND); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_add_bypath(index, "new_file")); cl_git_pass(git_index_write(index)); git_index_free(index); /* Actually returns GIT_STATUS_IGNORED on Windows */ cl_git_fail_with(git_status_file(&status, repo, "NEW_FILE"), GIT_ENOTFOUND); }
/* this test is similar to t18-status.c:statuscb3 */ void test_status_worktree__swap_subdir_and_file(void) { status_entry_counts counts; git_repository *repo = cl_git_sandbox_init("status"); git_index *index; git_status_options opts; bool ignore_case; cl_git_pass(git_repository_index(&index, repo)); ignore_case = index->ignore_case; git_index_free(index); /* first alter the contents of the worktree */ cl_git_pass(p_rename("status/current_file", "status/swap")); cl_git_pass(p_rename("status/subdir", "status/current_file")); cl_git_pass(p_rename("status/swap", "status/subdir")); cl_git_mkfile("status/.HEADER", "dummy"); cl_git_mkfile("status/42-is-not-prime.sigh", "dummy"); cl_git_mkfile("status/README.md", "dummy"); /* now get status */ memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = entry_count3; counts.expected_paths = ignore_case ? entry_paths3_icase : entry_paths3; counts.expected_statuses = ignore_case ? entry_statuses3_icase : entry_statuses3; memset(&opts, 0, sizeof(opts)); opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_INCLUDE_IGNORED; cl_git_pass( git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) ); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); }
static void write_invalid_filename(git_repository *repo, const char *fn_orig) { git_index *index; git_oid expected; const git_index_entry *entry; git_buf path = GIT_BUF_INIT; char *fn; cl_git_pass(git_repository_index(&index, repo)); cl_assert(git_index_entrycount(index) == 0); /* * Sneak a valid path into the index, we'll update it * to an invalid path when we try to write the index. */ fn = git__strdup(fn_orig); replace_char(fn, '/', '_'); git_buf_joinpath(&path, "./invalid", fn); cl_git_mkfile(path.ptr, NULL); cl_git_pass(git_index_add_bypath(index, fn)); cl_assert(entry = git_index_get_bypath(index, fn, 0)); /* kids, don't try this at home */ replace_char((char *)entry->path, '_', '/'); /* write-tree */ cl_git_fail(git_index_write_tree(&expected, index)); p_unlink(path.ptr); cl_git_pass(git_index_remove_all(index, NULL, NULL, NULL)); git_buf_free(&path); git_index_free(index); git__free(fn); }
void test_diff_iterator__index_handles_icase_range(void) { git_repository *repo; git_index *index; git_tree *head; repo = cl_git_sandbox_init("testrepo"); /* reset index to match HEAD */ cl_git_pass(git_repository_head_tree(&head, repo)); cl_git_pass(git_repository_index(&index, 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(repo, "B", "C", false, 0); check_index_range(repo, "B", "C", true, 1); check_index_range(repo, "a", "z", false, 3); check_index_range(repo, "a", "z", true, 4); }
void test_apply_both__can_apply_nonconflicting_file_changes(void) { git_diff *diff; git_index *index; const char *diff_file = DIFF_MODIFY_TWO_FILES; struct merge_index_entry both_expected[] = { { 0100644, "f8a701c8a1a22c1729ee50faff1111f2d64f96fc", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" }, }; size_t both_expected_cnt = sizeof(both_expected) / sizeof(struct merge_index_entry); /* * Replace the workdir file with a version that is different than * HEAD but such that the patch still applies cleanly. This item * has a new line appended. */ cl_git_append2file("merge-recursive/asparagus.txt", "This line is added in the index and the workdir.\n"); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_add_bypath(index, "asparagus.txt")); cl_git_pass(git_index_write(index)); git_index_free(index); cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file))); cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL)); validate_apply_index(repo, both_expected, both_expected_cnt); validate_apply_workdir(repo, both_expected, both_expected_cnt); git_diff_free(diff); }
void test_index_addall__files_in_folders(void) { git_index *index; git_strarray paths = { NULL, 0 }; addall_create_test_repo(true); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL)); check_stat_data(index, TEST_DIR "/file.bar", true); check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0); cl_must_pass(p_mkdir(TEST_DIR "/subdir", 0777)); cl_git_mkfile(TEST_DIR "/subdir/file", "hello!\n"); check_status(g_repo, 2, 0, 0, 1, 0, 0, 1, 0); cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL)); check_status(g_repo, 3, 0, 0, 0, 0, 0, 1, 0); git_index_free(index); }
void GitWrapper::initializeGitRepository(QString folder) { GIT_RETURN_IF_DISABLED() QMutexLocker l(&gitMutex); // this is not thread safe, we use locking elsewhere git_repository *repo = NULL; QByteArray ba = folder.toUtf8(); const char *cString = ba.data(); int error = git_repository_init(&repo, cString, false); if (error < 0) { const git_error *e = giterr_last(); kDebug() << e->message; } git_signature *sig = NULL; git_index *index = NULL; git_oid tree_id; git_oid commit_id; git_tree *tree = NULL; // no error handling at the moment git_signature_now(&sig, "AutoGit", "auto@localhost"); git_repository_index(&index, repo); git_index_write_tree(&tree_id, index); git_tree_lookup(&tree, repo, &tree_id); git_commit_create_v(&commit_id, repo, "HEAD", sig, sig, NULL, "Initial commit", tree, 0); git_signature_free(sig); git_index_free(index); git_tree_free(tree); //first commit commitPattern(repo, "*", "Initial full commit"); git_repository_free(repo); }
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 git_repository_free(git_repository *repo) { git_hashtable_iterator it; git_object *object; assert(repo); free(repo->path_workdir); free(repo->path_index); free(repo->path_repository); free(repo->path_odb); git_hashtable_iterator_init(repo->objects, &it); while ((object = (git_object *) git_hashtable_iterator_next(&it)) != NULL) git_object_free(object); git_hashtable_free(repo->objects); git_odb_close(repo->db); git_index_free(repo->index); free(repo); }
void test_status_worktree__status_file_without_index_or_workdir(void) { git_repository *repo; unsigned int status = 0; git_index *index; cl_git_pass(p_mkdir("wd", 0777)); cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_repository_set_workdir(repo, "wd", false)); cl_git_pass(git_index_open(&index, "empty-index")); cl_assert_equal_i(0, git_index_entrycount(index)); git_repository_set_index(repo, index); cl_git_pass(git_status_file(&status, repo, "branch_file.txt")); cl_assert_equal_i(GIT_STATUS_INDEX_DELETED, status); git_repository_free(repo); git_index_free(index); cl_git_pass(p_rmdir("wd")); }
void test_index_racy__adding_to_index_is_uptodate(void) { git_index *index; const git_index_entry *entry; setup_uptodate_files(); cl_git_pass(git_repository_index(&index, g_repo)); /* ensure that they're all uptodate */ cl_assert((entry = git_index_get_bypath(index, "A", 0))); cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE)); cl_assert((entry = git_index_get_bypath(index, "B", 0))); cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE)); cl_assert((entry = git_index_get_bypath(index, "C", 0))); cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE)); cl_git_pass(git_index_write(index)); git_index_free(index); }
void test_index_filemodes__untrusted(void) { git_index *index; cl_repo_set_bool(g_repo, "core.filemode", false); cl_git_pass(git_repository_index(&index, g_repo)); cl_assert((git_index_caps(index) & GIT_INDEXCAP_NO_FILEMODE) != 0); /* 1 - add 0644 over existing 0644 -> expect 0644 */ replace_file_with_mode("exec_off", "filemodes/exec_off.0", 0644); add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB); /* 2 - add 0644 over existing 0755 -> expect 0755 */ replace_file_with_mode("exec_on", "filemodes/exec_on.0", 0644); add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE); /* 3 - add 0755 over existing 0644 -> expect 0644 */ replace_file_with_mode("exec_off", "filemodes/exec_off.1", 0755); add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB); /* 4 - add 0755 over existing 0755 -> expect 0755 */ replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755); add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE); /* 5 - add new 0644 -> expect 0644 */ cl_git_write2file("filemodes/new_off", "blah", 0, O_WRONLY | O_CREAT | O_TRUNC, 0644); add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB); /* 6 - add new 0755 -> expect 0644 if core.filemode == false */ cl_git_write2file("filemodes/new_on", "blah", 0, O_WRONLY | O_CREAT | O_TRUNC, 0755); add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB); git_index_free(index); }
void test_reset_hard__resetting_reverts_unmerged(void) { git_index *index; int entries; /* Ensure every permutation of non-zero stage entries results in the * path being cleaned up. */ for (entries = 1; entries < 8; entries++) { cl_git_pass(git_repository_index(&index, repo)); unmerged_index_init(index, entries); cl_git_pass(git_index_write(index)); cl_git_pass(git_revparse_single(&target, repo, "26a125e")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); cl_assert(git_path_exists("status/conflicting_file") == 0); git_object_free(target); target = NULL; git_index_free(index); } }
void test_clone_nonetwork__clone_submodule(void) { git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_index *index; git_oid tree_id, commit_id; git_submodule *sm; git_signature *sig; git_repository *sm_repo; cl_git_pass(git_repository_init(&g_repo, "willaddsubmodule", false)); /* Create the submodule structure, clone into it and finalize */ cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo", true)); clone_opts.repository_cb = just_return_repo; clone_opts.repository_cb_payload = sm; clone_opts.remote_cb = just_return_origin; clone_opts.remote_cb_payload = sm; cl_git_pass(git_clone(&sm_repo, cl_fixture("testrepo.git"), "testrepo", &clone_opts)); cl_git_pass(git_submodule_add_finalize(sm)); git_repository_free(sm_repo); git_submodule_free(sm); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_write_tree(&tree_id, index)); git_index_free(index); cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local")); cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n", &tree_id, 0, NULL)); git_signature_free(sig); assert_submodule_exists(g_repo, "testrepo"); }
void test_index_tests__default_test_index(void) { git_index *index; unsigned int i; git_index_entry **entries; cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); cl_assert(index->on_disk); cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count); cl_assert(index->entries.sorted); entries = (git_index_entry **)index->entries.contents; for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { git_index_entry *e = entries[test_entries[i].index]; cl_assert_equal_s(e->path, test_entries[i].path); cl_assert(e->mtime.seconds == test_entries[i].mtime); cl_assert(e->file_size == test_entries[i].file_size); } git_index_free(index); }