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

Re: [PATCH 1/3] remote: reject invalid branch names in add -t

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 26, 2026, 03:07 UTC
Message-ID
<xmqqcxrss32d.fsf@gitster.g>
In-Reply-To
<b829c46707c53095345ae0480e7e82bd83afef02.1795632934.git.gitgitgadget@gmail.com>
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 7 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> A typo in "git remote add -t", or a shell that expands something
> unexpected like @{u}, silently produced a fetch refspec that could
> never match anything, or in some cases outright invalid syntax. Every
> later fetch from that remote then failed with a confusing error and
> nothing pointed back at the original mistake.

Our commit log messages normally describe the pre-patch behavior in the present tense ("produces", "fails", "points"). Also, syntactic validation only catches invalid refname syntax (like @{u}, which causes refspec_append() inside remote_get() to die on subsequent commands), whereas a "typo" that is still a valid refname will still be accepted.

Show 12 quoted lines
> +static void validate_tracked_branchname(const char *branchname, int mirror)
> +{
> +	struct strbuf ref = STRBUF_INIT;
> +
> +	if (mirror)
> +		strbuf_addf(&ref, "refs/%s", branchname);
> +	else
> +		strbuf_addf(&ref, "refs/heads/%s", branchname);
> +	if (check_refname_format(ref.buf, REFNAME_REFSPEC_PATTERN))
> +		die(_("'%s' is not a valid branch name"), branchname);
> +	strbuf_release(&ref);
> +}

Using "refs/%s" when mirror is true and "refs/heads/%s" otherwise matches add_branch() right below. Passing REFNAME_REFSPEC_PATTERN to check_refname_format() allows single wildcard patterns such as -t "topic/*" while rejecting invalid sequences like @{u}, "..", ":", or multiple asterisks.

Using check_refname_format() directly, instead of check_branch_ref(), makes sense. Because check_branch_ref() calls copy_branchname() with INTERPRET_BRANCH_LOCAL and would expand local shorthands like @{-1}, and we do not want that.

Show 9 quoted lines
>  static void add_branch(const char *key, const char *branchname,
>  		       const char *remotename, int mirror, struct strbuf *tmp)
>  {
> @@ -211,6 +224,8 @@ static int add(int argc, const char **argv, const char *prefix,
>  		die(_("specifying a master branch makes no sense with --mirror"));
>  	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
>  		die(_("specifying branches to track makes sense only with fetch mirrors"));
> +	for (size_t i = 0; i < track.nr; i++)
> +		validate_tracked_branchname(track.items[i].string, mirror);

Validating track entries here runs before remote_get() and before repo_config_set() writes remote.<name>.url, so a bad -t argument leaves no configuration behind. The new test in t/t5505-remote.sh verifies both the error message and the absence of remote.badtrack.url.

OK.
Previous: Harald Nordgren via GitGitGadgetNext: Harald Nordgren via GitGitGadget
Message 3 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.