Skip to content

worktree: teach "add" to check out existing branches

Thomas Gummerer git 10 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v9 Latest4 patches 3 repliesIn review
  2. v84 patches 3 repliesSuperseded
  3. v74 patches 5 repliesSuperseded
Earlier versions · 7
VersionPostedPatchesRepliesStatus
v6 posting 2/21 patch3 repliesSuperseded
v6 posting 1/26 patches18 repliesSuperseded
v56 patches9 repliesSuperseded
v44 patches10 repliesSuperseded
v34 patches1 replySuperseded
v23 patches12 repliesSuperseded
v11 patch3 repliesSuperseded

Patches in v9 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4worktree: remove extra members from struct add_optsbuiltin/worktree.c+16 -170 replies
2/4worktree: improve message when creating a new worktreebuiltin/worktree.c+36 -20 replies
3/4worktree: factor out dwim_branch functionbuiltin/worktree.c+18 -110 replies
4/4worktree: teach "add" to check out existing branchesDocumentation/git-worktree.txt builtin/worktree.c t/t2025-worktree-add.sh+37 -110 replies

Reviews trailers per patch

None of the 4 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchEric Sunshine
SeriesR-b
1/4
2/4Helped-by
3/4
4/4Helped-by

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v9 thread

Cover letter [PATCH v9 0/4] worktree: teach "add" to check out existing branches

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

Thanks Eric for the review and the suggestions on the previous round.

Changes since the previous round:

- UNLEAK new_branch after it was xstrndup'd
- update the commit message of 2/4 according to Eric's suggestions
- make force_new_branch a boolean flag in print_preparing_worktree_line, instead of using it as the branch name. Instead use new_branch as the new branch name everywhere in that function.
- get rid of the ckeckout_existing_branch flag

Interdiff below:

builtin/worktree.c +9 -17
index d52495f312..d3aeb4877d 100644
@@ -356,18 +356,15 @@ static int add_worktree(const char *path, const char *refname,
 static void print_preparing_worktree_line(int detach,
 					  const char *branch,
 					  const char *new_branch,
-					  const char *new_branch_force,
-					  int checkout_existing_branch)
+					  int force_new_branch)
 {
-	if (checkout_existing_branch) {
-		printf_ln(_("Preparing worktree (checking out '%s')"), branch);
-	} else if (new_branch_force) {
-		struct commit *commit = lookup_commit_reference_by_name(new_branch_force);
+	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_force);
+			printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
 		else
 			printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
-				  new_branch_force,
+				  new_branch,
 				  find_unique_abbrev(commit->object.oid.hash,
 						     DEFAULT_ABBREV));
 	} else if (new_branch) {
@@ -390,19 +387,17 @@ static void print_preparing_worktree_line(int detach,
 	}
 }
 
-static const char *dwim_branch(const char *path, const char **new_branch,
-			       int *checkout_existing_branch)
+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)) {
-		*checkout_existing_branch = 1;
 		strbuf_release(&ref);
-		UNLEAK(branchname);
 		return branchname;
 	}
 
@@ -421,7 +416,6 @@ static int add(int ac, const char **av, const char *prefix)
 	struct add_opts opts;
 	const char *new_branch_force = NULL;
 	char *path;
-	int checkout_existing_branch = 0;
 	const char *branch;
 	const char *new_branch = NULL;
 	const char *opt_track = NULL;
@@ -469,8 +463,7 @@ static int add(int ac, const char **av, const char *prefix)
 	}
 
 	if (ac < 2 && !new_branch && !opts.detach) {
-		const char *s = dwim_branch(path, &new_branch,
-					    &checkout_existing_branch);
+		const char *s = dwim_branch(path, &new_branch);
 		if (s)
 			branch = s;
 	}
@@ -490,8 +483,7 @@ static int add(int ac, const char **av, const char *prefix)
 		}
 	}
 
-	print_preparing_worktree_line(opts.detach, branch, new_branch, new_branch_force,
-				      checkout_existing_branch);
+	print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
 
 	if (new_branch) {
 		struct child_process cp = CHILD_PROCESS_INIT;

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             | 103 ++++++++++++++++++++++++++++++-----------
 t/t2025-worktree-add.sh        |  26 ++++++++---
 3 files changed, 102 insertions(+), 36 deletions(-)
Signature · 1 line
2.16.1.74.g7afd1c25cc.dirty