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