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