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

[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
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 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.