void test_checkout_tree__can_continue_when_dir_in_use(void) { #ifdef GIT_WIN32 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; git_object *obj = NULL; opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_assert(git_path_isfile("testrepo/a/b.txt")); git_object_free(obj); cl_git_pass(p_chdir("testrepo/a")); cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(p_chdir("../..")); cl_assert(git_path_is_empty_dir("testrepo/a")); git_object_free(obj); #endif }
void test_checkout_tree__can_checkout_and_remove_directory(void) { cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the * current working tree */ cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/")); cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt")); git_object_free(g_object); g_object = NULL; /* Checkout brach "master" and update HEAD, so that HEAD matches the * current working tree */ cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); /* This directory should no longer exist */ cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); }
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; opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_repository_index(&index, g_repo)); /* test a freshly added executable */ cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); 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(GIT_PERMS_IS_EXEC(entry->mode)); git_commit_free(commit); /* Now start with a commit which has a text file */ cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); cl_assert(!GIT_PERMS_IS_EXEC(entry->mode)); git_commit_free(commit); /* And then check out to a commit which converts the text file to an executable */ cl_git_pass(git_oid_fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); cl_assert(GIT_PERMS_IS_EXEC(entry->mode)); git_commit_free(commit); /* Finally, check out the text file again and check that the exec bit is cleared */ cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); cl_assert(!GIT_PERMS_IS_EXEC(entry->mode)); git_commit_free(commit); git_index_free(index); }
void assert_conflict( const char *entry_path, const char *new_content, const char *parent_sha, const char *commit_sha) { git_index *index; git_object *hack_tree; git_reference *branch, *head; git_buf file_path = GIT_BUF_INIT; cl_git_pass(git_repository_index(&index, g_repo)); /* Create a branch pointing at the parent */ cl_git_pass(git_revparse_single(&g_object, g_repo, parent_sha)); cl_git_pass(git_branch_create(&branch, g_repo, "potential_conflict", (git_commit *)g_object, 0)); /* Make HEAD point to this branch */ cl_git_pass(git_reference_symbolic_create( &head, g_repo, "HEAD", git_reference_name(branch), 1, NULL)); git_reference_free(head); git_reference_free(branch); /* Checkout the parent */ g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); /* Hack-ishy workaound to ensure *all* the index entries * match the content of the tree */ cl_git_pass(git_object_peel(&hack_tree, g_object, GIT_OBJ_TREE)); cl_git_pass(git_index_read_tree(index, (git_tree *)hack_tree)); git_object_free(hack_tree); git_object_free(g_object); g_object = NULL; /* Create a conflicting file */ cl_git_pass(git_buf_joinpath(&file_path, "./testrepo", entry_path)); cl_git_mkfile(git_buf_cstr(&file_path), new_content); git_buf_free(&file_path); /* Trying to checkout the original commit */ cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha)); g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_assert_equal_i( GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); /* Stage the conflicting change */ cl_git_pass(git_index_add_bypath(index, entry_path)); cl_git_pass(git_index_write(index)); git_index_free(index); cl_assert_equal_i( GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); }
void test_checkout_tree__can_switch_branches(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; git_object *obj = NULL; assert_on_branch(g_repo, "master"); /* do first checkout with FORCE because we don't know if testrepo * base data is clean for a checkout or not */ opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); cl_assert(git_path_isfile("testrepo/README")); cl_assert(git_path_isfile("testrepo/branch_file.txt")); cl_assert(git_path_isfile("testrepo/new.txt")); cl_assert(git_path_isfile("testrepo/a/b.txt")); cl_assert(!git_path_isdir("testrepo/ab")); assert_on_branch(g_repo, "dir"); git_object_free(obj); /* do second checkout safe because we should be clean after first */ opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); cl_assert(git_path_isfile("testrepo/README")); cl_assert(git_path_isfile("testrepo/branch_file.txt")); cl_assert(git_path_isfile("testrepo/new.txt")); cl_assert(git_path_isfile("testrepo/ab/4.txt")); cl_assert(git_path_isfile("testrepo/ab/c/3.txt")); cl_assert(git_path_isfile("testrepo/ab/de/2.txt")); cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt")); cl_assert(!git_path_isdir("testrepo/a")); assert_on_branch(g_repo, "subtrees"); git_object_free(obj); }
void test_checkout_tree__can_cancel_checkout_from_notify(void) { struct checkout_cancel_at ca; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; git_object *obj = NULL; assert_on_branch(g_repo, "master"); cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); ca.filename = "new.txt"; ca.error = -5555; ca.count = 0; opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED; opts.notify_cb = checkout_cancel_cb; opts.notify_payload = &ca; opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_assert(!git_path_exists("testrepo/new.txt")); cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555); cl_assert(!git_path_exists("testrepo/new.txt")); /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */ /* on case-sensitive FS = README, then above */ if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */ cl_assert_equal_i(3, ca.count); else cl_assert_equal_i(4, ca.count); /* and again with a different stopping point and return code */ ca.filename = "README"; ca.error = 123; ca.count = 0; cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123); cl_assert(!git_path_exists("testrepo/new.txt")); if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */ cl_assert_equal_i(4, ca.count); else cl_assert_equal_i(1, ca.count); git_object_free(obj); }
void test_checkout_tree__filemode_preserved_in_workdir(void) { #ifndef GIT_WIN32 git_oid executable_oid; git_commit *commit; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; /* test a freshly added executable */ cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt"))); git_commit_free(commit); /* Now start with a commit which has a text file */ cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); git_commit_free(commit); /* And then check out to a commit which converts the text file to an executable */ cl_git_pass(git_oid_fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); git_commit_free(commit); /* Finally, check out the text file again and check that the exec bit is cleared */ cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); git_commit_free(commit); #endif }
void test_checkout_typechange__checkout_with_conflicts(void) { int i; git_object *obj; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; notify_counts cts = {0}; opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_UNTRACKED; opts.notify_cb = notify_counter; opts.notify_payload = &cts; for (i = 0; g_typechange_oids[i] != NULL; ++i) { cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i])); force_create_file("typechanges/a/blocker"); force_create_file("typechanges/b"); force_create_file("typechanges/c/sub/sub/file"); git_futils_rmdir_r("typechanges/d", NULL, GIT_RMDIR_REMOVE_FILES); p_mkdir("typechanges/d", 0777); /* intentionally empty dir */ force_create_file("typechanges/untracked"); opts.checkout_strategy = GIT_CHECKOUT_SAFE; memset(&cts, 0, sizeof(cts)); cl_git_fail(git_checkout_tree(g_repo, obj, &opts)); cl_assert(cts.conflicts > 0); cl_assert(cts.untracked > 0); opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; memset(&cts, 0, sizeof(cts)); cl_assert(git_path_exists("typechanges/untracked")); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_assert_equal_i(0, cts.conflicts); cl_assert(!git_path_exists("typechanges/untracked")); cl_git_pass( git_repository_set_head_detached(g_repo, git_object_id(obj))); assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true); git_object_free(obj); } }
/* Emulate checking out in a repo created by clone --no-checkout, * which would not have written an index. */ void test_checkout_tree__safe_proceeds_if_no_index(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; git_object *obj = NULL; assert_on_branch(g_repo, "master"); cl_must_pass(p_unlink("testrepo/.git/index")); /* do second checkout safe because we should be clean after first */ opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); cl_assert(git_path_isfile("testrepo/README")); cl_assert(git_path_isfile("testrepo/branch_file.txt")); cl_assert(git_path_isfile("testrepo/new.txt")); cl_assert(git_path_isfile("testrepo/ab/4.txt")); cl_assert(git_path_isfile("testrepo/ab/c/3.txt")); cl_assert(git_path_isfile("testrepo/ab/de/2.txt")); cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt")); cl_assert(!git_path_isdir("testrepo/a")); assert_on_branch(g_repo, "subtrees"); git_object_free(obj); }
void test_checkout_tree__can_not_update_index(void) { git_oid oid; git_object *head; unsigned int status; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_index *index; opts.checkout_strategy |= GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts)); cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); cl_git_pass(git_checkout_tree(g_repo, g_object, &opts)); cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt")); cl_assert_equal_i(GIT_STATUS_WT_NEW, status); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_write(index)); cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt")); cl_assert_equal_i(GIT_STATUS_WT_NEW, status); git_object_free(head); git_index_free(index); }
void test_checkout_tree__cannot_checkout_a_non_treeish(void) { /* blob */ cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); cl_git_fail(git_checkout_tree(g_repo, g_object, NULL)); }
/* * Note: this test was flaky prior to fixing #4101 -- run it several * times to get a failure. The issues is that whether the fast * (stat-only) codepath is used inside stash's diff operation depends * on whether files are "racily clean", and there doesn't seem to be * an easy way to force the exact required state. */ void test_stash_save__untracked_regression(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; const char *paths[] = {"what", "where", "how", "why"}; git_reference *head; git_commit *head_commit; git_buf untracked_dir; const char* workdir = git_repository_workdir(repo); git_buf_init(&untracked_dir, 0); git_buf_printf(&untracked_dir, "%sz", workdir); cl_assert(!p_mkdir(untracked_dir.ptr, 0777)); cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)); opts.checkout_strategy = GIT_CHECKOUT_FORCE; opts.paths.strings = (char **)paths; opts.paths.count = 4; cl_git_pass(git_checkout_tree(repo, (git_object*)head_commit, &opts)); cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); assert_commit_message_contains("refs/stash", "WIP on master"); git_reference_free(head); git_commit_free(head_commit); git_buf_dispose(&untracked_dir); }
static void execute_test(void) { git_oid oid, check; git_commit *commit; git_tree *tree; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/branch1")); cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); cl_git_pass(git_commit_tree(&tree, commit)); opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_checkout_tree(g_repo, (git_object *)tree, &opts)); git_tree_free(tree); git_commit_free(commit); /* Verify that the lenna.jpg file was checked out correctly */ cl_git_pass(git_oid_fromstr(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1")); cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJ_BLOB)); cl_assert(git_oid_equal(&oid, &check)); /* Verify that the text file was checked out correctly */ cl_git_pass(git_oid_fromstr(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a")); cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJ_BLOB)); cl_assert(git_oid_equal(&oid, &check)); }
void test_checkout_icase__conflicts_with_casechanged_subtrees(void) { git_reference *orig_ref; git_object *orig, *subtrees; git_oid oid; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_reference_lookup_resolved(&orig_ref, repo, "HEAD", 100)); cl_git_pass(git_object_lookup(&orig, repo, git_reference_target(orig_ref), GIT_OBJ_COMMIT)); cl_git_pass(git_reset(repo, (git_object *)orig, GIT_RESET_HARD, NULL)); cl_must_pass(p_mkdir("testrepo/AB", 0777)); cl_must_pass(p_mkdir("testrepo/AB/C", 0777)); cl_git_write2file("testrepo/AB/C/3.txt", "Foobar!\n", 8, O_RDWR|O_CREAT, 0666); cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/subtrees")); cl_git_pass(git_object_lookup(&subtrees, repo, &oid, GIT_OBJ_ANY)); cl_git_fail(git_checkout_tree(repo, subtrees, &checkout_opts)); git_object_free(orig); git_object_free(subtrees); git_reference_free(orig_ref); }
void test_checkout_tree__extremely_long_file_name(void) { // A utf-8 string with 83 characters, but 249 bytes. const char *longname = "\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97"; char path[1024]; g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_revparse_single(&g_object, g_repo, "long-file-name")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); sprintf(path, "testrepo/%s.txt", longname); cl_assert(git_path_exists(path)); git_object_free(g_object); cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_assert(!git_path_exists(path)); }
void test_checkout_icase__refuses_to_overwrite_files_for_files(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING; cl_git_write2file("testrepo/BRANCH_FILE.txt", "neue file\n", 10, \ O_WRONLY | O_CREAT | O_TRUNC, 0644); cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts)); assert_name_is("testrepo/BRANCH_FILE.txt"); }
void test_checkout_tree__removes_conflicts_only_by_pathscope(void) { git_oid commit_id; git_commit *commit; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_index *index; const char *path = "executable.txt"; 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; opts.paths.count = 1; opts.paths.strings = (char **)&path; 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(git_index_get_bypath(index, "other.txt", 1) != NULL); cl_assert(git_index_get_bypath(index, "other.txt", 2) != NULL); cl_assert(git_index_get_bypath(index, "other.txt", 3) != NULL); cl_assert(git_path_exists("testrepo/other.txt")); git_commit_free(commit); git_index_free(index); }
void test_checkout_icase__overwrites_files_for_files_when_forced(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_write2file("testrepo/NEW.txt", "neue file\n", 10, \ O_WRONLY | O_CREAT | O_TRUNC, 0644); cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts)); assert_name_is("testrepo/new.txt"); }
void test_checkout_tree__calls_progress_callback(void) { bool was_called = 0; g_opts.progress_cb = progress; g_opts.progress_payload = &was_called; cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_assert_equal_i(was_called, true); }
static int rebase_init_merge( git_rebase *rebase, git_repository *repo, const git_annotated_commit *branch, const git_annotated_commit *upstream, const git_annotated_commit *onto) { git_reference *head_ref = NULL; git_commit *onto_commit = NULL; git_buf reflog = GIT_BUF_INIT; git_buf state_path = GIT_BUF_INIT; int error; GIT_UNUSED(upstream); if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0) goto done; rebase->state_path = git_buf_detach(&state_path); GITERR_CHECK_ALLOC(rebase->state_path); if (branch->ref_name) { rebase->orig_head_name = git__strdup(branch->ref_name); GITERR_CHECK_ALLOC(rebase->orig_head_name); } else { rebase->head_detached = 1; } rebase->onto_name = git__strdup(rebase_onto_name(onto)); GITERR_CHECK_ALLOC(rebase->onto_name); rebase->quiet = rebase->options.quiet; git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch)); git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto)); if ((error = rebase_setupfiles(rebase)) < 0 || (error = git_buf_printf(&reflog, "rebase: checkout %s", rebase_onto_name(onto))) < 0 || (error = git_commit_lookup( &onto_commit, repo, git_annotated_commit_id(onto))) < 0 || (error = git_checkout_tree(repo, (git_object *)onto_commit, &rebase->options.checkout_options)) < 0 || (error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE, git_annotated_commit_id(onto), 1, reflog.ptr)) < 0) goto done; done: git_reference_free(head_ref); git_commit_free(onto_commit); git_buf_free(&reflog); git_buf_free(&state_path); return error; }
void test_checkout_tree__can_disable_pattern_match(void) { char *entries[] = { "b*.txt" }; /* reset to beginning of history (i.e. just a README file) */ g_opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_git_pass( git_repository_set_head_detached(g_repo, git_object_id(g_object))); git_object_free(g_object); g_object = NULL; cl_assert(!git_path_isfile("testrepo/branch_file.txt")); /* now to a narrow patterned checkout, but disable pattern */ g_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; g_opts.paths.strings = entries; g_opts.paths.count = 1; cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_assert(!git_path_isfile("testrepo/branch_file.txt")); /* let's try that again, but allow the pattern match */ g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_assert(git_path_isfile("testrepo/branch_file.txt")); }
void test_checkout_icase__overwrites_links_for_files_when_forced(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/NEW.txt")); cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts)); cl_assert(!git_path_exists("tmp")); assert_name_is("testrepo/new.txt"); }
void test_checkout_icase__refuses_to_overwrite_links_for_files(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING; cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/BRANCH_FILE.txt")); cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts)); cl_assert(!git_path_exists("tmp")); assert_name_is("testrepo/BRANCH_FILE.txt"); }
void test_checkout_icase__refuses_to_overwrite_links_for_folders(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING; cl_must_pass(p_symlink("..", "testrepo/A")); cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts)); cl_assert(!git_path_exists("b.txt")); assert_name_is("testrepo/A"); }
void test_checkout_icase__overwrites_links_for_folders_when_forced(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_must_pass(p_symlink("..", "testrepo/A")); cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts)); cl_assert(!git_path_exists("b.txt")); assert_name_is("testrepo/a"); }
void test_checkout_icase__overwrites_empty_folders_for_files(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING; cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777)); cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts)); assert_name_is("testrepo/new.txt"); cl_assert(!git_path_isdir("testrepo/new.txt")); }
void test_checkout_tree__target_directory_from_bare(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; checkout_counts cts; memset(&cts, 0, sizeof(cts)); test_checkout_tree__cleanup(); /* cleanup default checkout */ g_repo = cl_git_sandbox_init("testrepo.git"); cl_assert(git_repository_is_bare(g_repo)); opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING; opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL; opts.notify_cb = checkout_count_callback; opts.notify_payload = &cts; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJ_ANY)); cl_git_fail(git_checkout_tree(g_repo, g_object, &opts)); opts.target_directory = "alternative"; cl_assert(!git_path_isdir("alternative")); cl_git_pass(git_checkout_tree(g_repo, g_object, &opts)); cl_assert_equal_i(0, cts.n_untracked); cl_assert_equal_i(0, cts.n_ignored); cl_assert_equal_i(3, cts.n_updates); check_file_contents_nocr("./alternative/README", "hey there\n"); check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n"); check_file_contents_nocr("./alternative/new.txt", "my new file\n"); cl_git_pass(git_futils_rmdir_r( "alternative", NULL, GIT_RMDIR_REMOVE_FILES)); }
void test_checkout_icase__refuses_to_overwrite_populated_folders_for_files(void) { checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING; cl_must_pass(p_mkdir("testrepo/BRANCH_FILE.txt", 0777)); cl_git_write2file("testrepo/BRANCH_FILE.txt/foobar", "neue file\n", 10, \ O_WRONLY | O_CREAT | O_TRUNC, 0644); cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts)); assert_name_is("testrepo/BRANCH_FILE.txt"); cl_assert(git_path_isdir("testrepo/BRANCH_FILE.txt")); }
/* A repo that has a HEAD (even a properly born HEAD that peels to * a commit) but no index should be treated as if it's an empty baseline */ void test_checkout_tree__baseline_is_empty_when_no_index(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_reference *head; git_object *obj; git_status_list *status; size_t conflicts = 0; assert_on_branch(g_repo, "master"); cl_git_pass(git_repository_head(&head, g_repo)); cl_git_pass(git_reference_peel(&obj, head, GIT_OBJ_COMMIT)); cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL)); cl_must_pass(p_unlink("testrepo/.git/index")); /* for a safe checkout, we should have checkout conflicts with * the existing untracked files. */ opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE; opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT; opts.notify_cb = checkout_conflict_count_cb; opts.notify_payload = &conflicts; cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, obj, &opts)); cl_assert_equal_i(4, conflicts); /* but force should succeed and update the index */ opts.checkout_strategy |= GIT_CHECKOUT_FORCE; cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_status_list_new(&status, g_repo, NULL)); cl_assert_equal_i(0, git_status_list_entrycount(status)); git_status_list_free(status); git_object_free(obj); git_reference_free(head); }
void test_checkout_tree__can_checkout_with_pattern(void) { char *entries[] = { "[l-z]*.txt" }; /* reset to beginning of history (i.e. just a README file) */ g_opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_git_pass( git_repository_set_head_detached(g_repo, git_object_id(g_object))); git_object_free(g_object); g_object = NULL; cl_assert(git_path_exists("testrepo/README")); cl_assert(!git_path_exists("testrepo/branch_file.txt")); cl_assert(!git_path_exists("testrepo/link_to_new.txt")); cl_assert(!git_path_exists("testrepo/new.txt")); /* now to a narrow patterned checkout */ g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; g_opts.paths.strings = entries; g_opts.paths.count = 1; cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); cl_assert(git_path_exists("testrepo/README")); cl_assert(!git_path_exists("testrepo/branch_file.txt")); cl_assert(git_path_exists("testrepo/link_to_new.txt")); cl_assert(git_path_exists("testrepo/new.txt")); }