Re: [PATCH 2/3] remote: reject invalid branch names in set-branches
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 26, 2026, 05:17 UTC
- Message-ID
- <xmqqa4mwqih9.fsf@gitster.g>
- In-Reply-To
- <5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com>
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 7 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Giving "git remote set-branches" a name like @{u} wrote a fetch
> refspec that could never match anything. Worse, without --add the
> existing remote.<name>.fetch entries were thrown away first, so a
> single typo left the remote with a broken configuration and no
> indication of what went wrong.The same "make an observation on the current state in the present tense" applies here.
In fact, once an invalid refspec like '+refs/heads/@{u}:...' is written to remote.<name>.fetch, even running 'git remote set-branches' again cannot fix it, because remote_get() dies while parsing the existing config. Good catch.
Show 11 quoted lines
> diff --git a/builtin/remote.c b/builtin/remote.c > index 63d2db39fe..b04cbe70b6 100644 > --- a/builtin/remote.c > +++ b/builtin/remote.c > @@ -1786,6 +1787,9 @@ static int set_remote_branches(const char *remotename, const char **branches, > exit(2); > } > > + for (branch = branches; *branch; branch++) > + validate_tracked_branchname(*branch, remote->mirror); > +
In the unshown call at the end of set_remote_branches(), add_branches() passes the '.mirror' member to add_branch():
static void add_branches(struct remote *remote, const char **branches,
const char *key)
{
const char *remotename = remote->name;
int mirror = remote->mirror;
...
for (; *branches; branches++)
add_branch(key, *branches, remotename, mirror, &refspec);So passing remote->mirror to validate_tracked_branchname() matches the refspec construction that follows, and checking every element of branches before remove_all_fetch_refspecs() ensures neither replace mode nor --add touches existing config on failure.
Looking good.