8 static const char fetch_pack_usage
[] =
9 "git-fetch-pack [-q] [-v] [--exec=upload-pack] [host:]directory <refs>...";
10 static const char *exec
= "git-upload-pack";
12 static int find_common(int fd
[2], unsigned char *result_sha1
,
15 static char line
[1000];
16 int count
= 0, flushes
= 0, retval
;
19 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
21 die("unable to run 'git-rev-list'");
24 unsigned char *remote
= refs
->old_sha1
;
27 "want %s (%s)\n", sha1_to_hex(remote
),
29 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
35 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
36 unsigned char sha1
[20];
37 if (get_sha1_hex(line
, sha1
))
38 die("git-fetch-pack: expected object name, got crud");
39 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
41 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
42 if (!(31 & ++count
)) {
47 * We keep one window "ahead" of the other side, and
48 * will wait for an ACK only on the next one
52 if (get_ack(fd
[0], result_sha1
)) {
56 fprintf(stderr
, "got ack\n");
63 packet_write(fd
[1], "done\n");
65 fprintf(stderr
, "done\n");
68 if (get_ack(fd
[0], result_sha1
)) {
70 fprintf(stderr
, "got ack\n");
77 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
80 unsigned char sha1
[20];
84 get_remote_heads(fd
[0], &ref
, nr_match
, match
, 1);
87 die("no matching remote head");
89 if (find_common(fd
, sha1
, ref
) < 0)
90 fprintf(stderr
, "warning: no common commits\n");
93 die("git-fetch-pack: unable to fork off git-unpack-objects");
98 execlp("git-unpack-objects", "git-unpack-objects",
99 quiet
? "-q" : NULL
, NULL
);
100 die("git-unpack-objects exec failed");
104 while (waitpid(pid
, &status
, 0) < 0) {
106 die("waiting for git-unpack-objects: %s", strerror(errno
));
108 if (WIFEXITED(status
)) {
109 int code
= WEXITSTATUS(status
);
111 die("git-unpack-objects died with error code %d", code
);
114 sha1_to_hex(ref
->old_sha1
), ref
->name
);
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 if (!strcmp("-q", arg
)) {
147 if (!strcmp("-v", arg
)) {
151 usage(fetch_pack_usage
);
154 heads
= argv
+ i
+ 1;
155 nr_heads
= argc
- i
- 1;
159 usage(fetch_pack_usage
);
160 pid
= git_connect(fd
, dest
, exec
);
163 ret
= fetch_pack(fd
, nr_heads
, heads
);