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