void test_checkout_index__can_update_prefixed_files(void) { 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")); cl_git_mkfile("./testrepo/READ", "content\n"); cl_git_mkfile("./testrepo/README.after", "content\n"); cl_git_pass(p_mkdir("./testrepo/branch_file", 0777)); cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777)); cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n"); cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777)); opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED; cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); /* remove untracked will remove the .gitattributes file before the blobs * were created, so they will have had crlf filtering applied on Windows */ test_file_contents_nocr("./testrepo/README", "hey there\n"); test_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n"); test_file_contents_nocr("./testrepo/new.txt", "my new file\n"); cl_assert(!git_path_exists("testrepo/READ")); cl_assert(!git_path_exists("testrepo/README.after")); cl_assert(!git_path_exists("testrepo/branch_file")); cl_assert(!git_path_exists("testrepo/branch_file.txt.after")); }
void test_core_rmdir__can_remove_empty_parents(void) { git_buf file = GIT_BUF_INIT; cl_git_pass( git_buf_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt")); cl_git_mkfile(git_buf_cstr(&file), "dummy"); cl_assert(git_path_isfile(git_buf_cstr(&file))); cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir, GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS)); cl_assert(!git_path_exists(git_buf_cstr(&file))); git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */ cl_assert(!git_path_exists(git_buf_cstr(&file))); git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */ cl_assert(!git_path_exists(git_buf_cstr(&file))); git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */ cl_assert(git_path_exists(git_buf_cstr(&file))); cl_assert(git_path_exists(empty_tmp_dir) == true); git_buf_dispose(&file); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); }
void test_core_filebuf__symlink_follow(void) { git_filebuf file = GIT_FILEBUF_INIT; const char *dir = "linkdir", *source = "linkdir/link"; #ifdef GIT_WIN32 cl_skip(); #endif cl_git_pass(p_mkdir(dir, 0777)); cl_git_pass(p_symlink("target", source)); cl_git_pass(git_filebuf_open(&file, source, 0, 0666)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_assert_equal_i(true, git_path_exists("linkdir/target.lock")); cl_git_pass(git_filebuf_commit(&file)); cl_assert_equal_i(true, git_path_exists("linkdir/target")); git_filebuf_cleanup(&file); /* The second time around, the target file does exist */ cl_git_pass(git_filebuf_open(&file, source, 0, 0666)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_assert_equal_i(true, git_path_exists("linkdir/target.lock")); cl_git_pass(git_filebuf_commit(&file)); cl_assert_equal_i(true, git_path_exists("linkdir/target")); git_filebuf_cleanup(&file); cl_git_pass(git_futils_rmdir_r(dir, NULL, GIT_RMDIR_REMOVE_FILES)); }
/* Test that writing an invalid filename fails */ void test_index_tests__add_invalid_filename(void) { git_repository *repo; p_mkdir("invalid", 0700); cl_git_pass(git_repository_init(&repo, "./invalid", 0)); cl_must_pass(p_mkdir("./invalid/subdir", 0777)); /* cl_git_mkfile() needs the dir to exist */ if (!git_path_exists("./invalid/.GIT")) cl_must_pass(p_mkdir("./invalid/.GIT", 0777)); if (!git_path_exists("./invalid/.GiT")) cl_must_pass(p_mkdir("./invalid/.GiT", 0777)); add_invalid_filename(repo, ".git/hello"); add_invalid_filename(repo, ".GIT/hello"); add_invalid_filename(repo, ".GiT/hello"); add_invalid_filename(repo, "./.git/hello"); add_invalid_filename(repo, "./foo"); add_invalid_filename(repo, "./bar"); add_invalid_filename(repo, "subdir/../bar"); git_repository_free(repo); cl_fixture_cleanup("invalid"); }
void test_core_filebuf__rename_error(void) { git_filebuf file = GIT_FILEBUF_INIT; char *dir = "subdir", *test = "subdir/test", *test_lock = "subdir/test.lock"; int fd; #ifndef GIT_WIN32 cl_skip(); #endif cl_git_pass(p_mkdir(dir, 0666)); cl_git_mkfile(test, "dummy content"); fd = p_open(test, O_RDONLY); cl_assert(fd > 0); cl_git_pass(git_filebuf_open(&file, test, 0, 0666)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_assert_equal_i(true, git_path_exists(test_lock)); cl_git_fail(git_filebuf_commit(&file)); p_close(fd); git_filebuf_cleanup(&file); cl_assert_equal_i(false, git_path_exists(test_lock)); }
void test_clone_nonetwork__do_not_clean_existing_directory(void) { git_buf path_buf = GIT_BUF_INIT; git_buf_put(&path_buf, "./foo", 5); /* Clone should not remove the directory if it already exists, but * Should clean up entries it creates. */ p_mkdir("./foo", GIT_DIR_MODE); cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); cl_assert(git_path_exists("./foo")); /* Make sure the directory is empty. */ cl_git_pass(git_path_direach(&path_buf, dont_call_me, NULL)); /* Try again with a bare repository. */ g_options.bare = true; cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); cl_assert(git_path_exists("./foo")); /* Make sure the directory is empty. */ cl_git_pass(git_path_direach(&path_buf, dont_call_me, NULL)); git_buf_free(&path_buf); }
void test_refs_delete__packed_loose(void) { // deleting a ref which is both packed and loose should remove both tracks in the filesystem git_reference *looked_up_ref, *another_looked_up_ref; git_buf temp_path = GIT_BUF_INIT; /* Ensure the loose reference exists on the file system */ cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name)); cl_assert(git_path_exists(temp_path.ptr)); /* Lookup the reference */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); /* Ensure it's the loose version that has been found */ cl_assert(reference_is_packed(looked_up_ref) == 0); /* Now that the reference is deleted... */ cl_git_pass(git_reference_delete(looked_up_ref)); git_reference_free(looked_up_ref); /* Looking up the reference once again should not retrieve it */ cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); /* Ensure the loose reference doesn't exist any longer on the file system */ cl_assert(!git_path_exists(temp_path.ptr)); git_reference_free(another_looked_up_ref); git_buf_free(&temp_path); }
void test_refs_pack__loose(void) { /* create a packfile from all the loose refs in a repo */ git_reference *reference; git_buf temp_path = GIT_BUF_INIT; /* Ensure a known loose ref can be looked up */ cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); cl_assert(reference_is_packed(reference) == 0); cl_assert_equal_s(reference->name, loose_tag_ref_name); git_reference_free(reference); /* * We are now trying to pack also a loose reference * called `points_to_blob`, to make sure we can properly * pack weak tags */ packall(); /* Ensure the packed-refs file exists */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE)); cl_assert(git_path_exists(temp_path.ptr)); /* Ensure the known ref can still be looked up but is now packed */ cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); cl_assert(reference_is_packed(reference)); cl_assert_equal_s(reference->name, loose_tag_ref_name); /* Ensure the known ref has been removed from the loose folder structure */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name)); cl_assert(!git_path_exists(temp_path.ptr)); git_reference_free(reference); git_buf_free(&temp_path); }
void test_reset_hard__cleans_up_merge(void) { git_buf merge_head_path = GIT_BUF_INIT, merge_msg_path = GIT_BUF_INIT, merge_mode_path = GIT_BUF_INIT, orig_head_path = GIT_BUF_INIT; cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); cl_git_pass(git_buf_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG")); cl_git_mkfile(git_buf_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n"); cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE")); cl_git_mkfile(git_buf_cstr(&merge_mode_path), ""); cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD")); cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); cl_git_pass(git_revparse_single(&target, repo, "0017bd4")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path))); cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path))); cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path))); cl_assert(git_path_exists(git_buf_cstr(&orig_head_path))); cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path))); git_buf_free(&merge_head_path); git_buf_free(&merge_msg_path); git_buf_free(&merge_mode_path); git_buf_free(&orig_head_path); }
void test_clone_nonetwork__bad_url(void) { /* Clone should clean up the mess if the URL isn't a git repository */ cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL, NULL, NULL)); cl_assert(!git_path_exists("./foo")); cl_git_fail(git_clone_bare(&g_repo, "not_a_repo", "./foo.git", NULL)); cl_assert(!git_path_exists("./foo.git")); }
static int lock_file(git_filebuf *file, int flags) { if (git_path_exists(file->path_lock) == true) { if (flags & GIT_FILEBUF_FORCE) p_unlink(file->path_lock); else { giterr_clear(); /* actual OS error code just confuses */ giterr_set(GITERR_OS, "Failed to lock file '%s' for writing", file->path_lock); return -1; } } /* create path to the file buffer is required */ if (flags & GIT_FILEBUF_FORCE) { /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, GIT_LOCK_FILE_MODE); } else { file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE); } if (file->fd < 0) return -1; file->fd_is_open = true; if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == true) { git_file source; char buffer[2048]; ssize_t read_bytes; source = p_open(file->path_original, O_RDONLY); if (source < 0) { giterr_set(GITERR_OS, "Failed to open file '%s' for reading", file->path_original); return -1; } while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { p_write(file->fd, buffer, read_bytes); if (file->digest) git_hash_update(file->digest, buffer, read_bytes); } p_close(source); if (read_bytes < 0) { giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original); return -1; } } return 0; }
int git_clone( git_repository **out, const char *url, const char *local_path, const git_clone_options *_options) { int retcode = GIT_ERROR; git_repository *repo = NULL; git_remote *origin; git_clone_options options = GIT_CLONE_OPTIONS_INIT; int remove_directory_on_failure = 0; assert(out && url && local_path); if (_options) memcpy(&options, _options, sizeof(git_clone_options)); GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); /* Only clone to a new directory or an empty directory */ if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) { giterr_set(GITERR_INVALID, "'%s' exists and is not an empty directory", local_path); return GIT_ERROR; } /* Only remove the directory on failure if we create it */ remove_directory_on_failure = !git_path_exists(local_path); if ((retcode = git_repository_init(&repo, local_path, options.bare)) < 0) return retcode; if ((retcode = create_and_configure_origin(&origin, repo, url, &options)) < 0) goto cleanup; retcode = git_clone_into(repo, origin, &options.checkout_opts, options.checkout_branch); git_remote_free(origin); if (retcode < 0) goto cleanup; *out = repo; return 0; cleanup: git_repository_free(repo); if (remove_directory_on_failure) git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES); else git_futils_cleanupdir_r(local_path); return retcode; }
static int transport_find_fn(const char *url, git_transport_cb *callback, void **param) { size_t i = 0; unsigned priority = 0; transport_definition *definition = NULL, *definition_iter; // First, check to see if it's an obvious URL, which a URL scheme for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { definition_iter = &transports[i]; if (strncasecmp(url, definition_iter->prefix, strlen(definition_iter->prefix))) continue; if (definition_iter->priority > priority) definition = definition_iter; } #ifdef GIT_WIN32 /* On Windows, it might not be possible to discern between absolute local * and ssh paths - first check if this is a valid local path that points * to a directory and if so assume local path, else assume SSH */ /* Check to see if the path points to a file on the local file system */ if (!definition && git_path_exists(url) && git_path_isdir(url)) definition = &local_transport_definition; /* It could be a SSH remote path. Check to see if there's a : * SSH is an unsupported transport mechanism in this version of libgit2 */ if (!definition && strrchr(url, ':')) definition = &dummy_transport_definition; #else /* For other systems, perform the SSH check first, to avoid going to the * filesystem if it is not necessary */ /* It could be a SSH remote path. Check to see if there's a : * SSH is an unsupported transport mechanism in this version of libgit2 */ if (!definition && strrchr(url, ':')) definition = &dummy_transport_definition; /* Check to see if the path points to a file on the local file system */ if (!definition && git_path_exists(url) && git_path_isdir(url)) definition = &local_transport_definition; #endif if (!definition) return -1; *callback = definition->fn; *param = definition->param; return 0; }
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); }
/* git reset --hard 5acdc74af27172ec491d213ee36cea7eb9ef2579 * git revert HEAD */ void test_revert_workdir__merge_fails_without_mainline_specified(void) { git_commit *head; git_oid head_oid; git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579"); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); cl_must_fail(git_revert(repo, head, NULL)); cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/REVERT_HEAD")); git_commit_free(head); }
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); } }
void test_clone_nonetwork__bad_urls(void) { /* Clone should clean up the mess if the URL isn't a git repository */ cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); cl_assert(!git_path_exists("./foo")); g_options.bare = true; cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options)); cl_assert(!git_path_exists("./foo")); cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options)); cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options)); cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git", "./foo", &g_options)); cl_git_fail(git_clone(&g_repo, "arrbee:my/bad:[email protected]:1111/strange:words.git", "./foo", &g_options)); }
void test_diff_iterator__index_case_folding(void) { git_buf path = GIT_BUF_INIT; int fs_is_ci = 0; cl_git_pass(git_buf_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg")); fs_is_ci = git_path_exists(path.ptr); git_buf_free(&path); index_iterator_test( "icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs), fs_is_ci ? expected_index_ci : expected_index_cs, NULL); cl_git_sandbox_cleanup(); index_iterator_test( "icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE, ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL); cl_git_sandbox_cleanup(); index_iterator_test( "icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE, ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL); }
void test_repo_init__at_filesystem_root(void) { git_repository *repo; const char *sandbox = clar_sandbox_path(); git_buf root = GIT_BUF_INIT; int root_len; if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) cl_skip(); root_len = git_path_root(sandbox); cl_assert(root_len >= 0); git_buf_put(&root, sandbox, root_len+1); git_buf_joinpath(&root, root.ptr, "libgit2_test_dir"); cl_assert(!git_path_exists(root.ptr)); cl_git_pass(git_repository_init(&repo, root.ptr, 0)); cl_assert(git_path_isdir(root.ptr)); cl_git_pass(git_futils_rmdir_r(root.ptr, NULL, GIT_RMDIR_REMOVE_FILES)); git_buf_free(&root); git_repository_free(repo); }
int git_futils_find_system_file(git_buf *path, const char *filename) { #ifdef GIT_WIN32 struct win32_path root; if (win32_expand_path(&root, L"%PROGRAMFILES%\\Git\\etc\\") < 0 || root.path[0] == L'%') /* i.e. no expansion happened */ { giterr_set(GITERR_OS, "Cannot locate the system's Program Files directory"); return -1; } if (win32_find_file(path, &root, filename) < 0) { git_buf_clear(path); return GIT_ENOTFOUND; } return 0; #else if (git_buf_joinpath(path, "/etc", filename) < 0) return -1; if (git_path_exists(path->ptr) == true) return 0; git_buf_clear(path); return GIT_ENOTFOUND; #endif }
void test_core_rmdir__can_skip__non_empty_dir(void) { git_buf file = GIT_BUF_INIT; cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt")); cl_git_mkfile(git_buf_cstr(&file), "dummy"); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_ONLY_EMPTY_DIRS)); cl_assert(git_path_exists(git_buf_cstr(&file)) == true); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_FILES_AND_DIRS)); cl_assert(git_path_exists(empty_tmp_dir) == false); git_buf_free(&file); }
int git_futils_cleanupdir_r(const char *path) { int error; git_buf fullpath = GIT_BUF_INIT; futils__rmdir_data data; if ((error = git_buf_put(&fullpath, path, strlen(path))) < 0) goto clean_up; data.base = ""; data.baselen = 0; data.flags = GIT_RMDIR_REMOVE_FILES; data.error = 0; if (!git_path_exists(path)) { giterr_set(GITERR_OS, "Path does not exist: %s" , path); error = GIT_ERROR; goto clean_up; } if (!git_path_isdir(path)) { giterr_set(GITERR_OS, "Path is not a directory: %s" , path); error = GIT_ERROR; goto clean_up; } error = git_path_direach(&fullpath, futils__rmdir_recurs_foreach, &data); if (error == GIT_EUSER) error = data.error; clean_up: git_buf_free(&fullpath); return error; }
void test_merge_workdir_simple__checkout_ours(void) { 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 }; merge_simple_branch(0, GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); cl_assert(git_path_exists(TEST_REPO_PATH "/conflicting.txt")); }
static int git_futils_find_in_dirlist( git_buf *path, const char *name, git_futils_dir_t which, const char *label) { size_t len; const char *scan, *next = NULL; const git_buf *syspath; GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which)); for (scan = git_buf_cstr(syspath); scan; scan = next) { for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR); next && next > scan && next[-1] == '\\'; next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR)) /* find unescaped separator or end of string */; len = next ? (size_t)(next++ - scan) : strlen(scan); if (!len) continue; GITERR_CHECK_ERROR(git_buf_set(path, scan, len)); GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name)); if (git_path_exists(path->ptr)) return 0; } git_buf_clear(path); giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name); return GIT_ENOTFOUND; }
void test_core_rmdir__can_skip_non_empty_dir(void) { git_buf file = GIT_BUF_INIT; cl_git_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt")); cl_git_mkfile(git_buf_cstr(&file), "dummy"); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY)); cl_assert(git_path_exists(git_buf_cstr(&file)) == true); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES)); cl_assert(git_path_exists(empty_tmp_dir) == false); git_buf_dispose(&file); }
void git_filebuf_cleanup(git_filebuf *file) { if (file->fd_is_open && file->fd >= 0) p_close(file->fd); if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock)) p_unlink(file->path_lock); if (file->digest) git_hash_free_ctx(file->digest); if (file->buffer) git__free(file->buffer); /* use the presence of z_buf to decide if we need to deflateEnd */ if (file->z_buf) { git__free(file->z_buf); deflateEnd(&file->zs); } if (file->path_original) git__free(file->path_original); if (file->path_lock) git__free(file->path_lock); memset(file, 0x0, sizeof(git_filebuf)); file->fd = -1; }
void test_stash_save__ignored_directory(void) { cl_git_pass(p_mkdir("stash/ignored_directory", 0777)); cl_git_pass(p_mkdir("stash/ignored_directory/sub", 0777)); cl_git_mkfile("stash/ignored_directory/sub/some_file", "stuff"); assert_status(repo, "ignored_directory/sub/some_file", GIT_STATUS_WT_NEW); cl_git_pass(git_ignore_add_rule(repo, "ignored_directory/")); assert_status(repo, "ignored_directory/sub/some_file", GIT_STATUS_IGNORED); cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)); cl_assert(!git_path_exists("stash/ignored_directory/sub/some_file")); cl_assert(!git_path_exists("stash/ignored_directory/sub")); cl_assert(!git_path_exists("stash/ignored_directory")); }
static int create_and_configure_origin( git_remote **out, git_repository *repo, const char *url, const git_clone_options *options) { int error; git_remote *origin = NULL; char buf[GIT_PATH_MAX]; git_remote_create_cb remote_create = options->remote_cb; void *payload = options->remote_cb_payload; /* If the path exists and is a dir, the url should be the absolute path */ if (git_path_root(url) < 0 && git_path_exists(url) && git_path_isdir(url)) { if (p_realpath(url, buf) == NULL) return -1; url = buf; } if (!remote_create) { remote_create = default_remote_create; payload = NULL; } if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0) goto on_error; *out = origin; return 0; on_error: git_remote_free(origin); return error; }
void test_config_readonly__writing_to_cfg_with_ro_precedence_succeeds(void) { git_config_backend *backend; cl_git_pass(git_config_backend_from_file(&backend, "local")); backend->readonly = 1; cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_backend_from_file(&backend, "global")); cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz")); cl_assert(!git_path_exists("local")); cl_assert(git_path_exists("global")); cl_git_pass(p_unlink("global")); }
void test_revert_workdir__nonmerge_fails_mainline_specified(void) { git_reference *head; git_commit *commit; git_revert_options opts = GIT_REVERT_OPTIONS_INIT; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT)); opts.mainline = 1; cl_must_fail(git_revert(repo, commit, &opts)); cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/REVERT_HEAD")); git_reference_free(head); git_commit_free(commit); }