From: "Phillip Wood via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Han Jiang <jhcarl0814@gmail.com>,
Phillip Wood <phillip.wood@dunelm.org.uk>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH 4/4] remote: check branch names
Date: Wed, 11 Sep 2024 15:18:37 +0000 [thread overview]
Message-ID: <dba31245607f85c48947da60fe0955a6ed3e2c43.1726067917.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1789.git.1726067917.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Make sure the names passed to "git remote add -t <branch>" and "git
remote set-branches <branch>" are syntactically valid so that we do not
create invalid refspecs. This check needs to be performed before
creating the remote or modifying the existing configuration so a new
function is added rather than modifying add_branch()
Tests are added for both commands that to ensure (i) we report all the
invalid branch names passed on the command line, (ii) the branch names
are validated before modifying the configuration and (iii) wildcards
are accepted.
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
builtin/remote.c | 19 +++++++++++++++++++
t/t5505-remote.sh | 28 ++++++++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/builtin/remote.c b/builtin/remote.c
index 318701496ed..fd84bfbfe7a 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -132,6 +132,19 @@ static void add_branch(const char *key, const char *branchname,
git_config_set_multivar(key, tmp->buf, "^$", 0);
}
+static int check_branch_names(const char **branches)
+{
+ int ret = 0;
+
+ for (const char **b = branches; *b; b++) {
+ if (check_refname_format(*b, REFNAME_ALLOW_ONELEVEL |
+ REFNAME_REFSPEC_PATTERN))
+ ret = error(_("invalid branch name '%s'"), *b);
+ }
+
+ return ret;
+}
+
static const char mirror_advice[] =
N_("--mirror is dangerous and deprecated; please\n"
"\t use --mirror=fetch or --mirror=push instead");
@@ -203,6 +216,9 @@ static int add(int argc, const char **argv, const char *prefix)
if (!valid_remote_name(name))
die(_("'%s' is not a valid remote name"), name);
+ if (check_branch_names(track.v))
+ exit(128);
+
strbuf_addf(&buf, "remote.%s.url", name);
git_config_set(buf.buf, url);
@@ -1601,6 +1617,9 @@ static int set_remote_branches(const char *remotename, const char **branches,
exit(2);
}
+ if (check_branch_names(branches))
+ exit(128);
+
if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
error(_("could not remove existing fetch refspec"));
strbuf_release(&key);
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index cfbd6139e00..709cbe65924 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -1195,6 +1195,34 @@ test_expect_success 'remote set-branches with --mirror' '
test_cmp expect.replace actual.replace
'
+test_expect_success 'remote set-branches rejects invalid branch name' '
+ git remote add test https://git.example.com/repo &&
+ test_when_finished "git config --unset-all remote.test.fetch; \
+ git config --unset remote.test.url" &&
+ test_must_fail git remote set-branches test "topic/*" in..valid \
+ feature "b a d" 2>err &&
+ cat >expect <<-EOF &&
+ error: invalid branch name ${SQ}in..valid${SQ}
+ error: invalid branch name ${SQ}b a d${SQ}
+ EOF
+ test_cmp expect err &&
+ git config --get-all remote.test.fetch >actual &&
+ echo "+refs/heads/*:refs/remotes/test/*" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remote add -t rejects invalid branch name' '
+ test_must_fail git remote add test -t .bad -t "topic/*" -t in:valid \
+ https://git.example.com/repo 2>err &&
+ cat >expect <<-EOF &&
+ error: invalid branch name ${SQ}.bad${SQ}
+ error: invalid branch name ${SQ}in:valid${SQ}
+ EOF
+ test_cmp expect err &&
+ test_expect_code 1 git config --get-regexp ^remote\\.test\\. >actual &&
+ test_must_be_empty actual
+'
+
test_expect_success 'new remote' '
git remote add someremote foo &&
echo foo >expect &&
--
gitgitgadget
next prev parent reply other threads:[~2024-09-11 15:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-11 15:18 [PATCH 0/4] remote: branch setting fixes Phillip Wood via GitGitGadget
2024-09-11 15:18 ` [PATCH 1/4] remote: fix set-branches when no branches are set Phillip Wood via GitGitGadget
2024-09-11 20:45 ` Junio C Hamano
2024-09-12 10:04 ` Patrick Steinhardt
2024-09-11 15:18 ` [PATCH 2/4] remote: print an error if refspec cannot be removed Phillip Wood via GitGitGadget
2024-09-11 20:52 ` Junio C Hamano
2024-09-12 10:04 ` Patrick Steinhardt
2024-09-12 16:22 ` Junio C Hamano
2024-09-13 3:08 ` Patrick Steinhardt
2024-09-13 15:11 ` phillip.wood123
2024-09-13 17:38 ` Junio C Hamano
2024-09-11 15:18 ` [PATCH 3/4] remote add: use strvec to store tracking branches Phillip Wood via GitGitGadget
2024-09-11 17:05 ` Junio C Hamano
2024-09-12 10:05 ` Patrick Steinhardt
2024-09-11 15:18 ` Phillip Wood via GitGitGadget [this message]
2024-09-11 17:03 ` [PATCH 4/4] remote: check branch names Junio C Hamano
2024-09-12 10:05 ` Patrick Steinhardt
2024-09-12 16:32 ` Junio C Hamano
2024-09-13 15:09 ` phillip.wood123
2024-09-13 17:49 ` Junio C Hamano
2024-09-18 13:18 ` phillip.wood123
2024-09-18 20:24 ` 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=dba31245607f85c48947da60fe0955a6ed3e2c43.1726067917.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=jhcarl0814@gmail.com \
--cc=phillip.wood@dunelm.org.uk \
/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;
as well as URLs for NNTP newsgroup(s).