Git development
 help / color / mirror / Atom feed
From: "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Harald Nordgren <haraldnordgren@gmail.com>,
	Harald Nordgren <haraldnordgren@gmail.com>
Subject: [PATCH 2/3] remote: reject invalid branch names in set-branches
Date: Wed, 25 Nov 2026 18:55:33 +0000	[thread overview]
Message-ID: <5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com> (raw)
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


  parent reply	other threads:[~2026-11-25 18:55 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
2026-11-25 18:55 ` Harald Nordgren via GitGitGadget [this message]
2026-11-26  5:17   ` [PATCH 2/3] remote: reject invalid branch names in set-branches 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=5140655d23413ea5e739ecd4f413b931bddfd5f8.1795632934.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --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