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 3/3] remote: reject invalid branch names in add -m
Date: Wed, 25 Nov 2026 18:55:34 +0000 [thread overview]
Message-ID: <bc1e30f6d05cf3e724a454e4c922a33f34c5a55c.1795632934.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2429.git.git.1795632934.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
"git remote add -m" only looked at its argument after the remote had
been created and fetched from. A bad name such as @{u} therefore got
as far as a network fetch before failing with "Could not setup
master", leaving a half configured remote behind.
Check the name up front, so the command fails before any remote is
created.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
builtin/remote.c | 8 ++++++++
t/t5505-remote.sh | 7 +++++++
2 files changed, 15 insertions(+)
diff --git a/builtin/remote.c b/builtin/remote.c
index b04cbe70b6..35c6a1f7fd 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -226,6 +226,14 @@ static int add(int argc, const char **argv, const char *prefix,
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);
+ if (master) {
+ struct strbuf ref = STRBUF_INIT;
+
+ strbuf_addf(&ref, "refs/heads/%s", master);
+ if (check_refname_format(ref.buf, 0))
+ die(_("'%s' is not a valid branch name"), master);
+ strbuf_release(&ref);
+ }
name = argv[0];
url = argv[1];
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 4bf97db6af..867ada059e 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -273,6 +273,13 @@ test_expect_success 'add -t with an invalid branch name' '
test_must_fail git config get remote.badtrack.url
'
+test_expect_success 'add -m with an invalid branch name' '
+ echo "fatal: '\''@{u}'\'' is not a valid branch name" >expect &&
+ test_must_fail git remote add -m "@{u}" badmaster bar 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git config get remote.badmaster.url
+'
+
test_expect_success 'without subcommand' '
echo origin >expect &&
git -C test remote >actual &&
--
gitgitgadget
next prev 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 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget
2026-11-26 5:17 ` Junio C Hamano
2026-11-25 18:55 ` Harald Nordgren via GitGitGadget [this message]
2026-11-26 5:17 ` [PATCH 3/3] remote: reject invalid branch names in add -m 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=bc1e30f6d05cf3e724a454e4c922a33f34c5a55c.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