From: Junio C Hamano Date: Thu, 26 Nov 2026 03:07:38 GMT Subject: Re: [PATCH 1/3] remote: reject invalid branch names in add -t Message-ID: In-Reply-To: "Harald Nordgren via GitGitGadget" writes: > From: Harald Nordgren > > 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..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.