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

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.
Previous: Harald Nordgren via GitGitGadgetNext: Harald Nordgren via GitGitGadget
Message 5 of 7 in “remote: reject invalid branch names”
  1. 0/3 remote: reject invalid branch namesHarald Nordgren via GitGitGadget, Nov 25, 2026
  2. 1/3 remote: reject invalid branch names in add -tHarald Nordgren via GitGitGadget, Nov 25, 2026
  3. Junio C HamanoNov 26, 2026
  4. 2/3 remote: reject invalid branch names in set-branchesHarald Nordgren via GitGitGadget, Nov 25, 2026
  5. Junio C HamanoNov 26, 2026
  6. 3/3 remote: reject invalid branch names in add -mHarald Nordgren via GitGitGadget, Nov 25, 2026
  7. Junio C HamanoNov 26, 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.