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>
This commit is contained in:

committed by
Junio C Hamano

parent
6a59974869
commit
83b0587527
47
connect.c
47
connect.c
@ -541,16 +541,13 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
|
|||||||
return proxy;
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *get_port(char *host)
|
static const char *get_port_numeric(const char *p)
|
||||||
{
|
{
|
||||||
char *end;
|
char *end;
|
||||||
char *p = strchr(host, ':');
|
|
||||||
|
|
||||||
if (p) {
|
if (p) {
|
||||||
long port = strtol(p + 1, &end, 10);
|
long port = strtol(p + 1, &end, 10);
|
||||||
if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
|
if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
|
||||||
*p = '\0';
|
return p;
|
||||||
return p+1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -562,7 +559,7 @@ static char *get_port(char *host)
|
|||||||
* The caller must free() the returned strings.
|
* The caller must free() the returned strings.
|
||||||
*/
|
*/
|
||||||
static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
|
static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
|
||||||
char **ret_port, char **ret_path)
|
char **ret_path)
|
||||||
{
|
{
|
||||||
char *url;
|
char *url;
|
||||||
char *host, *path;
|
char *host, *path;
|
||||||
@ -570,7 +567,6 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
|
|||||||
int separator;
|
int separator;
|
||||||
enum protocol protocol = PROTO_LOCAL;
|
enum protocol protocol = PROTO_LOCAL;
|
||||||
int free_path = 0;
|
int free_path = 0;
|
||||||
char *port = NULL;
|
|
||||||
|
|
||||||
if (is_url(url_orig))
|
if (is_url(url_orig))
|
||||||
url = url_decode(url_orig);
|
url = url_decode(url_orig);
|
||||||
@ -589,16 +585,12 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Don't do destructive transforms with git:// as that
|
* Don't do destructive transforms as protocol code does
|
||||||
* protocol code does '[]' unwrapping of its own.
|
* '[]' unwrapping in get_host_and_port()
|
||||||
*/
|
*/
|
||||||
if (host[0] == '[') {
|
if (host[0] == '[') {
|
||||||
end = strchr(host + 1, ']');
|
end = strchr(host + 1, ']');
|
||||||
if (end) {
|
if (end) {
|
||||||
if (protocol != PROTO_GIT) {
|
|
||||||
*end = 0;
|
|
||||||
host++;
|
|
||||||
}
|
|
||||||
end++;
|
end++;
|
||||||
} else
|
} else
|
||||||
end = host;
|
end = host;
|
||||||
@ -636,17 +628,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
|
|||||||
*ptr = '\0';
|
*ptr = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Add support for ssh port: ssh://host.xy:<port>/...
|
|
||||||
*/
|
|
||||||
if (protocol == PROTO_SSH && separator == '/')
|
|
||||||
port = get_port(end);
|
|
||||||
|
|
||||||
*ret_host = xstrdup(host);
|
*ret_host = xstrdup(host);
|
||||||
if (port)
|
|
||||||
*ret_port = xstrdup(port);
|
|
||||||
else
|
|
||||||
*ret_port = NULL;
|
|
||||||
if (free_path)
|
if (free_path)
|
||||||
*ret_path = path;
|
*ret_path = path;
|
||||||
else
|
else
|
||||||
@ -674,7 +656,6 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
char *host, *path;
|
char *host, *path;
|
||||||
struct child_process *conn = &no_fork;
|
struct child_process *conn = &no_fork;
|
||||||
enum protocol protocol;
|
enum protocol protocol;
|
||||||
char *port;
|
|
||||||
const char **arg;
|
const char **arg;
|
||||||
struct strbuf cmd = STRBUF_INIT;
|
struct strbuf cmd = STRBUF_INIT;
|
||||||
|
|
||||||
@ -683,18 +664,13 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
*/
|
*/
|
||||||
signal(SIGCHLD, SIG_DFL);
|
signal(SIGCHLD, SIG_DFL);
|
||||||
|
|
||||||
protocol = parse_connect_url(url, &host, &port, &path);
|
protocol = parse_connect_url(url, &host, &path);
|
||||||
if (flags & CONNECT_DIAG_URL) {
|
if (flags & CONNECT_DIAG_URL) {
|
||||||
printf("Diag: url=%s\n", url ? url : "NULL");
|
printf("Diag: url=%s\n", url ? url : "NULL");
|
||||||
printf("Diag: protocol=%s\n", prot_name(protocol));
|
printf("Diag: protocol=%s\n", prot_name(protocol));
|
||||||
printf("Diag: hostandport=%s", host ? host : "NULL");
|
printf("Diag: hostandport=%s\n", host ? host : "NULL");
|
||||||
if (port)
|
|
||||||
printf(":%s\n", port);
|
|
||||||
else
|
|
||||||
printf("\n");
|
|
||||||
printf("Diag: path=%s\n", path ? path : "NULL");
|
printf("Diag: path=%s\n", path ? path : "NULL");
|
||||||
free(host);
|
free(host);
|
||||||
free(port);
|
|
||||||
free(path);
|
free(path);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -721,7 +697,6 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
target_host, 0);
|
target_host, 0);
|
||||||
free(target_host);
|
free(target_host);
|
||||||
free(host);
|
free(host);
|
||||||
free(port);
|
|
||||||
free(path);
|
free(path);
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
@ -737,6 +712,11 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
if (protocol == PROTO_SSH) {
|
if (protocol == PROTO_SSH) {
|
||||||
const char *ssh = getenv("GIT_SSH");
|
const char *ssh = getenv("GIT_SSH");
|
||||||
int putty = ssh && strcasestr(ssh, "plink");
|
int putty = ssh && strcasestr(ssh, "plink");
|
||||||
|
char *ssh_host = host; /* keep host for the free() below */
|
||||||
|
const char *port = NULL;
|
||||||
|
get_host_and_port(&ssh_host, &port);
|
||||||
|
port = get_port_numeric(port);
|
||||||
|
|
||||||
if (!ssh) ssh = "ssh";
|
if (!ssh) ssh = "ssh";
|
||||||
|
|
||||||
*arg++ = ssh;
|
*arg++ = ssh;
|
||||||
@ -747,7 +727,7 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
*arg++ = putty ? "-P" : "-p";
|
*arg++ = putty ? "-P" : "-p";
|
||||||
*arg++ = port;
|
*arg++ = port;
|
||||||
}
|
}
|
||||||
*arg++ = host;
|
*arg++ = ssh_host;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* remove repo-local variables from the environment */
|
/* remove repo-local variables from the environment */
|
||||||
@ -764,7 +744,6 @@ struct child_process *git_connect(int fd[2], const char *url,
|
|||||||
fd[1] = conn->in; /* write to child's stdin */
|
fd[1] = conn->in; /* write to child's stdin */
|
||||||
strbuf_release(&cmd);
|
strbuf_release(&cmd);
|
||||||
free(host);
|
free(host);
|
||||||
free(port);
|
|
||||||
free(path);
|
free(path);
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
@ -561,20 +561,18 @@ do
|
|||||||
do
|
do
|
||||||
case "$p" in
|
case "$p" in
|
||||||
*ssh*)
|
*ssh*)
|
||||||
hh=$(echo $h | tr -d "[]")
|
|
||||||
pp=ssh
|
pp=ssh
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
hh=$h
|
|
||||||
pp=$p
|
pp=$p
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
test_expect_success "fetch-pack --diag-url $p://$h/$r" '
|
test_expect_success "fetch-pack --diag-url $p://$h/$r" '
|
||||||
check_prot_host_path $p://$h/$r $pp "$hh" "/$r"
|
check_prot_host_path $p://$h/$r $pp "$h" "/$r"
|
||||||
'
|
'
|
||||||
# "/~" -> "~" conversion
|
# "/~" -> "~" conversion
|
||||||
test_expect_success "fetch-pack --diag-url $p://$h/~$r" '
|
test_expect_success "fetch-pack --diag-url $p://$h/~$r" '
|
||||||
check_prot_host_path $p://$h/~$r $pp "$hh" "~$r"
|
check_prot_host_path $p://$h/~$r $pp "$h" "~$r"
|
||||||
'
|
'
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
@ -604,13 +602,12 @@ do
|
|||||||
p=ssh
|
p=ssh
|
||||||
for h in host [::1]
|
for h in host [::1]
|
||||||
do
|
do
|
||||||
hh=$(echo $h | tr -d "[]")
|
|
||||||
test_expect_success "fetch-pack --diag-url $h:$r" '
|
test_expect_success "fetch-pack --diag-url $h:$r" '
|
||||||
check_prot_path $h:$r $p "$r"
|
check_prot_path $h:$r $p "$r"
|
||||||
'
|
'
|
||||||
# Do "/~" -> "~" conversion
|
# Do "/~" -> "~" conversion
|
||||||
test_expect_success "fetch-pack --diag-url $h:/~$r" '
|
test_expect_success "fetch-pack --diag-url $h:/~$r" '
|
||||||
check_prot_host_path $h:/~$r $p "$hh" "~$r"
|
check_prot_host_path $h:/~$r $p "$h" "~$r"
|
||||||
'
|
'
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
@ -364,15 +364,7 @@ do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# Corner cases
|
# Corner cases
|
||||||
# failing
|
for url in foo/bar:baz [foo]bar/baz:qux [foo/bar]:baz
|
||||||
for url in [foo]bar/baz:qux [foo/bar]:baz
|
|
||||||
do
|
|
||||||
test_expect_failure "clone $url is not ssh" '
|
|
||||||
test_clone_url $url none
|
|
||||||
'
|
|
||||||
done
|
|
||||||
|
|
||||||
for url in foo/bar:baz
|
|
||||||
do
|
do
|
||||||
test_expect_success "clone $url is not ssh" '
|
test_expect_success "clone $url is not ssh" '
|
||||||
test_clone_url $url none
|
test_clone_url $url none
|
||||||
|
Reference in New Issue
Block a user