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 get_ack(int fd
, unsigned char *result_sha1
)
11 static char line
[1000];
12 int len
= packet_read_line(fd
, line
, sizeof(line
));
15 die("git-fetch-pack: expected ACK/NAK, got EOF");
16 if (line
[len
-1] == '\n')
18 if (!strcmp(line
, "NAK"))
20 if (!strncmp(line
, "ACK ", 3)) {
21 if (!get_sha1_hex(line
+4, result_sha1
))
24 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
27 static int find_common(int fd
[2], unsigned char *result_sha1
, unsigned char *remote
)
29 static char line
[1000];
30 int count
= 0, flushes
= 0, retval
;
33 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
35 die("unable to run 'git-rev-list'");
36 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
40 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
41 unsigned char sha1
[20];
42 if (get_sha1_hex(line
, sha1
))
43 die("git-fetch-pack: expected object name, got crud");
44 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
45 if (!(31 & ++count
)) {
50 * We keep one window "ahead" of the other side, and
51 * will wait for an ACK only on the next one
55 if (get_ack(fd
[0], result_sha1
)) {
64 packet_write(fd
[1], "done\n");
67 if (get_ack(fd
[0], result_sha1
))
73 static int get_remote_heads(int fd
, int nr_match
, char **match
, unsigned char *result
)
78 static char line
[1000];
79 unsigned char sha1
[20];
83 len
= packet_read_line(fd
, line
, sizeof(line
));
86 if (line
[len
-1] == '\n')
88 if (len
< 42 || get_sha1_hex(line
, sha1
))
89 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s¤'", line
);
91 if (nr_match
&& !path_match(refname
, nr_match
, match
))
94 memcpy(result
, sha1
, 20);
99 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
101 unsigned char sha1
[20], remote
[20];
105 heads
= get_remote_heads(fd
[0], nr_match
, match
, remote
);
108 die(heads
? "multiple remote heads" : "no matching remote head");
110 if (find_common(fd
, sha1
, remote
) < 0)
111 die("git-fetch-pack: no common commits");
114 die("git-fetch-pack: unable to fork off git-unpack-objects");
119 execlp("git-unpack-objects", "git-unpack-objects", NULL
);
120 die("git-unpack-objects exec failed");
124 while (waitpid(pid
, &status
, 0) < 0) {
126 die("waiting for git-unpack-objects: %s", strerror(errno
));
128 if (WIFEXITED(status
)) {
129 int code
= WEXITSTATUS(status
);
131 die("git-unpack-objects died with error code %d", code
);
132 puts(sha1_to_hex(remote
));
135 if (WIFSIGNALED(status
)) {
136 int sig
= WTERMSIG(status
);
137 die("git-unpack-objects died of signal %d", sig
);
139 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
142 int main(int argc
, char **argv
)
144 int i
, ret
, nr_heads
;
145 char *dest
= NULL
, **heads
;
151 for (i
= 1; i
< argc
; i
++) {
155 /* Arguments go here */
156 usage(fetch_pack_usage
);
159 heads
= argv
+ i
+ 1;
160 nr_heads
= argc
- i
- 1;
164 usage(fetch_pack_usage
);
165 pid
= git_connect(fd
, dest
, exec
);
168 ret
= fetch_pack(fd
, nr_heads
, heads
);