Merge branch 'tg/worktree-add-existing-branch'
authorJunio C Hamano <gitster@pobox.com>
Wed, 23 May 2018 05:38:18 +0000 (14:38 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 23 May 2018 05:38:18 +0000 (14:38 +0900)
"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

1  2 
Documentation/git-worktree.txt
builtin/worktree.c
t/t2025-worktree-add.sh
Simple merge
index 30647b30c5337e23c70720558a5ab96e9a8d68fd,d3aeb4877dbd1f0398d7a3ace61785d9e890fd09..5c7d2bb1807f942139b3ec41b426320e4b0cfc2a
@@@ -366,18 -353,75 +362,75 @@@ done
        return ret;
  }
  
 -                                find_unique_abbrev(commit->object.oid.hash,
 -                                                   DEFAULT_ABBREV));
+ 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, 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 <branch> even if already checked out in other worktree")),
 +              OPT__FORCE(&opts.force,
 +                         N_("checkout <branch> 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")),
Simple merge