Merge branch 'vd/fix-perf-tests'
[alt-git.git] / fetch-pack.c
bloba1a508ee72e2f6f58ed4b61430bf2a6d61c00651
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 "oid-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 #include "fsck.h"
25 #include "shallow.h"
26 #include "commit-reach.h"
27 #include "commit-graph.h"
28 #include "sigchain.h"
29 #include "mergesort.h"
31 static int transfer_unpack_limit = -1;
32 static int fetch_unpack_limit = -1;
33 static int unpack_limit = 100;
34 static int prefer_ofs_delta = 1;
35 static int no_done;
36 static int deepen_since_ok;
37 static int deepen_not_ok;
38 static int fetch_fsck_objects = -1;
39 static int transfer_fsck_objects = -1;
40 static int agent_supported;
41 static int server_supports_filtering;
42 static int advertise_sid;
43 static struct shallow_lock shallow_lock;
44 static const char *alternate_shallow_file;
45 static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
46 static struct strbuf fsck_msg_types = STRBUF_INIT;
47 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
49 /* Remember to update object flag allocation in object.h */
50 #define COMPLETE (1U << 0)
51 #define ALTERNATE (1U << 1)
52 #define COMMON (1U << 6)
53 #define REACH_SCRATCH (1U << 7)
56 * After sending this many "have"s if we do not get any new ACK , we
57 * give up traversing our history.
59 #define MAX_IN_VAIN 256
61 static int multi_ack, use_sideband;
62 /* Allow specifying sha1 if it is a ref tip. */
63 #define ALLOW_TIP_SHA1 01
64 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
65 #define ALLOW_REACHABLE_SHA1 02
66 static unsigned int allow_unadvertised_object_request;
68 __attribute__((format (printf, 2, 3)))
69 static inline void print_verbose(const struct fetch_pack_args *args,
70 const char *fmt, ...)
72 va_list params;
74 if (!args->verbose)
75 return;
77 va_start(params, fmt);
78 vfprintf(stderr, fmt, params);
79 va_end(params);
80 fputc('\n', stderr);
83 struct alternate_object_cache {
84 struct object **items;
85 size_t nr, alloc;
88 static void cache_one_alternate(const struct object_id *oid,
89 void *vcache)
91 struct alternate_object_cache *cache = vcache;
92 struct object *obj = parse_object(the_repository, oid);
94 if (!obj || (obj->flags & ALTERNATE))
95 return;
97 obj->flags |= ALTERNATE;
98 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
99 cache->items[cache->nr++] = obj;
102 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
103 void (*cb)(struct fetch_negotiator *,
104 struct object *))
106 static int initialized;
107 static struct alternate_object_cache cache;
108 size_t i;
110 if (!initialized) {
111 for_each_alternate_ref(cache_one_alternate, &cache);
112 initialized = 1;
115 for (i = 0; i < cache.nr; i++)
116 cb(negotiator, cache.items[i]);
119 static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid,
120 int mark_tags_complete,
121 enum object_type *type,
122 unsigned int oi_flags)
124 struct object_info info = { .typep = type };
125 struct commit *commit;
127 commit = lookup_commit_in_graph(the_repository, oid);
128 if (commit)
129 return commit;
131 while (1) {
132 if (oid_object_info_extended(the_repository, oid, &info,
133 oi_flags))
134 return NULL;
135 if (*type == OBJ_TAG) {
136 struct tag *tag = (struct tag *)
137 parse_object(the_repository, oid);
139 if (!tag->tagged)
140 return NULL;
141 if (mark_tags_complete)
142 tag->object.flags |= COMPLETE;
143 oid = &tag->tagged->oid;
144 } else {
145 break;
149 if (*type == OBJ_COMMIT) {
150 struct commit *commit = lookup_commit(the_repository, oid);
151 if (!commit || repo_parse_commit(the_repository, commit))
152 return NULL;
153 return commit;
156 return NULL;
160 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
161 int mark_tags_complete)
163 enum object_type type;
164 unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK;
165 return deref_without_lazy_fetch_extended(oid, mark_tags_complete,
166 &type, flags);
169 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
170 const struct object_id *oid)
172 struct commit *c = deref_without_lazy_fetch(oid, 0);
174 if (c)
175 negotiator->add_tip(negotiator, c);
176 return 0;
179 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
180 int flag, void *cb_data)
182 return rev_list_insert_ref(cb_data, oid);
185 enum ack_type {
186 NAK = 0,
187 ACK,
188 ACK_continue,
189 ACK_common,
190 ACK_ready
193 static void consume_shallow_list(struct fetch_pack_args *args,
194 struct packet_reader *reader)
196 if (args->stateless_rpc && args->deepen) {
197 /* If we sent a depth we will get back "duplicate"
198 * shallow and unshallow commands every time there
199 * is a block of have lines exchanged.
201 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
202 if (starts_with(reader->line, "shallow "))
203 continue;
204 if (starts_with(reader->line, "unshallow "))
205 continue;
206 die(_("git fetch-pack: expected shallow list"));
208 if (reader->status != PACKET_READ_FLUSH)
209 die(_("git fetch-pack: expected a flush packet after shallow list"));
213 static enum ack_type get_ack(struct packet_reader *reader,
214 struct object_id *result_oid)
216 int len;
217 const char *arg;
219 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
220 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
221 len = reader->pktlen;
223 if (!strcmp(reader->line, "NAK"))
224 return NAK;
225 if (skip_prefix(reader->line, "ACK ", &arg)) {
226 const char *p;
227 if (!parse_oid_hex(arg, result_oid, &p)) {
228 len -= p - reader->line;
229 if (len < 1)
230 return ACK;
231 if (strstr(p, "continue"))
232 return ACK_continue;
233 if (strstr(p, "common"))
234 return ACK_common;
235 if (strstr(p, "ready"))
236 return ACK_ready;
237 return ACK;
240 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
243 static void send_request(struct fetch_pack_args *args,
244 int fd, struct strbuf *buf)
246 if (args->stateless_rpc) {
247 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
248 packet_flush(fd);
249 } else {
250 if (write_in_full(fd, buf->buf, buf->len) < 0)
251 die_errno(_("unable to write to remote"));
255 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
256 struct object *obj)
258 rev_list_insert_ref(negotiator, &obj->oid);
261 #define INITIAL_FLUSH 16
262 #define PIPESAFE_FLUSH 32
263 #define LARGE_FLUSH 16384
265 static int next_flush(int stateless_rpc, int count)
267 if (stateless_rpc) {
268 if (count < LARGE_FLUSH)
269 count <<= 1;
270 else
271 count = count * 11 / 10;
272 } else {
273 if (count < PIPESAFE_FLUSH)
274 count <<= 1;
275 else
276 count += PIPESAFE_FLUSH;
278 return count;
281 static void mark_tips(struct fetch_negotiator *negotiator,
282 const struct oid_array *negotiation_tips)
284 int i;
286 if (!negotiation_tips) {
287 for_each_rawref(rev_list_insert_ref_oid, negotiator);
288 return;
291 for (i = 0; i < negotiation_tips->nr; i++)
292 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
293 return;
296 static void send_filter(struct fetch_pack_args *args,
297 struct strbuf *req_buf,
298 int server_supports_filter)
300 if (args->filter_options.choice) {
301 const char *spec =
302 expand_list_objects_filter_spec(&args->filter_options);
303 if (server_supports_filter) {
304 print_verbose(args, _("Server supports filter"));
305 packet_buf_write(req_buf, "filter %s", spec);
306 trace2_data_string("fetch", the_repository,
307 "filter/effective", spec);
308 } else {
309 warning("filtering not recognized by server, ignoring");
310 trace2_data_string("fetch", the_repository,
311 "filter/unsupported", spec);
313 } else {
314 trace2_data_string("fetch", the_repository,
315 "filter/none", "");
319 static int find_common(struct fetch_negotiator *negotiator,
320 struct fetch_pack_args *args,
321 int fd[2], struct object_id *result_oid,
322 struct ref *refs)
324 int fetching;
325 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
326 int negotiation_round = 0, haves = 0;
327 const struct object_id *oid;
328 unsigned in_vain = 0;
329 int got_continue = 0;
330 int got_ready = 0;
331 struct strbuf req_buf = STRBUF_INIT;
332 size_t state_len = 0;
333 struct packet_reader reader;
335 if (args->stateless_rpc && multi_ack == 1)
336 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
338 packet_reader_init(&reader, fd[0], NULL, 0,
339 PACKET_READ_CHOMP_NEWLINE |
340 PACKET_READ_DIE_ON_ERR_PACKET);
342 mark_tips(negotiator, args->negotiation_tips);
343 for_each_cached_alternate(negotiator, insert_one_alternate_object);
345 fetching = 0;
346 for ( ; refs ; refs = refs->next) {
347 struct object_id *remote = &refs->old_oid;
348 const char *remote_hex;
349 struct object *o;
351 if (!args->refetch) {
353 * If that object is complete (i.e. it is an ancestor of a
354 * local ref), we tell them we have it but do not have to
355 * tell them about its ancestors, which they already know
356 * about.
358 * We use lookup_object here because we are only
359 * interested in the case we *know* the object is
360 * reachable and we have already scanned it.
362 if (((o = lookup_object(the_repository, remote)) != NULL) &&
363 (o->flags & COMPLETE)) {
364 continue;
368 remote_hex = oid_to_hex(remote);
369 if (!fetching) {
370 struct strbuf c = STRBUF_INIT;
371 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
372 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
373 if (no_done) strbuf_addstr(&c, " no-done");
374 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
375 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
376 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
377 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
378 if (args->no_progress) strbuf_addstr(&c, " no-progress");
379 if (args->include_tag) strbuf_addstr(&c, " include-tag");
380 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
381 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
382 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
383 if (agent_supported) strbuf_addf(&c, " agent=%s",
384 git_user_agent_sanitized());
385 if (advertise_sid)
386 strbuf_addf(&c, " session-id=%s", trace2_session_id());
387 if (args->filter_options.choice)
388 strbuf_addstr(&c, " filter");
389 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
390 strbuf_release(&c);
391 } else
392 packet_buf_write(&req_buf, "want %s\n", remote_hex);
393 fetching++;
396 if (!fetching) {
397 strbuf_release(&req_buf);
398 packet_flush(fd[1]);
399 return 1;
402 if (is_repository_shallow(the_repository))
403 write_shallow_commits(&req_buf, 1, NULL);
404 if (args->depth > 0)
405 packet_buf_write(&req_buf, "deepen %d", args->depth);
406 if (args->deepen_since) {
407 timestamp_t max_age = approxidate(args->deepen_since);
408 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
410 if (args->deepen_not) {
411 int i;
412 for (i = 0; i < args->deepen_not->nr; i++) {
413 struct string_list_item *s = args->deepen_not->items + i;
414 packet_buf_write(&req_buf, "deepen-not %s", s->string);
417 send_filter(args, &req_buf, server_supports_filtering);
418 packet_buf_flush(&req_buf);
419 state_len = req_buf.len;
421 if (args->deepen) {
422 const char *arg;
423 struct object_id oid;
425 send_request(args, fd[1], &req_buf);
426 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
427 if (skip_prefix(reader.line, "shallow ", &arg)) {
428 if (get_oid_hex(arg, &oid))
429 die(_("invalid shallow line: %s"), reader.line);
430 register_shallow(the_repository, &oid);
431 continue;
433 if (skip_prefix(reader.line, "unshallow ", &arg)) {
434 if (get_oid_hex(arg, &oid))
435 die(_("invalid unshallow line: %s"), reader.line);
436 if (!lookup_object(the_repository, &oid))
437 die(_("object not found: %s"), reader.line);
438 /* make sure that it is parsed as shallow */
439 if (!parse_object(the_repository, &oid))
440 die(_("error in object: %s"), reader.line);
441 if (unregister_shallow(&oid))
442 die(_("no shallow found: %s"), reader.line);
443 continue;
445 die(_("expected shallow/unshallow, got %s"), reader.line);
447 } else if (!args->stateless_rpc)
448 send_request(args, fd[1], &req_buf);
450 if (!args->stateless_rpc) {
451 /* If we aren't using the stateless-rpc interface
452 * we don't need to retain the headers.
454 strbuf_setlen(&req_buf, 0);
455 state_len = 0;
458 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
459 flushes = 0;
460 retval = -1;
461 while ((oid = negotiator->next(negotiator))) {
462 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
463 print_verbose(args, "have %s", oid_to_hex(oid));
464 in_vain++;
465 haves++;
466 if (flush_at <= ++count) {
467 int ack;
469 negotiation_round++;
470 trace2_region_enter_printf("negotiation_v0_v1", "round",
471 the_repository, "%d",
472 negotiation_round);
473 trace2_data_intmax("negotiation_v0_v1", the_repository,
474 "haves_added", haves);
475 trace2_data_intmax("negotiation_v0_v1", the_repository,
476 "in_vain", in_vain);
477 haves = 0;
478 packet_buf_flush(&req_buf);
479 send_request(args, fd[1], &req_buf);
480 strbuf_setlen(&req_buf, state_len);
481 flushes++;
482 flush_at = next_flush(args->stateless_rpc, count);
485 * We keep one window "ahead" of the other side, and
486 * will wait for an ACK only on the next one
488 if (!args->stateless_rpc && count == INITIAL_FLUSH)
489 continue;
491 consume_shallow_list(args, &reader);
492 do {
493 ack = get_ack(&reader, result_oid);
494 if (ack)
495 print_verbose(args, _("got %s %d %s"), "ack",
496 ack, oid_to_hex(result_oid));
497 switch (ack) {
498 case ACK:
499 trace2_region_leave_printf("negotiation_v0_v1", "round",
500 the_repository, "%d",
501 negotiation_round);
502 flushes = 0;
503 multi_ack = 0;
504 retval = 0;
505 goto done;
506 case ACK_common:
507 case ACK_ready:
508 case ACK_continue: {
509 struct commit *commit =
510 lookup_commit(the_repository,
511 result_oid);
512 int was_common;
514 if (!commit)
515 die(_("invalid commit %s"), oid_to_hex(result_oid));
516 was_common = negotiator->ack(negotiator, commit);
517 if (args->stateless_rpc
518 && ack == ACK_common
519 && !was_common) {
520 /* We need to replay the have for this object
521 * on the next RPC request so the peer knows
522 * it is in common with us.
524 const char *hex = oid_to_hex(result_oid);
525 packet_buf_write(&req_buf, "have %s\n", hex);
526 state_len = req_buf.len;
527 haves++;
529 * Reset in_vain because an ack
530 * for this commit has not been
531 * seen.
533 in_vain = 0;
534 } else if (!args->stateless_rpc
535 || ack != ACK_common)
536 in_vain = 0;
537 retval = 0;
538 got_continue = 1;
539 if (ack == ACK_ready)
540 got_ready = 1;
541 break;
544 } while (ack);
545 flushes--;
546 trace2_region_leave_printf("negotiation_v0_v1", "round",
547 the_repository, "%d",
548 negotiation_round);
549 if (got_continue && MAX_IN_VAIN < in_vain) {
550 print_verbose(args, _("giving up"));
551 break; /* give up */
553 if (got_ready)
554 break;
557 done:
558 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
559 trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds",
560 negotiation_round);
561 if (!got_ready || !no_done) {
562 packet_buf_write(&req_buf, "done\n");
563 send_request(args, fd[1], &req_buf);
565 print_verbose(args, _("done"));
566 if (retval != 0) {
567 multi_ack = 0;
568 flushes++;
570 strbuf_release(&req_buf);
572 if (!got_ready || !no_done)
573 consume_shallow_list(args, &reader);
574 while (flushes || multi_ack) {
575 int ack = get_ack(&reader, result_oid);
576 if (ack) {
577 print_verbose(args, _("got %s (%d) %s"), "ack",
578 ack, oid_to_hex(result_oid));
579 if (ack == ACK)
580 return 0;
581 multi_ack = 1;
582 continue;
584 flushes--;
586 /* it is no error to fetch into a completely empty repo */
587 return count ? retval : 0;
590 static struct commit_list *complete;
592 static int mark_complete(const struct object_id *oid)
594 struct commit *commit = deref_without_lazy_fetch(oid, 1);
596 if (commit && !(commit->object.flags & COMPLETE)) {
597 commit->object.flags |= COMPLETE;
598 commit_list_insert(commit, &complete);
600 return 0;
603 static int mark_complete_oid(const char *refname, const struct object_id *oid,
604 int flag, void *cb_data)
606 return mark_complete(oid);
609 static void mark_recent_complete_commits(struct fetch_pack_args *args,
610 timestamp_t cutoff)
612 while (complete && cutoff <= complete->item->date) {
613 print_verbose(args, _("Marking %s as complete"),
614 oid_to_hex(&complete->item->object.oid));
615 pop_most_recent_commit(&complete, COMPLETE);
619 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
621 for (; refs; refs = refs->next)
622 oidset_insert(oids, &refs->old_oid);
625 static int is_unmatched_ref(const struct ref *ref)
627 struct object_id oid;
628 const char *p;
629 return ref->match_status == REF_NOT_MATCHED &&
630 !parse_oid_hex(ref->name, &oid, &p) &&
631 *p == '\0' &&
632 oideq(&oid, &ref->old_oid);
635 static void filter_refs(struct fetch_pack_args *args,
636 struct ref **refs,
637 struct ref **sought, int nr_sought)
639 struct ref *newlist = NULL;
640 struct ref **newtail = &newlist;
641 struct ref *unmatched = NULL;
642 struct ref *ref, *next;
643 struct oidset tip_oids = OIDSET_INIT;
644 int i;
645 int strict = !(allow_unadvertised_object_request &
646 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
648 i = 0;
649 for (ref = *refs; ref; ref = next) {
650 int keep = 0;
651 next = ref->next;
653 if (starts_with(ref->name, "refs/") &&
654 check_refname_format(ref->name, 0)) {
656 * trash or a peeled value; do not even add it to
657 * unmatched list
659 free_one_ref(ref);
660 continue;
661 } else {
662 while (i < nr_sought) {
663 int cmp = strcmp(ref->name, sought[i]->name);
664 if (cmp < 0)
665 break; /* definitely do not have it */
666 else if (cmp == 0) {
667 keep = 1; /* definitely have it */
668 sought[i]->match_status = REF_MATCHED;
670 i++;
673 if (!keep && args->fetch_all &&
674 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
675 keep = 1;
678 if (keep) {
679 *newtail = ref;
680 ref->next = NULL;
681 newtail = &ref->next;
682 } else {
683 ref->next = unmatched;
684 unmatched = ref;
688 if (strict) {
689 for (i = 0; i < nr_sought; i++) {
690 ref = sought[i];
691 if (!is_unmatched_ref(ref))
692 continue;
694 add_refs_to_oidset(&tip_oids, unmatched);
695 add_refs_to_oidset(&tip_oids, newlist);
696 break;
700 /* Append unmatched requests to the list */
701 for (i = 0; i < nr_sought; i++) {
702 ref = sought[i];
703 if (!is_unmatched_ref(ref))
704 continue;
706 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
707 ref->match_status = REF_MATCHED;
708 *newtail = copy_ref(ref);
709 newtail = &(*newtail)->next;
710 } else {
711 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
715 oidset_clear(&tip_oids);
716 free_refs(unmatched);
718 *refs = newlist;
721 static void mark_alternate_complete(struct fetch_negotiator *unused,
722 struct object *obj)
724 mark_complete(&obj->oid);
727 struct loose_object_iter {
728 struct oidset *loose_object_set;
729 struct ref *refs;
733 * Mark recent commits available locally and reachable from a local ref as
734 * COMPLETE.
736 * The cutoff time for recency is determined by this heuristic: it is the
737 * earliest commit time of the objects in refs that are commits and that we know
738 * the commit time of.
740 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
741 struct fetch_pack_args *args,
742 struct ref **refs)
744 struct ref *ref;
745 int old_save_commit_buffer = save_commit_buffer;
746 timestamp_t cutoff = 0;
748 if (args->refetch)
749 return;
751 save_commit_buffer = 0;
753 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
754 for (ref = *refs; ref; ref = ref->next) {
755 struct commit *commit;
757 commit = lookup_commit_in_graph(the_repository, &ref->old_oid);
758 if (!commit) {
759 struct object *o;
761 if (!has_object_file_with_flags(&ref->old_oid,
762 OBJECT_INFO_QUICK |
763 OBJECT_INFO_SKIP_FETCH_OBJECT))
764 continue;
765 o = parse_object(the_repository, &ref->old_oid);
766 if (!o || o->type != OBJ_COMMIT)
767 continue;
769 commit = (struct commit *)o;
773 * We already have it -- which may mean that we were
774 * in sync with the other side at some time after
775 * that (it is OK if we guess wrong here).
777 if (!cutoff || cutoff < commit->date)
778 cutoff = commit->date;
780 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
783 * This block marks all local refs as COMPLETE, and then recursively marks all
784 * parents of those refs as COMPLETE.
786 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
787 if (!args->deepen) {
788 for_each_rawref(mark_complete_oid, NULL);
789 for_each_cached_alternate(NULL, mark_alternate_complete);
790 commit_list_sort_by_date(&complete);
791 if (cutoff)
792 mark_recent_complete_commits(args, cutoff);
794 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
797 * Mark all complete remote refs as common refs.
798 * Don't mark them common yet; the server has to be told so first.
800 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
801 for (ref = *refs; ref; ref = ref->next) {
802 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
804 if (!c || !(c->object.flags & COMPLETE))
805 continue;
807 negotiator->known_common(negotiator, c);
809 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
811 save_commit_buffer = old_save_commit_buffer;
815 * Returns 1 if every object pointed to by the given remote refs is available
816 * locally and reachable from a local ref, and 0 otherwise.
818 static int everything_local(struct fetch_pack_args *args,
819 struct ref **refs)
821 struct ref *ref;
822 int retval;
824 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
825 const struct object_id *remote = &ref->old_oid;
826 struct object *o;
828 o = lookup_object(the_repository, remote);
829 if (!o || !(o->flags & COMPLETE)) {
830 retval = 0;
831 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
832 ref->name);
833 continue;
835 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
836 ref->name);
839 return retval;
842 static int sideband_demux(int in, int out, void *data)
844 int *xd = data;
845 int ret;
847 ret = recv_sideband("fetch-pack", xd[0], out);
848 close(out);
849 return ret;
852 static void create_promisor_file(const char *keep_name,
853 struct ref **sought, int nr_sought)
855 struct strbuf promisor_name = STRBUF_INIT;
856 int suffix_stripped;
858 strbuf_addstr(&promisor_name, keep_name);
859 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
860 if (!suffix_stripped)
861 BUG("name of pack lockfile should end with .keep (was '%s')",
862 keep_name);
863 strbuf_addstr(&promisor_name, ".promisor");
865 write_promisor_file(promisor_name.buf, sought, nr_sought);
867 strbuf_release(&promisor_name);
870 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
872 int len = the_hash_algo->hexsz + 1; /* hash + NL */
874 do {
875 char hex_hash[GIT_MAX_HEXSZ + 1];
876 int read_len = read_in_full(fd, hex_hash, len);
877 struct object_id oid;
878 const char *end;
880 if (!read_len)
881 return;
882 if (read_len != len)
883 die("invalid length read %d", read_len);
884 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
885 die("invalid hash");
886 oidset_insert(gitmodules_oids, &oid);
887 } while (1);
890 static void add_index_pack_keep_option(struct strvec *args)
892 char hostname[HOST_NAME_MAX + 1];
894 if (xgethostname(hostname, sizeof(hostname)))
895 xsnprintf(hostname, sizeof(hostname), "localhost");
896 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
897 (uintmax_t)getpid(), hostname);
901 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
902 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
903 * stored there. (It must be freed by the caller.)
905 static int get_pack(struct fetch_pack_args *args,
906 int xd[2], struct string_list *pack_lockfiles,
907 struct strvec *index_pack_args,
908 struct ref **sought, int nr_sought,
909 struct oidset *gitmodules_oids)
911 struct async demux;
912 int do_keep = args->keep_pack;
913 const char *cmd_name;
914 struct pack_header header;
915 int pass_header = 0;
916 struct child_process cmd = CHILD_PROCESS_INIT;
917 int fsck_objects = 0;
918 int ret;
920 memset(&demux, 0, sizeof(demux));
921 if (use_sideband) {
922 /* xd[] is talking with upload-pack; subprocess reads from
923 * xd[0], spits out band#2 to stderr, and feeds us band#1
924 * through demux->out.
926 demux.proc = sideband_demux;
927 demux.data = xd;
928 demux.out = -1;
929 demux.isolate_sigpipe = 1;
930 if (start_async(&demux))
931 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
933 else
934 demux.out = xd[0];
936 if (!args->keep_pack && unpack_limit && !index_pack_args) {
938 if (read_pack_header(demux.out, &header))
939 die(_("protocol error: bad pack header"));
940 pass_header = 1;
941 if (ntohl(header.hdr_entries) < unpack_limit)
942 do_keep = 0;
943 else
944 do_keep = 1;
947 if (alternate_shallow_file) {
948 strvec_push(&cmd.args, "--shallow-file");
949 strvec_push(&cmd.args, alternate_shallow_file);
952 if (fetch_fsck_objects >= 0
953 ? fetch_fsck_objects
954 : transfer_fsck_objects >= 0
955 ? transfer_fsck_objects
956 : 0)
957 fsck_objects = 1;
959 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
960 if (pack_lockfiles || fsck_objects)
961 cmd.out = -1;
962 cmd_name = "index-pack";
963 strvec_push(&cmd.args, cmd_name);
964 strvec_push(&cmd.args, "--stdin");
965 if (!args->quiet && !args->no_progress)
966 strvec_push(&cmd.args, "-v");
967 if (args->use_thin_pack)
968 strvec_push(&cmd.args, "--fix-thin");
969 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
970 add_index_pack_keep_option(&cmd.args);
971 if (!index_pack_args && args->check_self_contained_and_connected)
972 strvec_push(&cmd.args, "--check-self-contained-and-connected");
973 else
975 * We cannot perform any connectivity checks because
976 * not all packs have been downloaded; let the caller
977 * have this responsibility.
979 args->check_self_contained_and_connected = 0;
981 if (args->from_promisor)
983 * create_promisor_file() may be called afterwards but
984 * we still need index-pack to know that this is a
985 * promisor pack. For example, if transfer.fsckobjects
986 * is true, index-pack needs to know that .gitmodules
987 * is a promisor object (so that it won't complain if
988 * it is missing).
990 strvec_push(&cmd.args, "--promisor");
992 else {
993 cmd_name = "unpack-objects";
994 strvec_push(&cmd.args, cmd_name);
995 if (args->quiet || args->no_progress)
996 strvec_push(&cmd.args, "-q");
997 args->check_self_contained_and_connected = 0;
1000 if (pass_header)
1001 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
1002 ntohl(header.hdr_version),
1003 ntohl(header.hdr_entries));
1004 if (fsck_objects) {
1005 if (args->from_promisor || index_pack_args)
1007 * We cannot use --strict in index-pack because it
1008 * checks both broken objects and links, but we only
1009 * want to check for broken objects.
1011 strvec_push(&cmd.args, "--fsck-objects");
1012 else
1013 strvec_pushf(&cmd.args, "--strict%s",
1014 fsck_msg_types.buf);
1017 if (index_pack_args) {
1018 int i;
1020 for (i = 0; i < cmd.args.nr; i++)
1021 strvec_push(index_pack_args, cmd.args.v[i]);
1024 sigchain_push(SIGPIPE, SIG_IGN);
1026 cmd.in = demux.out;
1027 cmd.git_cmd = 1;
1028 if (start_command(&cmd))
1029 die(_("fetch-pack: unable to fork off %s"), cmd_name);
1030 if (do_keep && (pack_lockfiles || fsck_objects)) {
1031 int is_well_formed;
1032 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1034 if (!is_well_formed)
1035 die(_("fetch-pack: invalid index-pack output"));
1036 if (pack_lockfile)
1037 string_list_append_nodup(pack_lockfiles, pack_lockfile);
1038 parse_gitmodules_oids(cmd.out, gitmodules_oids);
1039 close(cmd.out);
1042 if (!use_sideband)
1043 /* Closed by start_command() */
1044 xd[0] = -1;
1046 ret = finish_command(&cmd);
1047 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1048 args->self_contained_and_connected =
1049 args->check_self_contained_and_connected &&
1050 ret == 0;
1051 else
1052 die(_("%s failed"), cmd_name);
1053 if (use_sideband && finish_async(&demux))
1054 die(_("error in sideband demultiplexer"));
1056 sigchain_pop(SIGPIPE);
1059 * Now that index-pack has succeeded, write the promisor file using the
1060 * obtained .keep filename if necessary
1062 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
1063 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
1065 return 0;
1068 static int ref_compare_name(const struct ref *a, const struct ref *b)
1070 return strcmp(a->name, b->name);
1073 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1075 static int cmp_ref_by_name(const void *a_, const void *b_)
1077 const struct ref *a = *((const struct ref **)a_);
1078 const struct ref *b = *((const struct ref **)b_);
1079 return strcmp(a->name, b->name);
1082 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1083 int fd[2],
1084 const struct ref *orig_ref,
1085 struct ref **sought, int nr_sought,
1086 struct shallow_info *si,
1087 struct string_list *pack_lockfiles)
1089 struct repository *r = the_repository;
1090 struct ref *ref = copy_ref_list(orig_ref);
1091 struct object_id oid;
1092 const char *agent_feature;
1093 int agent_len;
1094 struct fetch_negotiator negotiator_alloc;
1095 struct fetch_negotiator *negotiator;
1097 negotiator = &negotiator_alloc;
1098 if (args->refetch) {
1099 fetch_negotiator_init_noop(negotiator);
1100 } else {
1101 fetch_negotiator_init(r, negotiator);
1104 sort_ref_list(&ref, ref_compare_name);
1105 QSORT(sought, nr_sought, cmp_ref_by_name);
1107 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1108 agent_supported = 1;
1109 if (agent_len)
1110 print_verbose(args, _("Server version is %.*s"),
1111 agent_len, agent_feature);
1114 if (!server_supports("session-id"))
1115 advertise_sid = 0;
1117 if (server_supports("shallow"))
1118 print_verbose(args, _("Server supports %s"), "shallow");
1119 else if (args->depth > 0 || is_repository_shallow(r))
1120 die(_("Server does not support shallow clients"));
1121 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1122 args->deepen = 1;
1123 if (server_supports("multi_ack_detailed")) {
1124 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1125 multi_ack = 2;
1126 if (server_supports("no-done")) {
1127 print_verbose(args, _("Server supports %s"), "no-done");
1128 if (args->stateless_rpc)
1129 no_done = 1;
1132 else if (server_supports("multi_ack")) {
1133 print_verbose(args, _("Server supports %s"), "multi_ack");
1134 multi_ack = 1;
1136 if (server_supports("side-band-64k")) {
1137 print_verbose(args, _("Server supports %s"), "side-band-64k");
1138 use_sideband = 2;
1140 else if (server_supports("side-band")) {
1141 print_verbose(args, _("Server supports %s"), "side-band");
1142 use_sideband = 1;
1144 if (server_supports("allow-tip-sha1-in-want")) {
1145 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1146 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1148 if (server_supports("allow-reachable-sha1-in-want")) {
1149 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1150 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1152 if (server_supports("thin-pack"))
1153 print_verbose(args, _("Server supports %s"), "thin-pack");
1154 else
1155 args->use_thin_pack = 0;
1156 if (server_supports("no-progress"))
1157 print_verbose(args, _("Server supports %s"), "no-progress");
1158 else
1159 args->no_progress = 0;
1160 if (server_supports("include-tag"))
1161 print_verbose(args, _("Server supports %s"), "include-tag");
1162 else
1163 args->include_tag = 0;
1164 if (server_supports("ofs-delta"))
1165 print_verbose(args, _("Server supports %s"), "ofs-delta");
1166 else
1167 prefer_ofs_delta = 0;
1169 if (server_supports("filter")) {
1170 server_supports_filtering = 1;
1171 print_verbose(args, _("Server supports %s"), "filter");
1172 } else if (args->filter_options.choice) {
1173 warning("filtering not recognized by server, ignoring");
1176 if (server_supports("deepen-since")) {
1177 print_verbose(args, _("Server supports %s"), "deepen-since");
1178 deepen_since_ok = 1;
1179 } else if (args->deepen_since)
1180 die(_("Server does not support --shallow-since"));
1181 if (server_supports("deepen-not")) {
1182 print_verbose(args, _("Server supports %s"), "deepen-not");
1183 deepen_not_ok = 1;
1184 } else if (args->deepen_not)
1185 die(_("Server does not support --shallow-exclude"));
1186 if (server_supports("deepen-relative"))
1187 print_verbose(args, _("Server supports %s"), "deepen-relative");
1188 else if (args->deepen_relative)
1189 die(_("Server does not support --deepen"));
1190 if (!server_supports_hash(the_hash_algo->name, NULL))
1191 die(_("Server does not support this repository's object format"));
1193 mark_complete_and_common_ref(negotiator, args, &ref);
1194 filter_refs(args, &ref, sought, nr_sought);
1195 if (!args->refetch && everything_local(args, &ref)) {
1196 packet_flush(fd[1]);
1197 goto all_done;
1199 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1200 if (!args->keep_pack)
1201 /* When cloning, it is not unusual to have
1202 * no common commit.
1204 warning(_("no common commits"));
1206 if (args->stateless_rpc)
1207 packet_flush(fd[1]);
1208 if (args->deepen)
1209 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1210 NULL);
1211 else if (si->nr_ours || si->nr_theirs) {
1212 if (args->reject_shallow_remote)
1213 die(_("source repository is shallow, reject to clone."));
1214 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1215 } else
1216 alternate_shallow_file = NULL;
1217 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1218 &fsck_options.gitmodules_found))
1219 die(_("git fetch-pack: fetch failed."));
1220 if (fsck_finish(&fsck_options))
1221 die("fsck failed");
1223 all_done:
1224 if (negotiator)
1225 negotiator->release(negotiator);
1226 return ref;
1229 static void add_shallow_requests(struct strbuf *req_buf,
1230 const struct fetch_pack_args *args)
1232 if (is_repository_shallow(the_repository))
1233 write_shallow_commits(req_buf, 1, NULL);
1234 if (args->depth > 0)
1235 packet_buf_write(req_buf, "deepen %d", args->depth);
1236 if (args->deepen_since) {
1237 timestamp_t max_age = approxidate(args->deepen_since);
1238 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1240 if (args->deepen_not) {
1241 int i;
1242 for (i = 0; i < args->deepen_not->nr; i++) {
1243 struct string_list_item *s = args->deepen_not->items + i;
1244 packet_buf_write(req_buf, "deepen-not %s", s->string);
1247 if (args->deepen_relative)
1248 packet_buf_write(req_buf, "deepen-relative\n");
1251 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1253 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1255 for ( ; wants ; wants = wants->next) {
1256 const struct object_id *remote = &wants->old_oid;
1257 struct object *o;
1260 * If that object is complete (i.e. it is an ancestor of a
1261 * local ref), we tell them we have it but do not have to
1262 * tell them about its ancestors, which they already know
1263 * about.
1265 * We use lookup_object here because we are only
1266 * interested in the case we *know* the object is
1267 * reachable and we have already scanned it.
1269 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1270 (o->flags & COMPLETE)) {
1271 continue;
1274 if (!use_ref_in_want || wants->exact_oid)
1275 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1276 else
1277 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1281 static void add_common(struct strbuf *req_buf, struct oidset *common)
1283 struct oidset_iter iter;
1284 const struct object_id *oid;
1285 oidset_iter_init(common, &iter);
1287 while ((oid = oidset_iter_next(&iter))) {
1288 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1292 static int add_haves(struct fetch_negotiator *negotiator,
1293 struct strbuf *req_buf,
1294 int *haves_to_send)
1296 int haves_added = 0;
1297 const struct object_id *oid;
1299 while ((oid = negotiator->next(negotiator))) {
1300 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1301 if (++haves_added >= *haves_to_send)
1302 break;
1305 /* Increase haves to send on next round */
1306 *haves_to_send = next_flush(1, *haves_to_send);
1308 return haves_added;
1311 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1312 const struct string_list *server_options)
1314 const char *hash_name;
1316 if (server_supports_v2("fetch", 1))
1317 packet_buf_write(req_buf, "command=fetch");
1318 if (server_supports_v2("agent", 0))
1319 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1320 if (advertise_sid && server_supports_v2("session-id", 0))
1321 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1322 if (server_options && server_options->nr &&
1323 server_supports_v2("server-option", 1)) {
1324 int i;
1325 for (i = 0; i < server_options->nr; i++)
1326 packet_buf_write(req_buf, "server-option=%s",
1327 server_options->items[i].string);
1330 if (server_feature_v2("object-format", &hash_name)) {
1331 int hash_algo = hash_algo_by_name(hash_name);
1332 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1333 die(_("mismatched algorithms: client %s; server %s"),
1334 the_hash_algo->name, hash_name);
1335 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1336 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1337 die(_("the server does not support algorithm '%s'"),
1338 the_hash_algo->name);
1340 packet_buf_delim(req_buf);
1343 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1344 struct fetch_pack_args *args,
1345 const struct ref *wants, struct oidset *common,
1346 int *haves_to_send, int *in_vain,
1347 int sideband_all, int seen_ack)
1349 int haves_added;
1350 int done_sent = 0;
1351 struct strbuf req_buf = STRBUF_INIT;
1353 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1355 if (args->use_thin_pack)
1356 packet_buf_write(&req_buf, "thin-pack");
1357 if (args->no_progress)
1358 packet_buf_write(&req_buf, "no-progress");
1359 if (args->include_tag)
1360 packet_buf_write(&req_buf, "include-tag");
1361 if (prefer_ofs_delta)
1362 packet_buf_write(&req_buf, "ofs-delta");
1363 if (sideband_all)
1364 packet_buf_write(&req_buf, "sideband-all");
1366 /* Add shallow-info and deepen request */
1367 if (server_supports_feature("fetch", "shallow", 0))
1368 add_shallow_requests(&req_buf, args);
1369 else if (is_repository_shallow(the_repository) || args->deepen)
1370 die(_("Server does not support shallow requests"));
1372 /* Add filter */
1373 send_filter(args, &req_buf,
1374 server_supports_feature("fetch", "filter", 0));
1376 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1377 int i;
1378 struct strbuf to_send = STRBUF_INIT;
1380 for (i = 0; i < uri_protocols.nr; i++) {
1381 const char *s = uri_protocols.items[i].string;
1383 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1384 if (to_send.len)
1385 strbuf_addch(&to_send, ',');
1386 strbuf_addstr(&to_send, s);
1389 if (to_send.len) {
1390 packet_buf_write(&req_buf, "packfile-uris %s",
1391 to_send.buf);
1392 strbuf_release(&to_send);
1396 /* add wants */
1397 add_wants(wants, &req_buf);
1399 /* Add all of the common commits we've found in previous rounds */
1400 add_common(&req_buf, common);
1402 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1403 *in_vain += haves_added;
1404 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1405 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
1406 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1407 /* Send Done */
1408 packet_buf_write(&req_buf, "done\n");
1409 done_sent = 1;
1412 /* Send request */
1413 packet_buf_flush(&req_buf);
1414 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1415 die_errno(_("unable to write request to remote"));
1417 strbuf_release(&req_buf);
1418 return done_sent;
1422 * Processes a section header in a server's response and checks if it matches
1423 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1424 * not consumed); if 0, the line will be consumed and the function will die if
1425 * the section header doesn't match what was expected.
1427 static int process_section_header(struct packet_reader *reader,
1428 const char *section, int peek)
1430 int ret = 0;
1432 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1433 !strcmp(reader->line, section))
1434 ret = 1;
1436 if (!peek) {
1437 if (!ret) {
1438 if (reader->line)
1439 die(_("expected '%s', received '%s'"),
1440 section, reader->line);
1441 else
1442 die(_("expected '%s'"), section);
1444 packet_reader_read(reader);
1447 return ret;
1450 static int process_ack(struct fetch_negotiator *negotiator,
1451 struct packet_reader *reader,
1452 struct object_id *common_oid,
1453 int *received_ready)
1455 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1456 const char *arg;
1458 if (!strcmp(reader->line, "NAK"))
1459 continue;
1461 if (skip_prefix(reader->line, "ACK ", &arg)) {
1462 if (!get_oid_hex(arg, common_oid)) {
1463 struct commit *commit;
1464 commit = lookup_commit(the_repository, common_oid);
1465 if (negotiator)
1466 negotiator->ack(negotiator, commit);
1468 return 1;
1471 if (!strcmp(reader->line, "ready")) {
1472 *received_ready = 1;
1473 continue;
1476 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1479 if (reader->status != PACKET_READ_FLUSH &&
1480 reader->status != PACKET_READ_DELIM)
1481 die(_("error processing acks: %d"), reader->status);
1484 * If an "acknowledgments" section is sent, a packfile is sent if and
1485 * only if "ready" was sent in this section. The other sections
1486 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1487 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1488 * otherwise.
1490 if (*received_ready && reader->status != PACKET_READ_DELIM)
1492 * TRANSLATORS: The parameter will be 'ready', a protocol
1493 * keyword.
1495 die(_("expected packfile to be sent after '%s'"), "ready");
1496 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1498 * TRANSLATORS: The parameter will be 'ready', a protocol
1499 * keyword.
1501 die(_("expected no other sections to be sent after no '%s'"), "ready");
1503 return 0;
1506 static void receive_shallow_info(struct fetch_pack_args *args,
1507 struct packet_reader *reader,
1508 struct oid_array *shallows,
1509 struct shallow_info *si)
1511 int unshallow_received = 0;
1513 process_section_header(reader, "shallow-info", 0);
1514 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1515 const char *arg;
1516 struct object_id oid;
1518 if (skip_prefix(reader->line, "shallow ", &arg)) {
1519 if (get_oid_hex(arg, &oid))
1520 die(_("invalid shallow line: %s"), reader->line);
1521 oid_array_append(shallows, &oid);
1522 continue;
1524 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1525 if (get_oid_hex(arg, &oid))
1526 die(_("invalid unshallow line: %s"), reader->line);
1527 if (!lookup_object(the_repository, &oid))
1528 die(_("object not found: %s"), reader->line);
1529 /* make sure that it is parsed as shallow */
1530 if (!parse_object(the_repository, &oid))
1531 die(_("error in object: %s"), reader->line);
1532 if (unregister_shallow(&oid))
1533 die(_("no shallow found: %s"), reader->line);
1534 unshallow_received = 1;
1535 continue;
1537 die(_("expected shallow/unshallow, got %s"), reader->line);
1540 if (reader->status != PACKET_READ_FLUSH &&
1541 reader->status != PACKET_READ_DELIM)
1542 die(_("error processing shallow info: %d"), reader->status);
1544 if (args->deepen || unshallow_received) {
1546 * Treat these as shallow lines caused by our depth settings.
1547 * In v0, these lines cannot cause refs to be rejected; do the
1548 * same.
1550 int i;
1552 for (i = 0; i < shallows->nr; i++)
1553 register_shallow(the_repository, &shallows->oid[i]);
1554 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1555 NULL);
1556 args->deepen = 1;
1557 } else if (shallows->nr) {
1559 * Treat these as shallow lines caused by the remote being
1560 * shallow. In v0, remote refs that reach these objects are
1561 * rejected (unless --update-shallow is set); do the same.
1563 prepare_shallow_info(si, shallows);
1564 if (si->nr_ours || si->nr_theirs) {
1565 if (args->reject_shallow_remote)
1566 die(_("source repository is shallow, reject to clone."));
1567 alternate_shallow_file =
1568 setup_temporary_shallow(si->shallow);
1569 } else
1570 alternate_shallow_file = NULL;
1571 } else {
1572 alternate_shallow_file = NULL;
1576 static int cmp_name_ref(const void *name, const void *ref)
1578 return strcmp(name, (*(struct ref **)ref)->name);
1581 static void receive_wanted_refs(struct packet_reader *reader,
1582 struct ref **sought, int nr_sought)
1584 process_section_header(reader, "wanted-refs", 0);
1585 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1586 struct object_id oid;
1587 const char *end;
1588 struct ref **found;
1590 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1591 die(_("expected wanted-ref, got '%s'"), reader->line);
1593 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1594 cmp_name_ref);
1595 if (!found)
1596 die(_("unexpected wanted-ref: '%s'"), reader->line);
1597 oidcpy(&(*found)->old_oid, &oid);
1600 if (reader->status != PACKET_READ_DELIM)
1601 die(_("error processing wanted refs: %d"), reader->status);
1604 static void receive_packfile_uris(struct packet_reader *reader,
1605 struct string_list *uris)
1607 process_section_header(reader, "packfile-uris", 0);
1608 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1609 if (reader->pktlen < the_hash_algo->hexsz ||
1610 reader->line[the_hash_algo->hexsz] != ' ')
1611 die("expected '<hash> <uri>', got: %s\n", reader->line);
1613 string_list_append(uris, reader->line);
1615 if (reader->status != PACKET_READ_DELIM)
1616 die("expected DELIM");
1619 enum fetch_state {
1620 FETCH_CHECK_LOCAL = 0,
1621 FETCH_SEND_REQUEST,
1622 FETCH_PROCESS_ACKS,
1623 FETCH_GET_PACK,
1624 FETCH_DONE,
1627 static void do_check_stateless_delimiter(int stateless_rpc,
1628 struct packet_reader *reader)
1630 check_stateless_delimiter(stateless_rpc, reader,
1631 _("git fetch-pack: expected response end packet"));
1634 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1635 int fd[2],
1636 const struct ref *orig_ref,
1637 struct ref **sought, int nr_sought,
1638 struct oid_array *shallows,
1639 struct shallow_info *si,
1640 struct string_list *pack_lockfiles)
1642 struct repository *r = the_repository;
1643 struct ref *ref = copy_ref_list(orig_ref);
1644 enum fetch_state state = FETCH_CHECK_LOCAL;
1645 struct oidset common = OIDSET_INIT;
1646 struct packet_reader reader;
1647 int in_vain = 0, negotiation_started = 0;
1648 int negotiation_round = 0;
1649 int haves_to_send = INITIAL_FLUSH;
1650 struct fetch_negotiator negotiator_alloc;
1651 struct fetch_negotiator *negotiator;
1652 int seen_ack = 0;
1653 struct object_id common_oid;
1654 int received_ready = 0;
1655 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1656 int i;
1657 struct strvec index_pack_args = STRVEC_INIT;
1659 negotiator = &negotiator_alloc;
1660 if (args->refetch)
1661 fetch_negotiator_init_noop(negotiator);
1662 else
1663 fetch_negotiator_init(r, negotiator);
1665 packet_reader_init(&reader, fd[0], NULL, 0,
1666 PACKET_READ_CHOMP_NEWLINE |
1667 PACKET_READ_DIE_ON_ERR_PACKET);
1668 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1669 server_supports_feature("fetch", "sideband-all", 0)) {
1670 reader.use_sideband = 1;
1671 reader.me = "fetch-pack";
1674 while (state != FETCH_DONE) {
1675 switch (state) {
1676 case FETCH_CHECK_LOCAL:
1677 sort_ref_list(&ref, ref_compare_name);
1678 QSORT(sought, nr_sought, cmp_ref_by_name);
1680 /* v2 supports these by default */
1681 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1682 use_sideband = 2;
1683 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1684 args->deepen = 1;
1686 /* Filter 'ref' by 'sought' and those that aren't local */
1687 mark_complete_and_common_ref(negotiator, args, &ref);
1688 filter_refs(args, &ref, sought, nr_sought);
1689 if (!args->refetch && everything_local(args, &ref))
1690 state = FETCH_DONE;
1691 else
1692 state = FETCH_SEND_REQUEST;
1694 mark_tips(negotiator, args->negotiation_tips);
1695 for_each_cached_alternate(negotiator,
1696 insert_one_alternate_object);
1697 break;
1698 case FETCH_SEND_REQUEST:
1699 if (!negotiation_started) {
1700 negotiation_started = 1;
1701 trace2_region_enter("fetch-pack",
1702 "negotiation_v2",
1703 the_repository);
1705 negotiation_round++;
1706 trace2_region_enter_printf("negotiation_v2", "round",
1707 the_repository, "%d",
1708 negotiation_round);
1709 if (send_fetch_request(negotiator, fd[1], args, ref,
1710 &common,
1711 &haves_to_send, &in_vain,
1712 reader.use_sideband,
1713 seen_ack)) {
1714 trace2_region_leave_printf("negotiation_v2", "round",
1715 the_repository, "%d",
1716 negotiation_round);
1717 state = FETCH_GET_PACK;
1719 else
1720 state = FETCH_PROCESS_ACKS;
1721 break;
1722 case FETCH_PROCESS_ACKS:
1723 /* Process ACKs/NAKs */
1724 process_section_header(&reader, "acknowledgments", 0);
1725 while (process_ack(negotiator, &reader, &common_oid,
1726 &received_ready)) {
1727 in_vain = 0;
1728 seen_ack = 1;
1729 oidset_insert(&common, &common_oid);
1731 trace2_region_leave_printf("negotiation_v2", "round",
1732 the_repository, "%d",
1733 negotiation_round);
1734 if (received_ready) {
1736 * Don't check for response delimiter; get_pack() will
1737 * read the rest of this response.
1739 state = FETCH_GET_PACK;
1740 } else {
1741 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1742 state = FETCH_SEND_REQUEST;
1744 break;
1745 case FETCH_GET_PACK:
1746 trace2_region_leave("fetch-pack",
1747 "negotiation_v2",
1748 the_repository);
1749 trace2_data_intmax("negotiation_v2", the_repository,
1750 "total_rounds", negotiation_round);
1751 /* Check for shallow-info section */
1752 if (process_section_header(&reader, "shallow-info", 1))
1753 receive_shallow_info(args, &reader, shallows, si);
1755 if (process_section_header(&reader, "wanted-refs", 1))
1756 receive_wanted_refs(&reader, sought, nr_sought);
1758 /* get the pack(s) */
1759 if (git_env_bool("GIT_TRACE_REDACT", 1))
1760 reader.options |= PACKET_READ_REDACT_URI_PATH;
1761 if (process_section_header(&reader, "packfile-uris", 1))
1762 receive_packfile_uris(&reader, &packfile_uris);
1763 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1764 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1766 process_section_header(&reader, "packfile", 0);
1769 * this is the final request we'll make of the server;
1770 * do a half-duplex shutdown to indicate that they can
1771 * hang up as soon as the pack is sent.
1773 close(fd[1]);
1774 fd[1] = -1;
1776 if (get_pack(args, fd, pack_lockfiles,
1777 packfile_uris.nr ? &index_pack_args : NULL,
1778 sought, nr_sought, &fsck_options.gitmodules_found))
1779 die(_("git fetch-pack: fetch failed."));
1780 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1782 state = FETCH_DONE;
1783 break;
1784 case FETCH_DONE:
1785 continue;
1789 for (i = 0; i < packfile_uris.nr; i++) {
1790 int j;
1791 struct child_process cmd = CHILD_PROCESS_INIT;
1792 char packname[GIT_MAX_HEXSZ + 1];
1793 const char *uri = packfile_uris.items[i].string +
1794 the_hash_algo->hexsz + 1;
1796 strvec_push(&cmd.args, "http-fetch");
1797 strvec_pushf(&cmd.args, "--packfile=%.*s",
1798 (int) the_hash_algo->hexsz,
1799 packfile_uris.items[i].string);
1800 for (j = 0; j < index_pack_args.nr; j++)
1801 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1802 index_pack_args.v[j]);
1803 strvec_push(&cmd.args, uri);
1804 cmd.git_cmd = 1;
1805 cmd.no_stdin = 1;
1806 cmd.out = -1;
1807 if (start_command(&cmd))
1808 die("fetch-pack: unable to spawn http-fetch");
1810 if (read_in_full(cmd.out, packname, 5) < 0 ||
1811 memcmp(packname, "keep\t", 5))
1812 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1814 if (read_in_full(cmd.out, packname,
1815 the_hash_algo->hexsz + 1) < 0 ||
1816 packname[the_hash_algo->hexsz] != '\n')
1817 die("fetch-pack: expected hash then LF at end of http-fetch output");
1819 packname[the_hash_algo->hexsz] = '\0';
1821 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1823 close(cmd.out);
1825 if (finish_command(&cmd))
1826 die("fetch-pack: unable to finish http-fetch");
1828 if (memcmp(packfile_uris.items[i].string, packname,
1829 the_hash_algo->hexsz))
1830 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1831 uri, (int) the_hash_algo->hexsz,
1832 packfile_uris.items[i].string);
1834 string_list_append_nodup(pack_lockfiles,
1835 xstrfmt("%s/pack/pack-%s.keep",
1836 get_object_directory(),
1837 packname));
1839 string_list_clear(&packfile_uris, 0);
1840 strvec_clear(&index_pack_args);
1842 if (fsck_finish(&fsck_options))
1843 die("fsck failed");
1845 if (negotiator)
1846 negotiator->release(negotiator);
1848 oidset_clear(&common);
1849 return ref;
1852 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1854 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1855 const char *path;
1857 if (git_config_pathname(&path, var, value))
1858 return 1;
1859 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1860 fsck_msg_types.len ? ',' : '=', path);
1861 free((char *)path);
1862 return 0;
1865 if (skip_prefix(var, "fetch.fsck.", &var)) {
1866 if (is_valid_msg_type(var, value))
1867 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1868 fsck_msg_types.len ? ',' : '=', var, value);
1869 else
1870 warning("Skipping unknown msg id '%s'", var);
1871 return 0;
1874 return git_default_config(var, value, cb);
1877 static void fetch_pack_config(void)
1879 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1880 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1881 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1882 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1883 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1884 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1885 if (!uri_protocols.nr) {
1886 char *str;
1888 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1889 string_list_split(&uri_protocols, str, ',', -1);
1890 free(str);
1894 git_config(fetch_pack_config_cb, NULL);
1897 static void fetch_pack_setup(void)
1899 static int did_setup;
1900 if (did_setup)
1901 return;
1902 fetch_pack_config();
1903 if (0 <= transfer_unpack_limit)
1904 unpack_limit = transfer_unpack_limit;
1905 else if (0 <= fetch_unpack_limit)
1906 unpack_limit = fetch_unpack_limit;
1907 did_setup = 1;
1910 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1912 struct string_list names = STRING_LIST_INIT_NODUP;
1913 int src, dst;
1915 for (src = dst = 0; src < nr; src++) {
1916 struct string_list_item *item;
1917 item = string_list_insert(&names, ref[src]->name);
1918 if (item->util)
1919 continue; /* already have it */
1920 item->util = ref[src];
1921 if (src != dst)
1922 ref[dst] = ref[src];
1923 dst++;
1925 for (src = dst; src < nr; src++)
1926 ref[src] = NULL;
1927 string_list_clear(&names, 0);
1928 return dst;
1931 static void update_shallow(struct fetch_pack_args *args,
1932 struct ref **sought, int nr_sought,
1933 struct shallow_info *si)
1935 struct oid_array ref = OID_ARRAY_INIT;
1936 int *status;
1937 int i;
1939 if (args->deepen && alternate_shallow_file) {
1940 if (*alternate_shallow_file == '\0') { /* --unshallow */
1941 unlink_or_warn(git_path_shallow(the_repository));
1942 rollback_shallow_file(the_repository, &shallow_lock);
1943 } else
1944 commit_shallow_file(the_repository, &shallow_lock);
1945 alternate_shallow_file = NULL;
1946 return;
1949 if (!si->shallow || !si->shallow->nr)
1950 return;
1952 if (args->cloning) {
1954 * remote is shallow, but this is a clone, there are
1955 * no objects in repo to worry about. Accept any
1956 * shallow points that exist in the pack (iow in repo
1957 * after get_pack() and reprepare_packed_git())
1959 struct oid_array extra = OID_ARRAY_INIT;
1960 struct object_id *oid = si->shallow->oid;
1961 for (i = 0; i < si->shallow->nr; i++)
1962 if (has_object_file(&oid[i]))
1963 oid_array_append(&extra, &oid[i]);
1964 if (extra.nr) {
1965 setup_alternate_shallow(&shallow_lock,
1966 &alternate_shallow_file,
1967 &extra);
1968 commit_shallow_file(the_repository, &shallow_lock);
1969 alternate_shallow_file = NULL;
1971 oid_array_clear(&extra);
1972 return;
1975 if (!si->nr_ours && !si->nr_theirs)
1976 return;
1978 remove_nonexistent_theirs_shallow(si);
1979 if (!si->nr_ours && !si->nr_theirs)
1980 return;
1981 for (i = 0; i < nr_sought; i++)
1982 oid_array_append(&ref, &sought[i]->old_oid);
1983 si->ref = &ref;
1985 if (args->update_shallow) {
1987 * remote is also shallow, .git/shallow may be updated
1988 * so all refs can be accepted. Make sure we only add
1989 * shallow roots that are actually reachable from new
1990 * refs.
1992 struct oid_array extra = OID_ARRAY_INIT;
1993 struct object_id *oid = si->shallow->oid;
1994 assign_shallow_commits_to_refs(si, NULL, NULL);
1995 if (!si->nr_ours && !si->nr_theirs) {
1996 oid_array_clear(&ref);
1997 return;
1999 for (i = 0; i < si->nr_ours; i++)
2000 oid_array_append(&extra, &oid[si->ours[i]]);
2001 for (i = 0; i < si->nr_theirs; i++)
2002 oid_array_append(&extra, &oid[si->theirs[i]]);
2003 setup_alternate_shallow(&shallow_lock,
2004 &alternate_shallow_file,
2005 &extra);
2006 commit_shallow_file(the_repository, &shallow_lock);
2007 oid_array_clear(&extra);
2008 oid_array_clear(&ref);
2009 alternate_shallow_file = NULL;
2010 return;
2014 * remote is also shallow, check what ref is safe to update
2015 * without updating .git/shallow
2017 CALLOC_ARRAY(status, nr_sought);
2018 assign_shallow_commits_to_refs(si, NULL, status);
2019 if (si->nr_ours || si->nr_theirs) {
2020 for (i = 0; i < nr_sought; i++)
2021 if (status[i])
2022 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
2024 free(status);
2025 oid_array_clear(&ref);
2028 static const struct object_id *iterate_ref_map(void *cb_data)
2030 struct ref **rm = cb_data;
2031 struct ref *ref = *rm;
2033 if (!ref)
2034 return NULL;
2035 *rm = ref->next;
2036 return &ref->old_oid;
2039 struct ref *fetch_pack(struct fetch_pack_args *args,
2040 int fd[],
2041 const struct ref *ref,
2042 struct ref **sought, int nr_sought,
2043 struct oid_array *shallow,
2044 struct string_list *pack_lockfiles,
2045 enum protocol_version version)
2047 struct ref *ref_cpy;
2048 struct shallow_info si;
2049 struct oid_array shallows_scratch = OID_ARRAY_INIT;
2051 fetch_pack_setup();
2052 if (nr_sought)
2053 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
2055 if (version != protocol_v2 && !ref) {
2056 packet_flush(fd[1]);
2057 die(_("no matching remote head"));
2059 if (version == protocol_v2) {
2060 if (shallow->nr)
2061 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2062 memset(&si, 0, sizeof(si));
2063 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
2064 &shallows_scratch, &si,
2065 pack_lockfiles);
2066 } else {
2067 prepare_shallow_info(&si, shallow);
2068 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
2069 &si, pack_lockfiles);
2071 reprepare_packed_git(the_repository);
2073 if (!args->cloning && args->deepen) {
2074 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2075 struct ref *iterator = ref_cpy;
2076 opt.shallow_file = alternate_shallow_file;
2077 if (args->deepen)
2078 opt.is_deepening_fetch = 1;
2079 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2080 error(_("remote did not send all necessary objects"));
2081 free_refs(ref_cpy);
2082 ref_cpy = NULL;
2083 rollback_shallow_file(the_repository, &shallow_lock);
2084 goto cleanup;
2086 args->connectivity_checked = 1;
2089 update_shallow(args, sought, nr_sought, &si);
2090 cleanup:
2091 clear_shallow_info(&si);
2092 oid_array_clear(&shallows_scratch);
2093 return ref_cpy;
2096 static int add_to_object_array(const struct object_id *oid, void *data)
2098 struct object_array *a = data;
2100 add_object_array(lookup_object(the_repository, oid), "", a);
2101 return 0;
2104 static void clear_common_flag(struct oidset *s)
2106 struct oidset_iter iter;
2107 const struct object_id *oid;
2108 oidset_iter_init(s, &iter);
2110 while ((oid = oidset_iter_next(&iter))) {
2111 struct object *obj = lookup_object(the_repository, oid);
2112 obj->flags &= ~COMMON;
2116 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2117 const struct string_list *server_options,
2118 int stateless_rpc,
2119 int fd[],
2120 struct oidset *acked_commits)
2122 struct fetch_negotiator negotiator;
2123 struct packet_reader reader;
2124 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2125 struct strbuf req_buf = STRBUF_INIT;
2126 int haves_to_send = INITIAL_FLUSH;
2127 int in_vain = 0;
2128 int seen_ack = 0;
2129 int last_iteration = 0;
2130 int negotiation_round = 0;
2131 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2133 fetch_negotiator_init(the_repository, &negotiator);
2134 mark_tips(&negotiator, negotiation_tips);
2136 packet_reader_init(&reader, fd[0], NULL, 0,
2137 PACKET_READ_CHOMP_NEWLINE |
2138 PACKET_READ_DIE_ON_ERR_PACKET);
2140 oid_array_for_each((struct oid_array *) negotiation_tips,
2141 add_to_object_array,
2142 &nt_object_array);
2144 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2145 while (!last_iteration) {
2146 int haves_added;
2147 struct object_id common_oid;
2148 int received_ready = 0;
2150 negotiation_round++;
2152 trace2_region_enter_printf("negotiate_using_fetch", "round",
2153 the_repository, "%d",
2154 negotiation_round);
2155 strbuf_reset(&req_buf);
2156 write_fetch_command_and_capabilities(&req_buf, server_options);
2158 packet_buf_write(&req_buf, "wait-for-done");
2160 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2161 in_vain += haves_added;
2162 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2163 last_iteration = 1;
2165 trace2_data_intmax("negotiate_using_fetch", the_repository,
2166 "haves_added", haves_added);
2167 trace2_data_intmax("negotiate_using_fetch", the_repository,
2168 "in_vain", in_vain);
2170 /* Send request */
2171 packet_buf_flush(&req_buf);
2172 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2173 die_errno(_("unable to write request to remote"));
2175 /* Process ACKs/NAKs */
2176 process_section_header(&reader, "acknowledgments", 0);
2177 while (process_ack(&negotiator, &reader, &common_oid,
2178 &received_ready)) {
2179 struct commit *commit = lookup_commit(the_repository,
2180 &common_oid);
2181 if (commit) {
2182 timestamp_t generation;
2184 parse_commit_or_die(commit);
2185 commit->object.flags |= COMMON;
2186 generation = commit_graph_generation(commit);
2187 if (generation < min_generation)
2188 min_generation = generation;
2190 in_vain = 0;
2191 seen_ack = 1;
2192 oidset_insert(acked_commits, &common_oid);
2194 if (received_ready)
2195 die(_("unexpected 'ready' from remote"));
2196 else
2197 do_check_stateless_delimiter(stateless_rpc, &reader);
2198 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2199 REACH_SCRATCH, 0,
2200 min_generation))
2201 last_iteration = 1;
2202 trace2_region_leave_printf("negotiation", "round",
2203 the_repository, "%d",
2204 negotiation_round);
2206 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2207 trace2_data_intmax("negotiate_using_fetch", the_repository,
2208 "total_rounds", negotiation_round);
2209 clear_common_flag(acked_commits);
2210 strbuf_release(&req_buf);
2213 int report_unmatched_refs(struct ref **sought, int nr_sought)
2215 int i, ret = 0;
2217 for (i = 0; i < nr_sought; i++) {
2218 if (!sought[i])
2219 continue;
2220 switch (sought[i]->match_status) {
2221 case REF_MATCHED:
2222 continue;
2223 case REF_NOT_MATCHED:
2224 error(_("no such remote ref %s"), sought[i]->name);
2225 break;
2226 case REF_UNADVERTISED_NOT_ALLOWED:
2227 error(_("Server does not allow request for unadvertised object %s"),
2228 sought[i]->name);
2229 break;
2231 ret = 1;
2233 return ret;