{"thread":{"id":"66931","subject":"[PATCH 0/3] remote: reject invalid branch names","startedAt":"2026-11-25T18:55:31Z","lastAt":"2026-11-26T05:17:40Z","messageCount":7,"participants":["Harald Nordgren via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"559373","messageId":"pull.2429.git.git.1795632934.gitgitgadget@gmail.com","threadId":"66931","inReplyTo":null,"subject":"[PATCH 0/3] remote: reject invalid branch names","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-25T18:55:31Z","receivedAt":"2026-11-25T18:55:31Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Reject invalid branch names in remote add -t, add -m and set-branches before\nany configuration is written, instead of silently breaking every future\nfetch on that remote.\n\nHarald Nordgren (3):\n  remote: reject invalid branch names in add -t\n  remote: reject invalid branch names in set-branches\n  remote: reject invalid branch names in add -m\n\n builtin/remote.c  | 27 +++++++++++++++++++++++++++\n t/t5505-remote.sh | 28 ++++++++++++++++++++++++++++\n 2 files changed, 55 insertions(+)\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2429%2FHaraldNordgren%2Fremote-add-branch-validate-name-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2429/HaraldNordgren/remote-add-branch-validate-name-v1\nPull-Request: https://github.com/git/git/pull/2429\n-- \ngitgitgadget\n\n"},{"id":"559374","messageId":"b829c46707c53095345ae0480e7e82bd83afef02.1795632934.git.gitgitgadget@gmail.com","threadId":"66931","inReplyTo":"pull.2429.git.git.1795632934.gitgitgadget@gmail.com","subject":"[PATCH 1/3] remote: reject invalid branch names in add -t","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-25T18:55:32Z","receivedAt":"2026-11-25T18:55:32Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA typo in \"git remote add -t\", or a shell that expands something\nunexpected like @{u}, silently produced a fetch refspec that could\nnever match anything, or in some cases outright invalid syntax. Every\nlater fetch from that remote then failed with a confusing error and\nnothing pointed back at the original mistake.\n\nCheck each branch name given to -t before anything is written, so the\ncommand fails without creating the remote at all.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/remote.c  | 15 +++++++++++++++\n t/t5505-remote.sh |  7 +++++++\n 2 files changed, 22 insertions(+)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..63d2db39fe 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -123,6 +123,19 @@ enum {\n #define MIRROR_PUSH 2\n #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)\n \n+static void validate_tracked_branchname(const char *branchname, int mirror)\n+{\n+\tstruct strbuf ref = STRBUF_INIT;\n+\n+\tif (mirror)\n+\t\tstrbuf_addf(&ref, \"refs/%s\", branchname);\n+\telse\n+\t\tstrbuf_addf(&ref, \"refs/heads/%s\", branchname);\n+\tif (check_refname_format(ref.buf, REFNAME_REFSPEC_PATTERN))\n+\t\tdie(_(\"'%s' is not a valid branch name\"), branchname);\n+\tstrbuf_release(&ref);\n+}\n+\n static void add_branch(const char *key, const char *branchname,\n \t\t       const char *remotename, int mirror, struct strbuf *tmp)\n {\n@@ -211,6 +224,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tfor (size_t i = 0; i < track.nr; i++)\n+\t\tvalidate_tracked_branchname(track.items[i].string, mirror);\n \n \tname = argv[0];\n \turl = argv[1];\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..055fa42e27 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -266,6 +266,13 @@ test_expect_success 'add invalid foreign_vcs remote' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'add -t with an invalid branch name' '\n+\techo \"fatal: '\\''@{u}'\\'' is not a valid branch name\" >expect &&\n+\ttest_must_fail git remote add -t \"@{u}\" badtrack bar 2>actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git config get remote.badtrack.url\n+'\n+\n test_expect_success 'without subcommand' '\n \techo origin >expect &&\n \tgit -C test remote >actual &&\n-- \ngitgitgadget\n\n\n"},{"id":"559375","messageId":"5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com","threadId":"66931","inReplyTo":"pull.2429.git.git.1795632934.gitgitgadget@gmail.com","subject":"[PATCH 2/3] remote: reject invalid branch names in set-branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-25T18:55:33Z","receivedAt":"2026-11-25T18:55:33Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nGiving \"git remote set-branches\" a name like @{u} wrote a fetch\nrefspec that could never match anything. Worse, without --add the\nexisting remote.<name>.fetch entries were thrown away first, so a\nsingle typo left the remote with a broken configuration and no\nindication of what went wrong.\n\nCheck every branch name before touching the configuration, so a bad\nname leaves the existing fetch refspecs as they were.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/remote.c  |  4 ++++\n t/t5505-remote.sh | 14 ++++++++++++++\n 2 files changed, 18 insertions(+)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 63d2db39fe..b04cbe70b6 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -1777,6 +1777,7 @@ static int set_remote_branches(const char *remotename, const char **branches,\n {\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct remote *remote;\n+\tconst char **branch;\n \n \tstrbuf_addf(&key, \"remote.%s.fetch\", remotename);\n \n@@ -1786,6 +1787,9 @@ static int set_remote_branches(const char *remotename, const char **branches,\n \t\texit(2);\n \t}\n \n+\tfor (branch = branches; *branch; branch++)\n+\t\tvalidate_tracked_branchname(*branch, remote->mirror);\n+\n \tif (!add_mode && remove_all_fetch_refspecs(key.buf)) {\n \t\tstrbuf_release(&key);\n \t\treturn 1;\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 055fa42e27..4bf97db6af 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -1454,6 +1454,20 @@ test_expect_success 'remote set-branches with --mirror' '\n \ttest_cmp expect.replace actual.replace\n '\n \n+test_expect_success 'remote set-branches with an invalid branch name' '\n+\tgit clone .git/ setbranches-invalid &&\n+\t(\n+\t\tcd setbranches-invalid &&\n+\t\tgit config --get-all remote.origin.fetch >expect &&\n+\t\ttest_must_fail git remote set-branches origin main \"@{u}\" 2>err &&\n+\t\ttest_grep \"is not a valid branch name\" err &&\n+\t\ttest_must_fail git remote set-branches --add origin \"@{u}\" 2>err &&\n+\t\ttest_grep \"is not a valid branch name\" err &&\n+\t\tgit config --get-all remote.origin.fetch >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'new remote' '\n \tgit remote add someremote foo &&\n \techo foo >expect &&\n-- \ngitgitgadget\n\n\n"},{"id":"559376","messageId":"bc1e30f6d05cf3e724a454e4c922a33f34c5a55c.1795632934.git.gitgitgadget@gmail.com","threadId":"66931","inReplyTo":"pull.2429.git.git.1795632934.gitgitgadget@gmail.com","subject":"[PATCH 3/3] remote: reject invalid branch names in add -m","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-25T18:55:34Z","receivedAt":"2026-11-25T18:55:34Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\n\"git remote add -m\" only looked at its argument after the remote had\nbeen created and fetched from. A bad name such as @{u} therefore got\nas far as a network fetch before failing with \"Could not setup\nmaster\", leaving a half configured remote behind.\n\nCheck the name up front, so the command fails before any remote is\ncreated.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/remote.c  | 8 ++++++++\n t/t5505-remote.sh | 7 +++++++\n 2 files changed, 15 insertions(+)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex b04cbe70b6..35c6a1f7fd 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -226,6 +226,14 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n \tfor (size_t i = 0; i < track.nr; i++)\n \t\tvalidate_tracked_branchname(track.items[i].string, mirror);\n+\tif (master) {\n+\t\tstruct strbuf ref = STRBUF_INIT;\n+\n+\t\tstrbuf_addf(&ref, \"refs/heads/%s\", master);\n+\t\tif (check_refname_format(ref.buf, 0))\n+\t\t\tdie(_(\"'%s' is not a valid branch name\"), master);\n+\t\tstrbuf_release(&ref);\n+\t}\n \n \tname = argv[0];\n \turl = argv[1];\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 4bf97db6af..867ada059e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -273,6 +273,13 @@ test_expect_success 'add -t with an invalid branch name' '\n \ttest_must_fail git config get remote.badtrack.url\n '\n \n+test_expect_success 'add -m with an invalid branch name' '\n+\techo \"fatal: '\\''@{u}'\\'' is not a valid branch name\" >expect &&\n+\ttest_must_fail git remote add -m \"@{u}\" badmaster bar 2>actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git config get remote.badmaster.url\n+'\n+\n test_expect_success 'without subcommand' '\n \techo origin >expect &&\n \tgit -C test remote >actual &&\n-- \ngitgitgadget\n\n"},{"id":"559430","messageId":"xmqqcxrss32d.fsf@gitster.g","threadId":"66931","inReplyTo":"b829c46707c53095345ae0480e7e82bd83afef02.1795632934.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/3] remote: reject invalid branch names in add -t","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-26T03:07:38Z","receivedAt":"2026-11-26T03:07:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A typo in \"git remote add -t\", or a shell that expands something\n> unexpected like @{u}, silently produced a fetch refspec that could\n> never match anything, or in some cases outright invalid syntax. Every\n> later fetch from that remote then failed with a confusing error and\n> nothing pointed back at the original mistake.\n\nOur commit log messages normally describe the pre-patch behavior in the\npresent tense (\"produces\", \"fails\", \"points\").  Also, syntactic\nvalidation only catches invalid refname syntax (like @{u}, which causes\nrefspec_append() inside remote_get() to die on subsequent commands),\nwhereas a \"typo\" that is still a valid refname will still be accepted.\n\n> +static void validate_tracked_branchname(const char *branchname, int mirror)\n> +{\n> +\tstruct strbuf ref = STRBUF_INIT;\n> +\n> +\tif (mirror)\n> +\t\tstrbuf_addf(&ref, \"refs/%s\", branchname);\n> +\telse\n> +\t\tstrbuf_addf(&ref, \"refs/heads/%s\", branchname);\n> +\tif (check_refname_format(ref.buf, REFNAME_REFSPEC_PATTERN))\n> +\t\tdie(_(\"'%s' is not a valid branch name\"), branchname);\n> +\tstrbuf_release(&ref);\n> +}\n\nUsing \"refs/%s\" when mirror is true and \"refs/heads/%s\" otherwise\nmatches add_branch() right below.  Passing REFNAME_REFSPEC_PATTERN to\ncheck_refname_format() allows single wildcard patterns such as -t\n\"topic/*\" while rejecting invalid sequences like @{u}, \"..\", \":\", or\nmultiple asterisks.\n\nUsing check_refname_format() directly, instead of check_branch_ref(),\nmakes sense.  Because check_branch_ref() calls copy_branchname() with\nINTERPRET_BRANCH_LOCAL and would expand local shorthands like @{-1},\nand we do not want that.\n\n>  static void add_branch(const char *key, const char *branchname,\n>  \t\t       const char *remotename, int mirror, struct strbuf *tmp)\n>  {\n> @@ -211,6 +224,8 @@ static int add(int argc, const char **argv, const char *prefix,\n>  \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n>  \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n>  \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n> +\tfor (size_t i = 0; i < track.nr; i++)\n> +\t\tvalidate_tracked_branchname(track.items[i].string, mirror);\n\nValidating track entries here runs before remote_get() and before\nrepo_config_set() writes remote.<name>.url, so a bad -t argument leaves\nno configuration behind.  The new test in t/t5505-remote.sh verifies\nboth the error message and the absence of remote.badtrack.url.\n\nOK.\n\n"},{"id":"559437","messageId":"xmqqa4mwqih9.fsf@gitster.g","threadId":"66931","inReplyTo":"5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 2/3] remote: reject invalid branch names in set-branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-26T05:17:38Z","receivedAt":"2026-11-26T05:17:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Giving \"git remote set-branches\" a name like @{u} wrote a fetch\n> refspec that could never match anything. Worse, without --add the\n> existing remote.<name>.fetch entries were thrown away first, so a\n> single typo left the remote with a broken configuration and no\n> indication of what went wrong.\n\nThe same \"make an observation on the current state in the present tense\"\napplies here.\n\nIn fact, once an invalid refspec like '+refs/heads/@{u}:...' is written\nto remote.<name>.fetch, even running 'git remote set-branches' again\ncannot fix it, because remote_get() dies while parsing the existing\nconfig.  Good catch.\n\n> diff --git a/builtin/remote.c b/builtin/remote.c\n> index 63d2db39fe..b04cbe70b6 100644\n> --- a/builtin/remote.c\n> +++ b/builtin/remote.c\n> @@ -1786,6 +1787,9 @@ static int set_remote_branches(const char *remotename, const char **branches,\n>  \t\texit(2);\n>  \t}\n>  \n> +\tfor (branch = branches; *branch; branch++)\n> +\t\tvalidate_tracked_branchname(*branch, remote->mirror);\n> +\n\nIn the unshown call at the end of set_remote_branches(), add_branches()\npasses the '.mirror' member to add_branch():\n\n    static void add_branches(struct remote *remote, const char **branches,\n                             const char *key)\n    {\n            const char *remotename = remote->name;\n            int mirror = remote->mirror;\n            ...\n            for (; *branches; branches++)\n                    add_branch(key, *branches, remotename, mirror, &refspec);\n\nSo passing remote->mirror to validate_tracked_branchname() matches the\nrefspec construction that follows, and checking every element of\nbranches before remove_all_fetch_refspecs() ensures neither replace mode\nnor --add touches existing config on failure.\n\nLooking good.\n\n"},{"id":"559438","messageId":"xmqq5wxkqih7.fsf@gitster.g","threadId":"66931","inReplyTo":"bc1e30f6d05cf3e724a454e4c922a33f34c5a55c.1795632934.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 3/3] remote: reject invalid branch names in add -m","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-26T05:17:40Z","receivedAt":"2026-11-26T05:17:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> \"git remote add -m\" only looked at its argument after the remote had\n> been created and fetched from. A bad name such as @{u} therefore got\n> as far as a network fetch before failing with \"Could not setup\n> master\", leaving a half configured remote behind.\n\nAgain, log messages are written in present tense.  Have you perhaps\nchanged your LLM settings or something?\n\nMinor factual nit on the commit log.  In add(), fetch_remote() only runs\nwhen -f is passed, so without -f no network fetch happens before\nrefs_update_symref() fails with \"Could not setup master '%s'\".  Saying\n\"after the remote has been created and (with -f) fetched from\" would be\nmore accurate.\n\n> diff --git a/builtin/remote.c b/builtin/remote.c\n> index b04cbe70b6..35c6a1f7fd 100644\n> --- a/builtin/remote.c\n> +++ b/builtin/remote.c\n> @@ -226,6 +226,14 @@ static int add(int argc, const char **argv, const char *prefix,\n>  \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n>  \tfor (size_t i = 0; i < track.nr; i++)\n>  \t\tvalidate_tracked_branchname(track.items[i].string, mirror);\n> +\tif (master) {\n> +\t\tstruct strbuf ref = STRBUF_INIT;\n> +\n> +\t\tstrbuf_addf(&ref, \"refs/heads/%s\", master);\n> +\t\tif (check_refname_format(ref.buf, 0))\n> +\t\t\tdie(_(\"'%s' is not a valid branch name\"), master);\n> +\t\tstrbuf_release(&ref);\n> +\t}\n\nPassing 0 instead of REFNAME_REFSPEC_PATTERN here is the right choice,\nbecause -m names a single branch rather than a refspec glob pattern.\n\nBecause check_refname_format() allows \"HEAD\" as a component, -m HEAD\nstill passes this check.  We would then later attempt to point\nrefs/remotes/<name>/HEAD to itself, which would be similar to the crazy\nconfigurations this series tries to prevent.\n\n\n"}]}