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 <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed Nov 28, 2013 at 20:50 UTC a2036d7e00ad8aa16ba010a80078e10f0e4568a3
1 file changed +50 -58
connect.c
+50 -58
@@ -656,7 +656,7 @@ static struct child_process no_fork;
656 struct child_process *git_connect(int fd[2], const char *url,
657 const char *prog, int flags)
658 {
659 - char *host, *path;
659 + char *hostandport, *path;
660 struct child_process *conn = &no_fork;
661 enum protocol protocol;
662 const char **arg;
@@ -667,26 +667,22 @@ struct child_process *git_connect(int fd[2], const char *url,
667 */
668 signal(SIGCHLD, SIG_DFL);
669
670 - protocol = parse_connect_url(url, &host, &path);
670 + protocol = parse_connect_url(url, &hostandport, &path);
671 if (flags & CONNECT_DIAG_URL) {
672 printf("Diag: url=%s\n", url ? url : "NULL");
673 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");
675 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) {
678 /* These underlying connection commands die() if they
679 * cannot connect.
680 */
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);
684 else
689 - git_tcp_connect(fd, host, flags);
685 + git_tcp_connect(fd, hostandport, flags);
686 /*
687 * Separate original protocol components prog and path
688 * from extended host header with a NUL byte.
@@ -699,54 +695,50 @@ struct child_process *git_connect(int fd[2], const char *url,
695 prog, path, 0,
696 target_host, 0);
697 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;
730 }
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;
733
743 - if (start_command(conn))
744 - die("unable to fork");
734 + if (start_command(conn))
735 + die("unable to fork");
736
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);
742 free(path);
743 return conn;
744 }