@matheusbsbs / matheus-bagliano / commits / 24b6177e02
Fork 0

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 <fonseca@diku.dk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonas Fonseca committed Apr 13, 2008 at 11:56 UTC 24b6177e0261efba063c0b83ae0cdc5993945da9
4 files changed +32 -6
builtin-remote.c
+7 -3
@@ -88,18 +88,22 @@ static int add(int argc, const char **argv)
88 strbuf_init(&buf, 0);
89 strbuf_init(&buf2, 0);
90
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 +
95 strbuf_addf(&buf, "remote.%s.url", name);
96 if (git_config_set(buf.buf, url))
97 return 1;
98
99 + strbuf_reset(&buf);
100 + strbuf_addf(&buf, "remote.%s.fetch", name);
101 +
102 if (track.nr == 0)
103 path_list_append("*", &track);
104 for (i = 0; i < track.nr; i++) {
105 struct path_list_item *item = track.items + i;
106
100 - strbuf_reset(&buf);
101 - strbuf_addf(&buf, "remote.%s.fetch", name);
102 -
107 strbuf_reset(&buf2);
108 if (mirror)
109 strbuf_addf(&buf2, "refs/%s:refs/%s",
remote.c
+18 -3
@@ -409,7 +409,7 @@ static void read_config(void)
409 alias_all_urls();
410 }
411
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)
413 {
414 int i;
415 int st;
@@ -519,17 +519,32 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
519 return rs;
520
521 invalid:
522 + if (verify) {
523 + free(rs);
524 + return NULL;
525 + }
526 die("Invalid refspec '%s'", refspec[i]);
527 }
528
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 +
540 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
541 {
527 - return parse_refspec_internal(nr_refspec, refspec, 1);
542 + return parse_refspec_internal(nr_refspec, refspec, 1, 0);
543 }
544
545 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
546 {
532 - return parse_refspec_internal(nr_refspec, refspec, 0);
547 + return parse_refspec_internal(nr_refspec, refspec, 0, 0);
548 }
549
550 static int valid_remote_nick(const char *name)
remote.h
+1
@@ -67,6 +67,7 @@ void free_refs(struct ref *ref);
67 */
68 void ref_remove_duplicates(struct ref *ref_map);
69
70 +int valid_fetch_refspec(const char *refspec);
71 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec);
72 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec);
73
t/t5505-remote.sh
+6
@@ -253,4 +253,10 @@ test_expect_success '"remote show" does not show symbolic refs' '
253
254 '
255
256 +test_expect_success 'reject adding remote with an invalid name' '
257 +
258 + ! git remote add some:url desired-name
259 +
260 +'
261 +
262 test_done