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_old_sha1(const char *refname
, unsigned char *sha1
)
77 fd
= open(git_path("%s", refname
), O_RDONLY
);
81 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
82 ret
= get_sha1_hex(buffer
, sha1
);
88 static int check_ref(const char *refname
, const unsigned char *sha1
)
90 unsigned char mysha1
[20];
93 if (get_old_sha1(refname
, mysha1
) < 0)
94 memset(mysha1
, 0, 20);
96 if (!memcmp(sha1
, mysha1
, 20)) {
97 fprintf(stderr
, "%s: unchanged\n", refname
);
101 memcpy(oldhex
, sha1_to_hex(mysha1
), 41);
102 fprintf(stderr
, "%s: %s (%s)\n", refname
, sha1_to_hex(sha1
), oldhex
);
106 static int get_remote_heads(int fd
, int nr_match
, char **match
, unsigned char *result
)
111 static char line
[1000];
112 unsigned char sha1
[20];
116 len
= packet_read_line(fd
, line
, sizeof(line
));
119 if (line
[len
-1] == '\n')
121 if (len
< 42 || get_sha1_hex(line
, sha1
))
122 die("git-fetch-pack: protocol error - expected ref descriptor, got '%sä'", line
);
124 if (nr_match
&& !path_match(refname
, nr_match
, match
))
126 if (check_ref(refname
, sha1
)) {
128 memcpy(result
, sha1
, 20);
134 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
136 unsigned char sha1
[20], remote
[20];
140 heads
= get_remote_heads(fd
[0], nr_match
, match
, remote
);
143 die(heads
? "multiple remote heads" : "no matching remote head");
145 if (find_common(fd
, sha1
, remote
) < 0)
146 die("git-fetch-pack: no common commits");
149 die("git-fetch-pack: unable to fork off git-unpack-objects");
154 execlp("git-unpack-objects", "git-unpack-objects", NULL
);
155 die("git-unpack-objects exec failed");
159 while (waitpid(pid
, &status
, 0) < 0) {
161 die("waiting for git-unpack-objects: %s", strerror(errno
));
163 if (WIFEXITED(status
)) {
164 int code
= WEXITSTATUS(status
);
166 die("git-unpack-objects died with error code %d", code
);
167 puts(sha1_to_hex(remote
));
170 if (WIFSIGNALED(status
)) {
171 int sig
= WTERMSIG(status
);
172 die("git-unpack-objects died of signal %d", sig
);
174 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
177 int main(int argc
, char **argv
)
179 int i
, ret
, nr_heads
;
180 char *dest
= NULL
, **heads
;
186 for (i
= 1; i
< argc
; i
++) {
190 /* Arguments go here */
191 usage(fetch_pack_usage
);
194 heads
= argv
+ i
+ 1;
195 nr_heads
= argc
- i
- 1;
199 usage(fetch_pack_usage
);
200 pid
= git_connect(fd
, dest
, exec
);
203 ret
= fetch_pack(fd
, nr_heads
, heads
);