git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 20, 2026, 17:34 UTC
Message-ID
<xmqqecfssnk0.fsf@gitster.g>
In-Reply-To
<pull.2192.v4.git.1787221888406.gitgitgadget@gmail.com>
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 58 quoted lines
>       @@ 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;

Ah, that is a much smaller patch, and does not share the same issue as the very initial iteration.

Will queue.  Thanks.
Show 23 quoted lines
> 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
Previous: Yoichi NAKAYAMA via GitGitGadget
Message 10 of 10 in “worktree add: shouldn't dwim if -b or -B is given”
  1. worktree add: shouldn't dwim if -b or -B is givenYoichi NAKAYAMA via GitGitGadget, Aug 4, 2026
  2. Junio C HamanoAug 4, 2026
  3. Yoichi NakayamaAug 5, 2026
  4. worktree add: shouldn't dwim if -b or -B is givenYoichi NAKAYAMA via GitGitGadget, Aug 5, 2026
  5. Yoichi NakayamaAug 5, 2026
  6. Yoichi NakayamaAug 19, 2026
  7. worktree add: shouldn't dwim if -b or -B is givenYoichi NAKAYAMA via GitGitGadget, Aug 5, 2026
  8. Junio C HamanoAug 5, 2026
  9. worktree add: shouldn't dwim if -b or -B is givenYoichi NAKAYAMA via GitGitGadget, Aug 20, 2026
  10. Junio C HamanoAug 20, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.