Skip to content

Commit a2036d7

Browse files
tboegigitster
authored andcommitted
git_connect(): use common return point
Use only one return point from git_connect(), doing the free(); return conn; only at one place in the code. There may be a little confusion what the variable "host" is for. At some places it is only the host part, at other places it may include the port number, so change host into hostandport here. Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c59ab2e commit a2036d7

File tree

1 file changed

+50
-58
lines changed

1 file changed

+50
-58
lines changed

connect.c

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ static struct child_process no_fork;
656656
struct child_process *git_connect(int fd[2], const char *url,
657657
const char *prog, int flags)
658658
{
659-
char *host, *path;
659+
char *hostandport, *path;
660660
struct child_process *conn = &no_fork;
661661
enum protocol protocol;
662662
const char **arg;
@@ -667,26 +667,22 @@ struct child_process *git_connect(int fd[2], const char *url,
667667
*/
668668
signal(SIGCHLD, SIG_DFL);
669669

670-
protocol = parse_connect_url(url, &host, &path);
670+
protocol = parse_connect_url(url, &hostandport, &path);
671671
if (flags & CONNECT_DIAG_URL) {
672672
printf("Diag: url=%s\n", url ? url : "NULL");
673673
printf("Diag: protocol=%s\n", prot_name(protocol));
674-
printf("Diag: hostandport=%s\n", host ? host : "NULL");
674+
printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
675675
printf("Diag: path=%s\n", path ? path : "NULL");
676-
free(host);
677-
free(path);
678-
return NULL;
679-
}
680-
681-
if (protocol == PROTO_GIT) {
676+
conn = NULL;
677+
} else if (protocol == PROTO_GIT) {
682678
/* These underlying connection commands die() if they
683679
* cannot connect.
684680
*/
685-
char *target_host = xstrdup(host);
686-
if (git_use_proxy(host))
687-
conn = git_proxy_connect(fd, host);
681+
char *target_host = xstrdup(hostandport);
682+
if (git_use_proxy(hostandport))
683+
conn = git_proxy_connect(fd, hostandport);
688684
else
689-
git_tcp_connect(fd, host, flags);
685+
git_tcp_connect(fd, hostandport, flags);
690686
/*
691687
* Separate original protocol components prog and path
692688
* from extended host header with a NUL byte.
@@ -699,54 +695,50 @@ struct child_process *git_connect(int fd[2], const char *url,
699695
prog, path, 0,
700696
target_host, 0);
701697
free(target_host);
702-
free(host);
703-
free(path);
704-
return conn;
705-
}
706-
707-
conn = xcalloc(1, sizeof(*conn));
708-
709-
strbuf_addstr(&cmd, prog);
710-
strbuf_addch(&cmd, ' ');
711-
sq_quote_buf(&cmd, path);
712-
713-
conn->in = conn->out = -1;
714-
conn->argv = arg = xcalloc(7, sizeof(*arg));
715-
if (protocol == PROTO_SSH) {
716-
const char *ssh = getenv("GIT_SSH");
717-
int putty = ssh && strcasestr(ssh, "plink");
718-
char *ssh_host = host; /* keep host for the free() below */
719-
const char *port = NULL;
720-
get_host_and_port(&ssh_host, &port);
721-
port = get_port_numeric(port);
722-
723-
if (!ssh) ssh = "ssh";
724-
725-
*arg++ = ssh;
726-
if (putty && !strcasestr(ssh, "tortoiseplink"))
727-
*arg++ = "-batch";
728-
if (port) {
729-
/* P is for PuTTY, p is for OpenSSH */
730-
*arg++ = putty ? "-P" : "-p";
731-
*arg++ = port;
698+
} else {
699+
conn = xcalloc(1, sizeof(*conn));
700+
701+
strbuf_addstr(&cmd, prog);
702+
strbuf_addch(&cmd, ' ');
703+
sq_quote_buf(&cmd, path);
704+
705+
conn->in = conn->out = -1;
706+
conn->argv = arg = xcalloc(7, sizeof(*arg));
707+
if (protocol == PROTO_SSH) {
708+
const char *ssh = getenv("GIT_SSH");
709+
int putty = ssh && strcasestr(ssh, "plink");
710+
char *ssh_host = hostandport;
711+
const char *port = NULL;
712+
get_host_and_port(&ssh_host, &port);
713+
port = get_port_numeric(port);
714+
715+
if (!ssh) ssh = "ssh";
716+
717+
*arg++ = ssh;
718+
if (putty && !strcasestr(ssh, "tortoiseplink"))
719+
*arg++ = "-batch";
720+
if (port) {
721+
/* P is for PuTTY, p is for OpenSSH */
722+
*arg++ = putty ? "-P" : "-p";
723+
*arg++ = port;
724+
}
725+
*arg++ = ssh_host;
726+
} else {
727+
/* remove repo-local variables from the environment */
728+
conn->env = local_repo_env;
729+
conn->use_shell = 1;
732730
}
733-
*arg++ = ssh_host;
734-
}
735-
else {
736-
/* remove repo-local variables from the environment */
737-
conn->env = local_repo_env;
738-
conn->use_shell = 1;
739-
}
740-
*arg++ = cmd.buf;
741-
*arg = NULL;
731+
*arg++ = cmd.buf;
732+
*arg = NULL;
742733

743-
if (start_command(conn))
744-
die("unable to fork");
734+
if (start_command(conn))
735+
die("unable to fork");
745736

746-
fd[0] = conn->out; /* read from child's stdout */
747-
fd[1] = conn->in; /* write to child's stdin */
748-
strbuf_release(&cmd);
749-
free(host);
737+
fd[0] = conn->out; /* read from child's stdout */
738+
fd[1] = conn->in; /* write to child's stdin */
739+
strbuf_release(&cmd);
740+
}
741+
free(hostandport);
750742
free(path);
751743
return conn;
752744
}

0 commit comments

Comments
 (0)