worktree: teach "add" to check out existing branches
[gitweb.git] / builtin / worktree.c
index 6bd32b6090967b03072d9a08ed6aaa42f89c447f..d3aeb4877dbd1f0398d7a3ace61785d9e890fd09 100644 (file)
@@ -391,8 +391,17 @@ static const char *dwim_branch(const char *path, const char **new_branch)
 {
        int n;
        const char *s = worktree_basename(path, &n);
-       *new_branch = xstrndup(s, n);
-       UNLEAK(*new_branch);
+       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 =