Git 2.46-rc1
[git.git] / fetch-pack.c
blob732511604b1a6c72f2df5dd8413b1473715c3826
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "repository.h"
5 #include "config.h"
6 #include "date.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "lockfile.h"
11 #include "refs.h"
12 #include "pkt-line.h"
13 #include "commit.h"
14 #include "tag.h"
15 #include "pack.h"
16 #include "sideband.h"
17 #include "fetch-pack.h"
18 #include "remote.h"
19 #include "run-command.h"
20 #include "connect.h"
21 #include "trace2.h"
22 #include "version.h"
23 #include "oid-array.h"
24 #include "oidset.h"
25 #include "packfile.h"
26 #include "object-store-ll.h"
27 #include "path.h"
28 #include "connected.h"
29 #include "fetch-negotiator.h"
30 #include "fsck.h"
31 #include "shallow.h"
32 #include "commit-reach.h"
33 #include "commit-graph.h"
34 #include "sigchain.h"
35 #include "mergesort.h"
37 static int transfer_unpack_limit = -1;
38 static int fetch_unpack_limit = -1;
39 static int unpack_limit = 100;
40 static int prefer_ofs_delta = 1;
41 static int no_done;
42 static int deepen_since_ok;
43 static int deepen_not_ok;
44 static int fetch_fsck_objects = -1;
45 static int transfer_fsck_objects = -1;
46 static int agent_supported;
47 static int server_supports_filtering;
48 static int advertise_sid;
49 static struct shallow_lock shallow_lock;
50 static const char *alternate_shallow_file;
51 static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
52 static struct strbuf fsck_msg_types = STRBUF_INIT;
53 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
55 /* Remember to update object flag allocation in object.h */
56 #define COMPLETE (1U << 0)
57 #define ALTERNATE (1U << 1)
58 #define COMMON (1U << 6)
59 #define REACH_SCRATCH (1U << 7)
62 * After sending this many "have"s if we do not get any new ACK , we
63 * give up traversing our history.
65 #define MAX_IN_VAIN 256
67 static int multi_ack, use_sideband;
68 /* Allow specifying sha1 if it is a ref tip. */
69 #define ALLOW_TIP_SHA1 01
70 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
71 #define ALLOW_REACHABLE_SHA1 02
72 static unsigned int allow_unadvertised_object_request;
74 __attribute__((format (printf, 2, 3)))
75 static inline void print_verbose(const struct fetch_pack_args *args,
76 const char *fmt, ...)
78 va_list params;
80 if (!args->verbose)
81 return;
83 va_start(params, fmt);
84 vfprintf(stderr, fmt, params);
85 va_end(params);
86 fputc('\n', stderr);
89 struct alternate_object_cache {
90 struct object **items;
91 size_t nr, alloc;
94 static void cache_one_alternate(const struct object_id *oid,
95 void *vcache)
97 struct alternate_object_cache *cache = vcache;
98 struct object *obj = parse_object(the_repository, oid);
100 if (!obj || (obj->flags & ALTERNATE))
101 return;
103 obj->flags |= ALTERNATE;
104 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
105 cache->items[cache->nr++] = obj;
108 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
109 void (*cb)(struct fetch_negotiator *,
110 struct object *))
112 static int initialized;
113 static struct alternate_object_cache cache;
114 size_t i;
116 if (!initialized) {
117 for_each_alternate_ref(cache_one_alternate, &cache);
118 initialized = 1;
121 for (i = 0; i < cache.nr; i++)
122 cb(negotiator, cache.items[i]);
125 static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid,
126 int mark_tags_complete,
127 enum object_type *type,
128 unsigned int oi_flags)
130 struct object_info info = { .typep = type };
131 struct commit *commit;
133 commit = lookup_commit_in_graph(the_repository, oid);
134 if (commit)
135 return commit;
137 while (1) {
138 if (oid_object_info_extended(the_repository, oid, &info,
139 oi_flags))
140 return NULL;
141 if (*type == OBJ_TAG) {
142 struct tag *tag = (struct tag *)
143 parse_object(the_repository, oid);
145 if (!tag->tagged)
146 return NULL;
147 if (mark_tags_complete)
148 tag->object.flags |= COMPLETE;
149 oid = &tag->tagged->oid;
150 } else {
151 break;
155 if (*type == OBJ_COMMIT) {
156 struct commit *commit = lookup_commit(the_repository, oid);
157 if (!commit || repo_parse_commit(the_repository, commit))
158 return NULL;
159 return commit;
162 return NULL;
166 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
167 int mark_tags_complete)
169 enum object_type type;
170 unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK;
171 return deref_without_lazy_fetch_extended(oid, mark_tags_complete,
172 &type, flags);
175 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
176 const struct object_id *oid)
178 struct commit *c = deref_without_lazy_fetch(oid, 0);
180 if (c)
181 negotiator->add_tip(negotiator, c);
182 return 0;
185 static int rev_list_insert_ref_oid(const char *refname UNUSED,
186 const struct object_id *oid,
187 int flag UNUSED,
188 void *cb_data)
190 return rev_list_insert_ref(cb_data, oid);
193 enum ack_type {
194 NAK = 0,
195 ACK,
196 ACK_continue,
197 ACK_common,
198 ACK_ready
201 static void consume_shallow_list(struct fetch_pack_args *args,
202 struct packet_reader *reader)
204 if (args->stateless_rpc && args->deepen) {
205 /* If we sent a depth we will get back "duplicate"
206 * shallow and unshallow commands every time there
207 * is a block of have lines exchanged.
209 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
210 if (starts_with(reader->line, "shallow "))
211 continue;
212 if (starts_with(reader->line, "unshallow "))
213 continue;
214 die(_("git fetch-pack: expected shallow list"));
216 if (reader->status != PACKET_READ_FLUSH)
217 die(_("git fetch-pack: expected a flush packet after shallow list"));
221 static enum ack_type get_ack(struct packet_reader *reader,
222 struct object_id *result_oid)
224 int len;
225 const char *arg;
227 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
228 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
229 len = reader->pktlen;
231 if (!strcmp(reader->line, "NAK"))
232 return NAK;
233 if (skip_prefix(reader->line, "ACK ", &arg)) {
234 const char *p;
235 if (!parse_oid_hex(arg, result_oid, &p)) {
236 len -= p - reader->line;
237 if (len < 1)
238 return ACK;
239 if (strstr(p, "continue"))
240 return ACK_continue;
241 if (strstr(p, "common"))
242 return ACK_common;
243 if (strstr(p, "ready"))
244 return ACK_ready;
245 return ACK;
248 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
251 static void send_request(struct fetch_pack_args *args,
252 int fd, struct strbuf *buf)
254 if (args->stateless_rpc) {
255 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
256 packet_flush(fd);
257 } else {
258 if (write_in_full(fd, buf->buf, buf->len) < 0)
259 die_errno(_("unable to write to remote"));
263 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
264 struct object *obj)
266 rev_list_insert_ref(negotiator, &obj->oid);
269 #define INITIAL_FLUSH 16
270 #define PIPESAFE_FLUSH 32
271 #define LARGE_FLUSH 16384
273 static int next_flush(int stateless_rpc, int count)
275 if (stateless_rpc) {
276 if (count < LARGE_FLUSH)
277 count <<= 1;
278 else
279 count = count * 11 / 10;
280 } else {
281 if (count < PIPESAFE_FLUSH)
282 count <<= 1;
283 else
284 count += PIPESAFE_FLUSH;
286 return count;
289 static void mark_tips(struct fetch_negotiator *negotiator,
290 const struct oid_array *negotiation_tips)
292 int i;
294 if (!negotiation_tips) {
295 refs_for_each_rawref(get_main_ref_store(the_repository),
296 rev_list_insert_ref_oid, negotiator);
297 return;
300 for (i = 0; i < negotiation_tips->nr; i++)
301 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
302 return;
305 static void send_filter(struct fetch_pack_args *args,
306 struct strbuf *req_buf,
307 int server_supports_filter)
309 if (args->filter_options.choice) {
310 const char *spec =
311 expand_list_objects_filter_spec(&args->filter_options);
312 if (server_supports_filter) {
313 print_verbose(args, _("Server supports filter"));
314 packet_buf_write(req_buf, "filter %s", spec);
315 trace2_data_string("fetch", the_repository,
316 "filter/effective", spec);
317 } else {
318 warning("filtering not recognized by server, ignoring");
319 trace2_data_string("fetch", the_repository,
320 "filter/unsupported", spec);
322 } else {
323 trace2_data_string("fetch", the_repository,
324 "filter/none", "");
328 static int find_common(struct fetch_negotiator *negotiator,
329 struct fetch_pack_args *args,
330 int fd[2], struct object_id *result_oid,
331 struct ref *refs)
333 int fetching;
334 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
335 int negotiation_round = 0, haves = 0;
336 const struct object_id *oid;
337 unsigned in_vain = 0;
338 int got_continue = 0;
339 int got_ready = 0;
340 struct strbuf req_buf = STRBUF_INIT;
341 size_t state_len = 0;
342 struct packet_reader reader;
344 if (args->stateless_rpc && multi_ack == 1)
345 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
347 packet_reader_init(&reader, fd[0], NULL, 0,
348 PACKET_READ_CHOMP_NEWLINE |
349 PACKET_READ_DIE_ON_ERR_PACKET);
351 mark_tips(negotiator, args->negotiation_tips);
352 for_each_cached_alternate(negotiator, insert_one_alternate_object);
354 fetching = 0;
355 for ( ; refs ; refs = refs->next) {
356 struct object_id *remote = &refs->old_oid;
357 const char *remote_hex;
358 struct object *o;
360 if (!args->refetch) {
362 * If that object is complete (i.e. it is an ancestor of a
363 * local ref), we tell them we have it but do not have to
364 * tell them about its ancestors, which they already know
365 * about.
367 * We use lookup_object here because we are only
368 * interested in the case we *know* the object is
369 * reachable and we have already scanned it.
371 if (((o = lookup_object(the_repository, remote)) != NULL) &&
372 (o->flags & COMPLETE)) {
373 continue;
377 remote_hex = oid_to_hex(remote);
378 if (!fetching) {
379 struct strbuf c = STRBUF_INIT;
380 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
381 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
382 if (no_done) strbuf_addstr(&c, " no-done");
383 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
384 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
385 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
386 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
387 if (args->no_progress) strbuf_addstr(&c, " no-progress");
388 if (args->include_tag) strbuf_addstr(&c, " include-tag");
389 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
390 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
391 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
392 if (agent_supported) strbuf_addf(&c, " agent=%s",
393 git_user_agent_sanitized());
394 if (advertise_sid)
395 strbuf_addf(&c, " session-id=%s", trace2_session_id());
396 if (args->filter_options.choice)
397 strbuf_addstr(&c, " filter");
398 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
399 strbuf_release(&c);
400 } else
401 packet_buf_write(&req_buf, "want %s\n", remote_hex);
402 fetching++;
405 if (!fetching) {
406 strbuf_release(&req_buf);
407 packet_flush(fd[1]);
408 return 1;
411 if (is_repository_shallow(the_repository))
412 write_shallow_commits(&req_buf, 1, NULL);
413 if (args->depth > 0)
414 packet_buf_write(&req_buf, "deepen %d", args->depth);
415 if (args->deepen_since) {
416 timestamp_t max_age = approxidate(args->deepen_since);
417 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
419 if (args->deepen_not) {
420 int i;
421 for (i = 0; i < args->deepen_not->nr; i++) {
422 struct string_list_item *s = args->deepen_not->items + i;
423 packet_buf_write(&req_buf, "deepen-not %s", s->string);
426 send_filter(args, &req_buf, server_supports_filtering);
427 packet_buf_flush(&req_buf);
428 state_len = req_buf.len;
430 if (args->deepen) {
431 const char *arg;
432 struct object_id oid;
434 send_request(args, fd[1], &req_buf);
435 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
436 if (skip_prefix(reader.line, "shallow ", &arg)) {
437 if (get_oid_hex(arg, &oid))
438 die(_("invalid shallow line: %s"), reader.line);
439 register_shallow(the_repository, &oid);
440 continue;
442 if (skip_prefix(reader.line, "unshallow ", &arg)) {
443 if (get_oid_hex(arg, &oid))
444 die(_("invalid unshallow line: %s"), reader.line);
445 if (!lookup_object(the_repository, &oid))
446 die(_("object not found: %s"), reader.line);
447 /* make sure that it is parsed as shallow */
448 if (!parse_object(the_repository, &oid))
449 die(_("error in object: %s"), reader.line);
450 if (unregister_shallow(&oid))
451 die(_("no shallow found: %s"), reader.line);
452 continue;
454 die(_("expected shallow/unshallow, got %s"), reader.line);
456 } else if (!args->stateless_rpc)
457 send_request(args, fd[1], &req_buf);
459 if (!args->stateless_rpc) {
460 /* If we aren't using the stateless-rpc interface
461 * we don't need to retain the headers.
463 strbuf_setlen(&req_buf, 0);
464 state_len = 0;
467 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
468 flushes = 0;
469 retval = -1;
470 while ((oid = negotiator->next(negotiator))) {
471 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
472 print_verbose(args, "have %s", oid_to_hex(oid));
473 in_vain++;
474 haves++;
475 if (flush_at <= ++count) {
476 int ack;
478 negotiation_round++;
479 trace2_region_enter_printf("negotiation_v0_v1", "round",
480 the_repository, "%d",
481 negotiation_round);
482 trace2_data_intmax("negotiation_v0_v1", the_repository,
483 "haves_added", haves);
484 trace2_data_intmax("negotiation_v0_v1", the_repository,
485 "in_vain", in_vain);
486 haves = 0;
487 packet_buf_flush(&req_buf);
488 send_request(args, fd[1], &req_buf);
489 strbuf_setlen(&req_buf, state_len);
490 flushes++;
491 flush_at = next_flush(args->stateless_rpc, count);
494 * We keep one window "ahead" of the other side, and
495 * will wait for an ACK only on the next one
497 if (!args->stateless_rpc && count == INITIAL_FLUSH)
498 continue;
500 consume_shallow_list(args, &reader);
501 do {
502 ack = get_ack(&reader, result_oid);
503 if (ack)
504 print_verbose(args, _("got %s %d %s"), "ack",
505 ack, oid_to_hex(result_oid));
506 switch (ack) {
507 case ACK:
508 trace2_region_leave_printf("negotiation_v0_v1", "round",
509 the_repository, "%d",
510 negotiation_round);
511 flushes = 0;
512 multi_ack = 0;
513 retval = 0;
514 goto done;
515 case ACK_common:
516 case ACK_ready:
517 case ACK_continue: {
518 struct commit *commit =
519 lookup_commit(the_repository,
520 result_oid);
521 int was_common;
523 if (!commit)
524 die(_("invalid commit %s"), oid_to_hex(result_oid));
525 was_common = negotiator->ack(negotiator, commit);
526 if (args->stateless_rpc
527 && ack == ACK_common
528 && !was_common) {
529 /* We need to replay the have for this object
530 * on the next RPC request so the peer knows
531 * it is in common with us.
533 const char *hex = oid_to_hex(result_oid);
534 packet_buf_write(&req_buf, "have %s\n", hex);
535 state_len = req_buf.len;
536 haves++;
538 * Reset in_vain because an ack
539 * for this commit has not been
540 * seen.
542 in_vain = 0;
543 } else if (!args->stateless_rpc
544 || ack != ACK_common)
545 in_vain = 0;
546 retval = 0;
547 got_continue = 1;
548 if (ack == ACK_ready)
549 got_ready = 1;
550 break;
553 } while (ack);
554 flushes--;
555 trace2_region_leave_printf("negotiation_v0_v1", "round",
556 the_repository, "%d",
557 negotiation_round);
558 if (got_continue && MAX_IN_VAIN < in_vain) {
559 print_verbose(args, _("giving up"));
560 break; /* give up */
562 if (got_ready)
563 break;
566 done:
567 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
568 trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds",
569 negotiation_round);
570 if (!got_ready || !no_done) {
571 packet_buf_write(&req_buf, "done\n");
572 send_request(args, fd[1], &req_buf);
574 print_verbose(args, _("done"));
575 if (retval != 0) {
576 multi_ack = 0;
577 flushes++;
579 strbuf_release(&req_buf);
581 if (!got_ready || !no_done)
582 consume_shallow_list(args, &reader);
583 while (flushes || multi_ack) {
584 int ack = get_ack(&reader, result_oid);
585 if (ack) {
586 print_verbose(args, _("got %s (%d) %s"), "ack",
587 ack, oid_to_hex(result_oid));
588 if (ack == ACK)
589 return 0;
590 multi_ack = 1;
591 continue;
593 flushes--;
595 /* it is no error to fetch into a completely empty repo */
596 return count ? retval : 0;
599 static struct commit_list *complete;
601 static int mark_complete(const struct object_id *oid)
603 struct commit *commit = deref_without_lazy_fetch(oid, 1);
605 if (commit && !(commit->object.flags & COMPLETE)) {
606 commit->object.flags |= COMPLETE;
607 commit_list_insert(commit, &complete);
609 return 0;
612 static int mark_complete_oid(const char *refname UNUSED,
613 const struct object_id *oid,
614 int flag UNUSED,
615 void *cb_data UNUSED)
617 return mark_complete(oid);
620 static void mark_recent_complete_commits(struct fetch_pack_args *args,
621 timestamp_t cutoff)
623 while (complete && cutoff <= complete->item->date) {
624 print_verbose(args, _("Marking %s as complete"),
625 oid_to_hex(&complete->item->object.oid));
626 pop_most_recent_commit(&complete, COMPLETE);
630 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
632 for (; refs; refs = refs->next)
633 oidset_insert(oids, &refs->old_oid);
636 static int is_unmatched_ref(const struct ref *ref)
638 struct object_id oid;
639 const char *p;
640 return ref->match_status == REF_NOT_MATCHED &&
641 !parse_oid_hex(ref->name, &oid, &p) &&
642 *p == '\0' &&
643 oideq(&oid, &ref->old_oid);
646 static void filter_refs(struct fetch_pack_args *args,
647 struct ref **refs,
648 struct ref **sought, int nr_sought)
650 struct ref *newlist = NULL;
651 struct ref **newtail = &newlist;
652 struct ref *unmatched = NULL;
653 struct ref *ref, *next;
654 struct oidset tip_oids = OIDSET_INIT;
655 int i;
656 int strict = !(allow_unadvertised_object_request &
657 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
659 i = 0;
660 for (ref = *refs; ref; ref = next) {
661 int keep = 0;
662 next = ref->next;
664 if (starts_with(ref->name, "refs/") &&
665 check_refname_format(ref->name, 0)) {
667 * trash or a peeled value; do not even add it to
668 * unmatched list
670 free_one_ref(ref);
671 continue;
672 } else {
673 while (i < nr_sought) {
674 int cmp = strcmp(ref->name, sought[i]->name);
675 if (cmp < 0)
676 break; /* definitely do not have it */
677 else if (cmp == 0) {
678 keep = 1; /* definitely have it */
679 sought[i]->match_status = REF_MATCHED;
681 i++;
684 if (!keep && args->fetch_all &&
685 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
686 keep = 1;
689 if (keep) {
690 *newtail = ref;
691 ref->next = NULL;
692 newtail = &ref->next;
693 } else {
694 ref->next = unmatched;
695 unmatched = ref;
699 if (strict) {
700 for (i = 0; i < nr_sought; i++) {
701 ref = sought[i];
702 if (!is_unmatched_ref(ref))
703 continue;
705 add_refs_to_oidset(&tip_oids, unmatched);
706 add_refs_to_oidset(&tip_oids, newlist);
707 break;
711 /* Append unmatched requests to the list */
712 for (i = 0; i < nr_sought; i++) {
713 ref = sought[i];
714 if (!is_unmatched_ref(ref))
715 continue;
717 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
718 ref->match_status = REF_MATCHED;
719 *newtail = copy_ref(ref);
720 newtail = &(*newtail)->next;
721 } else {
722 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
726 oidset_clear(&tip_oids);
727 free_refs(unmatched);
729 *refs = newlist;
732 static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED,
733 struct object *obj)
735 mark_complete(&obj->oid);
739 * Mark recent commits available locally and reachable from a local ref as
740 * COMPLETE.
742 * The cutoff time for recency is determined by this heuristic: it is the
743 * earliest commit time of the objects in refs that are commits and that we know
744 * the commit time of.
746 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
747 struct fetch_pack_args *args,
748 struct ref **refs)
750 struct ref *ref;
751 int old_save_commit_buffer = save_commit_buffer;
752 timestamp_t cutoff = 0;
754 if (args->refetch)
755 return;
757 save_commit_buffer = 0;
759 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
760 for (ref = *refs; ref; ref = ref->next) {
761 struct commit *commit;
763 commit = lookup_commit_in_graph(the_repository, &ref->old_oid);
764 if (!commit) {
765 struct object *o;
767 if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
768 OBJECT_INFO_QUICK |
769 OBJECT_INFO_SKIP_FETCH_OBJECT))
770 continue;
771 o = parse_object(the_repository, &ref->old_oid);
772 if (!o || o->type != OBJ_COMMIT)
773 continue;
775 commit = (struct commit *)o;
779 * We already have it -- which may mean that we were
780 * in sync with the other side at some time after
781 * that (it is OK if we guess wrong here).
783 if (!cutoff || cutoff < commit->date)
784 cutoff = commit->date;
786 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
789 * This block marks all local refs as COMPLETE, and then recursively marks all
790 * parents of those refs as COMPLETE.
792 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
793 if (!args->deepen) {
794 refs_for_each_rawref(get_main_ref_store(the_repository),
795 mark_complete_oid, NULL);
796 for_each_cached_alternate(NULL, mark_alternate_complete);
797 commit_list_sort_by_date(&complete);
798 if (cutoff)
799 mark_recent_complete_commits(args, cutoff);
801 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
804 * Mark all complete remote refs as common refs.
805 * Don't mark them common yet; the server has to be told so first.
807 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
808 for (ref = *refs; ref; ref = ref->next) {
809 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
811 if (!c || !(c->object.flags & COMPLETE))
812 continue;
814 negotiator->known_common(negotiator, c);
816 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
818 save_commit_buffer = old_save_commit_buffer;
822 * Returns 1 if every object pointed to by the given remote refs is available
823 * locally and reachable from a local ref, and 0 otherwise.
825 static int everything_local(struct fetch_pack_args *args,
826 struct ref **refs)
828 struct ref *ref;
829 int retval;
831 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
832 const struct object_id *remote = &ref->old_oid;
833 struct object *o;
835 o = lookup_object(the_repository, remote);
836 if (!o || !(o->flags & COMPLETE)) {
837 retval = 0;
838 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
839 ref->name);
840 continue;
842 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
843 ref->name);
846 return retval;
849 static int sideband_demux(int in UNUSED, int out, void *data)
851 int *xd = data;
852 int ret;
854 ret = recv_sideband("fetch-pack", xd[0], out);
855 close(out);
856 return ret;
859 static void create_promisor_file(const char *keep_name,
860 struct ref **sought, int nr_sought)
862 struct strbuf promisor_name = STRBUF_INIT;
863 int suffix_stripped;
865 strbuf_addstr(&promisor_name, keep_name);
866 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
867 if (!suffix_stripped)
868 BUG("name of pack lockfile should end with .keep (was '%s')",
869 keep_name);
870 strbuf_addstr(&promisor_name, ".promisor");
872 write_promisor_file(promisor_name.buf, sought, nr_sought);
874 strbuf_release(&promisor_name);
877 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
879 int len = the_hash_algo->hexsz + 1; /* hash + NL */
881 do {
882 char hex_hash[GIT_MAX_HEXSZ + 1];
883 int read_len = read_in_full(fd, hex_hash, len);
884 struct object_id oid;
885 const char *end;
887 if (!read_len)
888 return;
889 if (read_len != len)
890 die("invalid length read %d", read_len);
891 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
892 die("invalid hash");
893 oidset_insert(gitmodules_oids, &oid);
894 } while (1);
897 static void add_index_pack_keep_option(struct strvec *args)
899 char hostname[HOST_NAME_MAX + 1];
901 if (xgethostname(hostname, sizeof(hostname)))
902 xsnprintf(hostname, sizeof(hostname), "localhost");
903 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
904 (uintmax_t)getpid(), hostname);
908 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
909 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
910 * stored there. (It must be freed by the caller.)
912 static int get_pack(struct fetch_pack_args *args,
913 int xd[2], struct string_list *pack_lockfiles,
914 struct strvec *index_pack_args,
915 struct ref **sought, int nr_sought,
916 struct oidset *gitmodules_oids)
918 struct async demux;
919 int do_keep = args->keep_pack;
920 const char *cmd_name;
921 struct pack_header header;
922 int pass_header = 0;
923 struct child_process cmd = CHILD_PROCESS_INIT;
924 int fsck_objects = 0;
925 int ret;
927 memset(&demux, 0, sizeof(demux));
928 if (use_sideband) {
929 /* xd[] is talking with upload-pack; subprocess reads from
930 * xd[0], spits out band#2 to stderr, and feeds us band#1
931 * through demux->out.
933 demux.proc = sideband_demux;
934 demux.data = xd;
935 demux.out = -1;
936 demux.isolate_sigpipe = 1;
937 if (start_async(&demux))
938 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
940 else
941 demux.out = xd[0];
943 if (!args->keep_pack && unpack_limit && !index_pack_args) {
945 if (read_pack_header(demux.out, &header))
946 die(_("protocol error: bad pack header"));
947 pass_header = 1;
948 if (ntohl(header.hdr_entries) < unpack_limit)
949 do_keep = 0;
950 else
951 do_keep = 1;
954 if (alternate_shallow_file) {
955 strvec_push(&cmd.args, "--shallow-file");
956 strvec_push(&cmd.args, alternate_shallow_file);
959 fsck_objects = fetch_pack_fsck_objects();
961 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
962 if (pack_lockfiles || fsck_objects)
963 cmd.out = -1;
964 cmd_name = "index-pack";
965 strvec_push(&cmd.args, cmd_name);
966 strvec_push(&cmd.args, "--stdin");
967 if (!args->quiet && !args->no_progress)
968 strvec_push(&cmd.args, "-v");
969 if (args->use_thin_pack)
970 strvec_push(&cmd.args, "--fix-thin");
971 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
972 add_index_pack_keep_option(&cmd.args);
973 if (!index_pack_args && args->check_self_contained_and_connected)
974 strvec_push(&cmd.args, "--check-self-contained-and-connected");
975 else
977 * We cannot perform any connectivity checks because
978 * not all packs have been downloaded; let the caller
979 * have this responsibility.
981 args->check_self_contained_and_connected = 0;
983 if (args->from_promisor)
985 * create_promisor_file() may be called afterwards but
986 * we still need index-pack to know that this is a
987 * promisor pack. For example, if transfer.fsckobjects
988 * is true, index-pack needs to know that .gitmodules
989 * is a promisor object (so that it won't complain if
990 * it is missing).
992 strvec_push(&cmd.args, "--promisor");
994 else {
995 cmd_name = "unpack-objects";
996 strvec_push(&cmd.args, cmd_name);
997 if (args->quiet || args->no_progress)
998 strvec_push(&cmd.args, "-q");
999 args->check_self_contained_and_connected = 0;
1002 if (pass_header)
1003 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
1004 ntohl(header.hdr_version),
1005 ntohl(header.hdr_entries));
1006 if (fsck_objects) {
1007 if (args->from_promisor || index_pack_args)
1009 * We cannot use --strict in index-pack because it
1010 * checks both broken objects and links, but we only
1011 * want to check for broken objects.
1013 strvec_push(&cmd.args, "--fsck-objects");
1014 else
1015 strvec_pushf(&cmd.args, "--strict%s",
1016 fsck_msg_types.buf);
1019 if (index_pack_args) {
1020 int i;
1022 for (i = 0; i < cmd.args.nr; i++)
1023 strvec_push(index_pack_args, cmd.args.v[i]);
1026 sigchain_push(SIGPIPE, SIG_IGN);
1028 cmd.in = demux.out;
1029 cmd.git_cmd = 1;
1030 if (start_command(&cmd))
1031 die(_("fetch-pack: unable to fork off %s"), cmd_name);
1032 if (do_keep && (pack_lockfiles || fsck_objects)) {
1033 int is_well_formed;
1034 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1036 if (!is_well_formed)
1037 die(_("fetch-pack: invalid index-pack output"));
1038 if (pack_lockfiles && pack_lockfile)
1039 string_list_append_nodup(pack_lockfiles, pack_lockfile);
1040 else
1041 free(pack_lockfile);
1042 parse_gitmodules_oids(cmd.out, gitmodules_oids);
1043 close(cmd.out);
1046 if (!use_sideband)
1047 /* Closed by start_command() */
1048 xd[0] = -1;
1050 ret = finish_command(&cmd);
1051 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1052 args->self_contained_and_connected =
1053 args->check_self_contained_and_connected &&
1054 ret == 0;
1055 else
1056 die(_("%s failed"), cmd_name);
1057 if (use_sideband && finish_async(&demux))
1058 die(_("error in sideband demultiplexer"));
1060 sigchain_pop(SIGPIPE);
1063 * Now that index-pack has succeeded, write the promisor file using the
1064 * obtained .keep filename if necessary
1066 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
1067 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
1069 return 0;
1072 static int ref_compare_name(const struct ref *a, const struct ref *b)
1074 return strcmp(a->name, b->name);
1077 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1079 static int cmp_ref_by_name(const void *a_, const void *b_)
1081 const struct ref *a = *((const struct ref **)a_);
1082 const struct ref *b = *((const struct ref **)b_);
1083 return strcmp(a->name, b->name);
1086 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1087 int fd[2],
1088 const struct ref *orig_ref,
1089 struct ref **sought, int nr_sought,
1090 struct shallow_info *si,
1091 struct string_list *pack_lockfiles)
1093 struct repository *r = the_repository;
1094 struct ref *ref = copy_ref_list(orig_ref);
1095 struct object_id oid;
1096 const char *agent_feature;
1097 size_t agent_len;
1098 struct fetch_negotiator negotiator_alloc;
1099 struct fetch_negotiator *negotiator;
1101 negotiator = &negotiator_alloc;
1102 if (args->refetch) {
1103 fetch_negotiator_init_noop(negotiator);
1104 } else {
1105 fetch_negotiator_init(r, negotiator);
1108 sort_ref_list(&ref, ref_compare_name);
1109 QSORT(sought, nr_sought, cmp_ref_by_name);
1111 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1112 agent_supported = 1;
1113 if (agent_len)
1114 print_verbose(args, _("Server version is %.*s"),
1115 (int)agent_len, agent_feature);
1118 if (!server_supports("session-id"))
1119 advertise_sid = 0;
1121 if (server_supports("shallow"))
1122 print_verbose(args, _("Server supports %s"), "shallow");
1123 else if (args->depth > 0 || is_repository_shallow(r))
1124 die(_("Server does not support shallow clients"));
1125 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1126 args->deepen = 1;
1127 if (server_supports("multi_ack_detailed")) {
1128 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1129 multi_ack = 2;
1130 if (server_supports("no-done")) {
1131 print_verbose(args, _("Server supports %s"), "no-done");
1132 if (args->stateless_rpc)
1133 no_done = 1;
1136 else if (server_supports("multi_ack")) {
1137 print_verbose(args, _("Server supports %s"), "multi_ack");
1138 multi_ack = 1;
1140 if (server_supports("side-band-64k")) {
1141 print_verbose(args, _("Server supports %s"), "side-band-64k");
1142 use_sideband = 2;
1144 else if (server_supports("side-band")) {
1145 print_verbose(args, _("Server supports %s"), "side-band");
1146 use_sideband = 1;
1148 if (server_supports("allow-tip-sha1-in-want")) {
1149 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1150 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1152 if (server_supports("allow-reachable-sha1-in-want")) {
1153 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1154 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1156 if (server_supports("thin-pack"))
1157 print_verbose(args, _("Server supports %s"), "thin-pack");
1158 else
1159 args->use_thin_pack = 0;
1160 if (server_supports("no-progress"))
1161 print_verbose(args, _("Server supports %s"), "no-progress");
1162 else
1163 args->no_progress = 0;
1164 if (server_supports("include-tag"))
1165 print_verbose(args, _("Server supports %s"), "include-tag");
1166 else
1167 args->include_tag = 0;
1168 if (server_supports("ofs-delta"))
1169 print_verbose(args, _("Server supports %s"), "ofs-delta");
1170 else
1171 prefer_ofs_delta = 0;
1173 if (server_supports("filter")) {
1174 server_supports_filtering = 1;
1175 print_verbose(args, _("Server supports %s"), "filter");
1176 } else if (args->filter_options.choice) {
1177 warning("filtering not recognized by server, ignoring");
1180 if (server_supports("deepen-since")) {
1181 print_verbose(args, _("Server supports %s"), "deepen-since");
1182 deepen_since_ok = 1;
1183 } else if (args->deepen_since)
1184 die(_("Server does not support --shallow-since"));
1185 if (server_supports("deepen-not")) {
1186 print_verbose(args, _("Server supports %s"), "deepen-not");
1187 deepen_not_ok = 1;
1188 } else if (args->deepen_not)
1189 die(_("Server does not support --shallow-exclude"));
1190 if (server_supports("deepen-relative"))
1191 print_verbose(args, _("Server supports %s"), "deepen-relative");
1192 else if (args->deepen_relative)
1193 die(_("Server does not support --deepen"));
1194 if (!server_supports_hash(the_hash_algo->name, NULL))
1195 die(_("Server does not support this repository's object format"));
1197 mark_complete_and_common_ref(negotiator, args, &ref);
1198 filter_refs(args, &ref, sought, nr_sought);
1199 if (!args->refetch && everything_local(args, &ref)) {
1200 packet_flush(fd[1]);
1201 goto all_done;
1203 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1204 if (!args->keep_pack)
1205 /* When cloning, it is not unusual to have
1206 * no common commit.
1208 warning(_("no common commits"));
1210 if (args->stateless_rpc)
1211 packet_flush(fd[1]);
1212 if (args->deepen)
1213 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1214 NULL);
1215 else if (si->nr_ours || si->nr_theirs) {
1216 if (args->reject_shallow_remote)
1217 die(_("source repository is shallow, reject to clone."));
1218 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1219 } else
1220 alternate_shallow_file = NULL;
1221 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1222 &fsck_options.gitmodules_found))
1223 die(_("git fetch-pack: fetch failed."));
1224 if (fsck_finish(&fsck_options))
1225 die("fsck failed");
1227 all_done:
1228 if (negotiator)
1229 negotiator->release(negotiator);
1230 return ref;
1233 static void add_shallow_requests(struct strbuf *req_buf,
1234 const struct fetch_pack_args *args)
1236 if (is_repository_shallow(the_repository))
1237 write_shallow_commits(req_buf, 1, NULL);
1238 if (args->depth > 0)
1239 packet_buf_write(req_buf, "deepen %d", args->depth);
1240 if (args->deepen_since) {
1241 timestamp_t max_age = approxidate(args->deepen_since);
1242 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1244 if (args->deepen_not) {
1245 int i;
1246 for (i = 0; i < args->deepen_not->nr; i++) {
1247 struct string_list_item *s = args->deepen_not->items + i;
1248 packet_buf_write(req_buf, "deepen-not %s", s->string);
1251 if (args->deepen_relative)
1252 packet_buf_write(req_buf, "deepen-relative\n");
1255 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1257 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1259 for ( ; wants ; wants = wants->next) {
1260 const struct object_id *remote = &wants->old_oid;
1261 struct object *o;
1264 * If that object is complete (i.e. it is an ancestor of a
1265 * local ref), we tell them we have it but do not have to
1266 * tell them about its ancestors, which they already know
1267 * about.
1269 * We use lookup_object here because we are only
1270 * interested in the case we *know* the object is
1271 * reachable and we have already scanned it.
1273 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1274 (o->flags & COMPLETE)) {
1275 continue;
1278 if (!use_ref_in_want || wants->exact_oid)
1279 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1280 else
1281 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1285 static void add_common(struct strbuf *req_buf, struct oidset *common)
1287 struct oidset_iter iter;
1288 const struct object_id *oid;
1289 oidset_iter_init(common, &iter);
1291 while ((oid = oidset_iter_next(&iter))) {
1292 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1296 static int add_haves(struct fetch_negotiator *negotiator,
1297 struct strbuf *req_buf,
1298 int *haves_to_send)
1300 int haves_added = 0;
1301 const struct object_id *oid;
1303 while ((oid = negotiator->next(negotiator))) {
1304 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1305 if (++haves_added >= *haves_to_send)
1306 break;
1309 /* Increase haves to send on next round */
1310 *haves_to_send = next_flush(1, *haves_to_send);
1312 return haves_added;
1315 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1316 const struct string_list *server_options)
1318 const char *hash_name;
1320 ensure_server_supports_v2("fetch");
1321 packet_buf_write(req_buf, "command=fetch");
1322 if (server_supports_v2("agent"))
1323 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1324 if (advertise_sid && server_supports_v2("session-id"))
1325 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1326 if (server_options && server_options->nr) {
1327 int i;
1328 ensure_server_supports_v2("server-option");
1329 for (i = 0; i < server_options->nr; i++)
1330 packet_buf_write(req_buf, "server-option=%s",
1331 server_options->items[i].string);
1334 if (server_feature_v2("object-format", &hash_name)) {
1335 int hash_algo = hash_algo_by_name(hash_name);
1336 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1337 die(_("mismatched algorithms: client %s; server %s"),
1338 the_hash_algo->name, hash_name);
1339 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1340 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1341 die(_("the server does not support algorithm '%s'"),
1342 the_hash_algo->name);
1344 packet_buf_delim(req_buf);
1347 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1348 struct fetch_pack_args *args,
1349 const struct ref *wants, struct oidset *common,
1350 int *haves_to_send, int *in_vain,
1351 int sideband_all, int seen_ack)
1353 int haves_added;
1354 int done_sent = 0;
1355 struct strbuf req_buf = STRBUF_INIT;
1357 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1359 if (args->use_thin_pack)
1360 packet_buf_write(&req_buf, "thin-pack");
1361 if (args->no_progress)
1362 packet_buf_write(&req_buf, "no-progress");
1363 if (args->include_tag)
1364 packet_buf_write(&req_buf, "include-tag");
1365 if (prefer_ofs_delta)
1366 packet_buf_write(&req_buf, "ofs-delta");
1367 if (sideband_all)
1368 packet_buf_write(&req_buf, "sideband-all");
1370 /* Add shallow-info and deepen request */
1371 if (server_supports_feature("fetch", "shallow", 0))
1372 add_shallow_requests(&req_buf, args);
1373 else if (is_repository_shallow(the_repository) || args->deepen)
1374 die(_("Server does not support shallow requests"));
1376 /* Add filter */
1377 send_filter(args, &req_buf,
1378 server_supports_feature("fetch", "filter", 0));
1380 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1381 int i;
1382 struct strbuf to_send = STRBUF_INIT;
1384 for (i = 0; i < uri_protocols.nr; i++) {
1385 const char *s = uri_protocols.items[i].string;
1387 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1388 if (to_send.len)
1389 strbuf_addch(&to_send, ',');
1390 strbuf_addstr(&to_send, s);
1393 if (to_send.len) {
1394 packet_buf_write(&req_buf, "packfile-uris %s",
1395 to_send.buf);
1396 strbuf_release(&to_send);
1400 /* add wants */
1401 add_wants(wants, &req_buf);
1403 /* Add all of the common commits we've found in previous rounds */
1404 add_common(&req_buf, common);
1406 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1407 *in_vain += haves_added;
1408 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1409 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
1410 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1411 /* Send Done */
1412 packet_buf_write(&req_buf, "done\n");
1413 done_sent = 1;
1416 /* Send request */
1417 packet_buf_flush(&req_buf);
1418 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1419 die_errno(_("unable to write request to remote"));
1421 strbuf_release(&req_buf);
1422 return done_sent;
1426 * Processes a section header in a server's response and checks if it matches
1427 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1428 * not consumed); if 0, the line will be consumed and the function will die if
1429 * the section header doesn't match what was expected.
1431 static int process_section_header(struct packet_reader *reader,
1432 const char *section, int peek)
1434 int ret = 0;
1436 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1437 !strcmp(reader->line, section))
1438 ret = 1;
1440 if (!peek) {
1441 if (!ret) {
1442 if (reader->line)
1443 die(_("expected '%s', received '%s'"),
1444 section, reader->line);
1445 else
1446 die(_("expected '%s'"), section);
1448 packet_reader_read(reader);
1451 return ret;
1454 static int process_ack(struct fetch_negotiator *negotiator,
1455 struct packet_reader *reader,
1456 struct object_id *common_oid,
1457 int *received_ready)
1459 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1460 const char *arg;
1462 if (!strcmp(reader->line, "NAK"))
1463 continue;
1465 if (skip_prefix(reader->line, "ACK ", &arg)) {
1466 if (!get_oid_hex(arg, common_oid)) {
1467 struct commit *commit;
1468 commit = lookup_commit(the_repository, common_oid);
1469 if (negotiator)
1470 negotiator->ack(negotiator, commit);
1472 return 1;
1475 if (!strcmp(reader->line, "ready")) {
1476 *received_ready = 1;
1477 continue;
1480 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1483 if (reader->status != PACKET_READ_FLUSH &&
1484 reader->status != PACKET_READ_DELIM)
1485 die(_("error processing acks: %d"), reader->status);
1488 * If an "acknowledgments" section is sent, a packfile is sent if and
1489 * only if "ready" was sent in this section. The other sections
1490 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1491 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1492 * otherwise.
1494 if (*received_ready && reader->status != PACKET_READ_DELIM)
1496 * TRANSLATORS: The parameter will be 'ready', a protocol
1497 * keyword.
1499 die(_("expected packfile to be sent after '%s'"), "ready");
1500 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1502 * TRANSLATORS: The parameter will be 'ready', a protocol
1503 * keyword.
1505 die(_("expected no other sections to be sent after no '%s'"), "ready");
1507 return 0;
1510 static void receive_shallow_info(struct fetch_pack_args *args,
1511 struct packet_reader *reader,
1512 struct oid_array *shallows,
1513 struct shallow_info *si)
1515 int unshallow_received = 0;
1517 process_section_header(reader, "shallow-info", 0);
1518 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1519 const char *arg;
1520 struct object_id oid;
1522 if (skip_prefix(reader->line, "shallow ", &arg)) {
1523 if (get_oid_hex(arg, &oid))
1524 die(_("invalid shallow line: %s"), reader->line);
1525 oid_array_append(shallows, &oid);
1526 continue;
1528 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1529 if (get_oid_hex(arg, &oid))
1530 die(_("invalid unshallow line: %s"), reader->line);
1531 if (!lookup_object(the_repository, &oid))
1532 die(_("object not found: %s"), reader->line);
1533 /* make sure that it is parsed as shallow */
1534 if (!parse_object(the_repository, &oid))
1535 die(_("error in object: %s"), reader->line);
1536 if (unregister_shallow(&oid))
1537 die(_("no shallow found: %s"), reader->line);
1538 unshallow_received = 1;
1539 continue;
1541 die(_("expected shallow/unshallow, got %s"), reader->line);
1544 if (reader->status != PACKET_READ_FLUSH &&
1545 reader->status != PACKET_READ_DELIM)
1546 die(_("error processing shallow info: %d"), reader->status);
1548 if (args->deepen || unshallow_received) {
1550 * Treat these as shallow lines caused by our depth settings.
1551 * In v0, these lines cannot cause refs to be rejected; do the
1552 * same.
1554 int i;
1556 for (i = 0; i < shallows->nr; i++)
1557 register_shallow(the_repository, &shallows->oid[i]);
1558 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1559 NULL);
1560 args->deepen = 1;
1561 } else if (shallows->nr) {
1563 * Treat these as shallow lines caused by the remote being
1564 * shallow. In v0, remote refs that reach these objects are
1565 * rejected (unless --update-shallow is set); do the same.
1567 prepare_shallow_info(si, shallows);
1568 if (si->nr_ours || si->nr_theirs) {
1569 if (args->reject_shallow_remote)
1570 die(_("source repository is shallow, reject to clone."));
1571 alternate_shallow_file =
1572 setup_temporary_shallow(si->shallow);
1573 } else
1574 alternate_shallow_file = NULL;
1575 } else {
1576 alternate_shallow_file = NULL;
1580 static int cmp_name_ref(const void *name, const void *ref)
1582 return strcmp(name, (*(struct ref **)ref)->name);
1585 static void receive_wanted_refs(struct packet_reader *reader,
1586 struct ref **sought, int nr_sought)
1588 process_section_header(reader, "wanted-refs", 0);
1589 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1590 struct object_id oid;
1591 const char *end;
1592 struct ref **found;
1594 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1595 die(_("expected wanted-ref, got '%s'"), reader->line);
1597 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1598 cmp_name_ref);
1599 if (!found)
1600 die(_("unexpected wanted-ref: '%s'"), reader->line);
1601 oidcpy(&(*found)->old_oid, &oid);
1604 if (reader->status != PACKET_READ_DELIM)
1605 die(_("error processing wanted refs: %d"), reader->status);
1608 static void receive_packfile_uris(struct packet_reader *reader,
1609 struct string_list *uris)
1611 process_section_header(reader, "packfile-uris", 0);
1612 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1613 if (reader->pktlen < the_hash_algo->hexsz ||
1614 reader->line[the_hash_algo->hexsz] != ' ')
1615 die("expected '<hash> <uri>', got: %s\n", reader->line);
1617 string_list_append(uris, reader->line);
1619 if (reader->status != PACKET_READ_DELIM)
1620 die("expected DELIM");
1623 enum fetch_state {
1624 FETCH_CHECK_LOCAL = 0,
1625 FETCH_SEND_REQUEST,
1626 FETCH_PROCESS_ACKS,
1627 FETCH_GET_PACK,
1628 FETCH_DONE,
1631 static void do_check_stateless_delimiter(int stateless_rpc,
1632 struct packet_reader *reader)
1634 check_stateless_delimiter(stateless_rpc, reader,
1635 _("git fetch-pack: expected response end packet"));
1638 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1639 int fd[2],
1640 const struct ref *orig_ref,
1641 struct ref **sought, int nr_sought,
1642 struct oid_array *shallows,
1643 struct shallow_info *si,
1644 struct string_list *pack_lockfiles)
1646 struct repository *r = the_repository;
1647 struct ref *ref = copy_ref_list(orig_ref);
1648 enum fetch_state state = FETCH_CHECK_LOCAL;
1649 struct oidset common = OIDSET_INIT;
1650 struct packet_reader reader;
1651 int in_vain = 0, negotiation_started = 0;
1652 int negotiation_round = 0;
1653 int haves_to_send = INITIAL_FLUSH;
1654 struct fetch_negotiator negotiator_alloc;
1655 struct fetch_negotiator *negotiator;
1656 int seen_ack = 0;
1657 struct object_id common_oid;
1658 int received_ready = 0;
1659 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1660 int i;
1661 struct strvec index_pack_args = STRVEC_INIT;
1663 negotiator = &negotiator_alloc;
1664 if (args->refetch)
1665 fetch_negotiator_init_noop(negotiator);
1666 else
1667 fetch_negotiator_init(r, negotiator);
1669 packet_reader_init(&reader, fd[0], NULL, 0,
1670 PACKET_READ_CHOMP_NEWLINE |
1671 PACKET_READ_DIE_ON_ERR_PACKET);
1672 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1673 server_supports_feature("fetch", "sideband-all", 0)) {
1674 reader.use_sideband = 1;
1675 reader.me = "fetch-pack";
1678 while (state != FETCH_DONE) {
1679 switch (state) {
1680 case FETCH_CHECK_LOCAL:
1681 sort_ref_list(&ref, ref_compare_name);
1682 QSORT(sought, nr_sought, cmp_ref_by_name);
1684 /* v2 supports these by default */
1685 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1686 use_sideband = 2;
1687 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1688 args->deepen = 1;
1690 /* Filter 'ref' by 'sought' and those that aren't local */
1691 mark_complete_and_common_ref(negotiator, args, &ref);
1692 filter_refs(args, &ref, sought, nr_sought);
1693 if (!args->refetch && everything_local(args, &ref))
1694 state = FETCH_DONE;
1695 else
1696 state = FETCH_SEND_REQUEST;
1698 mark_tips(negotiator, args->negotiation_tips);
1699 for_each_cached_alternate(negotiator,
1700 insert_one_alternate_object);
1701 break;
1702 case FETCH_SEND_REQUEST:
1703 if (!negotiation_started) {
1704 negotiation_started = 1;
1705 trace2_region_enter("fetch-pack",
1706 "negotiation_v2",
1707 the_repository);
1709 negotiation_round++;
1710 trace2_region_enter_printf("negotiation_v2", "round",
1711 the_repository, "%d",
1712 negotiation_round);
1713 if (send_fetch_request(negotiator, fd[1], args, ref,
1714 &common,
1715 &haves_to_send, &in_vain,
1716 reader.use_sideband,
1717 seen_ack)) {
1718 trace2_region_leave_printf("negotiation_v2", "round",
1719 the_repository, "%d",
1720 negotiation_round);
1721 state = FETCH_GET_PACK;
1723 else
1724 state = FETCH_PROCESS_ACKS;
1725 break;
1726 case FETCH_PROCESS_ACKS:
1727 /* Process ACKs/NAKs */
1728 process_section_header(&reader, "acknowledgments", 0);
1729 while (process_ack(negotiator, &reader, &common_oid,
1730 &received_ready)) {
1731 in_vain = 0;
1732 seen_ack = 1;
1733 oidset_insert(&common, &common_oid);
1735 trace2_region_leave_printf("negotiation_v2", "round",
1736 the_repository, "%d",
1737 negotiation_round);
1738 if (received_ready) {
1740 * Don't check for response delimiter; get_pack() will
1741 * read the rest of this response.
1743 state = FETCH_GET_PACK;
1744 } else {
1745 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1746 state = FETCH_SEND_REQUEST;
1748 break;
1749 case FETCH_GET_PACK:
1750 trace2_region_leave("fetch-pack",
1751 "negotiation_v2",
1752 the_repository);
1753 trace2_data_intmax("negotiation_v2", the_repository,
1754 "total_rounds", negotiation_round);
1755 /* Check for shallow-info section */
1756 if (process_section_header(&reader, "shallow-info", 1))
1757 receive_shallow_info(args, &reader, shallows, si);
1759 if (process_section_header(&reader, "wanted-refs", 1))
1760 receive_wanted_refs(&reader, sought, nr_sought);
1762 /* get the pack(s) */
1763 if (git_env_bool("GIT_TRACE_REDACT", 1))
1764 reader.options |= PACKET_READ_REDACT_URI_PATH;
1765 if (process_section_header(&reader, "packfile-uris", 1))
1766 receive_packfile_uris(&reader, &packfile_uris);
1767 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1768 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1770 process_section_header(&reader, "packfile", 0);
1773 * this is the final request we'll make of the server;
1774 * do a half-duplex shutdown to indicate that they can
1775 * hang up as soon as the pack is sent.
1777 close(fd[1]);
1778 fd[1] = -1;
1780 if (get_pack(args, fd, pack_lockfiles,
1781 packfile_uris.nr ? &index_pack_args : NULL,
1782 sought, nr_sought, &fsck_options.gitmodules_found))
1783 die(_("git fetch-pack: fetch failed."));
1784 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1786 state = FETCH_DONE;
1787 break;
1788 case FETCH_DONE:
1789 continue;
1793 for (i = 0; i < packfile_uris.nr; i++) {
1794 int j;
1795 struct child_process cmd = CHILD_PROCESS_INIT;
1796 char packname[GIT_MAX_HEXSZ + 1];
1797 const char *uri = packfile_uris.items[i].string +
1798 the_hash_algo->hexsz + 1;
1800 strvec_push(&cmd.args, "http-fetch");
1801 strvec_pushf(&cmd.args, "--packfile=%.*s",
1802 (int) the_hash_algo->hexsz,
1803 packfile_uris.items[i].string);
1804 for (j = 0; j < index_pack_args.nr; j++)
1805 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1806 index_pack_args.v[j]);
1807 strvec_push(&cmd.args, uri);
1808 cmd.git_cmd = 1;
1809 cmd.no_stdin = 1;
1810 cmd.out = -1;
1811 if (start_command(&cmd))
1812 die("fetch-pack: unable to spawn http-fetch");
1814 if (read_in_full(cmd.out, packname, 5) < 0 ||
1815 memcmp(packname, "keep\t", 5))
1816 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1818 if (read_in_full(cmd.out, packname,
1819 the_hash_algo->hexsz + 1) < 0 ||
1820 packname[the_hash_algo->hexsz] != '\n')
1821 die("fetch-pack: expected hash then LF at end of http-fetch output");
1823 packname[the_hash_algo->hexsz] = '\0';
1825 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1827 close(cmd.out);
1829 if (finish_command(&cmd))
1830 die("fetch-pack: unable to finish http-fetch");
1832 if (memcmp(packfile_uris.items[i].string, packname,
1833 the_hash_algo->hexsz))
1834 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1835 uri, (int) the_hash_algo->hexsz,
1836 packfile_uris.items[i].string);
1838 string_list_append_nodup(pack_lockfiles,
1839 xstrfmt("%s/pack/pack-%s.keep",
1840 get_object_directory(),
1841 packname));
1843 string_list_clear(&packfile_uris, 0);
1844 strvec_clear(&index_pack_args);
1846 if (fsck_finish(&fsck_options))
1847 die("fsck failed");
1849 if (negotiator)
1850 negotiator->release(negotiator);
1852 oidset_clear(&common);
1853 return ref;
1856 static int fetch_pack_config_cb(const char *var, const char *value,
1857 const struct config_context *ctx, void *cb)
1859 const char *msg_id;
1861 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1862 char *path ;
1864 if (git_config_pathname(&path, var, value))
1865 return 1;
1866 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1867 fsck_msg_types.len ? ',' : '=', path);
1868 free(path);
1869 return 0;
1872 if (skip_prefix(var, "fetch.fsck.", &msg_id)) {
1873 if (!value)
1874 return config_error_nonbool(var);
1875 if (is_valid_msg_type(msg_id, value))
1876 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1877 fsck_msg_types.len ? ',' : '=', msg_id, value);
1878 else
1879 warning("Skipping unknown msg id '%s'", msg_id);
1880 return 0;
1883 return git_default_config(var, value, ctx, cb);
1886 static void fetch_pack_config(void)
1888 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1889 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1890 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1891 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1892 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1893 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1894 if (!uri_protocols.nr) {
1895 char *str;
1897 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1898 string_list_split(&uri_protocols, str, ',', -1);
1899 free(str);
1903 git_config(fetch_pack_config_cb, NULL);
1906 static void fetch_pack_setup(void)
1908 static int did_setup;
1909 if (did_setup)
1910 return;
1911 fetch_pack_config();
1912 if (0 <= fetch_unpack_limit)
1913 unpack_limit = fetch_unpack_limit;
1914 else if (0 <= transfer_unpack_limit)
1915 unpack_limit = transfer_unpack_limit;
1916 did_setup = 1;
1919 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1921 struct string_list names = STRING_LIST_INIT_NODUP;
1922 int src, dst;
1924 for (src = dst = 0; src < nr; src++) {
1925 struct string_list_item *item;
1926 item = string_list_insert(&names, ref[src]->name);
1927 if (item->util)
1928 continue; /* already have it */
1929 item->util = ref[src];
1930 if (src != dst)
1931 ref[dst] = ref[src];
1932 dst++;
1934 for (src = dst; src < nr; src++)
1935 ref[src] = NULL;
1936 string_list_clear(&names, 0);
1937 return dst;
1940 static void update_shallow(struct fetch_pack_args *args,
1941 struct ref **sought, int nr_sought,
1942 struct shallow_info *si)
1944 struct oid_array ref = OID_ARRAY_INIT;
1945 int *status;
1946 int i;
1948 if (args->deepen && alternate_shallow_file) {
1949 if (*alternate_shallow_file == '\0') { /* --unshallow */
1950 unlink_or_warn(git_path_shallow(the_repository));
1951 rollback_shallow_file(the_repository, &shallow_lock);
1952 } else
1953 commit_shallow_file(the_repository, &shallow_lock);
1954 alternate_shallow_file = NULL;
1955 return;
1958 if (!si->shallow || !si->shallow->nr)
1959 return;
1961 if (args->cloning) {
1963 * remote is shallow, but this is a clone, there are
1964 * no objects in repo to worry about. Accept any
1965 * shallow points that exist in the pack (iow in repo
1966 * after get_pack() and reprepare_packed_git())
1968 struct oid_array extra = OID_ARRAY_INIT;
1969 struct object_id *oid = si->shallow->oid;
1970 for (i = 0; i < si->shallow->nr; i++)
1971 if (repo_has_object_file(the_repository, &oid[i]))
1972 oid_array_append(&extra, &oid[i]);
1973 if (extra.nr) {
1974 setup_alternate_shallow(&shallow_lock,
1975 &alternate_shallow_file,
1976 &extra);
1977 commit_shallow_file(the_repository, &shallow_lock);
1978 alternate_shallow_file = NULL;
1980 oid_array_clear(&extra);
1981 return;
1984 if (!si->nr_ours && !si->nr_theirs)
1985 return;
1987 remove_nonexistent_theirs_shallow(si);
1988 if (!si->nr_ours && !si->nr_theirs)
1989 return;
1990 for (i = 0; i < nr_sought; i++)
1991 oid_array_append(&ref, &sought[i]->old_oid);
1992 si->ref = &ref;
1994 if (args->update_shallow) {
1996 * remote is also shallow, .git/shallow may be updated
1997 * so all refs can be accepted. Make sure we only add
1998 * shallow roots that are actually reachable from new
1999 * refs.
2001 struct oid_array extra = OID_ARRAY_INIT;
2002 struct object_id *oid = si->shallow->oid;
2003 assign_shallow_commits_to_refs(si, NULL, NULL);
2004 if (!si->nr_ours && !si->nr_theirs) {
2005 oid_array_clear(&ref);
2006 return;
2008 for (i = 0; i < si->nr_ours; i++)
2009 oid_array_append(&extra, &oid[si->ours[i]]);
2010 for (i = 0; i < si->nr_theirs; i++)
2011 oid_array_append(&extra, &oid[si->theirs[i]]);
2012 setup_alternate_shallow(&shallow_lock,
2013 &alternate_shallow_file,
2014 &extra);
2015 commit_shallow_file(the_repository, &shallow_lock);
2016 oid_array_clear(&extra);
2017 oid_array_clear(&ref);
2018 alternate_shallow_file = NULL;
2019 return;
2023 * remote is also shallow, check what ref is safe to update
2024 * without updating .git/shallow
2026 CALLOC_ARRAY(status, nr_sought);
2027 assign_shallow_commits_to_refs(si, NULL, status);
2028 if (si->nr_ours || si->nr_theirs) {
2029 for (i = 0; i < nr_sought; i++)
2030 if (status[i])
2031 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
2033 free(status);
2034 oid_array_clear(&ref);
2037 static const struct object_id *iterate_ref_map(void *cb_data)
2039 struct ref **rm = cb_data;
2040 struct ref *ref = *rm;
2042 if (!ref)
2043 return NULL;
2044 *rm = ref->next;
2045 return &ref->old_oid;
2048 int fetch_pack_fsck_objects(void)
2050 fetch_pack_setup();
2051 if (fetch_fsck_objects >= 0)
2052 return fetch_fsck_objects;
2053 if (transfer_fsck_objects >= 0)
2054 return transfer_fsck_objects;
2055 return 0;
2058 struct ref *fetch_pack(struct fetch_pack_args *args,
2059 int fd[],
2060 const struct ref *ref,
2061 struct ref **sought, int nr_sought,
2062 struct oid_array *shallow,
2063 struct string_list *pack_lockfiles,
2064 enum protocol_version version)
2066 struct ref *ref_cpy;
2067 struct shallow_info si;
2068 struct oid_array shallows_scratch = OID_ARRAY_INIT;
2070 fetch_pack_setup();
2071 if (nr_sought)
2072 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
2074 if (version != protocol_v2 && !ref) {
2075 packet_flush(fd[1]);
2076 die(_("no matching remote head"));
2078 if (version == protocol_v2) {
2079 if (shallow->nr)
2080 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2081 memset(&si, 0, sizeof(si));
2082 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
2083 &shallows_scratch, &si,
2084 pack_lockfiles);
2085 } else {
2086 prepare_shallow_info(&si, shallow);
2087 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
2088 &si, pack_lockfiles);
2090 reprepare_packed_git(the_repository);
2092 if (!args->cloning && args->deepen) {
2093 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2094 struct ref *iterator = ref_cpy;
2095 opt.shallow_file = alternate_shallow_file;
2096 if (args->deepen)
2097 opt.is_deepening_fetch = 1;
2098 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2099 error(_("remote did not send all necessary objects"));
2100 free_refs(ref_cpy);
2101 ref_cpy = NULL;
2102 rollback_shallow_file(the_repository, &shallow_lock);
2103 goto cleanup;
2105 args->connectivity_checked = 1;
2108 update_shallow(args, sought, nr_sought, &si);
2109 cleanup:
2110 clear_shallow_info(&si);
2111 oid_array_clear(&shallows_scratch);
2112 return ref_cpy;
2115 static int add_to_object_array(const struct object_id *oid, void *data)
2117 struct object_array *a = data;
2119 add_object_array(lookup_object(the_repository, oid), "", a);
2120 return 0;
2123 static void clear_common_flag(struct oidset *s)
2125 struct oidset_iter iter;
2126 const struct object_id *oid;
2127 oidset_iter_init(s, &iter);
2129 while ((oid = oidset_iter_next(&iter))) {
2130 struct object *obj = lookup_object(the_repository, oid);
2131 obj->flags &= ~COMMON;
2135 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2136 const struct string_list *server_options,
2137 int stateless_rpc,
2138 int fd[],
2139 struct oidset *acked_commits)
2141 struct fetch_negotiator negotiator;
2142 struct packet_reader reader;
2143 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2144 struct strbuf req_buf = STRBUF_INIT;
2145 int haves_to_send = INITIAL_FLUSH;
2146 int in_vain = 0;
2147 int seen_ack = 0;
2148 int last_iteration = 0;
2149 int negotiation_round = 0;
2150 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2152 fetch_negotiator_init(the_repository, &negotiator);
2153 mark_tips(&negotiator, negotiation_tips);
2155 packet_reader_init(&reader, fd[0], NULL, 0,
2156 PACKET_READ_CHOMP_NEWLINE |
2157 PACKET_READ_DIE_ON_ERR_PACKET);
2159 oid_array_for_each((struct oid_array *) negotiation_tips,
2160 add_to_object_array,
2161 &nt_object_array);
2163 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2164 while (!last_iteration) {
2165 int haves_added;
2166 struct object_id common_oid;
2167 int received_ready = 0;
2169 negotiation_round++;
2171 trace2_region_enter_printf("negotiate_using_fetch", "round",
2172 the_repository, "%d",
2173 negotiation_round);
2174 strbuf_reset(&req_buf);
2175 write_fetch_command_and_capabilities(&req_buf, server_options);
2177 packet_buf_write(&req_buf, "wait-for-done");
2179 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2180 in_vain += haves_added;
2181 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2182 last_iteration = 1;
2184 trace2_data_intmax("negotiate_using_fetch", the_repository,
2185 "haves_added", haves_added);
2186 trace2_data_intmax("negotiate_using_fetch", the_repository,
2187 "in_vain", in_vain);
2189 /* Send request */
2190 packet_buf_flush(&req_buf);
2191 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2192 die_errno(_("unable to write request to remote"));
2194 /* Process ACKs/NAKs */
2195 process_section_header(&reader, "acknowledgments", 0);
2196 while (process_ack(&negotiator, &reader, &common_oid,
2197 &received_ready)) {
2198 struct commit *commit = lookup_commit(the_repository,
2199 &common_oid);
2200 if (commit) {
2201 timestamp_t generation;
2203 parse_commit_or_die(commit);
2204 commit->object.flags |= COMMON;
2205 generation = commit_graph_generation(commit);
2206 if (generation < min_generation)
2207 min_generation = generation;
2209 in_vain = 0;
2210 seen_ack = 1;
2211 oidset_insert(acked_commits, &common_oid);
2213 if (received_ready)
2214 die(_("unexpected 'ready' from remote"));
2215 else
2216 do_check_stateless_delimiter(stateless_rpc, &reader);
2217 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2218 REACH_SCRATCH, 0,
2219 min_generation))
2220 last_iteration = 1;
2221 trace2_region_leave_printf("negotiation", "round",
2222 the_repository, "%d",
2223 negotiation_round);
2225 trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository);
2226 trace2_data_intmax("negotiate_using_fetch", the_repository,
2227 "total_rounds", negotiation_round);
2228 clear_common_flag(acked_commits);
2229 strbuf_release(&req_buf);
2232 int report_unmatched_refs(struct ref **sought, int nr_sought)
2234 int i, ret = 0;
2236 for (i = 0; i < nr_sought; i++) {
2237 if (!sought[i])
2238 continue;
2239 switch (sought[i]->match_status) {
2240 case REF_MATCHED:
2241 continue;
2242 case REF_NOT_MATCHED:
2243 error(_("no such remote ref %s"), sought[i]->name);
2244 break;
2245 case REF_UNADVERTISED_NOT_ALLOWED:
2246 error(_("Server does not allow request for unadvertised object %s"),
2247 sought[i]->name);
2248 break;
2250 ret = 1;
2252 return ret;