]>
Commit | Line | Data |
---|---|---|
def88e9a | 1 | #include "cache.h" |
fb9040cc | 2 | #include "refs.h" |
def88e9a | 3 | #include "pkt-line.h" |
49bb805e JH |
4 | #include "commit.h" |
5 | #include "tag.h" | |
da093d37 | 6 | #include "exec_cmd.h" |
9e10fd1a | 7 | #include "pack.h" |
da093d37 | 8 | #include "sideband.h" |
2d4177c0 | 9 | #include "fetch-pack.h" |
ba227857 | 10 | #include "remote.h" |
477822c3 | 11 | #include "run-command.h" |
def88e9a | 12 | |
e28714c5 JH |
13 | static int transfer_unpack_limit = -1; |
14 | static int fetch_unpack_limit = -1; | |
af7cf268 | 15 | static int unpack_limit = 100; |
f04833ef | 16 | static int prefer_ofs_delta = 1; |
761ecf0b | 17 | static int no_done = 0; |
09149c78 DB |
18 | static struct fetch_pack_args args = { |
19 | /* .uploadpack = */ "git-upload-pack", | |
20 | }; | |
fa740529 | 21 | |
33b83034 | 22 | static const char fetch_pack_usage[] = |
1b1dd23f | 23 | "git fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]"; |
def88e9a | 24 | |
0a8944dd | 25 | #define COMPLETE (1U << 0) |
23d61f83 JS |
26 | #define COMMON (1U << 1) |
27 | #define COMMON_REF (1U << 2) | |
28 | #define SEEN (1U << 3) | |
29 | #define POPPED (1U << 4) | |
30 | ||
420e9af4 DB |
31 | static int marked; |
32 | ||
f061e5fd JH |
33 | /* |
34 | * After sending this many "have"s if we do not get any new ACK , we | |
35 | * give up traversing our history. | |
36 | */ | |
37 | #define MAX_IN_VAIN 256 | |
38 | ||
96f1e58f | 39 | static struct commit_list *rev_list; |
3891f390 | 40 | static int non_common_revs, multi_ack, use_sideband; |
23d61f83 JS |
41 | |
42 | static void rev_list_push(struct commit *commit, int mark) | |
43 | { | |
44 | if (!(commit->object.flags & mark)) { | |
45 | commit->object.flags |= mark; | |
46 | ||
47 | if (!(commit->object.parsed)) | |
f3ec5494 MK |
48 | if (parse_commit(commit)) |
49 | return; | |
23d61f83 | 50 | |
47e44ed1 | 51 | commit_list_insert_by_date(commit, &rev_list); |
23d61f83 JS |
52 | |
53 | if (!(commit->object.flags & COMMON)) | |
54 | non_common_revs++; | |
55 | } | |
56 | } | |
57 | ||
8da19775 | 58 | static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
23d61f83 | 59 | { |
9534f40b | 60 | struct object *o = deref_tag(parse_object(sha1), path, 0); |
23d61f83 | 61 | |
1974632c | 62 | if (o && o->type == OBJ_COMMIT) |
23d61f83 JS |
63 | rev_list_push((struct commit *)o, SEEN); |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
420e9af4 DB |
68 | static int clear_marks(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
69 | { | |
70 | struct object *o = deref_tag(parse_object(sha1), path, 0); | |
71 | ||
72 | if (o && o->type == OBJ_COMMIT) | |
73 | clear_commit_marks((struct commit *)o, | |
74 | COMMON | COMMON_REF | SEEN | POPPED); | |
75 | return 0; | |
76 | } | |
77 | ||
23d61f83 JS |
78 | /* |
79 | This function marks a rev and its ancestors as common. | |
80 | In some cases, it is desirable to mark only the ancestors (for example | |
81 | when only the server does not yet know that they are common). | |
82 | */ | |
83 | ||
84 | static void mark_common(struct commit *commit, | |
85 | int ancestors_only, int dont_parse) | |
86 | { | |
87 | if (commit != NULL && !(commit->object.flags & COMMON)) { | |
88 | struct object *o = (struct object *)commit; | |
89 | ||
90 | if (!ancestors_only) | |
91 | o->flags |= COMMON; | |
92 | ||
93 | if (!(o->flags & SEEN)) | |
94 | rev_list_push(commit, SEEN); | |
95 | else { | |
96 | struct commit_list *parents; | |
97 | ||
98 | if (!ancestors_only && !(o->flags & POPPED)) | |
99 | non_common_revs--; | |
100 | if (!o->parsed && !dont_parse) | |
f3ec5494 MK |
101 | if (parse_commit(commit)) |
102 | return; | |
23d61f83 JS |
103 | |
104 | for (parents = commit->parents; | |
105 | parents; | |
106 | parents = parents->next) | |
107 | mark_common(parents->item, 0, dont_parse); | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | /* | |
113 | Get the next rev to send, ignoring the common. | |
114 | */ | |
115 | ||
4b25d091 | 116 | static const unsigned char *get_rev(void) |
23d61f83 JS |
117 | { |
118 | struct commit *commit = NULL; | |
119 | ||
120 | while (commit == NULL) { | |
121 | unsigned int mark; | |
72269ad9 | 122 | struct commit_list *parents; |
23d61f83 JS |
123 | |
124 | if (rev_list == NULL || non_common_revs == 0) | |
125 | return NULL; | |
126 | ||
127 | commit = rev_list->item; | |
2d8bed96 | 128 | if (!commit->object.parsed) |
72269ad9 LT |
129 | parse_commit(commit); |
130 | parents = commit->parents; | |
f3ec5494 | 131 | |
23d61f83 JS |
132 | commit->object.flags |= POPPED; |
133 | if (!(commit->object.flags & COMMON)) | |
134 | non_common_revs--; | |
a6080a0a | 135 | |
23d61f83 JS |
136 | if (commit->object.flags & COMMON) { |
137 | /* do not send "have", and ignore ancestors */ | |
138 | commit = NULL; | |
139 | mark = COMMON | SEEN; | |
140 | } else if (commit->object.flags & COMMON_REF) | |
141 | /* send "have", and ignore ancestors */ | |
142 | mark = COMMON | SEEN; | |
143 | else | |
144 | /* send "have", also for its ancestors */ | |
145 | mark = SEEN; | |
146 | ||
147 | while (parents) { | |
148 | if (!(parents->item->object.flags & SEEN)) | |
149 | rev_list_push(parents->item, mark); | |
150 | if (mark & COMMON) | |
151 | mark_common(parents->item, 1, 0); | |
152 | parents = parents->next; | |
153 | } | |
154 | ||
155 | rev_list = rev_list->next; | |
156 | } | |
157 | ||
158 | return commit->object.sha1; | |
159 | } | |
0a8944dd | 160 | |
78affc49 SP |
161 | enum ack_type { |
162 | NAK = 0, | |
163 | ACK, | |
164 | ACK_continue, | |
165 | ACK_common, | |
166 | ACK_ready | |
167 | }; | |
168 | ||
249b2004 SP |
169 | static void consume_shallow_list(int fd) |
170 | { | |
171 | if (args.stateless_rpc && args.depth > 0) { | |
172 | /* If we sent a depth we will get back "duplicate" | |
173 | * shallow and unshallow commands every time there | |
174 | * is a block of have lines exchanged. | |
175 | */ | |
176 | char line[1000]; | |
177 | while (packet_read_line(fd, line, sizeof(line))) { | |
178 | if (!prefixcmp(line, "shallow ")) | |
179 | continue; | |
180 | if (!prefixcmp(line, "unshallow ")) | |
181 | continue; | |
182 | die("git fetch-pack: expected shallow list"); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
78affc49 | 187 | static enum ack_type get_ack(int fd, unsigned char *result_sha1) |
28754ab5 SP |
188 | { |
189 | static char line[1000]; | |
190 | int len = packet_read_line(fd, line, sizeof(line)); | |
191 | ||
192 | if (!len) | |
193 | die("git fetch-pack: expected ACK/NAK, got EOF"); | |
194 | if (line[len-1] == '\n') | |
195 | line[--len] = 0; | |
196 | if (!strcmp(line, "NAK")) | |
78affc49 | 197 | return NAK; |
28754ab5 SP |
198 | if (!prefixcmp(line, "ACK ")) { |
199 | if (!get_sha1_hex(line+4, result_sha1)) { | |
200 | if (strstr(line+45, "continue")) | |
78affc49 SP |
201 | return ACK_continue; |
202 | if (strstr(line+45, "common")) | |
203 | return ACK_common; | |
204 | if (strstr(line+45, "ready")) | |
205 | return ACK_ready; | |
206 | return ACK; | |
28754ab5 SP |
207 | } |
208 | } | |
209 | die("git fetch_pack: expected ACK/NAK, got '%s'", line); | |
210 | } | |
211 | ||
249b2004 SP |
212 | static void send_request(int fd, struct strbuf *buf) |
213 | { | |
214 | if (args.stateless_rpc) { | |
215 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); | |
216 | packet_flush(fd); | |
217 | } else | |
218 | safe_write(fd, buf->buf, buf->len); | |
219 | } | |
220 | ||
33b83034 JH |
221 | static int find_common(int fd[2], unsigned char *result_sha1, |
222 | struct ref *refs) | |
def88e9a | 223 | { |
2759cbc7 | 224 | int fetching; |
23d61f83 JS |
225 | int count = 0, flushes = 0, retval; |
226 | const unsigned char *sha1; | |
f061e5fd JH |
227 | unsigned in_vain = 0; |
228 | int got_continue = 0; | |
761ecf0b | 229 | int got_ready = 0; |
edace6f0 | 230 | struct strbuf req_buf = STRBUF_INIT; |
249b2004 | 231 | size_t state_len = 0; |
23d61f83 | 232 | |
249b2004 SP |
233 | if (args.stateless_rpc && multi_ack == 1) |
234 | die("--stateless-rpc requires multi_ack_detailed"); | |
420e9af4 DB |
235 | if (marked) |
236 | for_each_ref(clear_marks, NULL); | |
237 | marked = 1; | |
238 | ||
cb5d709f | 239 | for_each_ref(rev_list_insert_ref, NULL); |
def88e9a | 240 | |
2759cbc7 LT |
241 | fetching = 0; |
242 | for ( ; refs ; refs = refs->next) { | |
33b83034 | 243 | unsigned char *remote = refs->old_sha1; |
edace6f0 | 244 | const char *remote_hex; |
4dab94d5 | 245 | struct object *o; |
2759cbc7 | 246 | |
0a8944dd | 247 | /* |
4dab94d5 JH |
248 | * If that object is complete (i.e. it is an ancestor of a |
249 | * local ref), we tell them we have it but do not have to | |
250 | * tell them about its ancestors, which they already know | |
251 | * about. | |
f1f0a2be JH |
252 | * |
253 | * We use lookup_object here because we are only | |
254 | * interested in the case we *know* the object is | |
255 | * reachable and we have already scanned it. | |
4dab94d5 | 256 | */ |
f1f0a2be | 257 | if (((o = lookup_object(remote)) != NULL) && |
1baaae5e | 258 | (o->flags & COMPLETE)) { |
2759cbc7 | 259 | continue; |
0a8944dd | 260 | } |
23d61f83 | 261 | |
edace6f0 SP |
262 | remote_hex = sha1_to_hex(remote); |
263 | if (!fetching) { | |
264 | struct strbuf c = STRBUF_INIT; | |
78affc49 SP |
265 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); |
266 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); | |
761ecf0b | 267 | if (no_done) strbuf_addstr(&c, " no-done"); |
edace6f0 SP |
268 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); |
269 | if (use_sideband == 1) strbuf_addstr(&c, " side-band"); | |
270 | if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack"); | |
271 | if (args.no_progress) strbuf_addstr(&c, " no-progress"); | |
272 | if (args.include_tag) strbuf_addstr(&c, " include-tag"); | |
273 | if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); | |
274 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); | |
275 | strbuf_release(&c); | |
276 | } else | |
277 | packet_buf_write(&req_buf, "want %s\n", remote_hex); | |
2759cbc7 | 278 | fetching++; |
33b83034 | 279 | } |
edace6f0 SP |
280 | |
281 | if (!fetching) { | |
282 | strbuf_release(&req_buf); | |
283 | packet_flush(fd[1]); | |
284 | return 1; | |
285 | } | |
286 | ||
ed09aef0 | 287 | if (is_repository_shallow()) |
edace6f0 | 288 | write_shallow_commits(&req_buf, 1); |
fa740529 | 289 | if (args.depth > 0) |
edace6f0 SP |
290 | packet_buf_write(&req_buf, "deepen %d", args.depth); |
291 | packet_buf_flush(&req_buf); | |
249b2004 | 292 | state_len = req_buf.len; |
0a8944dd | 293 | |
fa740529 | 294 | if (args.depth > 0) { |
016e6ccb JS |
295 | char line[1024]; |
296 | unsigned char sha1[20]; | |
016e6ccb | 297 | |
249b2004 | 298 | send_request(fd[1], &req_buf); |
eb3a9dd3 | 299 | while (packet_read_line(fd[0], line, sizeof(line))) { |
599065a3 | 300 | if (!prefixcmp(line, "shallow ")) { |
016e6ccb JS |
301 | if (get_sha1_hex(line + 8, sha1)) |
302 | die("invalid shallow line: %s", line); | |
016e6ccb | 303 | register_shallow(sha1); |
cf01bd52 JH |
304 | continue; |
305 | } | |
599065a3 | 306 | if (!prefixcmp(line, "unshallow ")) { |
f53514bc JS |
307 | if (get_sha1_hex(line + 10, sha1)) |
308 | die("invalid unshallow line: %s", line); | |
309 | if (!lookup_object(sha1)) | |
310 | die("object not found: %s", line); | |
311 | /* make sure that it is parsed as shallow */ | |
f3ec5494 MK |
312 | if (!parse_object(sha1)) |
313 | die("error in object: %s", line); | |
f53514bc JS |
314 | if (unregister_shallow(sha1)) |
315 | die("no shallow found: %s", line); | |
cf01bd52 JH |
316 | continue; |
317 | } | |
318 | die("expected shallow/unshallow, got %s", line); | |
016e6ccb | 319 | } |
249b2004 SP |
320 | } else if (!args.stateless_rpc) |
321 | send_request(fd[1], &req_buf); | |
322 | ||
323 | if (!args.stateless_rpc) { | |
324 | /* If we aren't using the stateless-rpc interface | |
325 | * we don't need to retain the headers. | |
326 | */ | |
327 | strbuf_setlen(&req_buf, 0); | |
328 | state_len = 0; | |
016e6ccb JS |
329 | } |
330 | ||
23d61f83 | 331 | flushes = 0; |
75bfc6c2 | 332 | retval = -1; |
23d61f83 | 333 | while ((sha1 = get_rev())) { |
249b2004 | 334 | packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1)); |
fa740529 | 335 | if (args.verbose) |
33b83034 | 336 | fprintf(stderr, "have %s\n", sha1_to_hex(sha1)); |
f061e5fd | 337 | in_vain++; |
def88e9a | 338 | if (!(31 & ++count)) { |
c4c86f07 JS |
339 | int ack; |
340 | ||
249b2004 SP |
341 | packet_buf_flush(&req_buf); |
342 | send_request(fd[1], &req_buf); | |
343 | strbuf_setlen(&req_buf, state_len); | |
def88e9a LT |
344 | flushes++; |
345 | ||
346 | /* | |
347 | * We keep one window "ahead" of the other side, and | |
348 | * will wait for an ACK only on the next one | |
349 | */ | |
249b2004 | 350 | if (!args.stateless_rpc && count == 32) |
def88e9a | 351 | continue; |
c4c86f07 | 352 | |
249b2004 | 353 | consume_shallow_list(fd[0]); |
c4c86f07 JS |
354 | do { |
355 | ack = get_ack(fd[0], result_sha1); | |
fa740529 | 356 | if (args.verbose && ack) |
c4c86f07 JS |
357 | fprintf(stderr, "got ack %d %s\n", ack, |
358 | sha1_to_hex(result_sha1)); | |
78affc49 SP |
359 | switch (ack) { |
360 | case ACK: | |
c4c86f07 JS |
361 | flushes = 0; |
362 | multi_ack = 0; | |
363 | retval = 0; | |
364 | goto done; | |
78affc49 SP |
365 | case ACK_common: |
366 | case ACK_ready: | |
367 | case ACK_continue: { | |
c4c86f07 JS |
368 | struct commit *commit = |
369 | lookup_commit(result_sha1); | |
249b2004 SP |
370 | if (args.stateless_rpc |
371 | && ack == ACK_common | |
372 | && !(commit->object.flags & COMMON)) { | |
373 | /* We need to replay the have for this object | |
374 | * on the next RPC request so the peer knows | |
375 | * it is in common with us. | |
376 | */ | |
377 | const char *hex = sha1_to_hex(result_sha1); | |
378 | packet_buf_write(&req_buf, "have %s\n", hex); | |
379 | state_len = req_buf.len; | |
380 | } | |
c4c86f07 JS |
381 | mark_common(commit, 0, 1); |
382 | retval = 0; | |
f061e5fd JH |
383 | in_vain = 0; |
384 | got_continue = 1; | |
761ecf0b | 385 | if (ack == ACK_ready) { |
f2cba929 | 386 | rev_list = NULL; |
761ecf0b SP |
387 | got_ready = 1; |
388 | } | |
78affc49 SP |
389 | break; |
390 | } | |
c4c86f07 JS |
391 | } |
392 | } while (ack); | |
def88e9a | 393 | flushes--; |
f061e5fd | 394 | if (got_continue && MAX_IN_VAIN < in_vain) { |
fa740529 | 395 | if (args.verbose) |
f061e5fd JH |
396 | fprintf(stderr, "giving up\n"); |
397 | break; /* give up */ | |
398 | } | |
def88e9a LT |
399 | } |
400 | } | |
c4c86f07 | 401 | done: |
761ecf0b SP |
402 | if (!got_ready || !no_done) { |
403 | packet_buf_write(&req_buf, "done\n"); | |
404 | send_request(fd[1], &req_buf); | |
405 | } | |
fa740529 | 406 | if (args.verbose) |
33b83034 | 407 | fprintf(stderr, "done\n"); |
c4c86f07 JS |
408 | if (retval != 0) { |
409 | multi_ack = 0; | |
23d61f83 | 410 | flushes++; |
c4c86f07 | 411 | } |
edace6f0 SP |
412 | strbuf_release(&req_buf); |
413 | ||
249b2004 | 414 | consume_shallow_list(fd[0]); |
c4c86f07 JS |
415 | while (flushes || multi_ack) { |
416 | int ack = get_ack(fd[0], result_sha1); | |
417 | if (ack) { | |
fa740529 | 418 | if (args.verbose) |
c4c86f07 JS |
419 | fprintf(stderr, "got ack (%d) %s\n", ack, |
420 | sha1_to_hex(result_sha1)); | |
78affc49 | 421 | if (ack == ACK) |
c4c86f07 JS |
422 | return 0; |
423 | multi_ack = 1; | |
424 | continue; | |
33b83034 | 425 | } |
c4c86f07 | 426 | flushes--; |
def88e9a | 427 | } |
8cb560fc JS |
428 | /* it is no error to fetch into a completely empty repo */ |
429 | return count ? retval : 0; | |
def88e9a LT |
430 | } |
431 | ||
96f1e58f | 432 | static struct commit_list *complete; |
49bb805e | 433 | |
8da19775 | 434 | static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
49bb805e JH |
435 | { |
436 | struct object *o = parse_object(sha1); | |
437 | ||
1974632c | 438 | while (o && o->type == OBJ_TAG) { |
f1f0a2be JH |
439 | struct tag *t = (struct tag *) o; |
440 | if (!t->tagged) | |
441 | break; /* broken repository */ | |
49bb805e | 442 | o->flags |= COMPLETE; |
f1f0a2be | 443 | o = parse_object(t->tagged->sha1); |
49bb805e | 444 | } |
1974632c | 445 | if (o && o->type == OBJ_COMMIT) { |
49bb805e JH |
446 | struct commit *commit = (struct commit *)o; |
447 | commit->object.flags |= COMPLETE; | |
47e44ed1 | 448 | commit_list_insert_by_date(commit, &complete); |
49bb805e JH |
449 | } |
450 | return 0; | |
451 | } | |
452 | ||
453 | static void mark_recent_complete_commits(unsigned long cutoff) | |
454 | { | |
455 | while (complete && cutoff <= complete->item->date) { | |
fa740529 | 456 | if (args.verbose) |
49bb805e JH |
457 | fprintf(stderr, "Marking %s as complete\n", |
458 | sha1_to_hex(complete->item->object.sha1)); | |
459 | pop_most_recent_commit(&complete, COMPLETE); | |
460 | } | |
461 | } | |
462 | ||
1baaae5e JS |
463 | static void filter_refs(struct ref **refs, int nr_match, char **match) |
464 | { | |
9546010b JH |
465 | struct ref **return_refs; |
466 | struct ref *newlist = NULL; | |
467 | struct ref **newtail = &newlist; | |
468 | struct ref *ref, *next; | |
469 | struct ref *fastarray[32]; | |
470 | ||
fa740529 | 471 | if (nr_match && !args.fetch_all) { |
9546010b JH |
472 | if (ARRAY_SIZE(fastarray) < nr_match) |
473 | return_refs = xcalloc(nr_match, sizeof(struct ref *)); | |
474 | else { | |
475 | return_refs = fastarray; | |
476 | memset(return_refs, 0, sizeof(struct ref *) * nr_match); | |
477 | } | |
478 | } | |
479 | else | |
480 | return_refs = NULL; | |
481 | ||
482 | for (ref = *refs; ref; ref = next) { | |
483 | next = ref->next; | |
484 | if (!memcmp(ref->name, "refs/", 5) && | |
485 | check_ref_format(ref->name + 5)) | |
486 | ; /* trash */ | |
fa740529 SP |
487 | else if (args.fetch_all && |
488 | (!args.depth || prefixcmp(ref->name, "refs/tags/") )) { | |
9546010b JH |
489 | *newtail = ref; |
490 | ref->next = NULL; | |
491 | newtail = &ref->next; | |
492 | continue; | |
493 | } | |
494 | else { | |
495 | int order = path_match(ref->name, nr_match, match); | |
496 | if (order) { | |
497 | return_refs[order-1] = ref; | |
498 | continue; /* we will link it later */ | |
499 | } | |
500 | } | |
501 | free(ref); | |
502 | } | |
503 | ||
fa740529 | 504 | if (!args.fetch_all) { |
9546010b JH |
505 | int i; |
506 | for (i = 0; i < nr_match; i++) { | |
507 | ref = return_refs[i]; | |
508 | if (ref) { | |
509 | *newtail = ref; | |
510 | ref->next = NULL; | |
511 | newtail = &ref->next; | |
512 | } | |
513 | } | |
514 | if (return_refs != fastarray) | |
515 | free(return_refs); | |
1baaae5e | 516 | } |
9546010b | 517 | *refs = newlist; |
1baaae5e JS |
518 | } |
519 | ||
520 | static int everything_local(struct ref **refs, int nr_match, char **match) | |
2759cbc7 | 521 | { |
49bb805e | 522 | struct ref *ref; |
2759cbc7 | 523 | int retval; |
49bb805e JH |
524 | unsigned long cutoff = 0; |
525 | ||
49bb805e JH |
526 | save_commit_buffer = 0; |
527 | ||
1baaae5e | 528 | for (ref = *refs; ref; ref = ref->next) { |
49bb805e JH |
529 | struct object *o; |
530 | ||
531 | o = parse_object(ref->old_sha1); | |
532 | if (!o) | |
533 | continue; | |
534 | ||
535 | /* We already have it -- which may mean that we were | |
536 | * in sync with the other side at some time after | |
537 | * that (it is OK if we guess wrong here). | |
538 | */ | |
1974632c | 539 | if (o->type == OBJ_COMMIT) { |
49bb805e JH |
540 | struct commit *commit = (struct commit *)o; |
541 | if (!cutoff || cutoff < commit->date) | |
542 | cutoff = commit->date; | |
543 | } | |
544 | } | |
545 | ||
fa740529 | 546 | if (!args.depth) { |
f53514bc JS |
547 | for_each_ref(mark_complete, NULL); |
548 | if (cutoff) | |
549 | mark_recent_complete_commits(cutoff); | |
550 | } | |
2759cbc7 | 551 | |
1baaae5e JS |
552 | /* |
553 | * Mark all complete remote refs as common refs. | |
554 | * Don't mark them common yet; the server has to be told so first. | |
555 | */ | |
556 | for (ref = *refs; ref; ref = ref->next) { | |
9534f40b JH |
557 | struct object *o = deref_tag(lookup_object(ref->old_sha1), |
558 | NULL, 0); | |
1baaae5e | 559 | |
1974632c | 560 | if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE)) |
1baaae5e JS |
561 | continue; |
562 | ||
563 | if (!(o->flags & SEEN)) { | |
564 | rev_list_push((struct commit *)o, COMMON_REF | SEEN); | |
565 | ||
566 | mark_common((struct commit *)o, 1, 1); | |
567 | } | |
568 | } | |
569 | ||
570 | filter_refs(refs, nr_match, match); | |
571 | ||
572 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { | |
573 | const unsigned char *remote = ref->old_sha1; | |
2759cbc7 | 574 | unsigned char local[20]; |
49bb805e | 575 | struct object *o; |
2759cbc7 | 576 | |
1baaae5e | 577 | o = lookup_object(remote); |
49bb805e | 578 | if (!o || !(o->flags & COMPLETE)) { |
2759cbc7 | 579 | retval = 0; |
fa740529 | 580 | if (!args.verbose) |
2759cbc7 LT |
581 | continue; |
582 | fprintf(stderr, | |
583 | "want %s (%s)\n", sha1_to_hex(remote), | |
1baaae5e | 584 | ref->name); |
2759cbc7 LT |
585 | continue; |
586 | } | |
587 | ||
e702496e | 588 | hashcpy(ref->new_sha1, local); |
fa740529 | 589 | if (!args.verbose) |
2759cbc7 LT |
590 | continue; |
591 | fprintf(stderr, | |
592 | "already have %s (%s)\n", sha1_to_hex(remote), | |
1baaae5e | 593 | ref->name); |
2759cbc7 LT |
594 | } |
595 | return retval; | |
596 | } | |
597 | ||
ae6a5609 | 598 | static int sideband_demux(int in, int out, void *data) |
da093d37 | 599 | { |
088fab5f | 600 | int *xd = data; |
da093d37 | 601 | |
ae6a5609 EFL |
602 | int ret = recv_sideband("fetch-pack", xd[0], out); |
603 | close(out); | |
3ef67cf2 | 604 | return ret; |
088fab5f JS |
605 | } |
606 | ||
1788c39c | 607 | static int get_pack(int xd[2], char **pack_lockfile) |
da093d37 | 608 | { |
088fab5f | 609 | struct async demux; |
9e10fd1a JH |
610 | const char *argv[20]; |
611 | char keep_arg[256]; | |
612 | char hdr_arg[256]; | |
613 | const char **av; | |
fa740529 | 614 | int do_keep = args.keep_pack; |
477822c3 | 615 | struct child_process cmd; |
da093d37 | 616 | |
1f759eee JS |
617 | memset(&demux, 0, sizeof(demux)); |
618 | if (use_sideband) { | |
619 | /* xd[] is talking with upload-pack; subprocess reads from | |
620 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
621 | * through demux->out. | |
622 | */ | |
623 | demux.proc = sideband_demux; | |
624 | demux.data = xd; | |
ae6a5609 | 625 | demux.out = -1; |
1f759eee JS |
626 | if (start_async(&demux)) |
627 | die("fetch-pack: unable to fork off sideband" | |
628 | " demultiplexer"); | |
629 | } | |
630 | else | |
631 | demux.out = xd[0]; | |
9e10fd1a | 632 | |
477822c3 JS |
633 | memset(&cmd, 0, sizeof(cmd)); |
634 | cmd.argv = argv; | |
9e10fd1a JH |
635 | av = argv; |
636 | *hdr_arg = 0; | |
fa740529 | 637 | if (!args.keep_pack && unpack_limit) { |
9e10fd1a JH |
638 | struct pack_header header; |
639 | ||
1f759eee | 640 | if (read_pack_header(demux.out, &header)) |
9e10fd1a | 641 | die("protocol error: bad pack header"); |
6e1c2344 RJ |
642 | snprintf(hdr_arg, sizeof(hdr_arg), |
643 | "--pack_header=%"PRIu32",%"PRIu32, | |
9e10fd1a | 644 | ntohl(header.hdr_version), ntohl(header.hdr_entries)); |
af7cf268 | 645 | if (ntohl(header.hdr_entries) < unpack_limit) |
9e10fd1a JH |
646 | do_keep = 0; |
647 | else | |
648 | do_keep = 1; | |
649 | } | |
650 | ||
651 | if (do_keep) { | |
477822c3 JS |
652 | if (pack_lockfile) |
653 | cmd.out = -1; | |
9e10fd1a JH |
654 | *av++ = "index-pack"; |
655 | *av++ = "--stdin"; | |
fa740529 | 656 | if (!args.quiet && !args.no_progress) |
9e10fd1a | 657 | *av++ = "-v"; |
fa740529 | 658 | if (args.use_thin_pack) |
9e10fd1a | 659 | *av++ = "--fix-thin"; |
fa740529 | 660 | if (args.lock_pack || unpack_limit) { |
9e10fd1a | 661 | int s = sprintf(keep_arg, |
85e72830 | 662 | "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid()); |
9e10fd1a JH |
663 | if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) |
664 | strcpy(keep_arg + s, "localhost"); | |
665 | *av++ = keep_arg; | |
666 | } | |
667 | } | |
668 | else { | |
669 | *av++ = "unpack-objects"; | |
fa740529 | 670 | if (args.quiet) |
9e10fd1a JH |
671 | *av++ = "-q"; |
672 | } | |
673 | if (*hdr_arg) | |
674 | *av++ = hdr_arg; | |
675 | *av++ = NULL; | |
676 | ||
1f759eee | 677 | cmd.in = demux.out; |
477822c3 JS |
678 | cmd.git_cmd = 1; |
679 | if (start_command(&cmd)) | |
da093d37 | 680 | die("fetch-pack: unable to fork off %s", argv[0]); |
e72ae288 | 681 | if (do_keep && pack_lockfile) { |
477822c3 | 682 | *pack_lockfile = index_pack_lockfile(cmd.out); |
e72ae288 JS |
683 | close(cmd.out); |
684 | } | |
477822c3 JS |
685 | |
686 | if (finish_command(&cmd)) | |
687 | die("%s failed", argv[0]); | |
088fab5f JS |
688 | if (use_sideband && finish_async(&demux)) |
689 | die("error in sideband demultiplexer"); | |
477822c3 | 690 | return 0; |
da093d37 NP |
691 | } |
692 | ||
1788c39c | 693 | static struct ref *do_fetch_pack(int fd[2], |
ba227857 | 694 | const struct ref *orig_ref, |
1788c39c SP |
695 | int nr_match, |
696 | char **match, | |
697 | char **pack_lockfile) | |
def88e9a | 698 | { |
ba227857 | 699 | struct ref *ref = copy_ref_list(orig_ref); |
d1c133f5 | 700 | unsigned char sha1[20]; |
def88e9a | 701 | |
ed09aef0 JS |
702 | if (is_repository_shallow() && !server_supports("shallow")) |
703 | die("Server does not support shallow clients"); | |
78affc49 SP |
704 | if (server_supports("multi_ack_detailed")) { |
705 | if (args.verbose) | |
706 | fprintf(stderr, "Server supports multi_ack_detailed\n"); | |
707 | multi_ack = 2; | |
761ecf0b SP |
708 | if (server_supports("no-done")) { |
709 | if (args.verbose) | |
710 | fprintf(stderr, "Server supports no-done\n"); | |
711 | no_done = 1; | |
712 | } | |
78affc49 SP |
713 | } |
714 | else if (server_supports("multi_ack")) { | |
fa740529 | 715 | if (args.verbose) |
c4c86f07 JS |
716 | fprintf(stderr, "Server supports multi_ack\n"); |
717 | multi_ack = 1; | |
718 | } | |
d47f3db7 | 719 | if (server_supports("side-band-64k")) { |
fa740529 | 720 | if (args.verbose) |
d47f3db7 JH |
721 | fprintf(stderr, "Server supports side-band-64k\n"); |
722 | use_sideband = 2; | |
723 | } | |
724 | else if (server_supports("side-band")) { | |
fa740529 | 725 | if (args.verbose) |
583b7ea3 JH |
726 | fprintf(stderr, "Server supports side-band\n"); |
727 | use_sideband = 1; | |
728 | } | |
f04833ef NP |
729 | if (server_supports("ofs-delta")) { |
730 | if (args.verbose) | |
731 | fprintf(stderr, "Server supports ofs-delta\n"); | |
732 | } else | |
733 | prefer_ofs_delta = 0; | |
1baaae5e | 734 | if (everything_local(&ref, nr_match, match)) { |
2759cbc7 LT |
735 | packet_flush(fd[1]); |
736 | goto all_done; | |
737 | } | |
33b83034 | 738 | if (find_common(fd, sha1, ref) < 0) |
fa740529 | 739 | if (!args.keep_pack) |
dfeff66e JH |
740 | /* When cloning, it is not unusual to have |
741 | * no common commit. | |
742 | */ | |
78509d21 | 743 | warning("no common commits"); |
ad897215 | 744 | |
249b2004 SP |
745 | if (args.stateless_rpc) |
746 | packet_flush(fd[1]); | |
1788c39c | 747 | if (get_pack(fd, pack_lockfile)) |
7e44c935 | 748 | die("git fetch-pack: fetch failed."); |
ad897215 JH |
749 | |
750 | all_done: | |
2d4177c0 | 751 | return ref; |
def88e9a LT |
752 | } |
753 | ||
310b86d4 JH |
754 | static int remove_duplicates(int nr_heads, char **heads) |
755 | { | |
756 | int src, dst; | |
757 | ||
758 | for (src = dst = 0; src < nr_heads; src++) { | |
759 | /* If heads[src] is different from any of | |
760 | * heads[0..dst], push it in. | |
761 | */ | |
762 | int i; | |
763 | for (i = 0; i < dst; i++) { | |
764 | if (!strcmp(heads[i], heads[src])) | |
765 | break; | |
766 | } | |
767 | if (i < dst) | |
768 | continue; | |
769 | if (src != dst) | |
770 | heads[dst] = heads[src]; | |
771 | dst++; | |
772 | } | |
310b86d4 JH |
773 | return dst; |
774 | } | |
775 | ||
ef90d6d4 | 776 | static int fetch_pack_config(const char *var, const char *value, void *cb) |
af7cf268 JH |
777 | { |
778 | if (strcmp(var, "fetch.unpacklimit") == 0) { | |
e28714c5 JH |
779 | fetch_unpack_limit = git_config_int(var, value); |
780 | return 0; | |
781 | } | |
782 | ||
783 | if (strcmp(var, "transfer.unpacklimit") == 0) { | |
784 | transfer_unpack_limit = git_config_int(var, value); | |
af7cf268 JH |
785 | return 0; |
786 | } | |
787 | ||
f04833ef NP |
788 | if (strcmp(var, "repack.usedeltabaseoffset") == 0) { |
789 | prefer_ofs_delta = git_config_bool(var, value); | |
790 | return 0; | |
791 | } | |
792 | ||
ef90d6d4 | 793 | return git_default_config(var, value, cb); |
af7cf268 JH |
794 | } |
795 | ||
54b9e022 JH |
796 | static struct lock_file lock; |
797 | ||
50ab5fd3 | 798 | static void fetch_pack_setup(void) |
def88e9a | 799 | { |
50ab5fd3 SP |
800 | static int did_setup; |
801 | if (did_setup) | |
802 | return; | |
ef90d6d4 | 803 | git_config(fetch_pack_config, NULL); |
e28714c5 JH |
804 | if (0 <= transfer_unpack_limit) |
805 | unpack_limit = transfer_unpack_limit; | |
806 | else if (0 <= fetch_unpack_limit) | |
807 | unpack_limit = fetch_unpack_limit; | |
50ab5fd3 SP |
808 | did_setup = 1; |
809 | } | |
810 | ||
811 | int cmd_fetch_pack(int argc, const char **argv, const char *prefix) | |
812 | { | |
813 | int i, ret, nr_heads; | |
ba227857 | 814 | struct ref *ref = NULL; |
50ab5fd3 | 815 | char *dest = NULL, **heads; |
ba227857 | 816 | int fd[2]; |
249b2004 SP |
817 | char *pack_lockfile = NULL; |
818 | char **pack_lockfile_ptr = NULL; | |
ba227857 | 819 | struct child_process *conn; |
e28714c5 | 820 | |
def88e9a LT |
821 | nr_heads = 0; |
822 | heads = NULL; | |
823 | for (i = 1; i < argc; i++) { | |
2d4177c0 | 824 | const char *arg = argv[i]; |
def88e9a LT |
825 | |
826 | if (*arg == '-') { | |
599065a3 | 827 | if (!prefixcmp(arg, "--upload-pack=")) { |
fa740529 | 828 | args.uploadpack = arg + 14; |
27dca07f UKK |
829 | continue; |
830 | } | |
599065a3 | 831 | if (!prefixcmp(arg, "--exec=")) { |
fa740529 | 832 | args.uploadpack = arg + 7; |
8b3d9dc0 JH |
833 | continue; |
834 | } | |
2247efb4 | 835 | if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { |
fa740529 | 836 | args.quiet = 1; |
33b83034 JH |
837 | continue; |
838 | } | |
2247efb4 | 839 | if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { |
fa740529 SP |
840 | args.lock_pack = args.keep_pack; |
841 | args.keep_pack = 1; | |
9e10fd1a JH |
842 | continue; |
843 | } | |
b19696c2 | 844 | if (!strcmp("--thin", arg)) { |
fa740529 | 845 | args.use_thin_pack = 1; |
b19696c2 JH |
846 | continue; |
847 | } | |
348e390b SP |
848 | if (!strcmp("--include-tag", arg)) { |
849 | args.include_tag = 1; | |
850 | continue; | |
851 | } | |
dfeff66e | 852 | if (!strcmp("--all", arg)) { |
fa740529 | 853 | args.fetch_all = 1; |
dfeff66e JH |
854 | continue; |
855 | } | |
33b83034 | 856 | if (!strcmp("-v", arg)) { |
fa740529 | 857 | args.verbose = 1; |
33b83034 JH |
858 | continue; |
859 | } | |
599065a3 | 860 | if (!prefixcmp(arg, "--depth=")) { |
fa740529 | 861 | args.depth = strtol(arg + 8, NULL, 0); |
016e6ccb JS |
862 | continue; |
863 | } | |
83a5ad61 | 864 | if (!strcmp("--no-progress", arg)) { |
fa740529 | 865 | args.no_progress = 1; |
83a5ad61 JS |
866 | continue; |
867 | } | |
249b2004 SP |
868 | if (!strcmp("--stateless-rpc", arg)) { |
869 | args.stateless_rpc = 1; | |
870 | continue; | |
871 | } | |
872 | if (!strcmp("--lock-pack", arg)) { | |
873 | args.lock_pack = 1; | |
874 | pack_lockfile_ptr = &pack_lockfile; | |
875 | continue; | |
876 | } | |
def88e9a LT |
877 | usage(fetch_pack_usage); |
878 | } | |
2d4177c0 DB |
879 | dest = (char *)arg; |
880 | heads = (char **)(argv + i + 1); | |
def88e9a LT |
881 | nr_heads = argc - i - 1; |
882 | break; | |
883 | } | |
884 | if (!dest) | |
885 | usage(fetch_pack_usage); | |
2d4177c0 | 886 | |
249b2004 SP |
887 | if (args.stateless_rpc) { |
888 | conn = NULL; | |
889 | fd[0] = 0; | |
890 | fd[1] = 1; | |
ba227857 | 891 | } else { |
249b2004 SP |
892 | conn = git_connect(fd, (char *)dest, args.uploadpack, |
893 | args.verbose ? CONNECT_VERBOSE : 0); | |
894 | } | |
895 | ||
896 | get_remote_heads(fd[0], &ref, 0, NULL, 0, NULL); | |
897 | ||
898 | ref = fetch_pack(&args, fd, conn, ref, dest, | |
899 | nr_heads, heads, pack_lockfile_ptr); | |
900 | if (pack_lockfile) { | |
901 | printf("lock %s\n", pack_lockfile); | |
902 | fflush(stdout); | |
ba227857 | 903 | } |
249b2004 SP |
904 | close(fd[0]); |
905 | close(fd[1]); | |
906 | if (finish_connect(conn)) | |
907 | ref = NULL; | |
2d4177c0 DB |
908 | ret = !ref; |
909 | ||
ba227857 DB |
910 | if (!ret && nr_heads) { |
911 | /* If the heads to pull were given, we should have | |
912 | * consumed all of them by matching the remote. | |
05207a28 | 913 | * Otherwise, 'git fetch remote no-such-ref' would |
ba227857 DB |
914 | * silently succeed without issuing an error. |
915 | */ | |
916 | for (i = 0; i < nr_heads; i++) | |
917 | if (heads[i] && heads[i][0]) { | |
918 | error("no such remote ref %s", heads[i]); | |
919 | ret = 1; | |
920 | } | |
921 | } | |
2d4177c0 DB |
922 | while (ref) { |
923 | printf("%s %s\n", | |
924 | sha1_to_hex(ref->old_sha1), ref->name); | |
925 | ref = ref->next; | |
926 | } | |
927 | ||
928 | return ret; | |
929 | } | |
930 | ||
fa740529 | 931 | struct ref *fetch_pack(struct fetch_pack_args *my_args, |
ba227857 DB |
932 | int fd[], struct child_process *conn, |
933 | const struct ref *ref, | |
fa740529 | 934 | const char *dest, |
1788c39c SP |
935 | int nr_heads, |
936 | char **heads, | |
937 | char **pack_lockfile) | |
2d4177c0 | 938 | { |
2d4177c0 | 939 | struct stat st; |
ba227857 | 940 | struct ref *ref_cpy; |
2d4177c0 | 941 | |
50ab5fd3 | 942 | fetch_pack_setup(); |
d551bbaf TR |
943 | if (&args != my_args) |
944 | memcpy(&args, my_args, sizeof(args)); | |
fa740529 | 945 | if (args.depth > 0) { |
2d4177c0 DB |
946 | if (stat(git_path("shallow"), &st)) |
947 | st.st_mtime = 0; | |
948 | } | |
949 | ||
310b86d4 JH |
950 | if (heads && nr_heads) |
951 | nr_heads = remove_duplicates(nr_heads, heads); | |
ba227857 DB |
952 | if (!ref) { |
953 | packet_flush(fd[1]); | |
954 | die("no matching remote head"); | |
9e5d2b40 | 955 | } |
ba227857 | 956 | ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile); |
9e5d2b40 | 957 | |
ba227857 | 958 | if (args.depth > 0) { |
016e6ccb | 959 | struct cache_time mtime; |
edace6f0 | 960 | struct strbuf sb = STRBUF_INIT; |
016e6ccb JS |
961 | char *shallow = git_path("shallow"); |
962 | int fd; | |
963 | ||
964 | mtime.sec = st.st_mtime; | |
c06ff490 | 965 | mtime.nsec = ST_MTIME_NSEC(st); |
016e6ccb JS |
966 | if (stat(shallow, &st)) { |
967 | if (mtime.sec) | |
968 | die("shallow file was removed during fetch"); | |
969 | } else if (st.st_mtime != mtime.sec | |
970 | #ifdef USE_NSEC | |
5bcf109c | 971 | || ST_MTIME_NSEC(st) != mtime.nsec |
016e6ccb JS |
972 | #endif |
973 | ) | |
974 | die("shallow file was changed during fetch"); | |
975 | ||
acd3b9ec JH |
976 | fd = hold_lock_file_for_update(&lock, shallow, |
977 | LOCK_DIE_ON_ERROR); | |
edace6f0 SP |
978 | if (!write_shallow_commits(&sb, 0) |
979 | || write_in_full(fd, sb.buf, sb.len) != sb.len) { | |
691f1a28 | 980 | unlink_or_warn(shallow); |
016e6ccb JS |
981 | rollback_lock_file(&lock); |
982 | } else { | |
016e6ccb JS |
983 | commit_lock_file(&lock); |
984 | } | |
edace6f0 | 985 | strbuf_release(&sb); |
016e6ccb JS |
986 | } |
987 | ||
48ec3e5c | 988 | reprepare_packed_git(); |
ba227857 | 989 | return ref_cpy; |
def88e9a | 990 | } |