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