Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Harald Nordgren <haraldnordgren@gmail.com>
Subject: Re: [PATCH 1/3] remote: reject invalid branch names in add -t
Date: Wed, 25 Nov 2026 19:07:38 -0800	[thread overview]
Message-ID: <xmqqcxrss32d.fsf@gitster.g> (raw)
In-Reply-To: <b829c46707c53095345ae0480e7e82bd83afef02.1795632934.git.gitgitgadget@gmail.com> (Harald Nordgren via GitGitGadget's message of "Wed, 25 Nov 2026 18:55:32 +0000")

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.

> +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.

>  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.

  reply	other threads:[~2026-11-26  3:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 18:55 [PATCH 0/3] remote: reject invalid branch names Harald Nordgren via GitGitGadget
2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget
2026-11-26  3:07   ` Junio C Hamano [this message]
2026-11-25 18:55 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget
2026-11-26  5:17   ` Junio C Hamano
2026-11-25 18:55 ` [PATCH 3/3] remote: reject invalid branch names in add -m Harald Nordgren via GitGitGadget
2026-11-26  5:17   ` 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=xmqqcxrss32d.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@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