vendor/libgit2/src/worktree.c in rugged-0.27.10.1 vs vendor/libgit2/src/worktree.c in rugged-0.28.0

- old
+ new

@@ -23,11 +23,11 @@ error = git_path_contains_file(&buf, "commondir") && git_path_contains_file(&buf, "gitdir") && git_path_contains_file(&buf, "HEAD"); - git_buf_free(&buf); + git_buf_dispose(&buf); return error; } int git_worktree_list(git_strarray *wts, git_repository *repo) { @@ -62,11 +62,11 @@ } wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees); exit: - git_buf_free(&path); + git_buf_dispose(&path); return error; } char *git_worktree__read_link(const char *base, const char *file) @@ -77,28 +77,28 @@ if (git_buf_joinpath(&path, base, file) < 0) goto err; if (git_futils_readbuffer(&buf, path.ptr) < 0) goto err; - git_buf_free(&path); + git_buf_dispose(&path); git_buf_rtrim(&buf); if (!git_path_is_relative(buf.ptr)) return git_buf_detach(&buf); if (git_buf_sets(&path, base) < 0) goto err; if (git_path_apply_relative(&path, buf.ptr) < 0) goto err; - git_buf_free(&buf); + git_buf_dispose(&buf); return git_buf_detach(&path); err: - git_buf_free(&buf); - git_buf_free(&path); + git_buf_dispose(&buf); + git_buf_dispose(&path); return NULL; } static int write_wtfile(const char *base, const char *file, const git_buf *buf) @@ -113,11 +113,11 @@ if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) goto out; out: - git_buf_free(&path); + git_buf_dispose(&path); return err; } static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name) @@ -137,11 +137,12 @@ } if ((wt->name = git__strdup(name)) == NULL || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL - || (parent && (wt->parent_path = git__strdup(parent)) == NULL)) { + || (parent && (wt->parent_path = git__strdup(parent)) == NULL) + || (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) { error = -1; goto out; } if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0) @@ -153,11 +154,11 @@ *out = wt; out: if (error) git_worktree_free(wt); - git_buf_free(&gitdir); + git_buf_dispose(&gitdir); return error; } int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name) @@ -175,11 +176,11 @@ if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0) goto out; out: - git_buf_free(&path); + git_buf_dispose(&path); if (error) git_worktree_free(wt); return error; @@ -191,11 +192,11 @@ const char *gitdir, *commondir; char *name = NULL; int error = 0; if (!git_repository_is_worktree(repo)) { - giterr_set(GITERR_WORKTREE, "cannot open worktree of a non-worktree repo"); + git_error_set(GIT_ERROR_WORKTREE, "cannot open worktree of a non-worktree repo"); error = -1; goto out; } gitdir = git_repository_path(repo); @@ -210,64 +211,55 @@ if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0) goto out; out: git__free(name); - git_buf_free(&parent); + git_buf_dispose(&parent); return error; } void git_worktree_free(git_worktree *wt) { if (!wt) return; git__free(wt->commondir_path); + git__free(wt->worktree_path); git__free(wt->gitlink_path); git__free(wt->gitdir_path); git__free(wt->parent_path); git__free(wt->name); git__free(wt); } int git_worktree_validate(const git_worktree *wt) { - git_buf buf = GIT_BUF_INIT; - int err = 0; - assert(wt); - git_buf_puts(&buf, wt->gitdir_path); - if (!is_worktree_dir(buf.ptr)) { - giterr_set(GITERR_WORKTREE, + if (!is_worktree_dir(wt->gitdir_path)) { + git_error_set(GIT_ERROR_WORKTREE, "Worktree gitdir ('%s') is not valid", wt->gitlink_path); - err = -1; - goto out; + return GIT_ERROR; } - if (!git_path_exists(wt->parent_path)) { - giterr_set(GITERR_WORKTREE, + if (wt->parent_path && !git_path_exists(wt->parent_path)) { + git_error_set(GIT_ERROR_WORKTREE, "Worktree parent directory ('%s') does not exist ", wt->parent_path); - err = -2; - goto out; + return GIT_ERROR; } if (!git_path_exists(wt->commondir_path)) { - giterr_set(GITERR_WORKTREE, + git_error_set(GIT_ERROR_WORKTREE, "Worktree common directory ('%s') does not exist ", wt->commondir_path); - err = -3; - goto out; + return GIT_ERROR; } -out: - git_buf_free(&buf); - - return err; + return 0; } int git_worktree_add_init_options(git_worktree_add_options *opts, unsigned int version) { @@ -286,11 +278,11 @@ git_repository *wt = NULL; git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT; git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT; int err; - GITERR_CHECK_VERSION( + GIT_ERROR_CHECK_VERSION( opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options"); if (opts) memcpy(&wtopts, opts, sizeof(wtopts)); @@ -346,18 +338,35 @@ if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0 || (err = git_buf_putc(&buf, '\n')) < 0 || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0) goto out; - /* Create new branch */ - if ((err = git_repository_head(&head, repo)) < 0) - goto out; - if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0) - goto out; - if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0) - goto out; + /* Set up worktree reference */ + if (wtopts.ref) { + if (!git_reference_is_branch(wtopts.ref)) { + git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch"); + err = -1; + goto out; + } + if (git_branch_is_checked_out(wtopts.ref)) { + git_error_set(GIT_ERROR_WORKTREE, "reference is already checked out"); + err = -1; + goto out; + } + + if ((err = git_reference_dup(&ref, wtopts.ref)) < 0) + goto out; + } else { + if ((err = git_repository_head(&head, repo)) < 0) + goto out; + if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0) + goto out; + if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0) + goto out; + } + /* Set worktree's HEAD */ if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0) goto out; if ((err = git_repository_open(&wt, wddir.ptr)) < 0) goto out; @@ -370,13 +379,13 @@ /* Load result */ if ((err = git_worktree_lookup(out, repo, name)) < 0) goto out; out: - git_buf_free(&gitdir); - git_buf_free(&wddir); - git_buf_free(&buf); + git_buf_dispose(&gitdir); + git_buf_dispose(&wddir); + git_buf_dispose(&buf); git_reference_free(ref); git_reference_free(head); git_commit_free(commit); git_repository_free(wt); @@ -403,11 +412,11 @@ goto out; wt->locked = 1; out: - git_buf_free(&path); + git_buf_dispose(&path); return err; } int git_worktree_unlock(git_worktree *wt) @@ -421,17 +430,17 @@ if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0) return -1; if (p_unlink(path.ptr) != 0) { - git_buf_free(&path); + git_buf_dispose(&path); return -1; } wt->locked = 0; - git_buf_free(&path); + git_buf_dispose(&path); return 0; } int git_worktree_is_locked(git_buf *reason, const git_worktree *wt) @@ -448,15 +457,27 @@ goto out; if ((ret = git_path_exists(path.ptr)) && reason) git_futils_readbuffer(reason, path.ptr); out: - git_buf_free(&path); + git_buf_dispose(&path); return ret; } +const char *git_worktree_name(const git_worktree *wt) +{ + assert(wt); + return wt->name; +} + +const char *git_worktree_path(const git_worktree *wt) +{ + assert(wt); + return wt->worktree_path; +} + int git_worktree_prune_init_options( git_worktree_prune_options *opts, unsigned int version) { GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version, @@ -468,11 +489,11 @@ git_worktree_prune_options *opts) { git_buf reason = GIT_BUF_INIT; git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; - GITERR_CHECK_VERSION( + GIT_ERROR_CHECK_VERSION( opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, "git_worktree_prune_options"); if (opts) memcpy(&popts, opts, sizeof(popts)); @@ -480,20 +501,20 @@ if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 && git_worktree_is_locked(&reason, wt)) { if (!reason.size) git_buf_attach_notowned(&reason, "no reason given", 15); - giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr); - git_buf_free(&reason); + git_error_set(GIT_ERROR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr); + git_buf_dispose(&reason); return 0; } if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 && git_worktree_validate(wt) == 0) { - giterr_set(GITERR_WORKTREE, "Not pruning valid working tree"); + git_error_set(GIT_ERROR_WORKTREE, "Not pruning valid working tree"); return 0; } return 1; } @@ -504,11 +525,11 @@ git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_buf path = GIT_BUF_INIT; char *wtpath; int err; - GITERR_CHECK_VERSION( + GIT_ERROR_CHECK_VERSION( opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, "git_worktree_prune_options"); if (opts) memcpy(&popts, opts, sizeof(popts)); @@ -521,11 +542,11 @@ /* Delete gitdir in parent repository */ if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0) goto out; if (!git_path_exists(path.ptr)) { - giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr); + git_error_set(GIT_ERROR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr); err = -1; goto out; } if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) goto out; @@ -541,17 +562,17 @@ if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL) goto out; git_buf_attach(&path, wtpath, 0); if (!git_path_exists(path.ptr)) { - giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr); + git_error_set(GIT_ERROR_WORKTREE, "Working tree '%s' does not exist", path.ptr); err = -1; goto out; } if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) goto out; out: - git_buf_free(&path); + git_buf_dispose(&path); return err; }