git_connect(): refactor the port handling for ssh
Use get_host_and_port() even for ssh. Remove the variable port git_connect(), and simplify parse_connect_url() Use only one return point in git_connect(), doing the free() and return conn. t5601 had 2 corner test cases which now pass. Signed-off-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Torsten Bögershausen committed
Nov 28, 2013 at 20:49 UTC
83b058752707a6ba4af51ebc98c47913bc7d2d25
3 files changed
+17
-49
connect.c
+13
-34
@@ -541,16 +541,13 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
541
return proxy;
542
}
543
544
-static char *get_port(char *host)
544
+static const char *get_port_numeric(const char *p)
545
{
546
char *end;
547
- char *p = strchr(host, ':');
548
-
547
if (p) {
548
long port = strtol(p + 1, &end, 10);
549
if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
552
- *p = '\0';
553
- return p+1;
550
+ return p;
551
}
552
}
553
@@ -562,7 +559,7 @@ static char *get_port(char *host)
559
* The caller must free() the returned strings.
560
*/
561
static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
565
- char **ret_port, char **ret_path)
562
+ char **ret_path)
563
{
564
char *url;
565
char *host, *path;
@@ -570,7 +567,6 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
567
int separator;
568
enum protocol protocol = PROTO_LOCAL;
569
int free_path = 0;
573
- char *port = NULL;
570
571
if (is_url(url_orig))
572
url = url_decode(url_orig);
@@ -589,16 +585,12 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
585
}
586
587
/*
592
- * Don't do destructive transforms with git:// as that
593
- * protocol code does '[]' unwrapping of its own.
588
+ * Don't do destructive transforms as protocol code does
589
+ * '[]' unwrapping in get_host_and_port()
590
*/
591
if (host[0] == '[') {
592
end = strchr(host + 1, ']');
593
if (end) {
598
- if (protocol != PROTO_GIT) {
599
- *end = 0;
600
- host++;
601
- }
594
end++;
595
} else
596
end = host;
@@ -636,17 +628,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
628
*ptr = '\0';
629
}
630
639
- /*
640
- * Add support for ssh port: ssh://host.xy:<port>/...
641
- */
642
- if (protocol == PROTO_SSH && separator == '/')
643
- port = get_port(end);
644
-
631
*ret_host = xstrdup(host);
646
- if (port)
647
- *ret_port = xstrdup(port);
648
- else
649
- *ret_port = NULL;
632
if (free_path)
633
*ret_path = path;
634
else
@@ -674,7 +656,6 @@ struct child_process *git_connect(int fd[2], const char *url,
656
char *host, *path;
657
struct child_process *conn = &no_fork;
658
enum protocol protocol;
677
- char *port;
659
const char **arg;
660
struct strbuf cmd = STRBUF_INIT;
661
@@ -683,18 +664,13 @@ struct child_process *git_connect(int fd[2], const char *url,
664
*/
665
signal(SIGCHLD, SIG_DFL);
666
686
- protocol = parse_connect_url(url, &host, &port, &path);
667
+ protocol = parse_connect_url(url, &host, &path);
668
if (flags & CONNECT_DIAG_URL) {
669
printf("Diag: url=%s\n", url ? url : "NULL");
670
printf("Diag: protocol=%s\n", prot_name(protocol));
690
- printf("Diag: hostandport=%s", host ? host : "NULL");
691
- if (port)
692
- printf(":%s\n", port);
693
- else
694
- printf("\n");
671
+ printf("Diag: hostandport=%s\n", host ? host : "NULL");
672
printf("Diag: path=%s\n", path ? path : "NULL");
673
free(host);
697
- free(port);
674
free(path);
675
return NULL;
676
}
@@ -721,7 +697,6 @@ struct child_process *git_connect(int fd[2], const char *url,
697
target_host, 0);
698
free(target_host);
699
free(host);
724
- free(port);
700
free(path);
701
return conn;
702
}
@@ -737,6 +712,11 @@ struct child_process *git_connect(int fd[2], const char *url,
712
if (protocol == PROTO_SSH) {
713
const char *ssh = getenv("GIT_SSH");
714
int putty = ssh && strcasestr(ssh, "plink");
715
+ char *ssh_host = host; /* keep host for the free() below */
716
+ const char *port = NULL;
717
+ get_host_and_port(&ssh_host, &port);
718
+ port = get_port_numeric(port);
719
+
720
if (!ssh) ssh = "ssh";
721
722
*arg++ = ssh;
@@ -747,7 +727,7 @@ struct child_process *git_connect(int fd[2], const char *url,
727
*arg++ = putty ? "-P" : "-p";
728
*arg++ = port;
729
}
750
- *arg++ = host;
730
+ *arg++ = ssh_host;
731
}
732
else {
733
/* remove repo-local variables from the environment */
@@ -764,7 +744,6 @@ struct child_process *git_connect(int fd[2], const char *url,
744
fd[1] = conn->in; /* write to child's stdin */
745
strbuf_release(&cmd);
746
free(host);
767
- free(port);
747
free(path);
748
return conn;
749
}
t/t5500-fetch-pack.sh
+3
-6
@@ -561,20 +561,18 @@ do
561
do
562
case "$p" in
563
*ssh*)
564
- hh=$(echo $h | tr -d "[]")
564
pp=ssh
565
;;
566
*)
568
- hh=$h
567
pp=$p
568
;;
569
esac
570
test_expect_success "fetch-pack --diag-url $p://$h/$r" '
573
- check_prot_host_path $p://$h/$r $pp "$hh" "/$r"
571
+ check_prot_host_path $p://$h/$r $pp "$h" "/$r"
572
'
573
# "/~" -> "~" conversion
574
test_expect_success "fetch-pack --diag-url $p://$h/~$r" '
577
- check_prot_host_path $p://$h/~$r $pp "$hh" "~$r"
575
+ check_prot_host_path $p://$h/~$r $pp "$h" "~$r"
576
'
577
done
578
done
@@ -604,13 +602,12 @@ do
602
p=ssh
603
for h in host [::1]
604
do
607
- hh=$(echo $h | tr -d "[]")
605
test_expect_success "fetch-pack --diag-url $h:$r" '
606
check_prot_path $h:$r $p "$r"
607
'
608
# Do "/~" -> "~" conversion
609
test_expect_success "fetch-pack --diag-url $h:/~$r" '
613
- check_prot_host_path $h:/~$r $p "$hh" "~$r"
610
+ check_prot_host_path $h:/~$r $p "$h" "~$r"
611
'
612
done
613
done
t/t5601-clone.sh
+1
-9
@@ -364,15 +364,7 @@ do
364
done
365
366
# Corner cases
367
-# failing
368
-for url in [foo]bar/baz:qux [foo/bar]:baz
369
-do
370
- test_expect_failure "clone $url is not ssh" '
371
- test_clone_url $url none
372
- '
373
-done
374
-
375
-for url in foo/bar:baz
367
+for url in foo/bar:baz [foo]bar/baz:qux [foo/bar]:baz
368
do
369
test_expect_success "clone $url is not ssh" '
370
test_clone_url $url none