Skip to content

Commit 8e34978

Browse files
jrngitster
authored andcommitted
connect: move no_fork fallback to git_tcp_connect
git_connect has the structure struct child_process *conn = &no_fork; ... switch (protocol) { case PROTO_GIT: if (git_use_proxy(hostandport)) conn = git_proxy_connect(fd, hostandport); else git_tcp_connect(fd, hostandport, flags); ... break; case PROTO_SSH: conn = xmalloc(sizeof(*conn)); child_process_init(conn); argv_array_push(&conn->args, ssh); ... break; ... return conn; In all cases except the git_tcp_connect case, conn is explicitly assigned a value. Make the code clearer by explicitly assigning 'conn = &no_fork' in the tcp case and eliminating the default so the compiler can ensure conn is always correctly assigned. Noticed-by: Junio C Hamano <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8339805 commit 8e34978

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

connect.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,25 @@ static int git_tcp_connect_sock(char *host, int flags)
582582
#endif /* NO_IPV6 */
583583

584584

585-
static void git_tcp_connect(int fd[2], char *host, int flags)
585+
/*
586+
* Dummy child_process returned by git_connect() if the transport protocol
587+
* does not need fork(2).
588+
*/
589+
static struct child_process no_fork = CHILD_PROCESS_INIT;
590+
591+
int git_connection_is_socket(struct child_process *conn)
592+
{
593+
return conn == &no_fork;
594+
}
595+
596+
static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
586597
{
587598
int sockfd = git_tcp_connect_sock(host, flags);
588599

589600
fd[0] = sockfd;
590601
fd[1] = dup(sockfd);
602+
603+
return &no_fork;
591604
}
592605

593606

@@ -761,8 +774,6 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
761774
return protocol;
762775
}
763776

764-
static struct child_process no_fork = CHILD_PROCESS_INIT;
765-
766777
static const char *get_ssh_command(void)
767778
{
768779
const char *ssh;
@@ -851,11 +862,11 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
851862
}
852863

853864
/*
854-
* This returns a dummy child_process if the transport protocol does not
855-
* need fork(2), or a struct child_process object if it does. Once done,
856-
* finish the connection with finish_connect() with the value returned from
857-
* this function (it is safe to call finish_connect() with NULL to support
858-
* the former case).
865+
* This returns the dummy child_process `no_fork` if the transport protocol
866+
* does not need fork(2), or a struct child_process object if it does. Once
867+
* done, finish the connection with finish_connect() with the value returned
868+
* from this function (it is safe to call finish_connect() with NULL to
869+
* support the former case).
859870
*
860871
* If it returns, the connect is successful; it just dies on errors (this
861872
* will hopefully be changed in a libification effort, to return NULL when
@@ -865,7 +876,7 @@ struct child_process *git_connect(int fd[2], const char *url,
865876
const char *prog, int flags)
866877
{
867878
char *hostandport, *path;
868-
struct child_process *conn = &no_fork;
879+
struct child_process *conn;
869880
enum protocol protocol;
870881

871882
/* Without this we cannot rely on waitpid() to tell
@@ -901,7 +912,7 @@ struct child_process *git_connect(int fd[2], const char *url,
901912
if (git_use_proxy(hostandport))
902913
conn = git_proxy_connect(fd, hostandport);
903914
else
904-
git_tcp_connect(fd, hostandport, flags);
915+
conn = git_tcp_connect(fd, hostandport, flags);
905916
/*
906917
* Separate original protocol components prog and path
907918
* from extended host header with a NUL byte.
@@ -1041,11 +1052,6 @@ struct child_process *git_connect(int fd[2], const char *url,
10411052
return conn;
10421053
}
10431054

1044-
int git_connection_is_socket(struct child_process *conn)
1045-
{
1046-
return conn == &no_fork;
1047-
}
1048-
10491055
int finish_connect(struct child_process *conn)
10501056
{
10511057
int code;

0 commit comments

Comments
 (0)