Please ignore v3 of this series, I just noticed that I botched the last commit :( sorry about the noise. Previous rounds (other than v3) are at <20180121120208.12760-1-t.gummerer@xxxxxxxxx> and <20180204221305.28300-1-t.gummerer@xxxxxxxxx>. Thanks Duy, Eric and Junio for the comments on the previous round. This round improves the end user facing messages, and factors out a dwim_branch function as suggested by Duy. Interdiff between this and v2 below: diff --git a/builtin/worktree.c b/builtin/worktree.c index ea420bb90b..df5c0427ba 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -304,8 +304,6 @@ static int add_worktree(const char *path, const char *refname, strbuf_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); - fprintf(stderr, _("Preparing %s (identifier %s)"), path, name); - argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); cp.git_cmd = 1; @@ -322,12 +320,12 @@ static int add_worktree(const char *path, const char *refname, goto done; if (opts->checkout_existing_branch) - fprintf(stderr, _(", checking out existing branch '%s'"), + fprintf(stderr, _("checking out branch '%s'"), refname); else if (opts->new_branch) - fprintf(stderr, _(", creating new branch '%s'"), opts->new_branch); + fprintf(stderr, _("creating branch '%s'"), opts->new_branch); - fprintf(stderr, _(", setting HEAD to %s"), + fprintf(stderr, _("worktree HEAD is now at %s"), find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV)); strbuf_reset(&sb); @@ -373,6 +371,31 @@ static int add_worktree(const char *path, const char *refname, return ret; } +static const char *dwim_branch(const char *path, struct add_opts *opts) +{ + int n; + const char *s = worktree_basename(path, &n); + const char *branchname = xstrndup(s, n); + struct strbuf ref = STRBUF_INIT; + + if (!strbuf_check_branch_ref(&ref, branchname) && + ref_exists(ref.buf)) { + opts->checkout_existing_branch = 1; + strbuf_release(&ref); + UNLEAK(branchname); + return branchname; + } + + opts->new_branch = branchname; + if (guess_remote) { + struct object_id oid; + const char *remote = + unique_tracking_name(opts->new_branch, &oid); + return remote; + } + return NULL; +} + static int add(int ac, const char **av, const char *prefix) { struct add_opts opts; @@ -425,27 +448,9 @@ static int add(int ac, const char **av, const char *prefix) } if (ac < 2 && !opts.new_branch && !opts.detach) { - int n; - const char *s = worktree_basename(path, &n); - const char *branchname = xstrndup(s, n); - struct strbuf ref = STRBUF_INIT; - - if (!strbuf_check_branch_ref(&ref, branchname) && - ref_exists(ref.buf)) { - branch = branchname; - opts.checkout_existing_branch = 1; - UNLEAK(branch); - } else { - opts.new_branch = branchname; - if (guess_remote) { - struct object_id oid; - const char *remote = - unique_tracking_name(opts.new_branch, &oid); - if (remote) - branch = remote; - } - } - strbuf_release(&ref); + const char *dwim_branchname = dwim_branch(path, &opts); + if (dwim_branchname) + branch = dwim_branchname; } if (ac == 2 && !opts.new_branch && !opts.detach) { Thomas Gummerer (4): worktree: improve message when creating a new worktree worktree: be clearer when "add" dwim-ery kicks in worktree: factor out dwim_branch function worktree: teach "add" to check out existing branches Documentation/git-worktree.txt | 9 ++++-- builtin/worktree.c | 58 ++++++++++++++++++++++++++-------- t/t2025-worktree-add.sh | 15 +++++++-- 3 files changed, 64 insertions(+), 18 deletions(-) -- 2.17.0.rc0.231.g781580f06