From: Junio C Hamano Date: Wed, 23 May 2018 05:38:18 +0000 (+0900) Subject: Merge branch 'tg/worktree-add-existing-branch' X-Git-Tag: v2.18.0-rc0~53 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/10174da9f1643eb939d93d479547494baf77377b Merge branch 'tg/worktree-add-existing-branch' "git worktree add" learned to check out an existing branch. * tg/worktree-add-existing-branch: worktree: teach "add" to check out existing branches worktree: factor out dwim_branch function worktree: improve message when creating a new worktree worktree: remove extra members from struct add_opts --- 10174da9f1643eb939d93d479547494baf77377b diff --cc builtin/worktree.c index 30647b30c5,d3aeb4877d..5c7d2bb180 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@@ -366,18 -353,75 +362,75 @@@ done return ret; } + static void print_preparing_worktree_line(int detach, + const char *branch, + const char *new_branch, + int force_new_branch) + { + if (force_new_branch) { + struct commit *commit = lookup_commit_reference_by_name(new_branch); + if (!commit) + printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); + else + printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"), + new_branch, - find_unique_abbrev(commit->object.oid.hash, - DEFAULT_ABBREV)); ++ find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); + } else if (new_branch) { + printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); + } else { + struct strbuf s = STRBUF_INIT; + if (!detach && !strbuf_check_branch_ref(&s, branch) && + ref_exists(s.buf)) + printf_ln(_("Preparing worktree (checking out '%s')"), + branch); + else { + struct commit *commit = lookup_commit_reference_by_name(branch); + if (!commit) + die(_("invalid reference: %s"), branch); + printf_ln(_("Preparing worktree (detached HEAD %s)"), - find_unique_abbrev(commit->object.oid.hash, - DEFAULT_ABBREV)); ++ find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); + } + strbuf_release(&s); + } + } + + static const char *dwim_branch(const char *path, const char **new_branch) + { + int n; + const char *s = worktree_basename(path, &n); + const char *branchname = xstrndup(s, n); + struct strbuf ref = STRBUF_INIT; + + UNLEAK(branchname); + if (!strbuf_check_branch_ref(&ref, branchname) && + ref_exists(ref.buf)) { + strbuf_release(&ref); + return branchname; + } + + *new_branch = branchname; + if (guess_remote) { + struct object_id oid; + const char *remote = + unique_tracking_name(*new_branch, &oid); + return remote; + } + return NULL; + } + static int add(int ac, const char **av, const char *prefix) { struct add_opts opts; const char *new_branch_force = NULL; char *path; const char *branch; + const char *new_branch = NULL; const char *opt_track = NULL; struct option options[] = { - OPT__FORCE(&opts.force, N_("checkout even if already checked out in other worktree")), + OPT__FORCE(&opts.force, + N_("checkout even if already checked out in other worktree"), + PARSE_OPT_NOCOMPLETE), - OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), + OPT_STRING('b', NULL, &new_branch, N_("branch"), N_("create a new branch")), OPT_STRING('B', NULL, &new_branch_force, N_("branch"), N_("create or reset a branch")),