git-fetch can use both --thin and --keep with fetch-pack now
[git/jrn.git] / fetch-clone.c
blobf629d8dacc1e9c83819641fa30fb09219a260148
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "pkt-line.h"
4 #include "sideband.h"
5 #include <sys/wait.h>
7 static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
9 pid_t side_pid;
11 if (!sideband) {
12 fd[0] = xd[0];
13 fd[1] = xd[1];
14 return 0;
16 /* xd[] is talking with upload-pack; subprocess reads from
17 * xd[0], spits out band#2 to stderr, and feeds us band#1
18 * through our fd[0].
20 if (pipe(fd) < 0)
21 die("%s: unable to set up pipe", me);
22 side_pid = fork();
23 if (side_pid < 0)
24 die("%s: unable to fork off sideband demultiplexer", me);
25 if (!side_pid) {
26 /* subprocess */
27 close(fd[0]);
28 if (xd[0] != xd[1])
29 close(xd[1]);
30 if (recv_sideband(me, xd[0], fd[1], 2))
31 exit(1);
32 exit(0);
34 close(xd[0]);
35 close(fd[1]);
36 fd[1] = xd[1];
37 return side_pid;
40 static int get_pack(int xd[2], const char *me, int sideband, const char **argv)
42 int status;
43 pid_t pid, side_pid;
44 int fd[2];
46 side_pid = setup_sideband(sideband, me, fd, xd);
47 pid = fork();
48 if (pid < 0)
49 die("%s: unable to fork off %s", me, argv[0]);
50 if (!pid) {
51 dup2(fd[0], 0);
52 close(fd[0]);
53 close(fd[1]);
54 execv_git_cmd(argv);
55 die("%s exec failed", argv[0]);
57 close(fd[0]);
58 close(fd[1]);
59 while (waitpid(pid, &status, 0) < 0) {
60 if (errno != EINTR)
61 die("waiting for %s: %s", argv[0], strerror(errno));
63 if (WIFEXITED(status)) {
64 int code = WEXITSTATUS(status);
65 if (code)
66 die("%s died with error code %d", argv[0], code);
67 return 0;
69 if (WIFSIGNALED(status)) {
70 int sig = WTERMSIG(status);
71 die("%s died of signal %d", argv[0], sig);
73 die("%s died of unnatural causes %d", argv[0], status);
76 int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
78 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
79 return get_pack(xd, me, sideband, argv);
82 int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
84 const char *argv[5] = { "index-pack", "--stdin", "--fix-thin",
85 quiet ? NULL : "-v", NULL };
86 return get_pack(xd, me, sideband, argv);