1#include "cache.h"
2#include "lockfile.h"
3#include "refs.h"
4#include "pkt-line.h"
5#include "commit.h"
6#include "tag.h"
7#include "exec_cmd.h"
8#include "pack.h"
9#include "sideband.h"
10#include "fetch-pack.h"
11#include "remote.h"
12#include "run-command.h"
13#include "connect.h"
14#include "transport.h"
15#include "version.h"
16#include "prio-queue.h"
17#include "sha1-array.h"
18#include "oidset.h"
19
20static int transfer_unpack_limit = -1;
21static int fetch_unpack_limit = -1;
22static int unpack_limit = 100;
23static int prefer_ofs_delta = 1;
24static int no_done;
25static int deepen_since_ok;
26static int deepen_not_ok;
27static int fetch_fsck_objects = -1;
28static int transfer_fsck_objects = -1;
29static int agent_supported;
30static struct lock_file shallow_lock;
31static const char *alternate_shallow_file;
32
33/* Remember to update object flag allocation in object.h */
34#define COMPLETE (1U << 0)
35#define COMMON (1U << 1)
36#define COMMON_REF (1U << 2)
37#define SEEN (1U << 3)
38#define POPPED (1U << 4)
39#define ALTERNATE (1U << 5)
40
41static int marked;
42
43/*
44 * After sending this many "have"s if we do not get any new ACK , we
45 * give up traversing our history.
46 */
47#define MAX_IN_VAIN 256
48
49static struct prio_queue rev_list = { compare_commits_by_commit_date };
50static int non_common_revs, multi_ack, use_sideband;
51/* Allow specifying sha1 if it is a ref tip. */
52#define ALLOW_TIP_SHA1 01
53/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54#define ALLOW_REACHABLE_SHA1 02
55static unsigned int allow_unadvertised_object_request;
56
57__attribute__((format (printf, 2, 3)))
58static inline void print_verbose(const struct fetch_pack_args *args,
59 const char *fmt, ...)
60{
61 va_list params;
62
63 if (!args->verbose)
64 return;
65
66 va_start(params, fmt);
67 vfprintf(stderr, fmt, params);
68 va_end(params);
69 fputc('\n', stderr);
70}
71
72struct alternate_object_cache {
73 struct object **items;
74 size_t nr, alloc;
75};
76
77static void cache_one_alternate(const char *refname,
78 const struct object_id *oid,
79 void *vcache)
80{
81 struct alternate_object_cache *cache = vcache;
82 struct object *obj = parse_object(oid->hash);
83
84 if (!obj || (obj->flags & ALTERNATE))
85 return;
86
87 obj->flags |= ALTERNATE;
88 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
89 cache->items[cache->nr++] = obj;
90}
91
92static void for_each_cached_alternate(void (*cb)(struct object *))
93{
94 static int initialized;
95 static struct alternate_object_cache cache;
96 size_t i;
97
98 if (!initialized) {
99 for_each_alternate_ref(cache_one_alternate, &cache);
100 initialized = 1;
101 }
102
103 for (i = 0; i < cache.nr; i++)
104 cb(cache.items[i]);
105}
106
107static void rev_list_push(struct commit *commit, int mark)
108{
109 if (!(commit->object.flags & mark)) {
110 commit->object.flags |= mark;
111
112 if (parse_commit(commit))
113 return;
114
115 prio_queue_put(&rev_list, commit);
116
117 if (!(commit->object.flags & COMMON))
118 non_common_revs++;
119 }
120}
121
122static int rev_list_insert_ref(const char *refname, const unsigned char *sha1)
123{
124 struct object *o = deref_tag(parse_object(sha1), refname, 0);
125
126 if (o && o->type == OBJ_COMMIT)
127 rev_list_push((struct commit *)o, SEEN);
128
129 return 0;
130}
131
132static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
133 int flag, void *cb_data)
134{
135 return rev_list_insert_ref(refname, oid->hash);
136}
137
138static int clear_marks(const char *refname, const struct object_id *oid,
139 int flag, void *cb_data)
140{
141 struct object *o = deref_tag(parse_object(oid->hash), refname, 0);
142
143 if (o && o->type == OBJ_COMMIT)
144 clear_commit_marks((struct commit *)o,
145 COMMON | COMMON_REF | SEEN | POPPED);
146 return 0;
147}
148
149/*
150 This function marks a rev and its ancestors as common.
151 In some cases, it is desirable to mark only the ancestors (for example
152 when only the server does not yet know that they are common).
153*/
154
155static void mark_common(struct commit *commit,
156 int ancestors_only, int dont_parse)
157{
158 if (commit != NULL && !(commit->object.flags & COMMON)) {
159 struct object *o = (struct object *)commit;
160
161 if (!ancestors_only)
162 o->flags |= COMMON;
163
164 if (!(o->flags & SEEN))
165 rev_list_push(commit, SEEN);
166 else {
167 struct commit_list *parents;
168
169 if (!ancestors_only && !(o->flags & POPPED))
170 non_common_revs--;
171 if (!o->parsed && !dont_parse)
172 if (parse_commit(commit))
173 return;
174
175 for (parents = commit->parents;
176 parents;
177 parents = parents->next)
178 mark_common(parents->item, 0, dont_parse);
179 }
180 }
181}
182
183/*
184 Get the next rev to send, ignoring the common.
185*/
186
187static const unsigned char *get_rev(void)
188{
189 struct commit *commit = NULL;
190
191 while (commit == NULL) {
192 unsigned int mark;
193 struct commit_list *parents;
194
195 if (rev_list.nr == 0 || non_common_revs == 0)
196 return NULL;
197
198 commit = prio_queue_get(&rev_list);
199 parse_commit(commit);
200 parents = commit->parents;
201
202 commit->object.flags |= POPPED;
203 if (!(commit->object.flags & COMMON))
204 non_common_revs--;
205
206 if (commit->object.flags & COMMON) {
207 /* do not send "have", and ignore ancestors */
208 commit = NULL;
209 mark = COMMON | SEEN;
210 } else if (commit->object.flags & COMMON_REF)
211 /* send "have", and ignore ancestors */
212 mark = COMMON | SEEN;
213 else
214 /* send "have", also for its ancestors */
215 mark = SEEN;
216
217 while (parents) {
218 if (!(parents->item->object.flags & SEEN))
219 rev_list_push(parents->item, mark);
220 if (mark & COMMON)
221 mark_common(parents->item, 1, 0);
222 parents = parents->next;
223 }
224 }
225
226 return commit->object.oid.hash;
227}
228
229enum ack_type {
230 NAK = 0,
231 ACK,
232 ACK_continue,
233 ACK_common,
234 ACK_ready
235};
236
237static void consume_shallow_list(struct fetch_pack_args *args, int fd)
238{
239 if (args->stateless_rpc && args->deepen) {
240 /* If we sent a depth we will get back "duplicate"
241 * shallow and unshallow commands every time there
242 * is a block of have lines exchanged.
243 */
244 char *line;
245 while ((line = packet_read_line(fd, NULL))) {
246 if (starts_with(line, "shallow "))
247 continue;
248 if (starts_with(line, "unshallow "))
249 continue;
250 die(_("git fetch-pack: expected shallow list"));
251 }
252 }
253}
254
255static enum ack_type get_ack(int fd, unsigned char *result_sha1)
256{
257 int len;
258 char *line = packet_read_line(fd, &len);
259 const char *arg;
260
261 if (!len)
262 die(_("git fetch-pack: expected ACK/NAK, got EOF"));
263 if (!strcmp(line, "NAK"))
264 return NAK;
265 if (skip_prefix(line, "ACK ", &arg)) {
266 if (!get_sha1_hex(arg, result_sha1)) {
267 arg += 40;
268 len -= arg - line;
269 if (len < 1)
270 return ACK;
271 if (strstr(arg, "continue"))
272 return ACK_continue;
273 if (strstr(arg, "common"))
274 return ACK_common;
275 if (strstr(arg, "ready"))
276 return ACK_ready;
277 return ACK;
278 }
279 }
280 if (skip_prefix(line, "ERR ", &arg))
281 die(_("remote error: %s"), arg);
282 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
283}
284
285static void send_request(struct fetch_pack_args *args,
286 int fd, struct strbuf *buf)
287{
288 if (args->stateless_rpc) {
289 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
290 packet_flush(fd);
291 } else
292 write_or_die(fd, buf->buf, buf->len);
293}
294
295static void insert_one_alternate_object(struct object *obj)
296{
297 rev_list_insert_ref(NULL, obj->oid.hash);
298}
299
300#define INITIAL_FLUSH 16
301#define PIPESAFE_FLUSH 32
302#define LARGE_FLUSH 16384
303
304static int next_flush(struct fetch_pack_args *args, int count)
305{
306 if (args->stateless_rpc) {
307 if (count < LARGE_FLUSH)
308 count <<= 1;
309 else
310 count = count * 11 / 10;
311 } else {
312 if (count < PIPESAFE_FLUSH)
313 count <<= 1;
314 else
315 count += PIPESAFE_FLUSH;
316 }
317 return count;
318}
319
320static int find_common(struct fetch_pack_args *args,
321 int fd[2], unsigned char *result_sha1,
322 struct ref *refs)
323{
324 int fetching;
325 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
326 const unsigned char *sha1;
327 unsigned in_vain = 0;
328 int got_continue = 0;
329 int got_ready = 0;
330 struct strbuf req_buf = STRBUF_INIT;
331 size_t state_len = 0;
332
333 if (args->stateless_rpc && multi_ack == 1)
334 die(_("--stateless-rpc requires multi_ack_detailed"));
335 if (marked)
336 for_each_ref(clear_marks, NULL);
337 marked = 1;
338
339 for_each_ref(rev_list_insert_ref_oid, NULL);
340 for_each_cached_alternate(insert_one_alternate_object);
341
342 fetching = 0;
343 for ( ; refs ; refs = refs->next) {
344 unsigned char *remote = refs->old_oid.hash;
345 const char *remote_hex;
346 struct object *o;
347
348 /*
349 * If that object is complete (i.e. it is an ancestor of a
350 * local ref), we tell them we have it but do not have to
351 * tell them about its ancestors, which they already know
352 * about.
353 *
354 * We use lookup_object here because we are only
355 * interested in the case we *know* the object is
356 * reachable and we have already scanned it.
357 */
358 if (((o = lookup_object(remote)) != NULL) &&
359 (o->flags & COMPLETE)) {
360 continue;
361 }
362
363 remote_hex = sha1_to_hex(remote);
364 if (!fetching) {
365 struct strbuf c = STRBUF_INIT;
366 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
367 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
368 if (no_done) strbuf_addstr(&c, " no-done");
369 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
370 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
371 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
372 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
373 if (args->no_progress) strbuf_addstr(&c, " no-progress");
374 if (args->include_tag) strbuf_addstr(&c, " include-tag");
375 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
376 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
377 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
378 if (agent_supported) strbuf_addf(&c, " agent=%s",
379 git_user_agent_sanitized());
380 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
381 strbuf_release(&c);
382 } else
383 packet_buf_write(&req_buf, "want %s\n", remote_hex);
384 fetching++;
385 }
386
387 if (!fetching) {
388 strbuf_release(&req_buf);
389 packet_flush(fd[1]);
390 return 1;
391 }
392
393 if (is_repository_shallow())
394 write_shallow_commits(&req_buf, 1, NULL);
395 if (args->depth > 0)
396 packet_buf_write(&req_buf, "deepen %d", args->depth);
397 if (args->deepen_since) {
398 unsigned long max_age = approxidate(args->deepen_since);
399 packet_buf_write(&req_buf, "deepen-since %lu", max_age);
400 }
401 if (args->deepen_not) {
402 int i;
403 for (i = 0; i < args->deepen_not->nr; i++) {
404 struct string_list_item *s = args->deepen_not->items + i;
405 packet_buf_write(&req_buf, "deepen-not %s", s->string);
406 }
407 }
408 packet_buf_flush(&req_buf);
409 state_len = req_buf.len;
410
411 if (args->deepen) {
412 char *line;
413 const char *arg;
414 unsigned char sha1[20];
415
416 send_request(args, fd[1], &req_buf);
417 while ((line = packet_read_line(fd[0], NULL))) {
418 if (skip_prefix(line, "shallow ", &arg)) {
419 if (get_sha1_hex(arg, sha1))
420 die(_("invalid shallow line: %s"), line);
421 register_shallow(sha1);
422 continue;
423 }
424 if (skip_prefix(line, "unshallow ", &arg)) {
425 if (get_sha1_hex(arg, sha1))
426 die(_("invalid unshallow line: %s"), line);
427 if (!lookup_object(sha1))
428 die(_("object not found: %s"), line);
429 /* make sure that it is parsed as shallow */
430 if (!parse_object(sha1))
431 die(_("error in object: %s"), line);
432 if (unregister_shallow(sha1))
433 die(_("no shallow found: %s"), line);
434 continue;
435 }
436 die(_("expected shallow/unshallow, got %s"), line);
437 }
438 } else if (!args->stateless_rpc)
439 send_request(args, fd[1], &req_buf);
440
441 if (!args->stateless_rpc) {
442 /* If we aren't using the stateless-rpc interface
443 * we don't need to retain the headers.
444 */
445 strbuf_setlen(&req_buf, 0);
446 state_len = 0;
447 }
448
449 flushes = 0;
450 retval = -1;
451 while ((sha1 = get_rev())) {
452 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
453 print_verbose(args, "have %s", sha1_to_hex(sha1));
454 in_vain++;
455 if (flush_at <= ++count) {
456 int ack;
457
458 packet_buf_flush(&req_buf);
459 send_request(args, fd[1], &req_buf);
460 strbuf_setlen(&req_buf, state_len);
461 flushes++;
462 flush_at = next_flush(args, count);
463
464 /*
465 * We keep one window "ahead" of the other side, and
466 * will wait for an ACK only on the next one
467 */
468 if (!args->stateless_rpc && count == INITIAL_FLUSH)
469 continue;
470
471 consume_shallow_list(args, fd[0]);
472 do {
473 ack = get_ack(fd[0], result_sha1);
474 if (ack)
475 print_verbose(args, _("got %s %d %s"), "ack",
476 ack, sha1_to_hex(result_sha1));
477 switch (ack) {
478 case ACK:
479 flushes = 0;
480 multi_ack = 0;
481 retval = 0;
482 goto done;
483 case ACK_common:
484 case ACK_ready:
485 case ACK_continue: {
486 struct commit *commit =
487 lookup_commit(result_sha1);
488 if (!commit)
489 die(_("invalid commit %s"), sha1_to_hex(result_sha1));
490 if (args->stateless_rpc
491 && ack == ACK_common
492 && !(commit->object.flags & COMMON)) {
493 /* We need to replay the have for this object
494 * on the next RPC request so the peer knows
495 * it is in common with us.
496 */
497 const char *hex = sha1_to_hex(result_sha1);
498 packet_buf_write(&req_buf, "have %s\n", hex);
499 state_len = req_buf.len;
500 /*
501 * Reset in_vain because an ack
502 * for this commit has not been
503 * seen.
504 */
505 in_vain = 0;
506 } else if (!args->stateless_rpc
507 || ack != ACK_common)
508 in_vain = 0;
509 mark_common(commit, 0, 1);
510 retval = 0;
511 got_continue = 1;
512 if (ack == ACK_ready) {
513 clear_prio_queue(&rev_list);
514 got_ready = 1;
515 }
516 break;
517 }
518 }
519 } while (ack);
520 flushes--;
521 if (got_continue && MAX_IN_VAIN < in_vain) {
522 print_verbose(args, _("giving up"));
523 break; /* give up */
524 }
525 }
526 }
527done:
528 if (!got_ready || !no_done) {
529 packet_buf_write(&req_buf, "done\n");
530 send_request(args, fd[1], &req_buf);
531 }
532 print_verbose(args, _("done"));
533 if (retval != 0) {
534 multi_ack = 0;
535 flushes++;
536 }
537 strbuf_release(&req_buf);
538
539 if (!got_ready || !no_done)
540 consume_shallow_list(args, fd[0]);
541 while (flushes || multi_ack) {
542 int ack = get_ack(fd[0], result_sha1);
543 if (ack) {
544 print_verbose(args, _("got %s (%d) %s"), "ack",
545 ack, sha1_to_hex(result_sha1));
546 if (ack == ACK)
547 return 0;
548 multi_ack = 1;
549 continue;
550 }
551 flushes--;
552 }
553 /* it is no error to fetch into a completely empty repo */
554 return count ? retval : 0;
555}
556
557static struct commit_list *complete;
558
559static int mark_complete(const unsigned char *sha1)
560{
561 struct object *o = parse_object(sha1);
562
563 while (o && o->type == OBJ_TAG) {
564 struct tag *t = (struct tag *) o;
565 if (!t->tagged)
566 break; /* broken repository */
567 o->flags |= COMPLETE;
568 o = parse_object(t->tagged->oid.hash);
569 }
570 if (o && o->type == OBJ_COMMIT) {
571 struct commit *commit = (struct commit *)o;
572 if (!(commit->object.flags & COMPLETE)) {
573 commit->object.flags |= COMPLETE;
574 commit_list_insert(commit, &complete);
575 }
576 }
577 return 0;
578}
579
580static int mark_complete_oid(const char *refname, const struct object_id *oid,
581 int flag, void *cb_data)
582{
583 return mark_complete(oid->hash);
584}
585
586static void mark_recent_complete_commits(struct fetch_pack_args *args,
587 unsigned long cutoff)
588{
589 while (complete && cutoff <= complete->item->date) {
590 print_verbose(args, _("Marking %s as complete"),
591 oid_to_hex(&complete->item->object.oid));
592 pop_most_recent_commit(&complete, COMPLETE);
593 }
594}
595
596static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
597{
598 for (; refs; refs = refs->next)
599 oidset_insert(oids, &refs->old_oid);
600}
601
602static int tip_oids_contain(struct oidset *tip_oids,
603 struct ref *unmatched, struct ref *newlist,
604 const struct object_id *id)
605{
606 /*
607 * Note that this only looks at the ref lists the first time it's
608 * called. This works out in filter_refs() because even though it may
609 * add to "newlist" between calls, the additions will always be for
610 * oids that are already in the set.
611 */
612 if (!tip_oids->map.tablesize) {
613 add_refs_to_oidset(tip_oids, unmatched);
614 add_refs_to_oidset(tip_oids, newlist);
615 }
616 return oidset_contains(tip_oids, id);
617}
618
619static void filter_refs(struct fetch_pack_args *args,
620 struct ref **refs,
621 struct ref **sought, int nr_sought)
622{
623 struct ref *newlist = NULL;
624 struct ref **newtail = &newlist;
625 struct ref *unmatched = NULL;
626 struct ref *ref, *next;
627 struct oidset tip_oids = OIDSET_INIT;
628 int i;
629
630 i = 0;
631 for (ref = *refs; ref; ref = next) {
632 int keep = 0;
633 next = ref->next;
634
635 if (starts_with(ref->name, "refs/") &&
636 check_refname_format(ref->name, 0))
637 ; /* trash */
638 else {
639 while (i < nr_sought) {
640 int cmp = strcmp(ref->name, sought[i]->name);
641 if (cmp < 0)
642 break; /* definitely do not have it */
643 else if (cmp == 0) {
644 keep = 1; /* definitely have it */
645 sought[i]->match_status = REF_MATCHED;
646 }
647 i++;
648 }
649 }
650
651 if (!keep && args->fetch_all &&
652 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
653 keep = 1;
654
655 if (keep) {
656 *newtail = ref;
657 ref->next = NULL;
658 newtail = &ref->next;
659 } else {
660 ref->next = unmatched;
661 unmatched = ref;
662 }
663 }
664
665 /* Append unmatched requests to the list */
666 for (i = 0; i < nr_sought; i++) {
667 unsigned char sha1[20];
668
669 ref = sought[i];
670 if (ref->match_status != REF_NOT_MATCHED)
671 continue;
672 if (get_sha1_hex(ref->name, sha1) ||
673 ref->name[40] != '\0' ||
674 hashcmp(sha1, ref->old_oid.hash))
675 continue;
676
677 if ((allow_unadvertised_object_request &
678 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
679 tip_oids_contain(&tip_oids, unmatched, newlist,
680 &ref->old_oid)) {
681 ref->match_status = REF_MATCHED;
682 *newtail = copy_ref(ref);
683 newtail = &(*newtail)->next;
684 } else {
685 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
686 }
687 }
688
689 oidset_clear(&tip_oids);
690 for (ref = unmatched; ref; ref = next) {
691 next = ref->next;
692 free(ref);
693 }
694
695 *refs = newlist;
696}
697
698static void mark_alternate_complete(struct object *obj)
699{
700 mark_complete(obj->oid.hash);
701}
702
703static int everything_local(struct fetch_pack_args *args,
704 struct ref **refs,
705 struct ref **sought, int nr_sought)
706{
707 struct ref *ref;
708 int retval;
709 unsigned long cutoff = 0;
710
711 save_commit_buffer = 0;
712
713 for (ref = *refs; ref; ref = ref->next) {
714 struct object *o;
715
716 if (!has_object_file(&ref->old_oid))
717 continue;
718
719 o = parse_object(ref->old_oid.hash);
720 if (!o)
721 continue;
722
723 /* We already have it -- which may mean that we were
724 * in sync with the other side at some time after
725 * that (it is OK if we guess wrong here).
726 */
727 if (o->type == OBJ_COMMIT) {
728 struct commit *commit = (struct commit *)o;
729 if (!cutoff || cutoff < commit->date)
730 cutoff = commit->date;
731 }
732 }
733
734 if (!args->deepen) {
735 for_each_ref(mark_complete_oid, NULL);
736 for_each_cached_alternate(mark_alternate_complete);
737 commit_list_sort_by_date(&complete);
738 if (cutoff)
739 mark_recent_complete_commits(args, cutoff);
740 }
741
742 /*
743 * Mark all complete remote refs as common refs.
744 * Don't mark them common yet; the server has to be told so first.
745 */
746 for (ref = *refs; ref; ref = ref->next) {
747 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
748 NULL, 0);
749
750 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
751 continue;
752
753 if (!(o->flags & SEEN)) {
754 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
755
756 mark_common((struct commit *)o, 1, 1);
757 }
758 }
759
760 filter_refs(args, refs, sought, nr_sought);
761
762 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
763 const unsigned char *remote = ref->old_oid.hash;
764 struct object *o;
765
766 o = lookup_object(remote);
767 if (!o || !(o->flags & COMPLETE)) {
768 retval = 0;
769 print_verbose(args, "want %s (%s)", sha1_to_hex(remote),
770 ref->name);
771 continue;
772 }
773 print_verbose(args, _("already have %s (%s)"), sha1_to_hex(remote),
774 ref->name);
775 }
776 return retval;
777}
778
779static int sideband_demux(int in, int out, void *data)
780{
781 int *xd = data;
782 int ret;
783
784 ret = recv_sideband("fetch-pack", xd[0], out);
785 close(out);
786 return ret;
787}
788
789static int get_pack(struct fetch_pack_args *args,
790 int xd[2], char **pack_lockfile)
791{
792 struct async demux;
793 int do_keep = args->keep_pack;
794 const char *cmd_name;
795 struct pack_header header;
796 int pass_header = 0;
797 struct child_process cmd = CHILD_PROCESS_INIT;
798 int ret;
799
800 memset(&demux, 0, sizeof(demux));
801 if (use_sideband) {
802 /* xd[] is talking with upload-pack; subprocess reads from
803 * xd[0], spits out band#2 to stderr, and feeds us band#1
804 * through demux->out.
805 */
806 demux.proc = sideband_demux;
807 demux.data = xd;
808 demux.out = -1;
809 demux.isolate_sigpipe = 1;
810 if (start_async(&demux))
811 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
812 }
813 else
814 demux.out = xd[0];
815
816 if (!args->keep_pack && unpack_limit) {
817
818 if (read_pack_header(demux.out, &header))
819 die(_("protocol error: bad pack header"));
820 pass_header = 1;
821 if (ntohl(header.hdr_entries) < unpack_limit)
822 do_keep = 0;
823 else
824 do_keep = 1;
825 }
826
827 if (alternate_shallow_file) {
828 argv_array_push(&cmd.args, "--shallow-file");
829 argv_array_push(&cmd.args, alternate_shallow_file);
830 }
831
832 if (do_keep) {
833 if (pack_lockfile)
834 cmd.out = -1;
835 cmd_name = "index-pack";
836 argv_array_push(&cmd.args, cmd_name);
837 argv_array_push(&cmd.args, "--stdin");
838 if (!args->quiet && !args->no_progress)
839 argv_array_push(&cmd.args, "-v");
840 if (args->use_thin_pack)
841 argv_array_push(&cmd.args, "--fix-thin");
842 if (args->lock_pack || unpack_limit) {
843 char hostname[HOST_NAME_MAX + 1];
844 if (xgethostname(hostname, sizeof(hostname)))
845 xsnprintf(hostname, sizeof(hostname), "localhost");
846 argv_array_pushf(&cmd.args,
847 "--keep=fetch-pack %"PRIuMAX " on %s",
848 (uintmax_t)getpid(), hostname);
849 }
850 if (args->check_self_contained_and_connected)
851 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
852 }
853 else {
854 cmd_name = "unpack-objects";
855 argv_array_push(&cmd.args, cmd_name);
856 if (args->quiet || args->no_progress)
857 argv_array_push(&cmd.args, "-q");
858 args->check_self_contained_and_connected = 0;
859 }
860
861 if (pass_header)
862 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
863 ntohl(header.hdr_version),
864 ntohl(header.hdr_entries));
865 if (fetch_fsck_objects >= 0
866 ? fetch_fsck_objects
867 : transfer_fsck_objects >= 0
868 ? transfer_fsck_objects
869 : 0)
870 argv_array_push(&cmd.args, "--strict");
871
872 cmd.in = demux.out;
873 cmd.git_cmd = 1;
874 if (start_command(&cmd))
875 die(_("fetch-pack: unable to fork off %s"), cmd_name);
876 if (do_keep && pack_lockfile) {
877 *pack_lockfile = index_pack_lockfile(cmd.out);
878 close(cmd.out);
879 }
880
881 if (!use_sideband)
882 /* Closed by start_command() */
883 xd[0] = -1;
884
885 ret = finish_command(&cmd);
886 if (!ret || (args->check_self_contained_and_connected && ret == 1))
887 args->self_contained_and_connected =
888 args->check_self_contained_and_connected &&
889 ret == 0;
890 else
891 die(_("%s failed"), cmd_name);
892 if (use_sideband && finish_async(&demux))
893 die(_("error in sideband demultiplexer"));
894 return 0;
895}
896
897static int cmp_ref_by_name(const void *a_, const void *b_)
898{
899 const struct ref *a = *((const struct ref **)a_);
900 const struct ref *b = *((const struct ref **)b_);
901 return strcmp(a->name, b->name);
902}
903
904static struct ref *do_fetch_pack(struct fetch_pack_args *args,
905 int fd[2],
906 const struct ref *orig_ref,
907 struct ref **sought, int nr_sought,
908 struct shallow_info *si,
909 char **pack_lockfile)
910{
911 struct ref *ref = copy_ref_list(orig_ref);
912 unsigned char sha1[20];
913 const char *agent_feature;
914 int agent_len;
915
916 sort_ref_list(&ref, ref_compare_name);
917 QSORT(sought, nr_sought, cmp_ref_by_name);
918
919 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
920 die(_("Server does not support shallow clients"));
921 if (args->depth > 0 || args->deepen_since || args->deepen_not)
922 args->deepen = 1;
923 if (server_supports("multi_ack_detailed")) {
924 print_verbose(args, _("Server supports multi_ack_detailed"));
925 multi_ack = 2;
926 if (server_supports("no-done")) {
927 print_verbose(args, _("Server supports no-done"));
928 if (args->stateless_rpc)
929 no_done = 1;
930 }
931 }
932 else if (server_supports("multi_ack")) {
933 print_verbose(args, _("Server supports multi_ack"));
934 multi_ack = 1;
935 }
936 if (server_supports("side-band-64k")) {
937 print_verbose(args, _("Server supports side-band-64k"));
938 use_sideband = 2;
939 }
940 else if (server_supports("side-band")) {
941 print_verbose(args, _("Server supports side-band"));
942 use_sideband = 1;
943 }
944 if (server_supports("allow-tip-sha1-in-want")) {
945 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
946 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
947 }
948 if (server_supports("allow-reachable-sha1-in-want")) {
949 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
950 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
951 }
952 if (!server_supports("thin-pack"))
953 args->use_thin_pack = 0;
954 if (!server_supports("no-progress"))
955 args->no_progress = 0;
956 if (!server_supports("include-tag"))
957 args->include_tag = 0;
958 if (server_supports("ofs-delta"))
959 print_verbose(args, _("Server supports ofs-delta"));
960 else
961 prefer_ofs_delta = 0;
962
963 if ((agent_feature = server_feature_value("agent", &agent_len))) {
964 agent_supported = 1;
965 if (agent_len)
966 print_verbose(args, _("Server version is %.*s"),
967 agent_len, agent_feature);
968 }
969 if (server_supports("deepen-since"))
970 deepen_since_ok = 1;
971 else if (args->deepen_since)
972 die(_("Server does not support --shallow-since"));
973 if (server_supports("deepen-not"))
974 deepen_not_ok = 1;
975 else if (args->deepen_not)
976 die(_("Server does not support --shallow-exclude"));
977 if (!server_supports("deepen-relative") && args->deepen_relative)
978 die(_("Server does not support --deepen"));
979
980 if (everything_local(args, &ref, sought, nr_sought)) {
981 packet_flush(fd[1]);
982 goto all_done;
983 }
984 if (find_common(args, fd, sha1, ref) < 0)
985 if (!args->keep_pack)
986 /* When cloning, it is not unusual to have
987 * no common commit.
988 */
989 warning(_("no common commits"));
990
991 if (args->stateless_rpc)
992 packet_flush(fd[1]);
993 if (args->deepen)
994 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
995 NULL);
996 else if (si->nr_ours || si->nr_theirs)
997 alternate_shallow_file = setup_temporary_shallow(si->shallow);
998 else
999 alternate_shallow_file = NULL;
1000 if (get_pack(args, fd, pack_lockfile))
1001 die(_("git fetch-pack: fetch failed."));
1002
1003 all_done:
1004 return ref;
1005}
1006
1007static void fetch_pack_config(void)
1008{
1009 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1010 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1011 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1012 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1013 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1014
1015 git_config(git_default_config, NULL);
1016}
1017
1018static void fetch_pack_setup(void)
1019{
1020 static int did_setup;
1021 if (did_setup)
1022 return;
1023 fetch_pack_config();
1024 if (0 <= transfer_unpack_limit)
1025 unpack_limit = transfer_unpack_limit;
1026 else if (0 <= fetch_unpack_limit)
1027 unpack_limit = fetch_unpack_limit;
1028 did_setup = 1;
1029}
1030
1031static int remove_duplicates_in_refs(struct ref **ref, int nr)
1032{
1033 struct string_list names = STRING_LIST_INIT_NODUP;
1034 int src, dst;
1035
1036 for (src = dst = 0; src < nr; src++) {
1037 struct string_list_item *item;
1038 item = string_list_insert(&names, ref[src]->name);
1039 if (item->util)
1040 continue; /* already have it */
1041 item->util = ref[src];
1042 if (src != dst)
1043 ref[dst] = ref[src];
1044 dst++;
1045 }
1046 for (src = dst; src < nr; src++)
1047 ref[src] = NULL;
1048 string_list_clear(&names, 0);
1049 return dst;
1050}
1051
1052static void update_shallow(struct fetch_pack_args *args,
1053 struct ref **sought, int nr_sought,
1054 struct shallow_info *si)
1055{
1056 struct oid_array ref = OID_ARRAY_INIT;
1057 int *status;
1058 int i;
1059
1060 if (args->deepen && alternate_shallow_file) {
1061 if (*alternate_shallow_file == '\0') { /* --unshallow */
1062 unlink_or_warn(git_path_shallow());
1063 rollback_lock_file(&shallow_lock);
1064 } else
1065 commit_lock_file(&shallow_lock);
1066 return;
1067 }
1068
1069 if (!si->shallow || !si->shallow->nr)
1070 return;
1071
1072 if (args->cloning) {
1073 /*
1074 * remote is shallow, but this is a clone, there are
1075 * no objects in repo to worry about. Accept any
1076 * shallow points that exist in the pack (iow in repo
1077 * after get_pack() and reprepare_packed_git())
1078 */
1079 struct oid_array extra = OID_ARRAY_INIT;
1080 struct object_id *oid = si->shallow->oid;
1081 for (i = 0; i < si->shallow->nr; i++)
1082 if (has_object_file(&oid[i]))
1083 oid_array_append(&extra, &oid[i]);
1084 if (extra.nr) {
1085 setup_alternate_shallow(&shallow_lock,
1086 &alternate_shallow_file,
1087 &extra);
1088 commit_lock_file(&shallow_lock);
1089 }
1090 oid_array_clear(&extra);
1091 return;
1092 }
1093
1094 if (!si->nr_ours && !si->nr_theirs)
1095 return;
1096
1097 remove_nonexistent_theirs_shallow(si);
1098 if (!si->nr_ours && !si->nr_theirs)
1099 return;
1100 for (i = 0; i < nr_sought; i++)
1101 oid_array_append(&ref, &sought[i]->old_oid);
1102 si->ref = &ref;
1103
1104 if (args->update_shallow) {
1105 /*
1106 * remote is also shallow, .git/shallow may be updated
1107 * so all refs can be accepted. Make sure we only add
1108 * shallow roots that are actually reachable from new
1109 * refs.
1110 */
1111 struct oid_array extra = OID_ARRAY_INIT;
1112 struct object_id *oid = si->shallow->oid;
1113 assign_shallow_commits_to_refs(si, NULL, NULL);
1114 if (!si->nr_ours && !si->nr_theirs) {
1115 oid_array_clear(&ref);
1116 return;
1117 }
1118 for (i = 0; i < si->nr_ours; i++)
1119 oid_array_append(&extra, &oid[si->ours[i]]);
1120 for (i = 0; i < si->nr_theirs; i++)
1121 oid_array_append(&extra, &oid[si->theirs[i]]);
1122 setup_alternate_shallow(&shallow_lock,
1123 &alternate_shallow_file,
1124 &extra);
1125 commit_lock_file(&shallow_lock);
1126 oid_array_clear(&extra);
1127 oid_array_clear(&ref);
1128 return;
1129 }
1130
1131 /*
1132 * remote is also shallow, check what ref is safe to update
1133 * without updating .git/shallow
1134 */
1135 status = xcalloc(nr_sought, sizeof(*status));
1136 assign_shallow_commits_to_refs(si, NULL, status);
1137 if (si->nr_ours || si->nr_theirs) {
1138 for (i = 0; i < nr_sought; i++)
1139 if (status[i])
1140 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1141 }
1142 free(status);
1143 oid_array_clear(&ref);
1144}
1145
1146struct ref *fetch_pack(struct fetch_pack_args *args,
1147 int fd[], struct child_process *conn,
1148 const struct ref *ref,
1149 const char *dest,
1150 struct ref **sought, int nr_sought,
1151 struct oid_array *shallow,
1152 char **pack_lockfile)
1153{
1154 struct ref *ref_cpy;
1155 struct shallow_info si;
1156
1157 fetch_pack_setup();
1158 if (nr_sought)
1159 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1160
1161 if (!ref) {
1162 packet_flush(fd[1]);
1163 die(_("no matching remote head"));
1164 }
1165 prepare_shallow_info(&si, shallow);
1166 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1167 &si, pack_lockfile);
1168 reprepare_packed_git();
1169 update_shallow(args, sought, nr_sought, &si);
1170 clear_shallow_info(&si);
1171 return ref_cpy;
1172}
1173
1174int report_unmatched_refs(struct ref **sought, int nr_sought)
1175{
1176 int i, ret = 0;
1177
1178 for (i = 0; i < nr_sought; i++) {
1179 if (!sought[i])
1180 continue;
1181 switch (sought[i]->match_status) {
1182 case REF_MATCHED:
1183 continue;
1184 case REF_NOT_MATCHED:
1185 error(_("no such remote ref %s"), sought[i]->name);
1186 break;
1187 case REF_UNADVERTISED_NOT_ALLOWED:
1188 error(_("Server does not allow request for unadvertised object %s"),
1189 sought[i]->name);
1190 break;
1191 }
1192 ret = 1;
1193 }
1194 return ret;
1195}