Skip to content

Commit 83b0587

Browse files
tboegigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a59974 commit 83b0587

File tree

3 files changed

+17
-49
lines changed

3 files changed

+17
-49
lines changed

connect.c

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -541,16 +541,13 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
541541
return proxy;
542542
}
543543

544-
static char *get_port(char *host)
544+
static const char *get_port_numeric(const char *p)
545545
{
546546
char *end;
547-
char *p = strchr(host, ':');
548-
549547
if (p) {
550548
long port = strtol(p + 1, &end, 10);
551549
if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
552-
*p = '\0';
553-
return p+1;
550+
return p;
554551
}
555552
}
556553

@@ -562,15 +559,14 @@ static char *get_port(char *host)
562559
* The caller must free() the returned strings.
563560
*/
564561
static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
565-
char **ret_port, char **ret_path)
562+
char **ret_path)
566563
{
567564
char *url;
568565
char *host, *path;
569566
char *end;
570567
int separator;
571568
enum protocol protocol = PROTO_LOCAL;
572569
int free_path = 0;
573-
char *port = NULL;
574570

575571
if (is_url(url_orig))
576572
url = url_decode(url_orig);
@@ -589,16 +585,12 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
589585
}
590586

591587
/*
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()
594590
*/
595591
if (host[0] == '[') {
596592
end = strchr(host + 1, ']');
597593
if (end) {
598-
if (protocol != PROTO_GIT) {
599-
*end = 0;
600-
host++;
601-
}
602594
end++;
603595
} else
604596
end = host;
@@ -636,17 +628,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
636628
*ptr = '\0';
637629
}
638630

639-
/*
640-
* Add support for ssh port: ssh://host.xy:<port>/...
641-
*/
642-
if (protocol == PROTO_SSH && separator == '/')
643-
port = get_port(end);
644-
645631
*ret_host = xstrdup(host);
646-
if (port)
647-
*ret_port = xstrdup(port);
648-
else
649-
*ret_port = NULL;
650632
if (free_path)
651633
*ret_path = path;
652634
else
@@ -674,7 +656,6 @@ struct child_process *git_connect(int fd[2], const char *url,
674656
char *host, *path;
675657
struct child_process *conn = &no_fork;
676658
enum protocol protocol;
677-
char *port;
678659
const char **arg;
679660
struct strbuf cmd = STRBUF_INIT;
680661

@@ -683,18 +664,13 @@ struct child_process *git_connect(int fd[2], const char *url,
683664
*/
684665
signal(SIGCHLD, SIG_DFL);
685666

686-
protocol = parse_connect_url(url, &host, &port, &path);
667+
protocol = parse_connect_url(url, &host, &path);
687668
if (flags & CONNECT_DIAG_URL) {
688669
printf("Diag: url=%s\n", url ? url : "NULL");
689670
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");
695672
printf("Diag: path=%s\n", path ? path : "NULL");
696673
free(host);
697-
free(port);
698674
free(path);
699675
return NULL;
700676
}
@@ -721,7 +697,6 @@ struct child_process *git_connect(int fd[2], const char *url,
721697
target_host, 0);
722698
free(target_host);
723699
free(host);
724-
free(port);
725700
free(path);
726701
return conn;
727702
}
@@ -737,6 +712,11 @@ struct child_process *git_connect(int fd[2], const char *url,
737712
if (protocol == PROTO_SSH) {
738713
const char *ssh = getenv("GIT_SSH");
739714
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+
740720
if (!ssh) ssh = "ssh";
741721

742722
*arg++ = ssh;
@@ -747,7 +727,7 @@ struct child_process *git_connect(int fd[2], const char *url,
747727
*arg++ = putty ? "-P" : "-p";
748728
*arg++ = port;
749729
}
750-
*arg++ = host;
730+
*arg++ = ssh_host;
751731
}
752732
else {
753733
/* remove repo-local variables from the environment */
@@ -764,7 +744,6 @@ struct child_process *git_connect(int fd[2], const char *url,
764744
fd[1] = conn->in; /* write to child's stdin */
765745
strbuf_release(&cmd);
766746
free(host);
767-
free(port);
768747
free(path);
769748
return conn;
770749
}

t/t5500-fetch-pack.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,20 +561,18 @@ do
561561
do
562562
case "$p" in
563563
*ssh*)
564-
hh=$(echo $h | tr -d "[]")
565564
pp=ssh
566565
;;
567566
*)
568-
hh=$h
569567
pp=$p
570568
;;
571569
esac
572570
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"
574572
'
575573
# "/~" -> "~" conversion
576574
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"
578576
'
579577
done
580578
done
@@ -604,13 +602,12 @@ do
604602
p=ssh
605603
for h in host [::1]
606604
do
607-
hh=$(echo $h | tr -d "[]")
608605
test_expect_success "fetch-pack --diag-url $h:$r" '
609606
check_prot_path $h:$r $p "$r"
610607
'
611608
# Do "/~" -> "~" conversion
612609
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"
614611
'
615612
done
616613
done

t/t5601-clone.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,7 @@ do
364364
done
365365

366366
# 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
376368
do
377369
test_expect_success "clone $url is not ssh" '
378370
test_clone_url $url none

0 commit comments

Comments
 (0)