connect: split git:// setup into a separate function

The git_connect function is growing long. Split the PROTO_GIT-specific portion to a separate function to make it easier to read. No functional change intended. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Nov 20, 2017 at 13:23 UTC 2ac67cb63b715989657cee97b3181455b1380b3f
1 file changed +59 -44
connect.c
+59 -44
@@ -861,6 +861,64 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
861 return ssh_variant;
862 }
863
864 +/*
865 + * Open a connection using Git's native protocol.
866 + *
867 + * The caller is responsible for freeing hostandport, but this function may
868 + * modify it (for example, to truncate it to remove the port part).
869 + */
870 +static struct child_process *git_connect_git(int fd[2], char *hostandport,
871 + const char *path, const char *prog,
872 + int flags)
873 +{
874 + struct child_process *conn;
875 + struct strbuf request = STRBUF_INIT;
876 + /*
877 + * Set up virtual host information based on where we will
878 + * connect, unless the user has overridden us in
879 + * the environment.
880 + */
881 + char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
882 + if (target_host)
883 + target_host = xstrdup(target_host);
884 + else
885 + target_host = xstrdup(hostandport);
886 +
887 + transport_check_allowed("git");
888 +
889 + /* These underlying connection commands die() if they
890 + * cannot connect.
891 + */
892 + if (git_use_proxy(hostandport))
893 + conn = git_proxy_connect(fd, hostandport);
894 + else
895 + conn = git_tcp_connect(fd, hostandport, flags);
896 + /*
897 + * Separate original protocol components prog and path
898 + * from extended host header with a NUL byte.
899 + *
900 + * Note: Do not add any other headers here! Doing so
901 + * will cause older git-daemon servers to crash.
902 + */
903 + strbuf_addf(&request,
904 + "%s %s%chost=%s%c",
905 + prog, path, 0,
906 + target_host, 0);
907 +
908 + /* If using a new version put that stuff here after a second null byte */
909 + if (get_protocol_version_config() > 0) {
910 + strbuf_addch(&request, '\0');
911 + strbuf_addf(&request, "version=%d%c",
912 + get_protocol_version_config(), '\0');
913 + }
914 +
915 + packet_write(fd[1], request.buf, request.len);
916 +
917 + free(target_host);
918 + strbuf_release(&request);
919 + return conn;
920 +}
921 +
922 /*
923 * This returns the dummy child_process `no_fork` if the transport protocol
924 * does not need fork(2), or a struct child_process object if it does. Once
@@ -892,50 +950,7 @@ struct child_process *git_connect(int fd[2], const char *url,
950 printf("Diag: path=%s\n", path ? path : "NULL");
951 conn = NULL;
952 } else if (protocol == PROTO_GIT) {
895 - struct strbuf request = STRBUF_INIT;
896 - /*
897 - * Set up virtual host information based on where we will
898 - * connect, unless the user has overridden us in
899 - * the environment.
900 - */
901 - char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
902 - if (target_host)
903 - target_host = xstrdup(target_host);
904 - else
905 - target_host = xstrdup(hostandport);
906 -
907 - transport_check_allowed("git");
908 -
909 - /* These underlying connection commands die() if they
910 - * cannot connect.
911 - */
912 - if (git_use_proxy(hostandport))
913 - conn = git_proxy_connect(fd, hostandport);
914 - else
915 - conn = git_tcp_connect(fd, hostandport, flags);
916 - /*
917 - * Separate original protocol components prog and path
918 - * from extended host header with a NUL byte.
919 - *
920 - * Note: Do not add any other headers here! Doing so
921 - * will cause older git-daemon servers to crash.
922 - */
923 - strbuf_addf(&request,
924 - "%s %s%chost=%s%c",
925 - prog, path, 0,
926 - target_host, 0);
927 -
928 - /* If using a new version put that stuff here after a second null byte */
929 - if (get_protocol_version_config() > 0) {
930 - strbuf_addch(&request, '\0');
931 - strbuf_addf(&request, "version=%d%c",
932 - get_protocol_version_config(), '\0');
933 - }
934 -
935 - packet_write(fd[1], request.buf, request.len);
936 -
937 - free(target_host);
938 - strbuf_release(&request);
953 + conn = git_connect_git(fd, hostandport, path, prog, flags);
954 } else {
955 struct strbuf cmd = STRBUF_INIT;
956 const char *const *var;