4 static const char fetch_pack_usage[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
5 static const char *exec = "git-upload-pack";
7 static int get_ack(int fd, unsigned char *result_sha1)
9 static char line[1000];
10 int len = packet_read_line(fd, line, sizeof(line));
13 die("git-fetch-pack: expected ACK/NAK, got EOF");
14 if (line[len-1] == '\n')
16 if (!strcmp(line, "NAK"))
18 if (!strncmp(line, "ACK ", 3)) {
19 if (!get_sha1_hex(line+4, result_sha1))
22 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
25 static int find_common(int fd[2], unsigned char *result_sha1)
27 static char line[1000];
28 int count = 0, flushes = 0;
30 while (fgets(line, sizeof(line), stdin) != NULL) {
31 unsigned char sha1[20];
32 if (get_sha1_hex(line, sha1))
33 die("git-fetch-pack: expected object name, got crud");
34 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
35 if (!(31 & ++count)) {
40 * We keep one window "ahead" of the other side, and
41 * will wait for an ACK only on the next one
45 if (get_ack(fd[0], result_sha1))
54 if (get_ack(fd[0], result_sha1))
60 static int get_remote_heads(int fd, int nr_match, char **match)
63 static char line[1000];
64 unsigned char sha1[20];
68 len = packet_read_line(fd, line, sizeof(line));
71 if (line[len-1] == '\n')
73 if (len < 42 || get_sha1_hex(line, sha1))
74 die("git-fetch-pack: protocol error - expected ref descriptor, got '%sä'", line);
76 if (nr_match && !path_match(refname, nr_match, match))
78 printf("%s %s\n", sha1_to_hex(sha1), refname);
83 static int fetch_pack(int fd[2], int nr_match, char **match)
85 unsigned char sha1[20];
87 get_remote_heads(fd[0], nr_match, match);
88 if (find_common(fd, sha1) < 0)
89 die("git-fetch-pack: no common commits");
90 printf("common commit: %s\n", sha1_to_hex(sha1));
94 int main(int argc, char **argv)
97 char *dest = NULL, **heads;
103 for (i = 1; i < argc; i++) {
107 /* Arguments go here */
108 usage(fetch_pack_usage);
111 heads = argv + i + 1;
112 nr_heads = argc - i - 1;
116 usage(fetch_pack_usage);
117 pid = git_connect(fd, dest, exec);
120 ret = fetch_pack(fd, nr_heads, heads);