Change git_connect() to return a struct child_process instead of a pid_t.
This prepares the API of git_connect() and finish_connect() to operate on a struct child_process. Currently, we just use that object as a placeholder for the pid that we used to return. A follow-up patch will change the implementation of git_connect() and finish_connect() to make full use of the object. Old code had early-return-on-error checks at the calling sites of git_connect(), but since git_connect() dies on errors anyway, these checks were removed. [sp: Corrected style nit of "conn == NULL" to "!conn"] Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Johannes Sixt committed
Oct 19, 2007 at 21:47 UTC
98158e9cfd2808613f305bf587ce697762c884bb
7 files changed
+33
-43
builtin-archive.c
+3
-5
@@ -30,7 +30,7 @@ static int run_remote_archiver(const char *remote, int argc,
30
{
31
char *url, buf[LARGE_PACKET_MAX];
32
int fd[2], i, len, rv;
33
- pid_t pid;
33
+ struct child_process *conn;
34
const char *exec = "git-upload-archive";
35
int exec_at = 0;
36
@@ -46,9 +46,7 @@ static int run_remote_archiver(const char *remote, int argc,
46
}
47
48
url = xstrdup(remote);
49
- pid = git_connect(fd, url, exec, 0);
50
- if (pid < 0)
51
- return pid;
49
+ conn = git_connect(fd, url, exec, 0);
50
51
for (i = 1; i < argc; i++) {
52
if (i == exec_at)
@@ -76,7 +74,7 @@ static int run_remote_archiver(const char *remote, int argc,
74
rv = recv_sideband("archive", fd[0], 1, 2);
75
close(fd[0]);
76
close(fd[1]);
79
- rv |= finish_connect(pid);
77
+ rv |= finish_connect(conn);
78
79
return !!rv;
80
}
builtin-fetch-pack.c
+3
-5
@@ -762,7 +762,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
762
{
763
int i, ret;
764
int fd[2];
765
- pid_t pid;
765
+ struct child_process *conn;
766
struct ref *ref;
767
struct stat st;
768
@@ -773,16 +773,14 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
773
st.st_mtime = 0;
774
}
775
776
- pid = git_connect(fd, (char *)dest, uploadpack,
776
+ conn = git_connect(fd, (char *)dest, uploadpack,
777
args.verbose ? CONNECT_VERBOSE : 0);
778
- if (pid < 0)
779
- return NULL;
778
if (heads && nr_heads)
779
nr_heads = remove_duplicates(nr_heads, heads);
780
ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile);
781
close(fd[0]);
782
close(fd[1]);
785
- ret = finish_connect(pid);
783
+ ret = finish_connect(conn);
784
785
if (!ret && nr_heads) {
786
/* If the heads to pull were given, we should have
cache.h
+2
-2
@@ -503,8 +503,8 @@ struct ref {
503
#define REF_TAGS (1u << 2)
504
505
#define CONNECT_VERBOSE (1u << 0)
506
-extern pid_t git_connect(int fd[2], char *url, const char *prog, int flags);
507
-extern int finish_connect(pid_t pid);
506
+extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags);
507
+extern int finish_connect(struct child_process *conn);
508
extern int path_match(const char *path, int nr, char **match);
509
extern int get_ack(int fd, unsigned char *result_sha1);
510
extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags);
connect.c
+17
-14
@@ -468,21 +468,22 @@ char *get_port(char *host)
468
}
469
470
/*
471
- * This returns 0 if the transport protocol does not need fork(2),
472
- * or a process id if it does. Once done, finish the connection
471
+ * This returns NULL if the transport protocol does not need fork(2), or a
472
+ * struct child_process object if it does. Once done, finish the connection
473
* with finish_connect() with the value returned from this function
474
- * (it is safe to call finish_connect() with 0 to support the former
474
+ * (it is safe to call finish_connect() with NULL to support the former
475
* case).
476
*
477
- * Does not return a negative value on error; it just dies.
477
+ * If it returns, the connect is successful; it just dies on errors.
478
*/
479
-pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
479
+struct child_process *git_connect(int fd[2], char *url,
480
+ const char *prog, int flags)
481
{
482
char *host, *path = url;
483
char *end;
484
int c;
485
int pipefd[2][2];
485
- pid_t pid;
486
+ struct child_process *conn;
487
enum protocol protocol = PROTO_LOCAL;
488
int free_path = 0;
489
char *port = NULL;
@@ -568,15 +569,16 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
569
free(target_host);
570
if (free_path)
571
free(path);
571
- return 0;
572
+ return NULL;
573
}
574
575
if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
576
die("unable to create pipe pair for communication");
576
- pid = fork();
577
- if (pid < 0)
577
+ conn = xcalloc(1, sizeof(*conn));
578
+ conn->pid = fork();
579
+ if (conn->pid < 0)
580
die("unable to fork");
579
- if (!pid) {
581
+ if (!conn->pid) {
582
struct strbuf cmd;
583
584
strbuf_init(&cmd, MAX_CMD_LEN);
@@ -625,17 +627,18 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
627
close(pipefd[1][0]);
628
if (free_path)
629
free(path);
628
- return pid;
630
+ return conn;
631
}
632
631
-int finish_connect(pid_t pid)
633
+int finish_connect(struct child_process *conn)
634
{
633
- if (pid == 0)
635
+ if (!conn)
636
return 0;
637
636
- while (waitpid(pid, NULL, 0) < 0) {
638
+ while (waitpid(conn->pid, NULL, 0) < 0) {
639
if (errno != EINTR)
640
return -1;
641
}
642
+ free(conn);
643
return 0;
644
}
peek-remote.c
+3
-5
@@ -25,7 +25,7 @@ int main(int argc, char **argv)
25
int i, ret;
26
char *dest = NULL;
27
int fd[2];
28
- pid_t pid;
28
+ struct child_process *conn;
29
int nongit = 0;
30
unsigned flags = 0;
31
@@ -64,12 +64,10 @@ int main(int argc, char **argv)
64
if (!dest || i != argc - 1)
65
usage(peek_remote_usage);
66
67
- pid = git_connect(fd, dest, uploadpack, 0);
68
- if (pid < 0)
69
- return 1;
67
+ conn = git_connect(fd, dest, uploadpack, 0);
68
ret = peek_remote(fd, flags);
69
close(fd[0]);
70
close(fd[1]);
73
- ret |= finish_connect(pid);
71
+ ret |= finish_connect(conn);
72
return !!ret;
73
}
send-pack.c
+3
-5
@@ -365,7 +365,7 @@ int main(int argc, char **argv)
365
char *dest = NULL;
366
char **heads = NULL;
367
int fd[2], ret;
368
- pid_t pid;
368
+ struct child_process *conn;
369
char *remote_name = NULL;
370
struct remote *remote = NULL;
371
@@ -433,12 +433,10 @@ int main(int argc, char **argv)
433
}
434
}
435
436
- pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
437
- if (pid < 0)
438
- return 1;
436
+ conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
437
ret = send_pack(fd[0], fd[1], remote, nr_heads, heads);
438
close(fd[0]);
439
close(fd[1]);
442
- ret |= finish_connect(pid);
440
+ ret |= finish_connect(conn);
441
return !!ret;
442
}
transport.c
+2
-7
@@ -601,18 +601,13 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
601
struct git_transport_data *data = transport->data;
602
struct ref *refs;
603
int fd[2];
604
- pid_t pid;
604
char *dest = xstrdup(transport->url);
606
-
607
- pid = git_connect(fd, dest, data->uploadpack, 0);
608
-
609
- if (pid < 0)
610
- die("Failed to connect to \"%s\"", transport->url);
605
+ struct child_process *conn = git_connect(fd, dest, data->uploadpack, 0);
606
607
get_remote_heads(fd[0], &refs, 0, NULL, 0);
608
packet_flush(fd[1]);
609
615
- finish_connect(pid);
610
+ finish_connect(conn);
611
612
free(dest);
613