[PATCH 2/3] remote: reject invalid branch names in set-branches
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 25, 2026, 18:55 UTC
- Message-ID
- <5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2429.git.git.1795632934.gitgitgadget@gmail.com>
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.
Check every branch name before touching the configuration, so a bad name leaves the existing fetch refspecs as they were.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- builtin/remote.c | 4 ++++ t/t5505-remote.sh | 14 ++++++++++++++ 2 files changed, 18 insertions(+)
diff --git a/builtin/remote.c b/builtin/remote.c index 63d2db39fe..b04cbe70b6 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1777,6 +1777,7 @@ static int set_remote_branches(const char *remotename, const char **branches, { struct strbuf key = STRBUF_INIT; struct remote *remote; + const char **branch; strbuf_addf(&key, "remote.%s.fetch", remotename); @@ -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); + if (!add_mode && remove_all_fetch_refspecs(key.buf)) { strbuf_release(&key); return 1; diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 055fa42e27..4bf97db6af 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -1454,6 +1454,20 @@ test_expect_success 'remote set-branches with --mirror' ' test_cmp expect.replace actual.replace ' +test_expect_success 'remote set-branches with an invalid branch name' ' + git clone .git/ setbranches-invalid && + ( + cd setbranches-invalid && + git config --get-all remote.origin.fetch >expect && + test_must_fail git remote set-branches origin main "@{u}" 2>err && + test_grep "is not a valid branch name" err && + test_must_fail git remote set-branches --add origin "@{u}" 2>err && + test_grep "is not a valid branch name" err && + git config --get-all remote.origin.fetch >actual && + test_cmp expect actual + ) +' + test_expect_success 'new remote' ' git remote add someremote foo && echo foo >expect &&
-- gitgitgadget