[PATCH v4] worktree add: shouldn't dwim if -b or -B is given

"Yoichi NAKAYAMA via GitGitGadget" <[email protected]>
Newsgroups org.kernel.vger.git
Message-ID <[email protected]>
From: Yoichi NAKAYAMA <[email protected]>

'git worktree add <path> <branch>' DWIMs <branch> to a
remote-tracking branch when neither -b, -B, nor --detach
is given.

However, 'git worktree add -b <new-branch> <path> <branch>' can
still DWIM <branch>, causing <new-branch> to be ignored.  This is a
regression introduced by 128e5496b3 (worktree add: extend DWIM to
infer --orphan, 2023-05-17), which appeared in Git 2.42.

Signed-off-by: Yoichi NAKAYAMA <[email protected]>
---
    worktree add: shouldn't dwim if -b or -B is given

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2192%2Fyoichi%2Fworktree-add-should-not-dwim-with-b-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2192/yoichi/worktree-add-should-not-dwim-with-b-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/2192

Range-diff vs v3:

 1:  477ea27c8b ! 1:  512b037bb7 worktree add: shouldn't dwim if -b or -B is given
     @@ Commit message
          is given.
      
          However, 'git worktree add -b <new-branch> <path> <branch>' can
     -    still DWIM <branch>, causing <new-branch> to be ignored.
     -
     -    This is a regression introduced in v2.42.0
     -    (128e5496b325640f0a09cc1d5b1e346c069b410f).
     +    still DWIM <branch>, causing <new-branch> to be ignored.  This is a
     +    regression introduced by 128e5496b3 (worktree add: extend DWIM to
     +    infer --orphan, 2023-05-17), which appeared in Git 2.42.
      
          Signed-off-by: Yoichi NAKAYAMA <[email protected]>
      
       ## builtin/worktree.c ##
      @@ builtin/worktree.c: static int add(int ac, const char **av, const char *prefix,
     + 
       		/* DWIM: Infer --orphan when repo has no refs. */
       		opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
     ++	} else if (ac == 2 && new_branch) {
     ++		if (!strcmp(branch, "HEAD"))
     ++			can_use_local_refs(&opts);
       	} else if (ac == 2) {
     --		struct object_id oid;
     --		struct commit *commit;
     --		char *remote;
     --
     --		commit = lookup_commit_reference_by_name(branch);
     --		if (!commit) {
     --			remote = unique_tracking_name(branch, &oid, NULL);
     --			if (remote) {
     --				new_branch = branch;
     --				branch = new_branch_to_free = remote;
     -+		if (!new_branch) {
     -+			struct object_id oid;
     -+			struct commit *commit;
     -+			char *remote;
     -+
     -+			commit = lookup_commit_reference_by_name(branch);
     -+			if (!commit) {
     -+				remote = unique_tracking_name(branch, &oid, NULL);
     -+				if (remote) {
     -+					new_branch = branch;
     -+					branch = new_branch_to_free = remote;
     -+				}
     - 			}
     - 		}
     - 
     + 		struct object_id oid;
     + 		struct commit *commit;
      
       ## t/t2400-worktree-add.sh ##
      @@ t/t2400-worktree-add.sh: test_expect_success '"add" <path> <branch> dwims' '


 builtin/worktree.c      |  3 +++
 t/t2400-worktree-add.sh | 10 ++++++++++
 2 files changed, 13 insertions(+)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 654d27c3e1..15a1c9624a 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -897,6 +897,9 @@ static int add(int ac, const char **av, const char *prefix,
 
 		/* DWIM: Infer --orphan when repo has no refs. */
 		opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
+	} else if (ac == 2 && new_branch) {
+		if (!strcmp(branch, "HEAD"))
+			can_use_local_refs(&opts);
 	} else if (ac == 2) {
 		struct object_id oid;
 		struct commit *commit;
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index 87b926728a..ba3bec078f 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -621,6 +621,16 @@ test_expect_success '"add" <path> <branch> dwims' '
 	)
 '
 
+test_expect_success '"add" <path> <branch> does not dwim with -b' '
+	test_when_finished rm -rf repo_upstream repo_dwim wt &&
+	setup_remote_repo repo_upstream repo_dwim &&
+	(
+		cd repo_dwim &&
+		test_must_fail git worktree add -b branch ../wt foo 2>actual &&
+		test_grep "^fatal: invalid reference: foo" actual
+	)
+'
+
 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
 	test_when_finished rm -rf repo_upstream repo_dwim foo &&
 	setup_remote_repo repo_upstream repo_dwim &&

base-commit: dea0ea3582e6980ddbc1173cc8e3e9f9db91cde0
-- 
gitgitgadget
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.