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