Subject: [PATCH v8 0/4] worktree: teach "add" to check out existing branches

From: Thomas Gummerer <[email-masked]>
Date: Mon, 23 Apr 2018 20:38:44 +0100

Thanks Eric and Junio for the review the suggestions in the last
round.

Previous rounds are at <[email-masked]>,
<[email-masked]>,
<[email-masked]>,
<[email-masked]>,
<[email-masked]>,
<[email-masked]> and
<[email-masked]>.

This round updates the output for "resetting branch ..." to not have
braces embedded inside of another pair of braces, and is not correctly
printing "checking out '<branch>'" when 'git worktree add <path>
<local-branch>' is used.  Both these changes are in patch 2/4, the
other patches are the same as in the previous round.

Interdiff below:

diff --git a/builtin/worktree.c b/builtin/worktree.c
index f5a5283b39..d52495f312 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -353,7 +353,8 @@ static int add_worktree(const char *path, const char *refname,
 	return ret;
 }
 
-static void print_preparing_worktree_line(const char *branch,
+static void print_preparing_worktree_line(int detach,
+					  const char *branch,
 					  const char *new_branch,
 					  const char *new_branch_force,
 					  int checkout_existing_branch)
@@ -365,19 +366,27 @@ static void print_preparing_worktree_line(const char *branch,
 		if (!commit)
 			printf_ln(_("Preparing worktree (new branch '%s')"), new_branch_force);
 		else
-			printf_ln(_("Preparing worktree (resetting branch '%s' (was at %s))"),
+			printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
 				  new_branch_force,
 				  find_unique_abbrev(commit->object.oid.hash,
 						     DEFAULT_ABBREV));
 	} else if (new_branch) {
 		printf_ln(_("Preparing worktree (new branch '%s')"), new_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));
+		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));
+		}
+		strbuf_release(&s);
 	}
 }
 
@@ -481,7 +490,7 @@ static int add(int ac, const char **av, const char *prefix)
 		}
 	}
 
-	print_preparing_worktree_line(branch, new_branch, new_branch_force,
+	print_preparing_worktree_line(opts.detach, branch, new_branch, new_branch_force,
 				      checkout_existing_branch);
 
 	if (new_branch) {

Thomas Gummerer (4):
  worktree: remove extra members from struct add_opts
  worktree: improve message when creating a new worktree
  worktree: factor out dwim_branch function
  worktree: teach "add" to check out existing branches

 Documentation/git-worktree.txt |   9 +++-
 builtin/worktree.c             | 111 +++++++++++++++++++++++++++++++----------
 t/t2025-worktree-add.sh        |  26 +++++++---
 3 files changed, 110 insertions(+), 36 deletions(-)

-- 
2.16.1.74.g7afd1c25cc.dirty