/* 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_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_refs_reflog_reflog__fails_gracefully_on_nonempty_reflog_dir(void) { git_reference *ref; git_buf log_path = GIT_BUF_INIT; git_oid id; /* Create a new branch pointing at the HEAD */ git_oid_fromstr(&id, current_master_tip); cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir/new-head", &id, 0, NULL)); git_reference_free(ref); git_buf_joinpath(&log_path, git_repository_path(g_repo), GIT_REFLOG_DIR); git_buf_joinpath(&log_path, git_buf_cstr(&log_path), "refs/heads/new-dir/new-head"); cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path))); /* delete the ref manually, leave the reflog */ cl_must_pass(p_unlink("testrepo.git/refs/heads/new-dir/new-head")); /* new ref creation should fail since new-dir contains reflogs still */ git_oid_fromstr(&id, current_master_tip); cl_git_fail_with(GIT_EDIRECTORY, git_reference_create(&ref, g_repo, "refs/heads/new-dir", &id, 0, NULL)); git_reference_free(ref); git_buf_dispose(&log_path); }
void test_checkout_index__conflicts_honor_coreautocrlf(void) { #ifdef GIT_WIN32 git_index *index; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_buf conflicting_buf = GIT_BUF_INIT; cl_git_pass(p_unlink("./testrepo/.gitattributes")); cl_repo_set_bool(g_repo, "core.autocrlf", true); cl_git_pass(git_repository_index(&index, g_repo)); add_conflict(index, "conflicting.txt"); cl_git_pass(git_index_write(index)); cl_git_pass(git_checkout_index(g_repo, NULL, &opts)); cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, "<<<<<<< ours\r\n" "this file is changed in master and branch\r\n" "=======\r\n" "this file is changed in branch and master\r\n" ">>>>>>> theirs\r\n") == 0); git_buf_free(&conflicting_buf); git_index_free(index); #endif }
void test_object_blob_fromchunks__doesnot_overwrite_an_already_existing_object(void) { git_buf path = GIT_BUF_INIT; git_buf content = GIT_BUF_INIT; git_oid expected_oid, oid; int howmany = 7; cl_git_pass(git_oid_fromstr(&expected_oid, "321cbdf08803c744082332332838df6bd160f8f9")); cl_git_pass(git_blob_create_fromchunks(&oid, repo, NULL, text_chunked_source_cb, &howmany)); /* Let's replace the content of the blob file storage with something else... */ cl_git_pass(git_buf_joinpath(&path, git_repository_path(repo), "objects/32/1cbdf08803c744082332332838df6bd160f8f9")); cl_git_pass(p_unlink(git_buf_cstr(&path))); cl_git_mkfile(git_buf_cstr(&path), "boom"); /* ...request a creation of the same blob... */ howmany = 7; cl_git_pass(git_blob_create_fromchunks(&oid, repo, NULL, text_chunked_source_cb, &howmany)); /* ...and ensure the content of the faked blob file hasn't been altered */ cl_git_pass(git_futils_readbuffer(&content, git_buf_cstr(&path))); cl_assert(!git__strcmp("boom", git_buf_cstr(&content))); git_buf_free(&path); git_buf_free(&content); }
void test_stash_save__cannot_stash_when_there_are_no_local_change(void) { git_index *index; git_oid stash_tip_oid; cl_git_pass(git_repository_index(&index, repo)); /* * 'what', 'where' and 'who' are being committed. * 'when' remains untracked. */ cl_git_pass(git_index_add_bypath(index, "what")); cl_git_pass(git_index_add_bypath(index, "where")); cl_git_pass(git_index_add_bypath(index, "who")); cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); git_index_free(index); cl_assert_equal_i(GIT_ENOTFOUND, git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); p_unlink("stash/when"); cl_assert_equal_i(GIT_ENOTFOUND, git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); }
static int _rmdir_recurs_foreach(void *opaque, git_buf *path) { git_directory_removal_type removal_type = *(git_directory_removal_type *)opaque; if (git_path_isdir(path->ptr) == true) { if (git_path_direach(path, _rmdir_recurs_foreach, opaque) < 0) return -1; if (p_rmdir(path->ptr) < 0) { if (removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS && (errno == ENOTEMPTY || errno == EEXIST)) return 0; giterr_set(GITERR_OS, "Could not remove directory '%s'", path->ptr); return -1; } return 0; } if (removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS) { if (p_unlink(path->ptr) < 0) { giterr_set(GITERR_OS, "Could not remove directory. File '%s' cannot be removed", path->ptr); return -1; } return 0; } if (removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY) { giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr); return -1; } return 0; }
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; }
/* make sure git_filebuf_commit takes umask into account */ void test_core_filebuf__umask(void) { git_filebuf file = GIT_FILEBUF_INIT; char test[] = "test"; struct stat statbuf; mode_t mask, os_mask; #ifdef GIT_WIN32 os_mask = 0600; #else os_mask = 0777; #endif p_umask(mask = p_umask(0)); cl_assert(file.buffer == NULL); cl_git_pass(git_filebuf_open(&file, test, 0, 0666)); cl_assert(file.buffer != NULL); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_assert(file.buffer != NULL); cl_git_pass(git_filebuf_commit(&file)); cl_assert(file.buffer == NULL); cl_must_pass(p_stat("test", &statbuf)); cl_assert_equal_i(statbuf.st_mode & os_mask, (0666 & ~mask) & os_mask); cl_must_pass(p_unlink(test)); }
void test_config_new__write_new_config(void) { git_config *config; git_buf buf = GIT_BUF_INIT; cl_git_mkfile(TEST_CONFIG, ""); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_set_string(config, "color.ui", "auto")); cl_git_pass(git_config_set_string(config, "core.editor", "ed")); git_config_free(config); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_get_string_buf(&buf, config, "color.ui")); cl_assert_equal_s("auto", git_buf_cstr(&buf)); git_buf_clear(&buf); cl_git_pass(git_config_get_string_buf(&buf, config, "core.editor")); cl_assert_equal_s("ed", git_buf_cstr(&buf)); git_buf_free(&buf); git_config_free(config); p_unlink(TEST_CONFIG); }
void test_status_worktree__status_file_with_clean_index_and_empty_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, "my-index")); fill_index_wth_head_entries(repo, index); git_repository_set_index(repo, index); cl_git_pass(git_status_file(&status, repo, "branch_file.txt")); cl_assert_equal_i(GIT_STATUS_WT_DELETED, status); git_repository_free(repo); git_index_free(index); cl_git_pass(p_rmdir("wd")); cl_git_pass(p_unlink("my-index")); }
static void create_index(struct checkout_index_entry *entries, size_t entries_len) { git_buf path = GIT_BUF_INIT; size_t i; for (i = 0; i < entries_len; i++) { git_buf_joinpath(&path, TEST_REPO_PATH, entries[i].path); if (entries[i].stage == 3 && (i == 0 || strcmp(entries[i-1].path, entries[i].path) != 0 || entries[i-1].stage != 2)) p_unlink(git_buf_cstr(&path)); git_index_remove_bypath(g_index, entries[i].path); } for (i = 0; i < entries_len; i++) { git_index_entry entry; memset(&entry, 0x0, sizeof(git_index_entry)); entry.mode = entries[i].mode; GIT_IDXENTRY_STAGE_SET(&entry, entries[i].stage); git_oid_fromstr(&entry.id, entries[i].oid_str); entry.path = entries[i].path; cl_git_pass(git_index_add(g_index, &entry)); } git_buf_free(&path); }
void test_repo_message__message(void) { const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n"; ssize_t len; cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG")); cl_git_mkfile(git_buf_cstr(&_path), expected); len = git_repository_message(NULL, 0, _repo); cl_assert(len > 0); _actual = git__malloc(len + 1); cl_assert(_actual != NULL); /* Test non truncation */ cl_assert(git_repository_message(_actual, len, _repo) > 0); cl_assert_equal_s(expected, _actual); /* Test truncation and that trailing NUL is inserted */ cl_assert(git_repository_message(_actual, 6, _repo) > 0); cl_assert_equal_s("Test\n", _actual); cl_git_pass(p_unlink(git_buf_cstr(&_path))); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); }
static bool try_create_file_with_nsec_timestamp(const char *path) { struct stat st; int try; /* retry a few times to avoid nanos *actually* equal 0 race condition */ for (try = 0; try < 3; try++) { cl_git_mkfile(path, "This is hopefully a file with nanoseconds!"); cl_must_pass(p_stat(path, &st)); if (st.st_ctime_nsec && st.st_mtime_nsec) return true; } return false; } /* try to determine if the underlying filesystem supports a resolution * higher than a single second. (i'm looking at you, hfs+) */ static bool should_expect_nsecs(void) { git_buf nsec_path = GIT_BUF_INIT; bool expect; git_buf_joinpath(&nsec_path, clar_sandbox_path(), "nsec_test"); expect = try_create_file_with_nsec_timestamp(nsec_path.ptr); p_unlink(nsec_path.ptr); git_buf_clear(&nsec_path); return expect; } static bool has_nsecs(void) { const git_index_entry *entry; size_t i; bool has_nsecs = false; for (i = 0; i < git_index_entrycount(repo_index); i++) { entry = git_index_get_byindex(repo_index, i); if (entry->ctime.nanoseconds || entry->mtime.nanoseconds) { has_nsecs = true; break; } } return has_nsecs; } void test_index_nsec__has_nanos(void) { cl_assert_equal_b(true, has_nsecs()); }
static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) { int error = 0; futils__rmdir_data *data = opaque; struct stat st; if (data->depth > FUTILS_MAX_DEPTH) error = futils__error_cannot_rmdir( path->ptr, "directory nesting too deep"); else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) { if (errno == ENOENT) error = 0; else if (errno == ENOTDIR) { /* asked to remove a/b/c/d/e and a/b is a normal file */ if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0) error = futils__rm_first_parent(path, data->base); else futils__error_cannot_rmdir( path->ptr, "parent is not directory"); } else error = git_path_set_error(errno, path->ptr, "rmdir"); } else if (S_ISDIR(st.st_mode)) { data->depth++; error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data); data->depth--; if (error < 0) return error; if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0) return error; if ((error = p_rmdir(path->ptr)) < 0) { if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY)) error = 0; else error = git_path_set_error(errno, path->ptr, "rmdir"); } } else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) { if (p_unlink(path->ptr) < 0) error = git_path_set_error(errno, path->ptr, "remove"); } else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) error = futils__error_cannot_rmdir(path->ptr, "still present"); return error; }
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; }
bool cl_is_chmod_supported(void) { static int _is_supported = -1; if (_is_supported < 0) { cl_git_mkfile("filemode.t", "Test if filemode can be modified"); _is_supported = cl_toggle_filemode("filemode.t"); cl_must_pass(p_unlink("filemode.t")); } return _is_supported; }
static bool are_symlinks_supported(const char *wd_path) { git_buf path = GIT_BUF_INIT; int fd; struct stat st; int symlinks_supported = -1; if ((fd = git_futils_mktmp(&path, wd_path, 0666)) < 0 || p_close(fd) < 0 || p_unlink(path.ptr) < 0 || p_symlink("testing", path.ptr) < 0 || p_lstat(path.ptr, &st) < 0) symlinks_supported = false; else symlinks_supported = (S_ISLNK(st.st_mode) != 0); (void)p_unlink(path.ptr); git_buf_free(&path); return symlinks_supported; }
void test_repo_init__detect_ignorecase(void) { struct stat st; bool found_without_match; cl_git_write2file("testCAPS", "whatever\n", 0, O_CREAT | O_WRONLY, 0666); found_without_match = (p_stat("Testcaps", &st) == 0); cl_must_pass(p_unlink("testCAPS")); assert_config_entry_on_init( "core.ignorecase", found_without_match ? true : GIT_ENOTFOUND); }
void test_checkout_conflict__ignored(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy |= GIT_CHECKOUT_SKIP_UNMERGED; create_conflicting_index(); cl_git_pass(p_unlink(TEST_REPO_PATH "/conflicting.txt")); cl_git_pass(git_checkout_index(g_repo, g_index, &opts)); cl_assert(!git_path_exists(TEST_REPO_PATH "/conflicting.txt")); }
void test_attr_ignore__skip_gitignore_directory(void) { cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder"); p_unlink("attr/.gitignore"); cl_assert(!git_path_exists("attr/.gitignore")); p_mkdir("attr/.gitignore", 0777); cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n"); assert_is_ignored(false, "File.txt"); assert_is_ignored(true, "NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); }
void test_status_worktree__issue_592_2(void) { git_repository *repo; git_buf path = GIT_BUF_INIT; repo = cl_git_sandbox_init("issue_592"); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(repo), "c/a.txt")); cl_git_pass(p_unlink(git_buf_cstr(&path))); cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt")); git_buf_free(&path); }
void test_checkout_index__wont_notify_of_expected_line_ending_changes(void) { cl_git_pass(p_unlink("./testrepo/.gitattributes")); set_core_autocrlf_to(true); cl_git_mkfile("./testrepo/new.txt", "my new file\r\n"); g_opts.checkout_strategy = GIT_CHECKOUT_CREATE_MISSING; g_opts.skipped_notify_cb = dont_notify_cb; g_opts.notify_payload = NULL; cl_git_pass(git_checkout_index(g_repo, &g_opts)); }
void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void) { #ifdef GIT_WIN32 const char *expected_readme_text = "hey there\r\n"; cl_git_pass(p_unlink("./testrepo/.gitattributes")); set_core_autocrlf_to(true); cl_git_pass(git_checkout_index(g_repo, &g_opts)); test_file_contents("./testrepo/README", expected_readme_text); #endif }
void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void) { cl_git_pass(p_unlink("stash/when")); assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); assert_status("how", GIT_STATUS_INDEX_MODIFIED); assert_status("who", GIT_STATUS_WT_MODIFIED); assert_status("when", GIT_ENOTFOUND); assert_status("just.ignore", GIT_STATUS_IGNORED); cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); assert_object_oid("stash^3^{tree}", EMPTY_TREE, GIT_OBJ_TREE); }
void test_merge_workdir_analysis__unborn(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; git_buf master = GIT_BUF_INIT; git_buf_joinpath(&master, git_repository_path(repo), "refs/heads/master"); p_unlink(git_buf_cstr(&master)); analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis); git_buf_dispose(&master); }
/* make sure git_filebuf_write writes large buffer correctly */ void test_core_filebuf__2(void) { git_filebuf file; char test[] = "test"; unsigned char buf[4096 * 4]; /* 2 * WRITE_BUFFER_SIZE */ memset(buf, 0xfe, sizeof(buf)); cl_git_pass(git_filebuf_open(&file, test, 0)); cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf))); cl_git_pass(git_filebuf_commit(&file)); cl_must_pass(p_unlink(test)); }
void test_index_tests__reload_from_disk(void) { git_repository *repo; git_index *read_index; git_index *write_index; cl_set_cleanup(&cleanup_myrepo, NULL); cl_git_pass(git_futils_mkdir("./myrepo", NULL, 0777, GIT_MKDIR_PATH)); cl_git_mkfile("./myrepo/a.txt", "a\n"); cl_git_mkfile("./myrepo/b.txt", "b\n"); cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); cl_git_pass(git_repository_index(&write_index, repo)); cl_assert_equal_i(false, write_index->on_disk); cl_git_pass(git_index_open(&read_index, write_index->index_file_path)); cl_assert_equal_i(false, read_index->on_disk); /* Stage two new files against the write_index */ cl_git_pass(git_index_add_bypath(write_index, "a.txt")); cl_git_pass(git_index_add_bypath(write_index, "b.txt")); cl_assert_equal_sz(2, git_index_entrycount(write_index)); /* Persist the index changes to disk */ cl_git_pass(git_index_write(write_index)); cl_assert_equal_i(true, write_index->on_disk); /* Sync the changes back into the read_index */ cl_assert_equal_sz(0, git_index_entrycount(read_index)); cl_git_pass(git_index_read(read_index, true)); cl_assert_equal_i(true, read_index->on_disk); cl_assert_equal_sz(2, git_index_entrycount(read_index)); /* Remove the index file from the filesystem */ cl_git_pass(p_unlink(write_index->index_file_path)); /* Sync the changes back into the read_index */ cl_git_pass(git_index_read(read_index, true)); cl_assert_equal_i(false, read_index->on_disk); cl_assert_equal_sz(0, git_index_entrycount(read_index)); git_index_free(read_index); git_index_free(write_index); git_repository_free(repo); }
/* make sure GIT_FILEBUF_APPEND works as expected */ void test_core_filebuf__1(void) { git_filebuf file = GIT_FILEBUF_INIT; char test[] = "test"; cl_git_mkfile(test, "libgit2 rocks\n"); cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND, 0666)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_git_pass(git_filebuf_commit(&file)); cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test); cl_must_pass(p_unlink(test)); }
/* make sure non-empty dir cannot be deleted recusively */ void test_core_rmdir__fail_to_delete_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_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); cl_must_pass(p_unlink(file.ptr)); cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); git_buf_dispose(&file); }