8 static const char fetch_pack_usage
[] =
9 "git-fetch-pack [-q] [-v] [--exec=upload-pack] [host:]directory <refs>...";
10 static const char *exec
= "git-upload-pack";
12 static int find_common(int fd
[2], unsigned char *result_sha1
,
16 static char line
[1000];
17 int count
= 0, flushes
= 0, retval
;
20 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
22 die("unable to run 'git-rev-list'");
25 for ( ; refs
; refs
= refs
->next
) {
26 unsigned char *remote
= refs
->old_sha1
;
27 unsigned char *local
= refs
->new_sha1
;
29 if (!memcmp(remote
, local
, 20))
31 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
39 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
40 unsigned char sha1
[20];
41 if (get_sha1_hex(line
, sha1
))
42 die("git-fetch-pack: expected object name, got crud");
43 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
45 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
46 if (!(31 & ++count
)) {
51 * We keep one window "ahead" of the other side, and
52 * will wait for an ACK only on the next one
56 if (get_ack(fd
[0], result_sha1
)) {
60 fprintf(stderr
, "got ack\n");
67 packet_write(fd
[1], "done\n");
69 fprintf(stderr
, "done\n");
72 if (get_ack(fd
[0], result_sha1
)) {
74 fprintf(stderr
, "got ack\n");
81 static int everything_local(struct ref
*refs
)
85 for (retval
= 1; refs
; refs
= refs
->next
) {
86 const unsigned char *remote
= refs
->old_sha1
;
87 unsigned char local
[20];
89 if (read_ref(git_path("%s", refs
->name
), local
) < 0 ||
90 memcmp(remote
, local
, 20)) {
95 "want %s (%s)\n", sha1_to_hex(remote
),
100 memcpy(refs
->new_sha1
, local
, 20);
104 "already have %s (%s)\n", sha1_to_hex(remote
),
110 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
113 unsigned char sha1
[20];
117 get_remote_heads(fd
[0], &ref
, nr_match
, match
, 1);
120 die("no matching remote head");
122 if (everything_local(ref
)) {
126 if (find_common(fd
, sha1
, ref
) < 0)
127 fprintf(stderr
, "warning: no common commits\n");
130 die("git-fetch-pack: unable to fork off git-unpack-objects");
135 execlp("git-unpack-objects", "git-unpack-objects",
136 quiet
? "-q" : NULL
, NULL
);
137 die("git-unpack-objects exec failed");
141 while (waitpid(pid
, &status
, 0) < 0) {
143 die("waiting for git-unpack-objects: %s", strerror(errno
));
145 if (WIFEXITED(status
)) {
146 int code
= WEXITSTATUS(status
);
148 die("git-unpack-objects died with error code %d", code
);
152 sha1_to_hex(ref
->old_sha1
), ref
->name
);
157 if (WIFSIGNALED(status
)) {
158 int sig
= WTERMSIG(status
);
159 die("git-unpack-objects died of signal %d", sig
);
161 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
164 int main(int argc
, char **argv
)
166 int i
, ret
, nr_heads
;
167 char *dest
= NULL
, **heads
;
173 for (i
= 1; i
< argc
; i
++) {
177 if (!strncmp("--exec=", arg
, 7)) {
181 if (!strcmp("-q", arg
)) {
185 if (!strcmp("-v", arg
)) {
189 usage(fetch_pack_usage
);
192 heads
= argv
+ i
+ 1;
193 nr_heads
= argc
- i
- 1;
197 usage(fetch_pack_usage
);
198 pid
= git_connect(fd
, dest
, exec
);
201 ret
= fetch_pack(fd
, nr_heads
, heads
);