6 static const char fetch_pack_usage
[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
7 static const char *exec
= "git-upload-pack";
9 static int find_common(int fd
[2], unsigned char *result_sha1
, unsigned char *remote
)
11 static char line
[1000];
12 int count
= 0, flushes
= 0, retval
;
15 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
17 die("unable to run 'git-rev-list'");
18 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
22 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
23 unsigned char sha1
[20];
24 if (get_sha1_hex(line
, sha1
))
25 die("git-fetch-pack: expected object name, got crud");
26 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
27 if (!(31 & ++count
)) {
32 * We keep one window "ahead" of the other side, and
33 * will wait for an ACK only on the next one
37 if (get_ack(fd
[0], result_sha1
)) {
46 packet_write(fd
[1], "done\n");
49 if (get_ack(fd
[0], result_sha1
))
55 static int get_remote_heads(int fd
, int nr_match
, char **match
, unsigned char *result
)
60 static char line
[1000];
61 unsigned char sha1
[20];
65 len
= packet_read_line(fd
, line
, sizeof(line
));
68 if (line
[len
-1] == '\n')
70 if (len
< 42 || get_sha1_hex(line
, sha1
))
71 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s'", line
);
73 if (nr_match
&& !path_match(refname
, nr_match
, match
))
76 memcpy(result
, sha1
, 20);
81 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
83 unsigned char sha1
[20], remote
[20];
87 heads
= get_remote_heads(fd
[0], nr_match
, match
, remote
);
90 die(heads
? "multiple remote heads" : "no matching remote head");
92 if (find_common(fd
, sha1
, remote
) < 0)
93 die("git-fetch-pack: no common commits");
96 die("git-fetch-pack: unable to fork off git-unpack-objects");
101 execlp("git-unpack-objects", "git-unpack-objects", NULL
);
102 die("git-unpack-objects exec failed");
106 while (waitpid(pid
, &status
, 0) < 0) {
108 die("waiting for git-unpack-objects: %s", strerror(errno
));
110 if (WIFEXITED(status
)) {
111 int code
= WEXITSTATUS(status
);
113 die("git-unpack-objects died with error code %d", code
);
114 puts(sha1_to_hex(remote
));
117 if (WIFSIGNALED(status
)) {
118 int sig
= WTERMSIG(status
);
119 die("git-unpack-objects died of signal %d", sig
);
121 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
124 int main(int argc
, char **argv
)
126 int i
, ret
, nr_heads
;
127 char *dest
= NULL
, **heads
;
133 for (i
= 1; i
< argc
; i
++) {
137 /* Arguments go here */
138 usage(fetch_pack_usage
);
141 heads
= argv
+ i
+ 1;
142 nr_heads
= argc
- i
- 1;
146 usage(fetch_pack_usage
);
147 pid
= git_connect(fd
, dest
, exec
);
150 ret
= fetch_pack(fd
, nr_heads
, heads
);