Skip to content

Commit 2ac67cb

Browse files
jrngitster
authored andcommitted
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 <[email protected]> Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e34978 commit 2ac67cb

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

connect.c

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,64 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
861861
return ssh_variant;
862862
}
863863

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+
864922
/*
865923
* This returns the dummy child_process `no_fork` if the transport protocol
866924
* 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,
892950
printf("Diag: path=%s\n", path ? path : "NULL");
893951
conn = NULL;
894952
} 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);
939954
} else {
940955
struct strbuf cmd = STRBUF_INIT;
941956
const char *const *var;

0 commit comments

Comments
 (0)