Skip to content

Commit 24b6177

Browse files
jonasgitster
authored andcommitted
git-remote: reject adding remotes with invalid names
This can happen if the arguments to git-remote add is switched by the user, and git would only show an error if fetching was also requested. Fix it by using the refspec parsing engine to check if the requested name can be parsed as a remote before add it. Also cleanup so that the "remote.<name>.url" config name buffer is only initialized once. Signed-off-by: Jonas Fonseca <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0ab520 commit 24b6177

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

builtin-remote.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ static int add(int argc, const char **argv)
8888
strbuf_init(&buf, 0);
8989
strbuf_init(&buf2, 0);
9090

91+
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
92+
if (!valid_fetch_refspec(buf2.buf))
93+
die("'%s' is not a valid remote name", name);
94+
9195
strbuf_addf(&buf, "remote.%s.url", name);
9296
if (git_config_set(buf.buf, url))
9397
return 1;
9498

99+
strbuf_reset(&buf);
100+
strbuf_addf(&buf, "remote.%s.fetch", name);
101+
95102
if (track.nr == 0)
96103
path_list_append("*", &track);
97104
for (i = 0; i < track.nr; i++) {
98105
struct path_list_item *item = track.items + i;
99106

100-
strbuf_reset(&buf);
101-
strbuf_addf(&buf, "remote.%s.fetch", name);
102-
103107
strbuf_reset(&buf2);
104108
if (mirror)
105109
strbuf_addf(&buf2, "refs/%s:refs/%s",

remote.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static void read_config(void)
409409
alias_all_urls();
410410
}
411411

412-
static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
412+
static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
413413
{
414414
int i;
415415
int st;
@@ -519,17 +519,32 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
519519
return rs;
520520

521521
invalid:
522+
if (verify) {
523+
free(rs);
524+
return NULL;
525+
}
522526
die("Invalid refspec '%s'", refspec[i]);
523527
}
524528

529+
int valid_fetch_refspec(const char *fetch_refspec_str)
530+
{
531+
const char *fetch_refspec[] = { fetch_refspec_str };
532+
struct refspec *refspec;
533+
534+
refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
535+
if (refspec)
536+
free(refspec);
537+
return !!refspec;
538+
}
539+
525540
struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
526541
{
527-
return parse_refspec_internal(nr_refspec, refspec, 1);
542+
return parse_refspec_internal(nr_refspec, refspec, 1, 0);
528543
}
529544

530545
struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
531546
{
532-
return parse_refspec_internal(nr_refspec, refspec, 0);
547+
return parse_refspec_internal(nr_refspec, refspec, 0, 0);
533548
}
534549

535550
static int valid_remote_nick(const char *name)

remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void free_refs(struct ref *ref);
6767
*/
6868
void ref_remove_duplicates(struct ref *ref_map);
6969

70+
int valid_fetch_refspec(const char *refspec);
7071
struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec);
7172
struct refspec *parse_push_refspec(int nr_refspec, const char **refspec);
7273

t/t5505-remote.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,10 @@ test_expect_success '"remote show" does not show symbolic refs' '
253253
254254
'
255255

256+
test_expect_success 'reject adding remote with an invalid name' '
257+
258+
! git remote add some:url desired-name
259+
260+
'
261+
256262
test_done

0 commit comments

Comments
 (0)