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)); }
static int write_merge_msg( git_repository *repo, const char *commit_oidstr, const char *commit_msgline) { git_filebuf file = GIT_FILEBUF_INIT; git_buf file_path = GIT_BUF_INIT; int error = 0; if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 || (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 || (error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n", commit_msgline, commit_oidstr)) < 0) goto cleanup; error = git_filebuf_commit(&file); cleanup: if (error < 0) git_filebuf_cleanup(&file); git_buf_free(&file_path); return error; }
static int fetchhead_ref_write( git_filebuf *file, git_fetchhead_ref *fetchhead_ref) { char oid[GIT_OID_HEXSZ + 1]; const char *type, *name; assert(file && fetchhead_ref); git_oid_fmt(oid, &fetchhead_ref->oid); oid[GIT_OID_HEXSZ] = '\0'; if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) { type = "branch "; name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR); } else if(git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_TAGS_DIR) == 0) { type = "tag "; name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR); } else { type = ""; name = fetchhead_ref->ref_name; } return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n", oid, (fetchhead_ref->is_merge) ? "" : "not-for-merge", type, name, fetchhead_ref->remote_url); }
/* 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_core_filebuf__hidden_file(void) { #ifndef GIT_WIN32 cl_skip(); #else git_filebuf file = GIT_FILEBUF_INIT; char *dir = "hidden", *test = "hidden/test"; bool hidden; cl_git_pass(p_mkdir(dir, 0666)); cl_git_mkfile(test, "dummy content"); cl_git_pass(git_win32__set_hidden(test, true)); cl_git_pass(git_win32__hidden(&hidden, test)); cl_assert(hidden); cl_git_pass(git_filebuf_open(&file, test, 0, 0666)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_git_pass(git_filebuf_commit(&file)); git_filebuf_cleanup(&file); #endif }
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)); }
static int repo_init_create_head(const char *git_dir, const char *ref_name) { git_buf ref_path = GIT_BUF_INIT; git_filebuf ref = GIT_FILEBUF_INIT; const char *fmt; if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 || git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0) goto fail; if (!ref_name) ref_name = GIT_BRANCH_MASTER; if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0) fmt = "ref: %s\n"; else fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n"; if (git_filebuf_printf(&ref, fmt, ref_name) < 0 || git_filebuf_commit(&ref) < 0) goto fail; git_buf_free(&ref_path); return 0; fail: git_buf_free(&ref_path); git_filebuf_cleanup(&ref); return -1; }
static int repo_init_createhead(const char *git_dir) { git_buf ref_path = GIT_BUF_INIT; git_filebuf ref = GIT_FILEBUF_INIT; if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 || git_filebuf_open(&ref, ref_path.ptr, 0) < 0 || git_filebuf_printf(&ref, "ref: refs/heads/master\n") < 0 || git_filebuf_commit(&ref, GIT_REFS_FILE_MODE) < 0) return -1; git_buf_free(&ref_path); return 0; }
/* 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 git_filebuf_commit clears the buffer */ void test_core_filebuf__5(void) { git_filebuf file = GIT_FILEBUF_INIT; char test[] = "test"; 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_unlink(test)); }
/* make sure GIT_FILEBUF_APPEND works as expected */ void test_core_filebuf__1(void) { git_filebuf file; int fd; char test[] = "test"; fd = p_creat(test, 0644); cl_must_pass(fd); cl_must_pass(p_write(fd, "libgit2 rocks\n", 14)); cl_must_pass(p_close(fd)); cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); cl_git_pass(git_filebuf_commit(&file)); cl_must_pass(p_unlink(test)); }
static void init_linked_repo(const char *path, const char *alternate) { git_buf_clear(&destpath); git_buf_clear(&filepath); cl_git_pass(git_repository_init(&repo, path, 1)); cl_git_pass(git_path_prettify(&destpath, alternate, NULL)); cl_git_pass(git_buf_joinpath(&destpath, destpath.ptr, "objects")); cl_git_pass(git_buf_joinpath(&filepath, git_repository_path(repo), "objects/info")); cl_git_pass(git_futils_mkdir(filepath.ptr, NULL, 0755, GIT_MKDIR_PATH)); cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates")); cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0)); git_filebuf_printf(&file, "%s\n", git_buf_cstr(&destpath)); cl_git_pass(git_filebuf_commit(&file, 0644)); git_repository_free(repo); }
static int write_cherry_pick_head( git_repository *repo, const char *commit_oidstr) { git_filebuf file = GIT_FILEBUF_INIT; git_buf file_path = GIT_BUF_INIT; int error = 0; if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_CHERRY_PICK_HEAD_FILE)) >= 0 && (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRY_PICK_FILE_MODE)) >= 0 && (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0) error = git_filebuf_commit(&file); if (error < 0) git_filebuf_cleanup(&file); git_buf_free(&file_path); return error; }
static int write_merge_msg( git_repository *repo, const char *commit_msg) { git_filebuf file = GIT_FILEBUF_INIT; git_buf file_path = GIT_BUF_INIT; int error = 0; if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 || (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRY_PICK_FILE_MODE)) < 0 || (error = git_filebuf_printf(&file, "%s", commit_msg)) < 0) goto cleanup; error = git_filebuf_commit(&file); cleanup: if (error < 0) git_filebuf_cleanup(&file); git_buf_free(&file_path); return error; }