Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Yoichi Nakayama <yoichi.nakayama@gmail.com>
Cc: Yoichi NAKAYAMA via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org,  Harald Nordgren <haraldnordgren@gmail.com>,
	 "D. Ben Knoble" <ben.knoble@gmail.com>
Subject: Re: [PATCH v6 3/3] worktree add: improve message for ambiguous remote branch name
Date: Sat, 22 Aug 2026 10:22:06 -0700	[thread overview]
Message-ID: <xmqqld9yvznl.fsf@gitster.g> (raw)
In-Reply-To: <CAF5D8-trxeMd8HYzy6kD4myf+bynkxOvxKDQrMdmqnvcdpdkEA@mail.gmail.com> (Yoichi Nakayama's message of "Sat, 22 Aug 2026 09:50:48 +0900")

Yoichi Nakayama <yoichi.nakayama@gmail.com> writes:

> No. The exit codes of the command 'git worktree add ../topic-branch'
> are the same (== 0). but the results are different.
>
> If there is a unique match found in dwim_branch(), it creates a local
> branch named topic-branch which tracks <remote>/topic-branch.
> In case of no match or multiple matches, it creates a local branch
> named topic-branch from HEAD.
>
> Since Git treats both cases as successful, either can be considered
> the intended behavior.
> (Although, if there are multiple matches, there is a fair chance the
> result might not be what was intended.)
>
> I am confident that it is appropriate to provide a hint when a command
> fails, but it is difficult to decide what to do when a command succeeds.

I actually think it falls into the same class of bug you are fixing
in this topic, which was caused by not considering the possibility
that there can be any case other than 0-match and 1-match, and not
thinking through the ramifications of treating 2-match and 0-match
the same way.

It is of course OK to fix one bug and leave the other one
unaddressed, to be fixed in a later follow-up effort.

The rest of this message is only for those who will tackle the
"later follow-up effort" part after the dust settles once the
current topic lands (aka #leftoverbits).

In the beginning, before Thomas Gummerer started his topic in
November 2017 [*1*], 'git worktree add <path> [<branch>]' created a
new branch from the checked-out HEAD, without looking at any
remote.

 - 'git worktree add <path> <branch>' before Thomas's effort errored
   out if <branch> did not exist.  It was safe to add DWIM from
   remote-tracking branches without requiring any option.

 - 'git worktree add <path>' used to create a new branch whose name
   is derived from basename(path) that points at the current HEAD,
   without erroring out.  Enabling DWIM from remote-tracking
   branches unconditionally would have meant a silent behavior
   change.  So DWIM was added to this case to require the
   '--guess-remote' option to enable [*2*].

Back then, unique_tracking_name() did not let the callers
distinguish between 0-match and multiple-match cases, so when you
had multiple matches, 'git worktree add <path> [<branch>]' triggered
the same code path as 0-matches.  When the DWIM feature was
designed, handling the multiple-match case correctly was on nobody's
radar.

Even when Ævar Arnfjörð Bjarmason updated unique_tracking_name() in
3c87aa946a (checkout: pass the "num_matches" up to callers,
2018-06-05), in a topic that ends at 8d7b558bae (checkout &
worktree: introduce checkout.defaultRemote, 2018-06-05), to allow
callers to distinguish between 0-match and ambiguous multi-match
cases, this work unfortunately concentrated on improving "git
checkout", and callers of unique_tracking_name() in "git worktree"
were updated to pass NULL, i.e., teaching them to count how many
matches they got was postponed.

We know that the update to unique_tracking_name() in this work back
then was not complete on the "git worktree" side.  After all, that
is how this topic arose to fix one of the two code paths that call
the function so that we react differently between 0-match and
multiple-match cases.

Now that we are aware of the issue, I think the code should error
out, instead of creating the new branch out of HEAD, when there are
multiple remotes with the name of the branch.  In other words, the
existing code that behaves the same way in 0-match and 2-match cases
is buggy, and we should eventually fix it.


[Footnotes]

 *1* https://lore.kernel.org/git/20171112134305.3949-1-t.gummerer@gmail.com/
 *2* https://lore.kernel.org/git/20171126194356.16187-1-t.gummerer@gmail.com/

  reply	other threads:[~2026-08-22 17:22 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  8:21 [PATCH] worktree add: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-08 17:00 ` Junio C Hamano
2026-08-08 21:57   ` Junio C Hamano
2026-08-09  7:45     ` Harald Nordgren
2026-08-09 18:19       ` Junio C Hamano
2026-08-10 10:12         ` Harald Nordgren
2026-08-09 18:17     ` Junio C Hamano
2026-08-10 13:04     ` Yoichi Nakayama
2026-08-10 13:00   ` Yoichi Nakayama
2026-08-10 13:07 ` D. Ben Knoble
2026-08-10 13:35   ` Yoichi Nakayama
2026-08-10 15:06     ` Junio C Hamano
2026-08-10 21:36   ` Yoichi Nakayama
2026-08-11 16:38     ` Ben Knoble
2026-08-12 13:14       ` Yoichi Nakayama
2026-08-10 15:07 ` [PATCH v2] " Yoichi NAKAYAMA via GitGitGadget
2026-08-10 20:55 ` [PATCH v3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-11  0:03   ` Junio C Hamano
2026-08-11  6:31     ` Yoichi Nakayama
2026-08-12 19:22       ` Junio C Hamano
2026-08-15  4:36         ` Yoichi Nakayama
2026-08-11  6:35 ` [PATCH v4] " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 12:50 ` [PATCH v5 0/2] " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 12:50   ` [PATCH v5 1/2] checkout: " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 22:54     ` D. Ben Knoble
2026-08-20  2:18       ` Junio C Hamano
2026-08-20 15:41         ` Yoichi Nakayama
2026-08-19 12:50   ` [PATCH v5 2/2] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03 ` [PATCH v6 0/3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03   ` [PATCH v6 1/3] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03   ` [PATCH v6 2/3] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03   ` [PATCH v6 3/3] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-21  3:54     ` Junio C Hamano
2026-08-21 23:15       ` Yoichi Nakayama
2026-08-21 23:49         ` Junio C Hamano
2026-08-22  0:50           ` Yoichi Nakayama
2026-08-22 17:22             ` Junio C Hamano [this message]
2026-08-24 22:25               ` Yoichi Nakayama
2026-08-22  3:22 ` [PATCH v7 0/3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-22  3:22   ` [PATCH v7 1/3] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-22  3:22   ` [PATCH v7 2/3] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-22  3:22   ` [PATCH v7 3/3] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-22 18:08   ` [PATCH v7 0/3] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqld9yvznl.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=ben.knoble@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=yoichi.nakayama@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox