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
))
57 * Eventually we'll want to be able to fetch multiple heads.
59 * Right now we'll just require a single match.
61 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
64 unsigned char sha1
[20];
68 get_remote_heads(fd
[0], &ref
, nr_match
, match
);
71 die("no matching remote head");
75 die("multiple remote heads");
77 if (find_common(fd
, sha1
, ref
->old_sha1
) < 0)
78 die("git-fetch-pack: no common commits");
81 die("git-fetch-pack: unable to fork off git-unpack-objects");
86 execlp("git-unpack-objects", "git-unpack-objects",
87 quiet
? "-q" : NULL
, NULL
);
88 die("git-unpack-objects exec failed");
92 while (waitpid(pid
, &status
, 0) < 0) {
94 die("waiting for git-unpack-objects: %s", strerror(errno
));
96 if (WIFEXITED(status
)) {
97 int code
= WEXITSTATUS(status
);
99 die("git-unpack-objects died with error code %d", code
);
100 puts(sha1_to_hex(ref
->old_sha1
));
103 if (WIFSIGNALED(status
)) {
104 int sig
= WTERMSIG(status
);
105 die("git-unpack-objects died of signal %d", sig
);
107 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
110 int main(int argc
, char **argv
)
112 int i
, ret
, nr_heads
;
113 char *dest
= NULL
, **heads
;
119 for (i
= 1; i
< argc
; i
++) {
123 if (!strncmp("--exec=", arg
, 7)) {
127 usage(fetch_pack_usage
);
130 heads
= argv
+ i
+ 1;
131 nr_heads
= argc
- i
- 1;
135 usage(fetch_pack_usage
);
136 pid
= git_connect(fd
, dest
, exec
);
139 ret
= fetch_pack(fd
, nr_heads
, heads
);