7 static const char fetch_pack_usage
[] = "git-fetch-pack [-q] [--exec=upload-pack] [host:]directory [heads]* < mycommitlist";
8 static const char *exec
= "git-upload-pack";
10 static int find_common(int fd
[2], unsigned char *result_sha1
, unsigned char *remote
)
12 static char line
[1000];
13 int count
= 0, flushes
= 0, retval
;
16 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
18 die("unable to run 'git-rev-list'");
19 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
23 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
24 unsigned char sha1
[20];
25 if (get_sha1_hex(line
, sha1
))
26 die("git-fetch-pack: expected object name, got crud");
27 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
28 if (!(31 & ++count
)) {
33 * We keep one window "ahead" of the other side, and
34 * will wait for an ACK only on the next one
38 if (get_ack(fd
[0], result_sha1
)) {
47 packet_write(fd
[1], "done\n");
50 if (get_ack(fd
[0], result_sha1
))
56 static int get_remote_heads(int fd
, int nr_match
, char **match
, unsigned char *result
)
61 static char line
[1000];
62 unsigned char sha1
[20];
66 len
= packet_read_line(fd
, line
, sizeof(line
));
69 if (line
[len
-1] == '\n')
71 if (len
< 42 || get_sha1_hex(line
, sha1
))
72 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s'", line
);
74 if (nr_match
&& !path_match(refname
, nr_match
, match
))
77 memcpy(result
, sha1
, 20);
82 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
84 unsigned char sha1
[20], remote
[20];
88 heads
= get_remote_heads(fd
[0], nr_match
, match
, remote
);
91 die(heads
? "multiple remote heads" : "no matching remote head");
93 if (find_common(fd
, sha1
, remote
) < 0)
94 die("git-fetch-pack: no common commits");
97 die("git-fetch-pack: unable to fork off git-unpack-objects");
102 execlp("git-unpack-objects", "git-unpack-objects",
103 quiet
? "-q" : NULL
, NULL
);
104 die("git-unpack-objects exec failed");
108 while (waitpid(pid
, &status
, 0) < 0) {
110 die("waiting for git-unpack-objects: %s", strerror(errno
));
112 if (WIFEXITED(status
)) {
113 int code
= WEXITSTATUS(status
);
115 die("git-unpack-objects died with error code %d", code
);
116 puts(sha1_to_hex(remote
));
119 if (WIFSIGNALED(status
)) {
120 int sig
= WTERMSIG(status
);
121 die("git-unpack-objects died of signal %d", sig
);
123 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
126 int main(int argc
, char **argv
)
128 int i
, ret
, nr_heads
;
129 char *dest
= NULL
, **heads
;
135 for (i
= 1; i
< argc
; i
++) {
139 if (!strncmp("--exec=", arg
, 7)) {
143 usage(fetch_pack_usage
);
146 heads
= argv
+ i
+ 1;
147 nr_heads
= argc
- i
- 1;
151 usage(fetch_pack_usage
);
152 pid
= git_connect(fd
, dest
, exec
);
155 ret
= fetch_pack(fd
, nr_heads
, heads
);