Skip to content

Commit dba3124

Browse files
committed
remote: check branch names
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 <[email protected]>
1 parent f30c77b commit dba3124

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

builtin/remote.c

+19
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ static void add_branch(const char *key, const char *branchname,
132132
git_config_set_multivar(key, tmp->buf, "^$", 0);
133133
}
134134

135+
static int check_branch_names(const char **branches)
136+
{
137+
int ret = 0;
138+
139+
for (const char **b = branches; *b; b++) {
140+
if (check_refname_format(*b, REFNAME_ALLOW_ONELEVEL |
141+
REFNAME_REFSPEC_PATTERN))
142+
ret = error(_("invalid branch name '%s'"), *b);
143+
}
144+
145+
return ret;
146+
}
147+
135148
static const char mirror_advice[] =
136149
N_("--mirror is dangerous and deprecated; please\n"
137150
"\t use --mirror=fetch or --mirror=push instead");
@@ -203,6 +216,9 @@ static int add(int argc, const char **argv, const char *prefix)
203216
if (!valid_remote_name(name))
204217
die(_("'%s' is not a valid remote name"), name);
205218

219+
if (check_branch_names(track.v))
220+
exit(128);
221+
206222
strbuf_addf(&buf, "remote.%s.url", name);
207223
git_config_set(buf.buf, url);
208224

@@ -1601,6 +1617,9 @@ static int set_remote_branches(const char *remotename, const char **branches,
16011617
exit(2);
16021618
}
16031619

1620+
if (check_branch_names(branches))
1621+
exit(128);
1622+
16041623
if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
16051624
error(_("could not remove existing fetch refspec"));
16061625
strbuf_release(&key);

t/t5505-remote.sh

+28
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,34 @@ test_expect_success 'remote set-branches with --mirror' '
11951195
test_cmp expect.replace actual.replace
11961196
'
11971197

1198+
test_expect_success 'remote set-branches rejects invalid branch name' '
1199+
git remote add test https://git.example.com/repo &&
1200+
test_when_finished "git config --unset-all remote.test.fetch; \
1201+
git config --unset remote.test.url" &&
1202+
test_must_fail git remote set-branches test "topic/*" in..valid \
1203+
feature "b a d" 2>err &&
1204+
cat >expect <<-EOF &&
1205+
error: invalid branch name ${SQ}in..valid${SQ}
1206+
error: invalid branch name ${SQ}b a d${SQ}
1207+
EOF
1208+
test_cmp expect err &&
1209+
git config --get-all remote.test.fetch >actual &&
1210+
echo "+refs/heads/*:refs/remotes/test/*" >expect &&
1211+
test_cmp expect actual
1212+
'
1213+
1214+
test_expect_success 'remote add -t rejects invalid branch name' '
1215+
test_must_fail git remote add test -t .bad -t "topic/*" -t in:valid \
1216+
https://git.example.com/repo 2>err &&
1217+
cat >expect <<-EOF &&
1218+
error: invalid branch name ${SQ}.bad${SQ}
1219+
error: invalid branch name ${SQ}in:valid${SQ}
1220+
EOF
1221+
test_cmp expect err &&
1222+
test_expect_code 1 git config --get-regexp ^remote\\.test\\. >actual &&
1223+
test_must_be_empty actual
1224+
'
1225+
11981226
test_expect_success 'new remote' '
11991227
git remote add someremote foo &&
12001228
echo foo >expect &&

0 commit comments

Comments
 (0)