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