]> git.ipfire.org Git - thirdparty/git.git/blob - fetch-pack.c
fetch-pack: do not take shallow lock unnecessarily
[thirdparty/git.git] / fetch-pack.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "lockfile.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "exec-cmd.h"
10 #include "pack.h"
11 #include "sideband.h"
12 #include "fetch-pack.h"
13 #include "remote.h"
14 #include "run-command.h"
15 #include "connect.h"
16 #include "transport.h"
17 #include "version.h"
18 #include "sha1-array.h"
19 #include "oidset.h"
20 #include "packfile.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
24 #include "fsck.h"
25
26 static int transfer_unpack_limit = -1;
27 static int fetch_unpack_limit = -1;
28 static int unpack_limit = 100;
29 static int prefer_ofs_delta = 1;
30 static int no_done;
31 static int deepen_since_ok;
32 static int deepen_not_ok;
33 static int fetch_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int agent_supported;
36 static int server_supports_filtering;
37 static struct lock_file shallow_lock;
38 static const char *alternate_shallow_file;
39 static char *negotiation_algorithm;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
41
42 /* Remember to update object flag allocation in object.h */
43 #define COMPLETE (1U << 0)
44 #define ALTERNATE (1U << 1)
45
46 /*
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
49 */
50 #define MAX_IN_VAIN 256
51
52 static int multi_ack, use_sideband;
53 /* Allow specifying sha1 if it is a ref tip. */
54 #define ALLOW_TIP_SHA1 01
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 #define ALLOW_REACHABLE_SHA1 02
57 static unsigned int allow_unadvertised_object_request;
58
59 __attribute__((format (printf, 2, 3)))
60 static inline void print_verbose(const struct fetch_pack_args *args,
61 const char *fmt, ...)
62 {
63 va_list params;
64
65 if (!args->verbose)
66 return;
67
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
70 va_end(params);
71 fputc('\n', stderr);
72 }
73
74 struct alternate_object_cache {
75 struct object **items;
76 size_t nr, alloc;
77 };
78
79 static void cache_one_alternate(const struct object_id *oid,
80 void *vcache)
81 {
82 struct alternate_object_cache *cache = vcache;
83 struct object *obj = parse_object(the_repository, oid);
84
85 if (!obj || (obj->flags & ALTERNATE))
86 return;
87
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
91 }
92
93 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
95 struct object *))
96 {
97 static int initialized;
98 static struct alternate_object_cache cache;
99 size_t i;
100
101 if (!initialized) {
102 for_each_alternate_ref(cache_one_alternate, &cache);
103 initialized = 1;
104 }
105
106 for (i = 0; i < cache.nr; i++)
107 cb(negotiator, cache.items[i]);
108 }
109
110 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
111 const char *refname,
112 const struct object_id *oid)
113 {
114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
116 refname, 0);
117
118 if (o && o->type == OBJ_COMMIT)
119 negotiator->add_tip(negotiator, (struct commit *)o);
120
121 return 0;
122 }
123
124 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
126 {
127 return rev_list_insert_ref(cb_data, refname, oid);
128 }
129
130 enum ack_type {
131 NAK = 0,
132 ACK,
133 ACK_continue,
134 ACK_common,
135 ACK_ready
136 };
137
138 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
139 {
140 if (args->stateless_rpc && args->deepen) {
141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
144 */
145 char *line;
146 while ((line = packet_read_line(fd, NULL))) {
147 if (starts_with(line, "shallow "))
148 continue;
149 if (starts_with(line, "unshallow "))
150 continue;
151 die(_("git fetch-pack: expected shallow list"));
152 }
153 }
154 }
155
156 static enum ack_type get_ack(int fd, struct object_id *result_oid)
157 {
158 int len;
159 char *line = packet_read_line(fd, &len);
160 const char *arg;
161
162 if (!line)
163 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
164 if (!strcmp(line, "NAK"))
165 return NAK;
166 if (skip_prefix(line, "ACK ", &arg)) {
167 if (!get_oid_hex(arg, result_oid)) {
168 arg += 40;
169 len -= arg - line;
170 if (len < 1)
171 return ACK;
172 if (strstr(arg, "continue"))
173 return ACK_continue;
174 if (strstr(arg, "common"))
175 return ACK_common;
176 if (strstr(arg, "ready"))
177 return ACK_ready;
178 return ACK;
179 }
180 }
181 if (skip_prefix(line, "ERR ", &arg))
182 die(_("remote error: %s"), arg);
183 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
184 }
185
186 static void send_request(struct fetch_pack_args *args,
187 int fd, struct strbuf *buf)
188 {
189 if (args->stateless_rpc) {
190 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
191 packet_flush(fd);
192 } else
193 write_or_die(fd, buf->buf, buf->len);
194 }
195
196 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
197 struct object *obj)
198 {
199 rev_list_insert_ref(negotiator, NULL, &obj->oid);
200 }
201
202 #define INITIAL_FLUSH 16
203 #define PIPESAFE_FLUSH 32
204 #define LARGE_FLUSH 16384
205
206 static int next_flush(int stateless_rpc, int count)
207 {
208 if (stateless_rpc) {
209 if (count < LARGE_FLUSH)
210 count <<= 1;
211 else
212 count = count * 11 / 10;
213 } else {
214 if (count < PIPESAFE_FLUSH)
215 count <<= 1;
216 else
217 count += PIPESAFE_FLUSH;
218 }
219 return count;
220 }
221
222 static void mark_tips(struct fetch_negotiator *negotiator,
223 const struct oid_array *negotiation_tips)
224 {
225 int i;
226
227 if (!negotiation_tips) {
228 for_each_ref(rev_list_insert_ref_oid, negotiator);
229 return;
230 }
231
232 for (i = 0; i < negotiation_tips->nr; i++)
233 rev_list_insert_ref(negotiator, NULL,
234 &negotiation_tips->oid[i]);
235 return;
236 }
237
238 static int find_common(struct fetch_negotiator *negotiator,
239 struct fetch_pack_args *args,
240 int fd[2], struct object_id *result_oid,
241 struct ref *refs)
242 {
243 int fetching;
244 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
245 const struct object_id *oid;
246 unsigned in_vain = 0;
247 int got_continue = 0;
248 int got_ready = 0;
249 struct strbuf req_buf = STRBUF_INIT;
250 size_t state_len = 0;
251
252 if (args->stateless_rpc && multi_ack == 1)
253 die(_("--stateless-rpc requires multi_ack_detailed"));
254
255 if (!args->no_dependents) {
256 mark_tips(negotiator, args->negotiation_tips);
257 for_each_cached_alternate(negotiator, insert_one_alternate_object);
258 }
259
260 fetching = 0;
261 for ( ; refs ; refs = refs->next) {
262 struct object_id *remote = &refs->old_oid;
263 const char *remote_hex;
264 struct object *o;
265
266 /*
267 * If that object is complete (i.e. it is an ancestor of a
268 * local ref), we tell them we have it but do not have to
269 * tell them about its ancestors, which they already know
270 * about.
271 *
272 * We use lookup_object here because we are only
273 * interested in the case we *know* the object is
274 * reachable and we have already scanned it.
275 *
276 * Do this only if args->no_dependents is false (if it is true,
277 * we cannot trust the object flags).
278 */
279 if (!args->no_dependents &&
280 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
281 (o->flags & COMPLETE)) {
282 continue;
283 }
284
285 remote_hex = oid_to_hex(remote);
286 if (!fetching) {
287 struct strbuf c = STRBUF_INIT;
288 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
289 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
290 if (no_done) strbuf_addstr(&c, " no-done");
291 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
292 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
293 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
294 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
295 if (args->no_progress) strbuf_addstr(&c, " no-progress");
296 if (args->include_tag) strbuf_addstr(&c, " include-tag");
297 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
298 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
299 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
300 if (agent_supported) strbuf_addf(&c, " agent=%s",
301 git_user_agent_sanitized());
302 if (args->filter_options.choice)
303 strbuf_addstr(&c, " filter");
304 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
305 strbuf_release(&c);
306 } else
307 packet_buf_write(&req_buf, "want %s\n", remote_hex);
308 fetching++;
309 }
310
311 if (!fetching) {
312 strbuf_release(&req_buf);
313 packet_flush(fd[1]);
314 return 1;
315 }
316
317 if (is_repository_shallow(the_repository))
318 write_shallow_commits(&req_buf, 1, NULL);
319 if (args->depth > 0)
320 packet_buf_write(&req_buf, "deepen %d", args->depth);
321 if (args->deepen_since) {
322 timestamp_t max_age = approxidate(args->deepen_since);
323 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
324 }
325 if (args->deepen_not) {
326 int i;
327 for (i = 0; i < args->deepen_not->nr; i++) {
328 struct string_list_item *s = args->deepen_not->items + i;
329 packet_buf_write(&req_buf, "deepen-not %s", s->string);
330 }
331 }
332 if (server_supports_filtering && args->filter_options.choice)
333 packet_buf_write(&req_buf, "filter %s",
334 args->filter_options.filter_spec);
335 packet_buf_flush(&req_buf);
336 state_len = req_buf.len;
337
338 if (args->deepen) {
339 char *line;
340 const char *arg;
341 struct object_id oid;
342
343 send_request(args, fd[1], &req_buf);
344 while ((line = packet_read_line(fd[0], NULL))) {
345 if (skip_prefix(line, "shallow ", &arg)) {
346 if (get_oid_hex(arg, &oid))
347 die(_("invalid shallow line: %s"), line);
348 register_shallow(the_repository, &oid);
349 continue;
350 }
351 if (skip_prefix(line, "unshallow ", &arg)) {
352 if (get_oid_hex(arg, &oid))
353 die(_("invalid unshallow line: %s"), line);
354 if (!lookup_object(the_repository, oid.hash))
355 die(_("object not found: %s"), line);
356 /* make sure that it is parsed as shallow */
357 if (!parse_object(the_repository, &oid))
358 die(_("error in object: %s"), line);
359 if (unregister_shallow(&oid))
360 die(_("no shallow found: %s"), line);
361 continue;
362 }
363 die(_("expected shallow/unshallow, got %s"), line);
364 }
365 } else if (!args->stateless_rpc)
366 send_request(args, fd[1], &req_buf);
367
368 if (!args->stateless_rpc) {
369 /* If we aren't using the stateless-rpc interface
370 * we don't need to retain the headers.
371 */
372 strbuf_setlen(&req_buf, 0);
373 state_len = 0;
374 }
375
376 flushes = 0;
377 retval = -1;
378 if (args->no_dependents)
379 goto done;
380 while ((oid = negotiator->next(negotiator))) {
381 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
382 print_verbose(args, "have %s", oid_to_hex(oid));
383 in_vain++;
384 if (flush_at <= ++count) {
385 int ack;
386
387 packet_buf_flush(&req_buf);
388 send_request(args, fd[1], &req_buf);
389 strbuf_setlen(&req_buf, state_len);
390 flushes++;
391 flush_at = next_flush(args->stateless_rpc, count);
392
393 /*
394 * We keep one window "ahead" of the other side, and
395 * will wait for an ACK only on the next one
396 */
397 if (!args->stateless_rpc && count == INITIAL_FLUSH)
398 continue;
399
400 consume_shallow_list(args, fd[0]);
401 do {
402 ack = get_ack(fd[0], result_oid);
403 if (ack)
404 print_verbose(args, _("got %s %d %s"), "ack",
405 ack, oid_to_hex(result_oid));
406 switch (ack) {
407 case ACK:
408 flushes = 0;
409 multi_ack = 0;
410 retval = 0;
411 goto done;
412 case ACK_common:
413 case ACK_ready:
414 case ACK_continue: {
415 struct commit *commit =
416 lookup_commit(the_repository,
417 result_oid);
418 int was_common;
419
420 if (!commit)
421 die(_("invalid commit %s"), oid_to_hex(result_oid));
422 was_common = negotiator->ack(negotiator, commit);
423 if (args->stateless_rpc
424 && ack == ACK_common
425 && !was_common) {
426 /* We need to replay the have for this object
427 * on the next RPC request so the peer knows
428 * it is in common with us.
429 */
430 const char *hex = oid_to_hex(result_oid);
431 packet_buf_write(&req_buf, "have %s\n", hex);
432 state_len = req_buf.len;
433 /*
434 * Reset in_vain because an ack
435 * for this commit has not been
436 * seen.
437 */
438 in_vain = 0;
439 } else if (!args->stateless_rpc
440 || ack != ACK_common)
441 in_vain = 0;
442 retval = 0;
443 got_continue = 1;
444 if (ack == ACK_ready)
445 got_ready = 1;
446 break;
447 }
448 }
449 } while (ack);
450 flushes--;
451 if (got_continue && MAX_IN_VAIN < in_vain) {
452 print_verbose(args, _("giving up"));
453 break; /* give up */
454 }
455 if (got_ready)
456 break;
457 }
458 }
459 done:
460 if (!got_ready || !no_done) {
461 packet_buf_write(&req_buf, "done\n");
462 send_request(args, fd[1], &req_buf);
463 }
464 print_verbose(args, _("done"));
465 if (retval != 0) {
466 multi_ack = 0;
467 flushes++;
468 }
469 strbuf_release(&req_buf);
470
471 if (!got_ready || !no_done)
472 consume_shallow_list(args, fd[0]);
473 while (flushes || multi_ack) {
474 int ack = get_ack(fd[0], result_oid);
475 if (ack) {
476 print_verbose(args, _("got %s (%d) %s"), "ack",
477 ack, oid_to_hex(result_oid));
478 if (ack == ACK)
479 return 0;
480 multi_ack = 1;
481 continue;
482 }
483 flushes--;
484 }
485 /* it is no error to fetch into a completely empty repo */
486 return count ? retval : 0;
487 }
488
489 static struct commit_list *complete;
490
491 static int mark_complete(const struct object_id *oid)
492 {
493 struct object *o = parse_object(the_repository, oid);
494
495 while (o && o->type == OBJ_TAG) {
496 struct tag *t = (struct tag *) o;
497 if (!t->tagged)
498 break; /* broken repository */
499 o->flags |= COMPLETE;
500 o = parse_object(the_repository, &t->tagged->oid);
501 }
502 if (o && o->type == OBJ_COMMIT) {
503 struct commit *commit = (struct commit *)o;
504 if (!(commit->object.flags & COMPLETE)) {
505 commit->object.flags |= COMPLETE;
506 commit_list_insert(commit, &complete);
507 }
508 }
509 return 0;
510 }
511
512 static int mark_complete_oid(const char *refname, const struct object_id *oid,
513 int flag, void *cb_data)
514 {
515 return mark_complete(oid);
516 }
517
518 static void mark_recent_complete_commits(struct fetch_pack_args *args,
519 timestamp_t cutoff)
520 {
521 while (complete && cutoff <= complete->item->date) {
522 print_verbose(args, _("Marking %s as complete"),
523 oid_to_hex(&complete->item->object.oid));
524 pop_most_recent_commit(&complete, COMPLETE);
525 }
526 }
527
528 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
529 {
530 for (; refs; refs = refs->next)
531 oidset_insert(oids, &refs->old_oid);
532 }
533
534 static int is_unmatched_ref(const struct ref *ref)
535 {
536 struct object_id oid;
537 const char *p;
538 return ref->match_status == REF_NOT_MATCHED &&
539 !parse_oid_hex(ref->name, &oid, &p) &&
540 *p == '\0' &&
541 oideq(&oid, &ref->old_oid);
542 }
543
544 static void filter_refs(struct fetch_pack_args *args,
545 struct ref **refs,
546 struct ref **sought, int nr_sought)
547 {
548 struct ref *newlist = NULL;
549 struct ref **newtail = &newlist;
550 struct ref *unmatched = NULL;
551 struct ref *ref, *next;
552 struct oidset tip_oids = OIDSET_INIT;
553 int i;
554 int strict = !(allow_unadvertised_object_request &
555 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
556
557 i = 0;
558 for (ref = *refs; ref; ref = next) {
559 int keep = 0;
560 next = ref->next;
561
562 if (starts_with(ref->name, "refs/") &&
563 check_refname_format(ref->name, 0))
564 ; /* trash */
565 else {
566 while (i < nr_sought) {
567 int cmp = strcmp(ref->name, sought[i]->name);
568 if (cmp < 0)
569 break; /* definitely do not have it */
570 else if (cmp == 0) {
571 keep = 1; /* definitely have it */
572 sought[i]->match_status = REF_MATCHED;
573 }
574 i++;
575 }
576
577 if (!keep && args->fetch_all &&
578 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
579 keep = 1;
580 }
581
582 if (keep) {
583 *newtail = ref;
584 ref->next = NULL;
585 newtail = &ref->next;
586 } else {
587 ref->next = unmatched;
588 unmatched = ref;
589 }
590 }
591
592 if (strict) {
593 for (i = 0; i < nr_sought; i++) {
594 ref = sought[i];
595 if (!is_unmatched_ref(ref))
596 continue;
597
598 add_refs_to_oidset(&tip_oids, unmatched);
599 add_refs_to_oidset(&tip_oids, newlist);
600 break;
601 }
602 }
603
604 /* Append unmatched requests to the list */
605 for (i = 0; i < nr_sought; i++) {
606 ref = sought[i];
607 if (!is_unmatched_ref(ref))
608 continue;
609
610 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
611 ref->match_status = REF_MATCHED;
612 *newtail = copy_ref(ref);
613 newtail = &(*newtail)->next;
614 } else {
615 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
616 }
617 }
618
619 oidset_clear(&tip_oids);
620 for (ref = unmatched; ref; ref = next) {
621 next = ref->next;
622 free(ref);
623 }
624
625 *refs = newlist;
626 }
627
628 static void mark_alternate_complete(struct fetch_negotiator *unused,
629 struct object *obj)
630 {
631 mark_complete(&obj->oid);
632 }
633
634 struct loose_object_iter {
635 struct oidset *loose_object_set;
636 struct ref *refs;
637 };
638
639 /*
640 * Mark recent commits available locally and reachable from a local ref as
641 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
642 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
643 * thus do not need COMMON_REF marks).
644 *
645 * The cutoff time for recency is determined by this heuristic: it is the
646 * earliest commit time of the objects in refs that are commits and that we know
647 * the commit time of.
648 */
649 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
650 struct fetch_pack_args *args,
651 struct ref **refs)
652 {
653 struct ref *ref;
654 int old_save_commit_buffer = save_commit_buffer;
655 timestamp_t cutoff = 0;
656
657 save_commit_buffer = 0;
658
659 for (ref = *refs; ref; ref = ref->next) {
660 struct object *o;
661
662 if (!has_object_file_with_flags(&ref->old_oid,
663 OBJECT_INFO_QUICK))
664 continue;
665 o = parse_object(the_repository, &ref->old_oid);
666 if (!o)
667 continue;
668
669 /* We already have it -- which may mean that we were
670 * in sync with the other side at some time after
671 * that (it is OK if we guess wrong here).
672 */
673 if (o->type == OBJ_COMMIT) {
674 struct commit *commit = (struct commit *)o;
675 if (!cutoff || cutoff < commit->date)
676 cutoff = commit->date;
677 }
678 }
679
680 if (!args->deepen) {
681 for_each_ref(mark_complete_oid, NULL);
682 for_each_cached_alternate(NULL, mark_alternate_complete);
683 commit_list_sort_by_date(&complete);
684 if (cutoff)
685 mark_recent_complete_commits(args, cutoff);
686 }
687
688 /*
689 * Mark all complete remote refs as common refs.
690 * Don't mark them common yet; the server has to be told so first.
691 */
692 for (ref = *refs; ref; ref = ref->next) {
693 struct object *o = deref_tag(the_repository,
694 lookup_object(the_repository,
695 ref->old_oid.hash),
696 NULL, 0);
697
698 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
699 continue;
700
701 negotiator->known_common(negotiator,
702 (struct commit *)o);
703 }
704
705 save_commit_buffer = old_save_commit_buffer;
706 }
707
708 /*
709 * Returns 1 if every object pointed to by the given remote refs is available
710 * locally and reachable from a local ref, and 0 otherwise.
711 */
712 static int everything_local(struct fetch_pack_args *args,
713 struct ref **refs)
714 {
715 struct ref *ref;
716 int retval;
717
718 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
719 const struct object_id *remote = &ref->old_oid;
720 struct object *o;
721
722 o = lookup_object(the_repository, remote->hash);
723 if (!o || !(o->flags & COMPLETE)) {
724 retval = 0;
725 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
726 ref->name);
727 continue;
728 }
729 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
730 ref->name);
731 }
732
733 return retval;
734 }
735
736 static int sideband_demux(int in, int out, void *data)
737 {
738 int *xd = data;
739 int ret;
740
741 ret = recv_sideband("fetch-pack", xd[0], out);
742 close(out);
743 return ret;
744 }
745
746 static int get_pack(struct fetch_pack_args *args,
747 int xd[2], char **pack_lockfile)
748 {
749 struct async demux;
750 int do_keep = args->keep_pack;
751 const char *cmd_name;
752 struct pack_header header;
753 int pass_header = 0;
754 struct child_process cmd = CHILD_PROCESS_INIT;
755 int ret;
756
757 memset(&demux, 0, sizeof(demux));
758 if (use_sideband) {
759 /* xd[] is talking with upload-pack; subprocess reads from
760 * xd[0], spits out band#2 to stderr, and feeds us band#1
761 * through demux->out.
762 */
763 demux.proc = sideband_demux;
764 demux.data = xd;
765 demux.out = -1;
766 demux.isolate_sigpipe = 1;
767 if (start_async(&demux))
768 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
769 }
770 else
771 demux.out = xd[0];
772
773 if (!args->keep_pack && unpack_limit) {
774
775 if (read_pack_header(demux.out, &header))
776 die(_("protocol error: bad pack header"));
777 pass_header = 1;
778 if (ntohl(header.hdr_entries) < unpack_limit)
779 do_keep = 0;
780 else
781 do_keep = 1;
782 }
783
784 if (alternate_shallow_file) {
785 argv_array_push(&cmd.args, "--shallow-file");
786 argv_array_push(&cmd.args, alternate_shallow_file);
787 }
788
789 if (do_keep || args->from_promisor) {
790 if (pack_lockfile)
791 cmd.out = -1;
792 cmd_name = "index-pack";
793 argv_array_push(&cmd.args, cmd_name);
794 argv_array_push(&cmd.args, "--stdin");
795 if (!args->quiet && !args->no_progress)
796 argv_array_push(&cmd.args, "-v");
797 if (args->use_thin_pack)
798 argv_array_push(&cmd.args, "--fix-thin");
799 if (do_keep && (args->lock_pack || unpack_limit)) {
800 char hostname[HOST_NAME_MAX + 1];
801 if (xgethostname(hostname, sizeof(hostname)))
802 xsnprintf(hostname, sizeof(hostname), "localhost");
803 argv_array_pushf(&cmd.args,
804 "--keep=fetch-pack %"PRIuMAX " on %s",
805 (uintmax_t)getpid(), hostname);
806 }
807 if (args->check_self_contained_and_connected)
808 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
809 if (args->from_promisor)
810 argv_array_push(&cmd.args, "--promisor");
811 }
812 else {
813 cmd_name = "unpack-objects";
814 argv_array_push(&cmd.args, cmd_name);
815 if (args->quiet || args->no_progress)
816 argv_array_push(&cmd.args, "-q");
817 args->check_self_contained_and_connected = 0;
818 }
819
820 if (pass_header)
821 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
822 ntohl(header.hdr_version),
823 ntohl(header.hdr_entries));
824 if (fetch_fsck_objects >= 0
825 ? fetch_fsck_objects
826 : transfer_fsck_objects >= 0
827 ? transfer_fsck_objects
828 : 0) {
829 if (args->from_promisor)
830 /*
831 * We cannot use --strict in index-pack because it
832 * checks both broken objects and links, but we only
833 * want to check for broken objects.
834 */
835 argv_array_push(&cmd.args, "--fsck-objects");
836 else
837 argv_array_pushf(&cmd.args, "--strict%s",
838 fsck_msg_types.buf);
839 }
840
841 cmd.in = demux.out;
842 cmd.git_cmd = 1;
843 if (start_command(&cmd))
844 die(_("fetch-pack: unable to fork off %s"), cmd_name);
845 if (do_keep && pack_lockfile) {
846 *pack_lockfile = index_pack_lockfile(cmd.out);
847 close(cmd.out);
848 }
849
850 if (!use_sideband)
851 /* Closed by start_command() */
852 xd[0] = -1;
853
854 ret = finish_command(&cmd);
855 if (!ret || (args->check_self_contained_and_connected && ret == 1))
856 args->self_contained_and_connected =
857 args->check_self_contained_and_connected &&
858 ret == 0;
859 else
860 die(_("%s failed"), cmd_name);
861 if (use_sideband && finish_async(&demux))
862 die(_("error in sideband demultiplexer"));
863 return 0;
864 }
865
866 static int cmp_ref_by_name(const void *a_, const void *b_)
867 {
868 const struct ref *a = *((const struct ref **)a_);
869 const struct ref *b = *((const struct ref **)b_);
870 return strcmp(a->name, b->name);
871 }
872
873 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
874 int fd[2],
875 const struct ref *orig_ref,
876 struct ref **sought, int nr_sought,
877 struct shallow_info *si,
878 char **pack_lockfile)
879 {
880 struct ref *ref = copy_ref_list(orig_ref);
881 struct object_id oid;
882 const char *agent_feature;
883 int agent_len;
884 struct fetch_negotiator negotiator;
885 fetch_negotiator_init(&negotiator, negotiation_algorithm);
886
887 sort_ref_list(&ref, ref_compare_name);
888 QSORT(sought, nr_sought, cmp_ref_by_name);
889
890 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
891 die(_("Server does not support shallow clients"));
892 if (args->depth > 0 || args->deepen_since || args->deepen_not)
893 args->deepen = 1;
894 if (server_supports("multi_ack_detailed")) {
895 print_verbose(args, _("Server supports multi_ack_detailed"));
896 multi_ack = 2;
897 if (server_supports("no-done")) {
898 print_verbose(args, _("Server supports no-done"));
899 if (args->stateless_rpc)
900 no_done = 1;
901 }
902 }
903 else if (server_supports("multi_ack")) {
904 print_verbose(args, _("Server supports multi_ack"));
905 multi_ack = 1;
906 }
907 if (server_supports("side-band-64k")) {
908 print_verbose(args, _("Server supports side-band-64k"));
909 use_sideband = 2;
910 }
911 else if (server_supports("side-band")) {
912 print_verbose(args, _("Server supports side-band"));
913 use_sideband = 1;
914 }
915 if (server_supports("allow-tip-sha1-in-want")) {
916 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
917 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
918 }
919 if (server_supports("allow-reachable-sha1-in-want")) {
920 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
921 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
922 }
923 if (!server_supports("thin-pack"))
924 args->use_thin_pack = 0;
925 if (!server_supports("no-progress"))
926 args->no_progress = 0;
927 if (!server_supports("include-tag"))
928 args->include_tag = 0;
929 if (server_supports("ofs-delta"))
930 print_verbose(args, _("Server supports ofs-delta"));
931 else
932 prefer_ofs_delta = 0;
933
934 if (server_supports("filter")) {
935 server_supports_filtering = 1;
936 print_verbose(args, _("Server supports filter"));
937 } else if (args->filter_options.choice) {
938 warning("filtering not recognized by server, ignoring");
939 }
940
941 if ((agent_feature = server_feature_value("agent", &agent_len))) {
942 agent_supported = 1;
943 if (agent_len)
944 print_verbose(args, _("Server version is %.*s"),
945 agent_len, agent_feature);
946 }
947 if (server_supports("deepen-since"))
948 deepen_since_ok = 1;
949 else if (args->deepen_since)
950 die(_("Server does not support --shallow-since"));
951 if (server_supports("deepen-not"))
952 deepen_not_ok = 1;
953 else if (args->deepen_not)
954 die(_("Server does not support --shallow-exclude"));
955 if (!server_supports("deepen-relative") && args->deepen_relative)
956 die(_("Server does not support --deepen"));
957
958 if (!args->no_dependents) {
959 mark_complete_and_common_ref(&negotiator, args, &ref);
960 filter_refs(args, &ref, sought, nr_sought);
961 if (everything_local(args, &ref)) {
962 packet_flush(fd[1]);
963 goto all_done;
964 }
965 } else {
966 filter_refs(args, &ref, sought, nr_sought);
967 }
968 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
969 if (!args->keep_pack)
970 /* When cloning, it is not unusual to have
971 * no common commit.
972 */
973 warning(_("no common commits"));
974
975 if (args->stateless_rpc)
976 packet_flush(fd[1]);
977 if (args->deepen)
978 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
979 NULL);
980 else if (si->nr_ours || si->nr_theirs)
981 alternate_shallow_file = setup_temporary_shallow(si->shallow);
982 else
983 alternate_shallow_file = NULL;
984 if (get_pack(args, fd, pack_lockfile))
985 die(_("git fetch-pack: fetch failed."));
986
987 all_done:
988 negotiator.release(&negotiator);
989 return ref;
990 }
991
992 static void add_shallow_requests(struct strbuf *req_buf,
993 const struct fetch_pack_args *args)
994 {
995 if (is_repository_shallow(the_repository))
996 write_shallow_commits(req_buf, 1, NULL);
997 if (args->depth > 0)
998 packet_buf_write(req_buf, "deepen %d", args->depth);
999 if (args->deepen_since) {
1000 timestamp_t max_age = approxidate(args->deepen_since);
1001 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1002 }
1003 if (args->deepen_not) {
1004 int i;
1005 for (i = 0; i < args->deepen_not->nr; i++) {
1006 struct string_list_item *s = args->deepen_not->items + i;
1007 packet_buf_write(req_buf, "deepen-not %s", s->string);
1008 }
1009 }
1010 }
1011
1012 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1013 {
1014 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1015
1016 for ( ; wants ; wants = wants->next) {
1017 const struct object_id *remote = &wants->old_oid;
1018 struct object *o;
1019
1020 /*
1021 * If that object is complete (i.e. it is an ancestor of a
1022 * local ref), we tell them we have it but do not have to
1023 * tell them about its ancestors, which they already know
1024 * about.
1025 *
1026 * We use lookup_object here because we are only
1027 * interested in the case we *know* the object is
1028 * reachable and we have already scanned it.
1029 *
1030 * Do this only if args->no_dependents is false (if it is true,
1031 * we cannot trust the object flags).
1032 */
1033 if (!no_dependents &&
1034 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1035 (o->flags & COMPLETE)) {
1036 continue;
1037 }
1038
1039 if (!use_ref_in_want || wants->exact_oid)
1040 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1041 else
1042 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1043 }
1044 }
1045
1046 static void add_common(struct strbuf *req_buf, struct oidset *common)
1047 {
1048 struct oidset_iter iter;
1049 const struct object_id *oid;
1050 oidset_iter_init(common, &iter);
1051
1052 while ((oid = oidset_iter_next(&iter))) {
1053 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1054 }
1055 }
1056
1057 static int add_haves(struct fetch_negotiator *negotiator,
1058 struct strbuf *req_buf,
1059 int *haves_to_send, int *in_vain)
1060 {
1061 int ret = 0;
1062 int haves_added = 0;
1063 const struct object_id *oid;
1064
1065 while ((oid = negotiator->next(negotiator))) {
1066 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1067 if (++haves_added >= *haves_to_send)
1068 break;
1069 }
1070
1071 *in_vain += haves_added;
1072 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1073 /* Send Done */
1074 packet_buf_write(req_buf, "done\n");
1075 ret = 1;
1076 }
1077
1078 /* Increase haves to send on next round */
1079 *haves_to_send = next_flush(1, *haves_to_send);
1080
1081 return ret;
1082 }
1083
1084 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1085 const struct fetch_pack_args *args,
1086 const struct ref *wants, struct oidset *common,
1087 int *haves_to_send, int *in_vain)
1088 {
1089 int ret = 0;
1090 struct strbuf req_buf = STRBUF_INIT;
1091
1092 if (server_supports_v2("fetch", 1))
1093 packet_buf_write(&req_buf, "command=fetch");
1094 if (server_supports_v2("agent", 0))
1095 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1096 if (args->server_options && args->server_options->nr &&
1097 server_supports_v2("server-option", 1)) {
1098 int i;
1099 for (i = 0; i < args->server_options->nr; i++)
1100 packet_write_fmt(fd_out, "server-option=%s",
1101 args->server_options->items[i].string);
1102 }
1103
1104 packet_buf_delim(&req_buf);
1105 if (args->use_thin_pack)
1106 packet_buf_write(&req_buf, "thin-pack");
1107 if (args->no_progress)
1108 packet_buf_write(&req_buf, "no-progress");
1109 if (args->include_tag)
1110 packet_buf_write(&req_buf, "include-tag");
1111 if (prefer_ofs_delta)
1112 packet_buf_write(&req_buf, "ofs-delta");
1113
1114 /* Add shallow-info and deepen request */
1115 if (server_supports_feature("fetch", "shallow", 0))
1116 add_shallow_requests(&req_buf, args);
1117 else if (is_repository_shallow(the_repository) || args->deepen)
1118 die(_("Server does not support shallow requests"));
1119
1120 /* Add filter */
1121 if (server_supports_feature("fetch", "filter", 0) &&
1122 args->filter_options.choice) {
1123 print_verbose(args, _("Server supports filter"));
1124 packet_buf_write(&req_buf, "filter %s",
1125 args->filter_options.filter_spec);
1126 } else if (args->filter_options.choice) {
1127 warning("filtering not recognized by server, ignoring");
1128 }
1129
1130 /* add wants */
1131 add_wants(args->no_dependents, wants, &req_buf);
1132
1133 if (args->no_dependents) {
1134 packet_buf_write(&req_buf, "done");
1135 ret = 1;
1136 } else {
1137 /* Add all of the common commits we've found in previous rounds */
1138 add_common(&req_buf, common);
1139
1140 /* Add initial haves */
1141 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1142 }
1143
1144 /* Send request */
1145 packet_buf_flush(&req_buf);
1146 write_or_die(fd_out, req_buf.buf, req_buf.len);
1147
1148 strbuf_release(&req_buf);
1149 return ret;
1150 }
1151
1152 /*
1153 * Processes a section header in a server's response and checks if it matches
1154 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1155 * not consumed); if 0, the line will be consumed and the function will die if
1156 * the section header doesn't match what was expected.
1157 */
1158 static int process_section_header(struct packet_reader *reader,
1159 const char *section, int peek)
1160 {
1161 int ret;
1162
1163 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1164 die(_("error reading section header '%s'"), section);
1165
1166 ret = !strcmp(reader->line, section);
1167
1168 if (!peek) {
1169 if (!ret)
1170 die(_("expected '%s', received '%s'"),
1171 section, reader->line);
1172 packet_reader_read(reader);
1173 }
1174
1175 return ret;
1176 }
1177
1178 static int process_acks(struct fetch_negotiator *negotiator,
1179 struct packet_reader *reader,
1180 struct oidset *common)
1181 {
1182 /* received */
1183 int received_ready = 0;
1184 int received_ack = 0;
1185
1186 process_section_header(reader, "acknowledgments", 0);
1187 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1188 const char *arg;
1189
1190 if (!strcmp(reader->line, "NAK"))
1191 continue;
1192
1193 if (skip_prefix(reader->line, "ACK ", &arg)) {
1194 struct object_id oid;
1195 if (!get_oid_hex(arg, &oid)) {
1196 struct commit *commit;
1197 oidset_insert(common, &oid);
1198 commit = lookup_commit(the_repository, &oid);
1199 negotiator->ack(negotiator, commit);
1200 }
1201 continue;
1202 }
1203
1204 if (!strcmp(reader->line, "ready")) {
1205 received_ready = 1;
1206 continue;
1207 }
1208
1209 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1210 }
1211
1212 if (reader->status != PACKET_READ_FLUSH &&
1213 reader->status != PACKET_READ_DELIM)
1214 die(_("error processing acks: %d"), reader->status);
1215
1216 /*
1217 * If an "acknowledgments" section is sent, a packfile is sent if and
1218 * only if "ready" was sent in this section. The other sections
1219 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1220 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1221 * otherwise.
1222 */
1223 if (received_ready && reader->status != PACKET_READ_DELIM)
1224 die(_("expected packfile to be sent after 'ready'"));
1225 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1226 die(_("expected no other sections to be sent after no 'ready'"));
1227
1228 /* return 0 if no common, 1 if there are common, or 2 if ready */
1229 return received_ready ? 2 : (received_ack ? 1 : 0);
1230 }
1231
1232 static void receive_shallow_info(struct fetch_pack_args *args,
1233 struct packet_reader *reader)
1234 {
1235 int line_received = 0;
1236
1237 process_section_header(reader, "shallow-info", 0);
1238 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1239 const char *arg;
1240 struct object_id oid;
1241
1242 if (skip_prefix(reader->line, "shallow ", &arg)) {
1243 if (get_oid_hex(arg, &oid))
1244 die(_("invalid shallow line: %s"), reader->line);
1245 register_shallow(the_repository, &oid);
1246 line_received = 1;
1247 continue;
1248 }
1249 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1250 if (get_oid_hex(arg, &oid))
1251 die(_("invalid unshallow line: %s"), reader->line);
1252 if (!lookup_object(the_repository, oid.hash))
1253 die(_("object not found: %s"), reader->line);
1254 /* make sure that it is parsed as shallow */
1255 if (!parse_object(the_repository, &oid))
1256 die(_("error in object: %s"), reader->line);
1257 if (unregister_shallow(&oid))
1258 die(_("no shallow found: %s"), reader->line);
1259 line_received = 1;
1260 continue;
1261 }
1262 die(_("expected shallow/unshallow, got %s"), reader->line);
1263 }
1264
1265 if (reader->status != PACKET_READ_FLUSH &&
1266 reader->status != PACKET_READ_DELIM)
1267 die(_("error processing shallow info: %d"), reader->status);
1268
1269 if (line_received) {
1270 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1271 NULL);
1272 args->deepen = 1;
1273 }
1274 }
1275
1276 static void receive_wanted_refs(struct packet_reader *reader,
1277 struct ref **sought, int nr_sought)
1278 {
1279 process_section_header(reader, "wanted-refs", 0);
1280 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1281 struct object_id oid;
1282 const char *end;
1283 int i;
1284
1285 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1286 die(_("expected wanted-ref, got '%s'"), reader->line);
1287
1288 for (i = 0; i < nr_sought; i++) {
1289 if (!strcmp(end, sought[i]->name)) {
1290 oidcpy(&sought[i]->old_oid, &oid);
1291 break;
1292 }
1293 }
1294
1295 if (i == nr_sought)
1296 die(_("unexpected wanted-ref: '%s'"), reader->line);
1297 }
1298
1299 if (reader->status != PACKET_READ_DELIM)
1300 die(_("error processing wanted refs: %d"), reader->status);
1301 }
1302
1303 enum fetch_state {
1304 FETCH_CHECK_LOCAL = 0,
1305 FETCH_SEND_REQUEST,
1306 FETCH_PROCESS_ACKS,
1307 FETCH_GET_PACK,
1308 FETCH_DONE,
1309 };
1310
1311 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1312 int fd[2],
1313 const struct ref *orig_ref,
1314 struct ref **sought, int nr_sought,
1315 char **pack_lockfile)
1316 {
1317 struct ref *ref = copy_ref_list(orig_ref);
1318 enum fetch_state state = FETCH_CHECK_LOCAL;
1319 struct oidset common = OIDSET_INIT;
1320 struct packet_reader reader;
1321 int in_vain = 0;
1322 int haves_to_send = INITIAL_FLUSH;
1323 struct fetch_negotiator negotiator;
1324 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1325 packet_reader_init(&reader, fd[0], NULL, 0,
1326 PACKET_READ_CHOMP_NEWLINE);
1327
1328 while (state != FETCH_DONE) {
1329 switch (state) {
1330 case FETCH_CHECK_LOCAL:
1331 sort_ref_list(&ref, ref_compare_name);
1332 QSORT(sought, nr_sought, cmp_ref_by_name);
1333
1334 /* v2 supports these by default */
1335 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1336 use_sideband = 2;
1337 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1338 args->deepen = 1;
1339
1340 /* Filter 'ref' by 'sought' and those that aren't local */
1341 if (!args->no_dependents) {
1342 mark_complete_and_common_ref(&negotiator, args, &ref);
1343 filter_refs(args, &ref, sought, nr_sought);
1344 if (everything_local(args, &ref))
1345 state = FETCH_DONE;
1346 else
1347 state = FETCH_SEND_REQUEST;
1348
1349 mark_tips(&negotiator, args->negotiation_tips);
1350 for_each_cached_alternate(&negotiator,
1351 insert_one_alternate_object);
1352 } else {
1353 filter_refs(args, &ref, sought, nr_sought);
1354 state = FETCH_SEND_REQUEST;
1355 }
1356 break;
1357 case FETCH_SEND_REQUEST:
1358 if (send_fetch_request(&negotiator, fd[1], args, ref,
1359 &common,
1360 &haves_to_send, &in_vain))
1361 state = FETCH_GET_PACK;
1362 else
1363 state = FETCH_PROCESS_ACKS;
1364 break;
1365 case FETCH_PROCESS_ACKS:
1366 /* Process ACKs/NAKs */
1367 switch (process_acks(&negotiator, &reader, &common)) {
1368 case 2:
1369 state = FETCH_GET_PACK;
1370 break;
1371 case 1:
1372 in_vain = 0;
1373 /* fallthrough */
1374 default:
1375 state = FETCH_SEND_REQUEST;
1376 break;
1377 }
1378 break;
1379 case FETCH_GET_PACK:
1380 /* Check for shallow-info section */
1381 if (process_section_header(&reader, "shallow-info", 1))
1382 receive_shallow_info(args, &reader);
1383
1384 if (process_section_header(&reader, "wanted-refs", 1))
1385 receive_wanted_refs(&reader, sought, nr_sought);
1386
1387 /* get the pack */
1388 process_section_header(&reader, "packfile", 0);
1389 if (get_pack(args, fd, pack_lockfile))
1390 die(_("git fetch-pack: fetch failed."));
1391
1392 state = FETCH_DONE;
1393 break;
1394 case FETCH_DONE:
1395 continue;
1396 }
1397 }
1398
1399 negotiator.release(&negotiator);
1400 oidset_clear(&common);
1401 return ref;
1402 }
1403
1404 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1405 {
1406 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1407 const char *path;
1408
1409 if (git_config_pathname(&path, var, value))
1410 return 1;
1411 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1412 fsck_msg_types.len ? ',' : '=', path);
1413 free((char *)path);
1414 return 0;
1415 }
1416
1417 if (skip_prefix(var, "fetch.fsck.", &var)) {
1418 if (is_valid_msg_type(var, value))
1419 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1420 fsck_msg_types.len ? ',' : '=', var, value);
1421 else
1422 warning("Skipping unknown msg id '%s'", var);
1423 return 0;
1424 }
1425
1426 return git_default_config(var, value, cb);
1427 }
1428
1429 static void fetch_pack_config(void)
1430 {
1431 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1432 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1433 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1434 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1435 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1436 git_config_get_string("fetch.negotiationalgorithm",
1437 &negotiation_algorithm);
1438
1439 git_config(fetch_pack_config_cb, NULL);
1440 }
1441
1442 static void fetch_pack_setup(void)
1443 {
1444 static int did_setup;
1445 if (did_setup)
1446 return;
1447 fetch_pack_config();
1448 if (0 <= transfer_unpack_limit)
1449 unpack_limit = transfer_unpack_limit;
1450 else if (0 <= fetch_unpack_limit)
1451 unpack_limit = fetch_unpack_limit;
1452 did_setup = 1;
1453 }
1454
1455 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1456 {
1457 struct string_list names = STRING_LIST_INIT_NODUP;
1458 int src, dst;
1459
1460 for (src = dst = 0; src < nr; src++) {
1461 struct string_list_item *item;
1462 item = string_list_insert(&names, ref[src]->name);
1463 if (item->util)
1464 continue; /* already have it */
1465 item->util = ref[src];
1466 if (src != dst)
1467 ref[dst] = ref[src];
1468 dst++;
1469 }
1470 for (src = dst; src < nr; src++)
1471 ref[src] = NULL;
1472 string_list_clear(&names, 0);
1473 return dst;
1474 }
1475
1476 static void update_shallow(struct fetch_pack_args *args,
1477 struct ref **sought, int nr_sought,
1478 struct shallow_info *si)
1479 {
1480 struct oid_array ref = OID_ARRAY_INIT;
1481 int *status;
1482 int i;
1483
1484 if (args->deepen && alternate_shallow_file) {
1485 if (*alternate_shallow_file == '\0') { /* --unshallow */
1486 unlink_or_warn(git_path_shallow(the_repository));
1487 rollback_lock_file(&shallow_lock);
1488 } else
1489 commit_lock_file(&shallow_lock);
1490 return;
1491 }
1492
1493 if (!si->shallow || !si->shallow->nr)
1494 return;
1495
1496 if (args->cloning) {
1497 /*
1498 * remote is shallow, but this is a clone, there are
1499 * no objects in repo to worry about. Accept any
1500 * shallow points that exist in the pack (iow in repo
1501 * after get_pack() and reprepare_packed_git())
1502 */
1503 struct oid_array extra = OID_ARRAY_INIT;
1504 struct object_id *oid = si->shallow->oid;
1505 for (i = 0; i < si->shallow->nr; i++)
1506 if (has_object_file(&oid[i]))
1507 oid_array_append(&extra, &oid[i]);
1508 if (extra.nr) {
1509 setup_alternate_shallow(&shallow_lock,
1510 &alternate_shallow_file,
1511 &extra);
1512 commit_lock_file(&shallow_lock);
1513 }
1514 oid_array_clear(&extra);
1515 return;
1516 }
1517
1518 if (!si->nr_ours && !si->nr_theirs)
1519 return;
1520
1521 remove_nonexistent_theirs_shallow(si);
1522 if (!si->nr_ours && !si->nr_theirs)
1523 return;
1524 for (i = 0; i < nr_sought; i++)
1525 oid_array_append(&ref, &sought[i]->old_oid);
1526 si->ref = &ref;
1527
1528 if (args->update_shallow) {
1529 /*
1530 * remote is also shallow, .git/shallow may be updated
1531 * so all refs can be accepted. Make sure we only add
1532 * shallow roots that are actually reachable from new
1533 * refs.
1534 */
1535 struct oid_array extra = OID_ARRAY_INIT;
1536 struct object_id *oid = si->shallow->oid;
1537 assign_shallow_commits_to_refs(si, NULL, NULL);
1538 if (!si->nr_ours && !si->nr_theirs) {
1539 oid_array_clear(&ref);
1540 return;
1541 }
1542 for (i = 0; i < si->nr_ours; i++)
1543 oid_array_append(&extra, &oid[si->ours[i]]);
1544 for (i = 0; i < si->nr_theirs; i++)
1545 oid_array_append(&extra, &oid[si->theirs[i]]);
1546 setup_alternate_shallow(&shallow_lock,
1547 &alternate_shallow_file,
1548 &extra);
1549 commit_lock_file(&shallow_lock);
1550 oid_array_clear(&extra);
1551 oid_array_clear(&ref);
1552 return;
1553 }
1554
1555 /*
1556 * remote is also shallow, check what ref is safe to update
1557 * without updating .git/shallow
1558 */
1559 status = xcalloc(nr_sought, sizeof(*status));
1560 assign_shallow_commits_to_refs(si, NULL, status);
1561 if (si->nr_ours || si->nr_theirs) {
1562 for (i = 0; i < nr_sought; i++)
1563 if (status[i])
1564 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1565 }
1566 free(status);
1567 oid_array_clear(&ref);
1568 }
1569
1570 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1571 {
1572 struct ref **rm = cb_data;
1573 struct ref *ref = *rm;
1574
1575 if (!ref)
1576 return -1; /* end of the list */
1577 *rm = ref->next;
1578 oidcpy(oid, &ref->old_oid);
1579 return 0;
1580 }
1581
1582 struct ref *fetch_pack(struct fetch_pack_args *args,
1583 int fd[], struct child_process *conn,
1584 const struct ref *ref,
1585 const char *dest,
1586 struct ref **sought, int nr_sought,
1587 struct oid_array *shallow,
1588 char **pack_lockfile,
1589 enum protocol_version version)
1590 {
1591 struct ref *ref_cpy;
1592 struct shallow_info si;
1593
1594 fetch_pack_setup();
1595 if (nr_sought)
1596 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1597
1598 if (args->no_dependents && !args->filter_options.choice) {
1599 /*
1600 * The protocol does not support requesting that only the
1601 * wanted objects be sent, so approximate this by setting a
1602 * "blob:none" filter if no filter is already set. This works
1603 * for all object types: note that wanted blobs will still be
1604 * sent because they are directly specified as a "want".
1605 *
1606 * NEEDSWORK: Add an option in the protocol to request that
1607 * only the wanted objects be sent, and implement it.
1608 */
1609 parse_list_objects_filter(&args->filter_options, "blob:none");
1610 }
1611
1612 if (version != protocol_v2 && !ref) {
1613 packet_flush(fd[1]);
1614 die(_("no matching remote head"));
1615 }
1616 prepare_shallow_info(&si, shallow);
1617 if (version == protocol_v2)
1618 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1619 pack_lockfile);
1620 else
1621 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1622 &si, pack_lockfile);
1623 reprepare_packed_git(the_repository);
1624
1625 if (!args->cloning && args->deepen) {
1626 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1627 struct ref *iterator = ref_cpy;
1628 opt.shallow_file = alternate_shallow_file;
1629 if (args->deepen)
1630 opt.is_deepening_fetch = 1;
1631 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1632 error(_("remote did not send all necessary objects"));
1633 free_refs(ref_cpy);
1634 ref_cpy = NULL;
1635 rollback_lock_file(&shallow_lock);
1636 goto cleanup;
1637 }
1638 args->connectivity_checked = 1;
1639 }
1640
1641 update_shallow(args, sought, nr_sought, &si);
1642 cleanup:
1643 clear_shallow_info(&si);
1644 return ref_cpy;
1645 }
1646
1647 int report_unmatched_refs(struct ref **sought, int nr_sought)
1648 {
1649 int i, ret = 0;
1650
1651 for (i = 0; i < nr_sought; i++) {
1652 if (!sought[i])
1653 continue;
1654 switch (sought[i]->match_status) {
1655 case REF_MATCHED:
1656 continue;
1657 case REF_NOT_MATCHED:
1658 error(_("no such remote ref %s"), sought[i]->name);
1659 break;
1660 case REF_UNADVERTISED_NOT_ALLOWED:
1661 error(_("Server does not allow request for unadvertised object %s"),
1662 sought[i]->name);
1663 break;
1664 }
1665 ret = 1;
1666 }
1667 return ret;
1668 }