Merge branch 'ps/reftable-write-options'
[git.git] / fetch-pack.c
blob7d2aef21add8470792e844457314f9a2d8447ddb
1 #include "git-compat-util.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "date.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "refs.h"
10 #include "pkt-line.h"
11 #include "commit.h"
12 #include "tag.h"
13 #include "pack.h"
14 #include "sideband.h"
15 #include "fetch-pack.h"
16 #include "remote.h"
17 #include "run-command.h"
18 #include "connect.h"
19 #include "trace2.h"
20 #include "version.h"
21 #include "oid-array.h"
22 #include "oidset.h"
23 #include "packfile.h"
24 #include "object-store-ll.h"
25 #include "path.h"
26 #include "connected.h"
27 #include "fetch-negotiator.h"
28 #include "fsck.h"
29 #include "shallow.h"
30 #include "commit-reach.h"
31 #include "commit-graph.h"
32 #include "sigchain.h"
33 #include "mergesort.h"
35 static int transfer_unpack_limit = -1;
36 static int fetch_unpack_limit = -1;
37 static int unpack_limit = 100;
38 static int prefer_ofs_delta = 1;
39 static int no_done;
40 static int deepen_since_ok;
41 static int deepen_not_ok;
42 static int fetch_fsck_objects = -1;
43 static int transfer_fsck_objects = -1;
44 static int agent_supported;
45 static int server_supports_filtering;
46 static int advertise_sid;
47 static struct shallow_lock shallow_lock;
48 static const char *alternate_shallow_file;
49 static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
50 static struct strbuf fsck_msg_types = STRBUF_INIT;
51 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
53 /* Remember to update object flag allocation in object.h */
54 #define COMPLETE (1U << 0)
55 #define ALTERNATE (1U << 1)
56 #define COMMON (1U << 6)
57 #define REACH_SCRATCH (1U << 7)
60 * After sending this many "have"s if we do not get any new ACK , we
61 * give up traversing our history.
63 #define MAX_IN_VAIN 256
65 static int multi_ack, use_sideband;
66 /* Allow specifying sha1 if it is a ref tip. */
67 #define ALLOW_TIP_SHA1 01
68 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
69 #define ALLOW_REACHABLE_SHA1 02
70 static unsigned int allow_unadvertised_object_request;
72 __attribute__((format (printf, 2, 3)))
73 static inline void print_verbose(const struct fetch_pack_args *args,
74 const char *fmt, ...)
76 va_list params;
78 if (!args->verbose)
79 return;
81 va_start(params, fmt);
82 vfprintf(stderr, fmt, params);
83 va_end(params);
84 fputc('\n', stderr);
87 struct alternate_object_cache {
88 struct object **items;
89 size_t nr, alloc;
92 static void cache_one_alternate(const struct object_id *oid,
93 void *vcache)
95 struct alternate_object_cache *cache = vcache;
96 struct object *obj = parse_object(the_repository, oid);
98 if (!obj || (obj->flags & ALTERNATE))
99 return;
101 obj->flags |= ALTERNATE;
102 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
103 cache->items[cache->nr++] = obj;
106 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
107 void (*cb)(struct fetch_negotiator *,
108 struct object *))
110 static int initialized;
111 static struct alternate_object_cache cache;
112 size_t i;
114 if (!initialized) {
115 for_each_alternate_ref(cache_one_alternate, &cache);
116 initialized = 1;
119 for (i = 0; i < cache.nr; i++)
120 cb(negotiator, cache.items[i]);
123 static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid,
124 int mark_tags_complete,
125 enum object_type *type,
126 unsigned int oi_flags)
128 struct object_info info = { .typep = type };
129 struct commit *commit;
131 commit = lookup_commit_in_graph(the_repository, oid);
132 if (commit)
133 return commit;
135 while (1) {
136 if (oid_object_info_extended(the_repository, oid, &info,
137 oi_flags))
138 return NULL;
139 if (*type == OBJ_TAG) {
140 struct tag *tag = (struct tag *)
141 parse_object(the_repository, oid);
143 if (!tag->tagged)
144 return NULL;
145 if (mark_tags_complete)
146 tag->object.flags |= COMPLETE;
147 oid = &tag->tagged->oid;
148 } else {
149 break;
153 if (*type == OBJ_COMMIT) {
154 struct commit *commit = lookup_commit(the_repository, oid);
155 if (!commit || repo_parse_commit(the_repository, commit))
156 return NULL;
157 return commit;
160 return NULL;
164 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
165 int mark_tags_complete)
167 enum object_type type;
168 unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK;
169 return deref_without_lazy_fetch_extended(oid, mark_tags_complete,
170 &type, flags);
173 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
174 const struct object_id *oid)
176 struct commit *c = deref_without_lazy_fetch(oid, 0);
178 if (c)
179 negotiator->add_tip(negotiator, c);
180 return 0;
183 static int rev_list_insert_ref_oid(const char *refname UNUSED,
184 const struct object_id *oid,
185 int flag UNUSED,
186 void *cb_data)
188 return rev_list_insert_ref(cb_data, oid);
191 enum ack_type {
192 NAK = 0,
193 ACK,
194 ACK_continue,
195 ACK_common,
196 ACK_ready
199 static void consume_shallow_list(struct fetch_pack_args *args,
200 struct packet_reader *reader)
202 if (args->stateless_rpc && args->deepen) {
203 /* If we sent a depth we will get back "duplicate"
204 * shallow and unshallow commands every time there
205 * is a block of have lines exchanged.
207 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
208 if (starts_with(reader->line, "shallow "))
209 continue;
210 if (starts_with(reader->line, "unshallow "))
211 continue;
212 die(_("git fetch-pack: expected shallow list"));
214 if (reader->status != PACKET_READ_FLUSH)
215 die(_("git fetch-pack: expected a flush packet after shallow list"));
219 static enum ack_type get_ack(struct packet_reader *reader,
220 struct object_id *result_oid)
222 int len;
223 const char *arg;
225 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
226 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
227 len = reader->pktlen;
229 if (!strcmp(reader->line, "NAK"))
230 return NAK;
231 if (skip_prefix(reader->line, "ACK ", &arg)) {
232 const char *p;
233 if (!parse_oid_hex(arg, result_oid, &p)) {
234 len -= p - reader->line;
235 if (len < 1)
236 return ACK;
237 if (strstr(p, "continue"))
238 return ACK_continue;
239 if (strstr(p, "common"))
240 return ACK_common;
241 if (strstr(p, "ready"))
242 return ACK_ready;
243 return ACK;
246 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
249 static void send_request(struct fetch_pack_args *args,
250 int fd, struct strbuf *buf)
252 if (args->stateless_rpc) {
253 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
254 packet_flush(fd);
255 } else {
256 if (write_in_full(fd, buf->buf, buf->len) < 0)
257 die_errno(_("unable to write to remote"));
261 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
262 struct object *obj)
264 rev_list_insert_ref(negotiator, &obj->oid);
267 #define INITIAL_FLUSH 16
268 #define PIPESAFE_FLUSH 32
269 #define LARGE_FLUSH 16384
271 static int next_flush(int stateless_rpc, int count)
273 if (stateless_rpc) {
274 if (count < LARGE_FLUSH)
275 count <<= 1;
276 else
277 count = count * 11 / 10;
278 } else {
279 if (count < PIPESAFE_FLUSH)
280 count <<= 1;
281 else
282 count += PIPESAFE_FLUSH;
284 return count;
287 static void mark_tips(struct fetch_negotiator *negotiator,
288 const struct oid_array *negotiation_tips)
290 int i;
292 if (!negotiation_tips) {
293 refs_for_each_rawref(get_main_ref_store(the_repository),
294 rev_list_insert_ref_oid, negotiator);
295 return;
298 for (i = 0; i < negotiation_tips->nr; i++)
299 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
300 return;
303 static void send_filter(struct fetch_pack_args *args,
304 struct strbuf *req_buf,
305 int server_supports_filter)
307 if (args->filter_options.choice) {
308 const char *spec =
309 expand_list_objects_filter_spec(&args->filter_options);
310 if (server_supports_filter) {
311 print_verbose(args, _("Server supports filter"));
312 packet_buf_write(req_buf, "filter %s", spec);
313 trace2_data_string("fetch", the_repository,
314 "filter/effective", spec);
315 } else {
316 warning("filtering not recognized by server, ignoring");
317 trace2_data_string("fetch", the_repository,
318 "filter/unsupported", spec);
320 } else {
321 trace2_data_string("fetch", the_repository,
322 "filter/none", "");
326 static int find_common(struct fetch_negotiator *negotiator,
327 struct fetch_pack_args *args,
328 int fd[2], struct object_id *result_oid,
329 struct ref *refs)
331 int fetching;
332 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
333 int negotiation_round = 0, haves = 0;
334 const struct object_id *oid;
335 unsigned in_vain = 0;
336 int got_continue = 0;
337 int got_ready = 0;
338 struct strbuf req_buf = STRBUF_INIT;
339 size_t state_len = 0;
340 struct packet_reader reader;
342 if (args->stateless_rpc && multi_ack == 1)
343 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
345 packet_reader_init(&reader, fd[0], NULL, 0,
346 PACKET_READ_CHOMP_NEWLINE |
347 PACKET_READ_DIE_ON_ERR_PACKET);
349 mark_tips(negotiator, args->negotiation_tips);
350 for_each_cached_alternate(negotiator, insert_one_alternate_object);
352 fetching = 0;
353 for ( ; refs ; refs = refs->next) {
354 struct object_id *remote = &refs->old_oid;
355 const char *remote_hex;
356 struct object *o;
358 if (!args->refetch) {
360 * If that object is complete (i.e. it is an ancestor of a
361 * local ref), we tell them we have it but do not have to
362 * tell them about its ancestors, which they already know
363 * about.
365 * We use lookup_object here because we are only
366 * interested in the case we *know* the object is
367 * reachable and we have already scanned it.
369 if (((o = lookup_object(the_repository, remote)) != NULL) &&
370 (o->flags & COMPLETE)) {
371 continue;
375 remote_hex = oid_to_hex(remote);
376 if (!fetching) {
377 struct strbuf c = STRBUF_INIT;
378 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
379 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
380 if (no_done) strbuf_addstr(&c, " no-done");
381 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
382 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
383 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
384 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
385 if (args->no_progress) strbuf_addstr(&c, " no-progress");
386 if (args->include_tag) strbuf_addstr(&c, " include-tag");
387 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
388 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
389 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
390 if (agent_supported) strbuf_addf(&c, " agent=%s",
391 git_user_agent_sanitized());
392 if (advertise_sid)
393 strbuf_addf(&c, " session-id=%s", trace2_session_id());
394 if (args->filter_options.choice)
395 strbuf_addstr(&c, " filter");
396 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
397 strbuf_release(&c);
398 } else
399 packet_buf_write(&req_buf, "want %s\n", remote_hex);
400 fetching++;
403 if (!fetching) {
404 strbuf_release(&req_buf);
405 packet_flush(fd[1]);
406 return 1;
409 if (is_repository_shallow(the_repository))
410 write_shallow_commits(&req_buf, 1, NULL);
411 if (args->depth > 0)
412 packet_buf_write(&req_buf, "deepen %d", args->depth);
413 if (args->deepen_since) {
414 timestamp_t max_age = approxidate(args->deepen_since);
415 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
417 if (args->deepen_not) {
418 int i;
419 for (i = 0; i < args->deepen_not->nr; i++) {
420 struct string_list_item *s = args->deepen_not->items + i;
421 packet_buf_write(&req_buf, "deepen-not %s", s->string);
424 send_filter(args, &req_buf, server_supports_filtering);
425 packet_buf_flush(&req_buf);
426 state_len = req_buf.len;
428 if (args->deepen) {
429 const char *arg;
430 struct object_id oid;
432 send_request(args, fd[1], &req_buf);
433 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
434 if (skip_prefix(reader.line, "shallow ", &arg)) {
435 if (get_oid_hex(arg, &oid))
436 die(_("invalid shallow line: %s"), reader.line);
437 register_shallow(the_repository, &oid);
438 continue;
440 if (skip_prefix(reader.line, "unshallow ", &arg)) {
441 if (get_oid_hex(arg, &oid))
442 die(_("invalid unshallow line: %s"), reader.line);
443 if (!lookup_object(the_repository, &oid))
444 die(_("object not found: %s"), reader.line);
445 /* make sure that it is parsed as shallow */
446 if (!parse_object(the_repository, &oid))
447 die(_("error in object: %s"), reader.line);
448 if (unregister_shallow(&oid))
449 die(_("no shallow found: %s"), reader.line);
450 continue;
452 die(_("expected shallow/unshallow, got %s"), reader.line);
454 } else if (!args->stateless_rpc)
455 send_request(args, fd[1], &req_buf);
457 if (!args->stateless_rpc) {
458 /* If we aren't using the stateless-rpc interface
459 * we don't need to retain the headers.
461 strbuf_setlen(&req_buf, 0);
462 state_len = 0;
465 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
466 flushes = 0;
467 retval = -1;
468 while ((oid = negotiator->next(negotiator))) {
469 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
470 print_verbose(args, "have %s", oid_to_hex(oid));
471 in_vain++;
472 haves++;
473 if (flush_at <= ++count) {
474 int ack;
476 negotiation_round++;
477 trace2_region_enter_printf("negotiation_v0_v1", "round",
478 the_repository, "%d",
479 negotiation_round);
480 trace2_data_intmax("negotiation_v0_v1", the_repository,
481 "haves_added", haves);
482 trace2_data_intmax("negotiation_v0_v1", the_repository,
483 "in_vain", in_vain);
484 haves = 0;
485 packet_buf_flush(&req_buf);
486 send_request(args, fd[1], &req_buf);
487 strbuf_setlen(&req_buf, state_len);
488 flushes++;
489 flush_at = next_flush(args->stateless_rpc, count);
492 * We keep one window "ahead" of the other side, and
493 * will wait for an ACK only on the next one
495 if (!args->stateless_rpc && count == INITIAL_FLUSH)
496 continue;
498 consume_shallow_list(args, &reader);
499 do {
500 ack = get_ack(&reader, result_oid);
501 if (ack)
502 print_verbose(args, _("got %s %d %s"), "ack",
503 ack, oid_to_hex(result_oid));
504 switch (ack) {
505 case ACK:
506 trace2_region_leave_printf("negotiation_v0_v1", "round",
507 the_repository, "%d",
508 negotiation_round);
509 flushes = 0;
510 multi_ack = 0;
511 retval = 0;
512 goto done;
513 case ACK_common:
514 case ACK_ready:
515 case ACK_continue: {
516 struct commit *commit =
517 lookup_commit(the_repository,
518 result_oid);
519 int was_common;
521 if (!commit)
522 die(_("invalid commit %s"), oid_to_hex(result_oid));
523 was_common = negotiator->ack(negotiator, commit);
524 if (args->stateless_rpc
525 && ack == ACK_common
526 && !was_common) {
527 /* We need to replay the have for this object
528 * on the next RPC request so the peer knows
529 * it is in common with us.
531 const char *hex = oid_to_hex(result_oid);
532 packet_buf_write(&req_buf, "have %s\n", hex);
533 state_len = req_buf.len;
534 haves++;
536 * Reset in_vain because an ack
537 * for this commit has not been
538 * seen.
540 in_vain = 0;
541 } else if (!args->stateless_rpc
542 || ack != ACK_common)
543 in_vain = 0;
544 retval = 0;
545 got_continue = 1;
546 if (ack == ACK_ready)
547 got_ready = 1;
548 break;
551 } while (ack);
552 flushes--;
553 trace2_region_leave_printf("negotiation_v0_v1", "round",
554 the_repository, "%d",
555 negotiation_round);
556 if (got_continue && MAX_IN_VAIN < in_vain) {
557 print_verbose(args, _("giving up"));
558 break; /* give up */
560 if (got_ready)
561 break;
564 done:
565 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
566 trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds",
567 negotiation_round);
568 if (!got_ready || !no_done) {
569 packet_buf_write(&req_buf, "done\n");
570 send_request(args, fd[1], &req_buf);
572 print_verbose(args, _("done"));
573 if (retval != 0) {
574 multi_ack = 0;
575 flushes++;
577 strbuf_release(&req_buf);
579 if (!got_ready || !no_done)
580 consume_shallow_list(args, &reader);
581 while (flushes || multi_ack) {
582 int ack = get_ack(&reader, result_oid);
583 if (ack) {
584 print_verbose(args, _("got %s (%d) %s"), "ack",
585 ack, oid_to_hex(result_oid));
586 if (ack == ACK)
587 return 0;
588 multi_ack = 1;
589 continue;
591 flushes--;
593 /* it is no error to fetch into a completely empty repo */
594 return count ? retval : 0;
597 static struct commit_list *complete;
599 static int mark_complete(const struct object_id *oid)
601 struct commit *commit = deref_without_lazy_fetch(oid, 1);
603 if (commit && !(commit->object.flags & COMPLETE)) {
604 commit->object.flags |= COMPLETE;
605 commit_list_insert(commit, &complete);
607 return 0;
610 static int mark_complete_oid(const char *refname UNUSED,
611 const struct object_id *oid,
612 int flag UNUSED,
613 void *cb_data UNUSED)
615 return mark_complete(oid);
618 static void mark_recent_complete_commits(struct fetch_pack_args *args,
619 timestamp_t cutoff)
621 while (complete && cutoff <= complete->item->date) {
622 print_verbose(args, _("Marking %s as complete"),
623 oid_to_hex(&complete->item->object.oid));
624 pop_most_recent_commit(&complete, COMPLETE);
628 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
630 for (; refs; refs = refs->next)
631 oidset_insert(oids, &refs->old_oid);
634 static int is_unmatched_ref(const struct ref *ref)
636 struct object_id oid;
637 const char *p;
638 return ref->match_status == REF_NOT_MATCHED &&
639 !parse_oid_hex(ref->name, &oid, &p) &&
640 *p == '\0' &&
641 oideq(&oid, &ref->old_oid);
644 static void filter_refs(struct fetch_pack_args *args,
645 struct ref **refs,
646 struct ref **sought, int nr_sought)
648 struct ref *newlist = NULL;
649 struct ref **newtail = &newlist;
650 struct ref *unmatched = NULL;
651 struct ref *ref, *next;
652 struct oidset tip_oids = OIDSET_INIT;
653 int i;
654 int strict = !(allow_unadvertised_object_request &
655 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
657 i = 0;
658 for (ref = *refs; ref; ref = next) {
659 int keep = 0;
660 next = ref->next;
662 if (starts_with(ref->name, "refs/") &&
663 check_refname_format(ref->name, 0)) {
665 * trash or a peeled value; do not even add it to
666 * unmatched list
668 free_one_ref(ref);
669 continue;
670 } else {
671 while (i < nr_sought) {
672 int cmp = strcmp(ref->name, sought[i]->name);
673 if (cmp < 0)
674 break; /* definitely do not have it */
675 else if (cmp == 0) {
676 keep = 1; /* definitely have it */
677 sought[i]->match_status = REF_MATCHED;
679 i++;
682 if (!keep && args->fetch_all &&
683 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
684 keep = 1;
687 if (keep) {
688 *newtail = ref;
689 ref->next = NULL;
690 newtail = &ref->next;
691 } else {
692 ref->next = unmatched;
693 unmatched = ref;
697 if (strict) {
698 for (i = 0; i < nr_sought; i++) {
699 ref = sought[i];
700 if (!is_unmatched_ref(ref))
701 continue;
703 add_refs_to_oidset(&tip_oids, unmatched);
704 add_refs_to_oidset(&tip_oids, newlist);
705 break;
709 /* Append unmatched requests to the list */
710 for (i = 0; i < nr_sought; i++) {
711 ref = sought[i];
712 if (!is_unmatched_ref(ref))
713 continue;
715 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
716 ref->match_status = REF_MATCHED;
717 *newtail = copy_ref(ref);
718 newtail = &(*newtail)->next;
719 } else {
720 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
724 oidset_clear(&tip_oids);
725 free_refs(unmatched);
727 *refs = newlist;
730 static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED,
731 struct object *obj)
733 mark_complete(&obj->oid);
737 * Mark recent commits available locally and reachable from a local ref as
738 * COMPLETE.
740 * The cutoff time for recency is determined by this heuristic: it is the
741 * earliest commit time of the objects in refs that are commits and that we know
742 * the commit time of.
744 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
745 struct fetch_pack_args *args,
746 struct ref **refs)
748 struct ref *ref;
749 int old_save_commit_buffer = save_commit_buffer;
750 timestamp_t cutoff = 0;
752 if (args->refetch)
753 return;
755 save_commit_buffer = 0;
757 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
758 for (ref = *refs; ref; ref = ref->next) {
759 struct commit *commit;
761 commit = lookup_commit_in_graph(the_repository, &ref->old_oid);
762 if (!commit) {
763 struct object *o;
765 if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
766 OBJECT_INFO_QUICK |
767 OBJECT_INFO_SKIP_FETCH_OBJECT))
768 continue;
769 o = parse_object(the_repository, &ref->old_oid);
770 if (!o || o->type != OBJ_COMMIT)
771 continue;
773 commit = (struct commit *)o;
777 * We already have it -- which may mean that we were
778 * in sync with the other side at some time after
779 * that (it is OK if we guess wrong here).
781 if (!cutoff || cutoff < commit->date)
782 cutoff = commit->date;
784 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
787 * This block marks all local refs as COMPLETE, and then recursively marks all
788 * parents of those refs as COMPLETE.
790 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
791 if (!args->deepen) {
792 refs_for_each_rawref(get_main_ref_store(the_repository),
793 mark_complete_oid, NULL);
794 for_each_cached_alternate(NULL, mark_alternate_complete);
795 commit_list_sort_by_date(&complete);
796 if (cutoff)
797 mark_recent_complete_commits(args, cutoff);
799 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
802 * Mark all complete remote refs as common refs.
803 * Don't mark them common yet; the server has to be told so first.
805 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
806 for (ref = *refs; ref; ref = ref->next) {
807 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
809 if (!c || !(c->object.flags & COMPLETE))
810 continue;
812 negotiator->known_common(negotiator, c);
814 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
816 save_commit_buffer = old_save_commit_buffer;
820 * Returns 1 if every object pointed to by the given remote refs is available
821 * locally and reachable from a local ref, and 0 otherwise.
823 static int everything_local(struct fetch_pack_args *args,
824 struct ref **refs)
826 struct ref *ref;
827 int retval;
829 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
830 const struct object_id *remote = &ref->old_oid;
831 struct object *o;
833 o = lookup_object(the_repository, remote);
834 if (!o || !(o->flags & COMPLETE)) {
835 retval = 0;
836 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
837 ref->name);
838 continue;
840 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
841 ref->name);
844 return retval;
847 static int sideband_demux(int in UNUSED, int out, void *data)
849 int *xd = data;
850 int ret;
852 ret = recv_sideband("fetch-pack", xd[0], out);
853 close(out);
854 return ret;
857 static void create_promisor_file(const char *keep_name,
858 struct ref **sought, int nr_sought)
860 struct strbuf promisor_name = STRBUF_INIT;
861 int suffix_stripped;
863 strbuf_addstr(&promisor_name, keep_name);
864 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
865 if (!suffix_stripped)
866 BUG("name of pack lockfile should end with .keep (was '%s')",
867 keep_name);
868 strbuf_addstr(&promisor_name, ".promisor");
870 write_promisor_file(promisor_name.buf, sought, nr_sought);
872 strbuf_release(&promisor_name);
875 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
877 int len = the_hash_algo->hexsz + 1; /* hash + NL */
879 do {
880 char hex_hash[GIT_MAX_HEXSZ + 1];
881 int read_len = read_in_full(fd, hex_hash, len);
882 struct object_id oid;
883 const char *end;
885 if (!read_len)
886 return;
887 if (read_len != len)
888 die("invalid length read %d", read_len);
889 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
890 die("invalid hash");
891 oidset_insert(gitmodules_oids, &oid);
892 } while (1);
895 static void add_index_pack_keep_option(struct strvec *args)
897 char hostname[HOST_NAME_MAX + 1];
899 if (xgethostname(hostname, sizeof(hostname)))
900 xsnprintf(hostname, sizeof(hostname), "localhost");
901 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
902 (uintmax_t)getpid(), hostname);
906 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
907 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
908 * stored there. (It must be freed by the caller.)
910 static int get_pack(struct fetch_pack_args *args,
911 int xd[2], struct string_list *pack_lockfiles,
912 struct strvec *index_pack_args,
913 struct ref **sought, int nr_sought,
914 struct oidset *gitmodules_oids)
916 struct async demux;
917 int do_keep = args->keep_pack;
918 const char *cmd_name;
919 struct pack_header header;
920 int pass_header = 0;
921 struct child_process cmd = CHILD_PROCESS_INIT;
922 int fsck_objects = 0;
923 int ret;
925 memset(&demux, 0, sizeof(demux));
926 if (use_sideband) {
927 /* xd[] is talking with upload-pack; subprocess reads from
928 * xd[0], spits out band#2 to stderr, and feeds us band#1
929 * through demux->out.
931 demux.proc = sideband_demux;
932 demux.data = xd;
933 demux.out = -1;
934 demux.isolate_sigpipe = 1;
935 if (start_async(&demux))
936 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
938 else
939 demux.out = xd[0];
941 if (!args->keep_pack && unpack_limit && !index_pack_args) {
943 if (read_pack_header(demux.out, &header))
944 die(_("protocol error: bad pack header"));
945 pass_header = 1;
946 if (ntohl(header.hdr_entries) < unpack_limit)
947 do_keep = 0;
948 else
949 do_keep = 1;
952 if (alternate_shallow_file) {
953 strvec_push(&cmd.args, "--shallow-file");
954 strvec_push(&cmd.args, alternate_shallow_file);
957 if (fetch_fsck_objects >= 0
958 ? fetch_fsck_objects
959 : transfer_fsck_objects >= 0
960 ? transfer_fsck_objects
961 : 0)
962 fsck_objects = 1;
964 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
965 if (pack_lockfiles || fsck_objects)
966 cmd.out = -1;
967 cmd_name = "index-pack";
968 strvec_push(&cmd.args, cmd_name);
969 strvec_push(&cmd.args, "--stdin");
970 if (!args->quiet && !args->no_progress)
971 strvec_push(&cmd.args, "-v");
972 if (args->use_thin_pack)
973 strvec_push(&cmd.args, "--fix-thin");
974 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
975 add_index_pack_keep_option(&cmd.args);
976 if (!index_pack_args && args->check_self_contained_and_connected)
977 strvec_push(&cmd.args, "--check-self-contained-and-connected");
978 else
980 * We cannot perform any connectivity checks because
981 * not all packs have been downloaded; let the caller
982 * have this responsibility.
984 args->check_self_contained_and_connected = 0;
986 if (args->from_promisor)
988 * create_promisor_file() may be called afterwards but
989 * we still need index-pack to know that this is a
990 * promisor pack. For example, if transfer.fsckobjects
991 * is true, index-pack needs to know that .gitmodules
992 * is a promisor object (so that it won't complain if
993 * it is missing).
995 strvec_push(&cmd.args, "--promisor");
997 else {
998 cmd_name = "unpack-objects";
999 strvec_push(&cmd.args, cmd_name);
1000 if (args->quiet || args->no_progress)
1001 strvec_push(&cmd.args, "-q");
1002 args->check_self_contained_and_connected = 0;
1005 if (pass_header)
1006 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
1007 ntohl(header.hdr_version),
1008 ntohl(header.hdr_entries));
1009 if (fsck_objects) {
1010 if (args->from_promisor || index_pack_args)
1012 * We cannot use --strict in index-pack because it
1013 * checks both broken objects and links, but we only
1014 * want to check for broken objects.
1016 strvec_push(&cmd.args, "--fsck-objects");
1017 else
1018 strvec_pushf(&cmd.args, "--strict%s",
1019 fsck_msg_types.buf);
1022 if (index_pack_args) {
1023 int i;
1025 for (i = 0; i < cmd.args.nr; i++)
1026 strvec_push(index_pack_args, cmd.args.v[i]);
1029 sigchain_push(SIGPIPE, SIG_IGN);
1031 cmd.in = demux.out;
1032 cmd.git_cmd = 1;
1033 if (start_command(&cmd))
1034 die(_("fetch-pack: unable to fork off %s"), cmd_name);
1035 if (do_keep && (pack_lockfiles || fsck_objects)) {
1036 int is_well_formed;
1037 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1039 if (!is_well_formed)
1040 die(_("fetch-pack: invalid index-pack output"));
1041 if (pack_lockfile)
1042 string_list_append_nodup(pack_lockfiles, pack_lockfile);
1043 parse_gitmodules_oids(cmd.out, gitmodules_oids);
1044 close(cmd.out);
1047 if (!use_sideband)
1048 /* Closed by start_command() */
1049 xd[0] = -1;
1051 ret = finish_command(&cmd);
1052 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1053 args->self_contained_and_connected =
1054 args->check_self_contained_and_connected &&
1055 ret == 0;
1056 else
1057 die(_("%s failed"), cmd_name);
1058 if (use_sideband && finish_async(&demux))
1059 die(_("error in sideband demultiplexer"));
1061 sigchain_pop(SIGPIPE);
1064 * Now that index-pack has succeeded, write the promisor file using the
1065 * obtained .keep filename if necessary
1067 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
1068 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
1070 return 0;
1073 static int ref_compare_name(const struct ref *a, const struct ref *b)
1075 return strcmp(a->name, b->name);
1078 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1080 static int cmp_ref_by_name(const void *a_, const void *b_)
1082 const struct ref *a = *((const struct ref **)a_);
1083 const struct ref *b = *((const struct ref **)b_);
1084 return strcmp(a->name, b->name);
1087 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1088 int fd[2],
1089 const struct ref *orig_ref,
1090 struct ref **sought, int nr_sought,
1091 struct shallow_info *si,
1092 struct string_list *pack_lockfiles)
1094 struct repository *r = the_repository;
1095 struct ref *ref = copy_ref_list(orig_ref);
1096 struct object_id oid;
1097 const char *agent_feature;
1098 size_t agent_len;
1099 struct fetch_negotiator negotiator_alloc;
1100 struct fetch_negotiator *negotiator;
1102 negotiator = &negotiator_alloc;
1103 if (args->refetch) {
1104 fetch_negotiator_init_noop(negotiator);
1105 } else {
1106 fetch_negotiator_init(r, negotiator);
1109 sort_ref_list(&ref, ref_compare_name);
1110 QSORT(sought, nr_sought, cmp_ref_by_name);
1112 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1113 agent_supported = 1;
1114 if (agent_len)
1115 print_verbose(args, _("Server version is %.*s"),
1116 (int)agent_len, agent_feature);
1119 if (!server_supports("session-id"))
1120 advertise_sid = 0;
1122 if (server_supports("shallow"))
1123 print_verbose(args, _("Server supports %s"), "shallow");
1124 else if (args->depth > 0 || is_repository_shallow(r))
1125 die(_("Server does not support shallow clients"));
1126 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1127 args->deepen = 1;
1128 if (server_supports("multi_ack_detailed")) {
1129 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1130 multi_ack = 2;
1131 if (server_supports("no-done")) {
1132 print_verbose(args, _("Server supports %s"), "no-done");
1133 if (args->stateless_rpc)
1134 no_done = 1;
1137 else if (server_supports("multi_ack")) {
1138 print_verbose(args, _("Server supports %s"), "multi_ack");
1139 multi_ack = 1;
1141 if (server_supports("side-band-64k")) {
1142 print_verbose(args, _("Server supports %s"), "side-band-64k");
1143 use_sideband = 2;
1145 else if (server_supports("side-band")) {
1146 print_verbose(args, _("Server supports %s"), "side-band");
1147 use_sideband = 1;
1149 if (server_supports("allow-tip-sha1-in-want")) {
1150 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1151 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1153 if (server_supports("allow-reachable-sha1-in-want")) {
1154 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1155 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1157 if (server_supports("thin-pack"))
1158 print_verbose(args, _("Server supports %s"), "thin-pack");
1159 else
1160 args->use_thin_pack = 0;
1161 if (server_supports("no-progress"))
1162 print_verbose(args, _("Server supports %s"), "no-progress");
1163 else
1164 args->no_progress = 0;
1165 if (server_supports("include-tag"))
1166 print_verbose(args, _("Server supports %s"), "include-tag");
1167 else
1168 args->include_tag = 0;
1169 if (server_supports("ofs-delta"))
1170 print_verbose(args, _("Server supports %s"), "ofs-delta");
1171 else
1172 prefer_ofs_delta = 0;
1174 if (server_supports("filter")) {
1175 server_supports_filtering = 1;
1176 print_verbose(args, _("Server supports %s"), "filter");
1177 } else if (args->filter_options.choice) {
1178 warning("filtering not recognized by server, ignoring");
1181 if (server_supports("deepen-since")) {
1182 print_verbose(args, _("Server supports %s"), "deepen-since");
1183 deepen_since_ok = 1;
1184 } else if (args->deepen_since)
1185 die(_("Server does not support --shallow-since"));
1186 if (server_supports("deepen-not")) {
1187 print_verbose(args, _("Server supports %s"), "deepen-not");
1188 deepen_not_ok = 1;
1189 } else if (args->deepen_not)
1190 die(_("Server does not support --shallow-exclude"));
1191 if (server_supports("deepen-relative"))
1192 print_verbose(args, _("Server supports %s"), "deepen-relative");
1193 else if (args->deepen_relative)
1194 die(_("Server does not support --deepen"));
1195 if (!server_supports_hash(the_hash_algo->name, NULL))
1196 die(_("Server does not support this repository's object format"));
1198 mark_complete_and_common_ref(negotiator, args, &ref);
1199 filter_refs(args, &ref, sought, nr_sought);
1200 if (!args->refetch && everything_local(args, &ref)) {
1201 packet_flush(fd[1]);
1202 goto all_done;
1204 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1205 if (!args->keep_pack)
1206 /* When cloning, it is not unusual to have
1207 * no common commit.
1209 warning(_("no common commits"));
1211 if (args->stateless_rpc)
1212 packet_flush(fd[1]);
1213 if (args->deepen)
1214 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1215 NULL);
1216 else if (si->nr_ours || si->nr_theirs) {
1217 if (args->reject_shallow_remote)
1218 die(_("source repository is shallow, reject to clone."));
1219 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1220 } else
1221 alternate_shallow_file = NULL;
1222 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1223 &fsck_options.gitmodules_found))
1224 die(_("git fetch-pack: fetch failed."));
1225 if (fsck_finish(&fsck_options))
1226 die("fsck failed");
1228 all_done:
1229 if (negotiator)
1230 negotiator->release(negotiator);
1231 return ref;
1234 static void add_shallow_requests(struct strbuf *req_buf,
1235 const struct fetch_pack_args *args)
1237 if (is_repository_shallow(the_repository))
1238 write_shallow_commits(req_buf, 1, NULL);
1239 if (args->depth > 0)
1240 packet_buf_write(req_buf, "deepen %d", args->depth);
1241 if (args->deepen_since) {
1242 timestamp_t max_age = approxidate(args->deepen_since);
1243 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1245 if (args->deepen_not) {
1246 int i;
1247 for (i = 0; i < args->deepen_not->nr; i++) {
1248 struct string_list_item *s = args->deepen_not->items + i;
1249 packet_buf_write(req_buf, "deepen-not %s", s->string);
1252 if (args->deepen_relative)
1253 packet_buf_write(req_buf, "deepen-relative\n");
1256 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1258 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1260 for ( ; wants ; wants = wants->next) {
1261 const struct object_id *remote = &wants->old_oid;
1262 struct object *o;
1265 * If that object is complete (i.e. it is an ancestor of a
1266 * local ref), we tell them we have it but do not have to
1267 * tell them about its ancestors, which they already know
1268 * about.
1270 * We use lookup_object here because we are only
1271 * interested in the case we *know* the object is
1272 * reachable and we have already scanned it.
1274 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1275 (o->flags & COMPLETE)) {
1276 continue;
1279 if (!use_ref_in_want || wants->exact_oid)
1280 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1281 else
1282 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1286 static void add_common(struct strbuf *req_buf, struct oidset *common)
1288 struct oidset_iter iter;
1289 const struct object_id *oid;
1290 oidset_iter_init(common, &iter);
1292 while ((oid = oidset_iter_next(&iter))) {
1293 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1297 static int add_haves(struct fetch_negotiator *negotiator,
1298 struct strbuf *req_buf,
1299 int *haves_to_send)
1301 int haves_added = 0;
1302 const struct object_id *oid;
1304 while ((oid = negotiator->next(negotiator))) {
1305 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1306 if (++haves_added >= *haves_to_send)
1307 break;
1310 /* Increase haves to send on next round */
1311 *haves_to_send = next_flush(1, *haves_to_send);
1313 return haves_added;
1316 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1317 const struct string_list *server_options)
1319 const char *hash_name;
1321 ensure_server_supports_v2("fetch");
1322 packet_buf_write(req_buf, "command=fetch");
1323 if (server_supports_v2("agent"))
1324 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1325 if (advertise_sid && server_supports_v2("session-id"))
1326 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1327 if (server_options && server_options->nr) {
1328 int i;
1329 ensure_server_supports_v2("server-option");
1330 for (i = 0; i < server_options->nr; i++)
1331 packet_buf_write(req_buf, "server-option=%s",
1332 server_options->items[i].string);
1335 if (server_feature_v2("object-format", &hash_name)) {
1336 int hash_algo = hash_algo_by_name(hash_name);
1337 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1338 die(_("mismatched algorithms: client %s; server %s"),
1339 the_hash_algo->name, hash_name);
1340 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1341 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1342 die(_("the server does not support algorithm '%s'"),
1343 the_hash_algo->name);
1345 packet_buf_delim(req_buf);
1348 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1349 struct fetch_pack_args *args,
1350 const struct ref *wants, struct oidset *common,
1351 int *haves_to_send, int *in_vain,
1352 int sideband_all, int seen_ack)
1354 int haves_added;
1355 int done_sent = 0;
1356 struct strbuf req_buf = STRBUF_INIT;
1358 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1360 if (args->use_thin_pack)
1361 packet_buf_write(&req_buf, "thin-pack");
1362 if (args->no_progress)
1363 packet_buf_write(&req_buf, "no-progress");
1364 if (args->include_tag)
1365 packet_buf_write(&req_buf, "include-tag");
1366 if (prefer_ofs_delta)
1367 packet_buf_write(&req_buf, "ofs-delta");
1368 if (sideband_all)
1369 packet_buf_write(&req_buf, "sideband-all");
1371 /* Add shallow-info and deepen request */
1372 if (server_supports_feature("fetch", "shallow", 0))
1373 add_shallow_requests(&req_buf, args);
1374 else if (is_repository_shallow(the_repository) || args->deepen)
1375 die(_("Server does not support shallow requests"));
1377 /* Add filter */
1378 send_filter(args, &req_buf,
1379 server_supports_feature("fetch", "filter", 0));
1381 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1382 int i;
1383 struct strbuf to_send = STRBUF_INIT;
1385 for (i = 0; i < uri_protocols.nr; i++) {
1386 const char *s = uri_protocols.items[i].string;
1388 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1389 if (to_send.len)
1390 strbuf_addch(&to_send, ',');
1391 strbuf_addstr(&to_send, s);
1394 if (to_send.len) {
1395 packet_buf_write(&req_buf, "packfile-uris %s",
1396 to_send.buf);
1397 strbuf_release(&to_send);
1401 /* add wants */
1402 add_wants(wants, &req_buf);
1404 /* Add all of the common commits we've found in previous rounds */
1405 add_common(&req_buf, common);
1407 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1408 *in_vain += haves_added;
1409 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1410 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
1411 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1412 /* Send Done */
1413 packet_buf_write(&req_buf, "done\n");
1414 done_sent = 1;
1417 /* Send request */
1418 packet_buf_flush(&req_buf);
1419 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1420 die_errno(_("unable to write request to remote"));
1422 strbuf_release(&req_buf);
1423 return done_sent;
1427 * Processes a section header in a server's response and checks if it matches
1428 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1429 * not consumed); if 0, the line will be consumed and the function will die if
1430 * the section header doesn't match what was expected.
1432 static int process_section_header(struct packet_reader *reader,
1433 const char *section, int peek)
1435 int ret = 0;
1437 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1438 !strcmp(reader->line, section))
1439 ret = 1;
1441 if (!peek) {
1442 if (!ret) {
1443 if (reader->line)
1444 die(_("expected '%s', received '%s'"),
1445 section, reader->line);
1446 else
1447 die(_("expected '%s'"), section);
1449 packet_reader_read(reader);
1452 return ret;
1455 static int process_ack(struct fetch_negotiator *negotiator,
1456 struct packet_reader *reader,
1457 struct object_id *common_oid,
1458 int *received_ready)
1460 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1461 const char *arg;
1463 if (!strcmp(reader->line, "NAK"))
1464 continue;
1466 if (skip_prefix(reader->line, "ACK ", &arg)) {
1467 if (!get_oid_hex(arg, common_oid)) {
1468 struct commit *commit;
1469 commit = lookup_commit(the_repository, common_oid);
1470 if (negotiator)
1471 negotiator->ack(negotiator, commit);
1473 return 1;
1476 if (!strcmp(reader->line, "ready")) {
1477 *received_ready = 1;
1478 continue;
1481 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1484 if (reader->status != PACKET_READ_FLUSH &&
1485 reader->status != PACKET_READ_DELIM)
1486 die(_("error processing acks: %d"), reader->status);
1489 * If an "acknowledgments" section is sent, a packfile is sent if and
1490 * only if "ready" was sent in this section. The other sections
1491 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1492 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1493 * otherwise.
1495 if (*received_ready && reader->status != PACKET_READ_DELIM)
1497 * TRANSLATORS: The parameter will be 'ready', a protocol
1498 * keyword.
1500 die(_("expected packfile to be sent after '%s'"), "ready");
1501 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1503 * TRANSLATORS: The parameter will be 'ready', a protocol
1504 * keyword.
1506 die(_("expected no other sections to be sent after no '%s'"), "ready");
1508 return 0;
1511 static void receive_shallow_info(struct fetch_pack_args *args,
1512 struct packet_reader *reader,
1513 struct oid_array *shallows,
1514 struct shallow_info *si)
1516 int unshallow_received = 0;
1518 process_section_header(reader, "shallow-info", 0);
1519 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1520 const char *arg;
1521 struct object_id oid;
1523 if (skip_prefix(reader->line, "shallow ", &arg)) {
1524 if (get_oid_hex(arg, &oid))
1525 die(_("invalid shallow line: %s"), reader->line);
1526 oid_array_append(shallows, &oid);
1527 continue;
1529 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1530 if (get_oid_hex(arg, &oid))
1531 die(_("invalid unshallow line: %s"), reader->line);
1532 if (!lookup_object(the_repository, &oid))
1533 die(_("object not found: %s"), reader->line);
1534 /* make sure that it is parsed as shallow */
1535 if (!parse_object(the_repository, &oid))
1536 die(_("error in object: %s"), reader->line);
1537 if (unregister_shallow(&oid))
1538 die(_("no shallow found: %s"), reader->line);
1539 unshallow_received = 1;
1540 continue;
1542 die(_("expected shallow/unshallow, got %s"), reader->line);
1545 if (reader->status != PACKET_READ_FLUSH &&
1546 reader->status != PACKET_READ_DELIM)
1547 die(_("error processing shallow info: %d"), reader->status);
1549 if (args->deepen || unshallow_received) {
1551 * Treat these as shallow lines caused by our depth settings.
1552 * In v0, these lines cannot cause refs to be rejected; do the
1553 * same.
1555 int i;
1557 for (i = 0; i < shallows->nr; i++)
1558 register_shallow(the_repository, &shallows->oid[i]);
1559 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1560 NULL);
1561 args->deepen = 1;
1562 } else if (shallows->nr) {
1564 * Treat these as shallow lines caused by the remote being
1565 * shallow. In v0, remote refs that reach these objects are
1566 * rejected (unless --update-shallow is set); do the same.
1568 prepare_shallow_info(si, shallows);
1569 if (si->nr_ours || si->nr_theirs) {
1570 if (args->reject_shallow_remote)
1571 die(_("source repository is shallow, reject to clone."));
1572 alternate_shallow_file =
1573 setup_temporary_shallow(si->shallow);
1574 } else
1575 alternate_shallow_file = NULL;
1576 } else {
1577 alternate_shallow_file = NULL;
1581 static int cmp_name_ref(const void *name, const void *ref)
1583 return strcmp(name, (*(struct ref **)ref)->name);
1586 static void receive_wanted_refs(struct packet_reader *reader,
1587 struct ref **sought, int nr_sought)
1589 process_section_header(reader, "wanted-refs", 0);
1590 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1591 struct object_id oid;
1592 const char *end;
1593 struct ref **found;
1595 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1596 die(_("expected wanted-ref, got '%s'"), reader->line);
1598 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1599 cmp_name_ref);
1600 if (!found)
1601 die(_("unexpected wanted-ref: '%s'"), reader->line);
1602 oidcpy(&(*found)->old_oid, &oid);
1605 if (reader->status != PACKET_READ_DELIM)
1606 die(_("error processing wanted refs: %d"), reader->status);
1609 static void receive_packfile_uris(struct packet_reader *reader,
1610 struct string_list *uris)
1612 process_section_header(reader, "packfile-uris", 0);
1613 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1614 if (reader->pktlen < the_hash_algo->hexsz ||
1615 reader->line[the_hash_algo->hexsz] != ' ')
1616 die("expected '<hash> <uri>', got: %s\n", reader->line);
1618 string_list_append(uris, reader->line);
1620 if (reader->status != PACKET_READ_DELIM)
1621 die("expected DELIM");
1624 enum fetch_state {
1625 FETCH_CHECK_LOCAL = 0,
1626 FETCH_SEND_REQUEST,
1627 FETCH_PROCESS_ACKS,
1628 FETCH_GET_PACK,
1629 FETCH_DONE,
1632 static void do_check_stateless_delimiter(int stateless_rpc,
1633 struct packet_reader *reader)
1635 check_stateless_delimiter(stateless_rpc, reader,
1636 _("git fetch-pack: expected response end packet"));
1639 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1640 int fd[2],
1641 const struct ref *orig_ref,
1642 struct ref **sought, int nr_sought,
1643 struct oid_array *shallows,
1644 struct shallow_info *si,
1645 struct string_list *pack_lockfiles)
1647 struct repository *r = the_repository;
1648 struct ref *ref = copy_ref_list(orig_ref);
1649 enum fetch_state state = FETCH_CHECK_LOCAL;
1650 struct oidset common = OIDSET_INIT;
1651 struct packet_reader reader;
1652 int in_vain = 0, negotiation_started = 0;
1653 int negotiation_round = 0;
1654 int haves_to_send = INITIAL_FLUSH;
1655 struct fetch_negotiator negotiator_alloc;
1656 struct fetch_negotiator *negotiator;
1657 int seen_ack = 0;
1658 struct object_id common_oid;
1659 int received_ready = 0;
1660 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1661 int i;
1662 struct strvec index_pack_args = STRVEC_INIT;
1664 negotiator = &negotiator_alloc;
1665 if (args->refetch)
1666 fetch_negotiator_init_noop(negotiator);
1667 else
1668 fetch_negotiator_init(r, negotiator);
1670 packet_reader_init(&reader, fd[0], NULL, 0,
1671 PACKET_READ_CHOMP_NEWLINE |
1672 PACKET_READ_DIE_ON_ERR_PACKET);
1673 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1674 server_supports_feature("fetch", "sideband-all", 0)) {
1675 reader.use_sideband = 1;
1676 reader.me = "fetch-pack";
1679 while (state != FETCH_DONE) {
1680 switch (state) {
1681 case FETCH_CHECK_LOCAL:
1682 sort_ref_list(&ref, ref_compare_name);
1683 QSORT(sought, nr_sought, cmp_ref_by_name);
1685 /* v2 supports these by default */
1686 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1687 use_sideband = 2;
1688 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1689 args->deepen = 1;
1691 /* Filter 'ref' by 'sought' and those that aren't local */
1692 mark_complete_and_common_ref(negotiator, args, &ref);
1693 filter_refs(args, &ref, sought, nr_sought);
1694 if (!args->refetch && everything_local(args, &ref))
1695 state = FETCH_DONE;
1696 else
1697 state = FETCH_SEND_REQUEST;
1699 mark_tips(negotiator, args->negotiation_tips);
1700 for_each_cached_alternate(negotiator,
1701 insert_one_alternate_object);
1702 break;
1703 case FETCH_SEND_REQUEST:
1704 if (!negotiation_started) {
1705 negotiation_started = 1;
1706 trace2_region_enter("fetch-pack",
1707 "negotiation_v2",
1708 the_repository);
1710 negotiation_round++;
1711 trace2_region_enter_printf("negotiation_v2", "round",
1712 the_repository, "%d",
1713 negotiation_round);
1714 if (send_fetch_request(negotiator, fd[1], args, ref,
1715 &common,
1716 &haves_to_send, &in_vain,
1717 reader.use_sideband,
1718 seen_ack)) {
1719 trace2_region_leave_printf("negotiation_v2", "round",
1720 the_repository, "%d",
1721 negotiation_round);
1722 state = FETCH_GET_PACK;
1724 else
1725 state = FETCH_PROCESS_ACKS;
1726 break;
1727 case FETCH_PROCESS_ACKS:
1728 /* Process ACKs/NAKs */
1729 process_section_header(&reader, "acknowledgments", 0);
1730 while (process_ack(negotiator, &reader, &common_oid,
1731 &received_ready)) {
1732 in_vain = 0;
1733 seen_ack = 1;
1734 oidset_insert(&common, &common_oid);
1736 trace2_region_leave_printf("negotiation_v2", "round",
1737 the_repository, "%d",
1738 negotiation_round);
1739 if (received_ready) {
1741 * Don't check for response delimiter; get_pack() will
1742 * read the rest of this response.
1744 state = FETCH_GET_PACK;
1745 } else {
1746 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1747 state = FETCH_SEND_REQUEST;
1749 break;
1750 case FETCH_GET_PACK:
1751 trace2_region_leave("fetch-pack",
1752 "negotiation_v2",
1753 the_repository);
1754 trace2_data_intmax("negotiation_v2", the_repository,
1755 "total_rounds", negotiation_round);
1756 /* Check for shallow-info section */
1757 if (process_section_header(&reader, "shallow-info", 1))
1758 receive_shallow_info(args, &reader, shallows, si);
1760 if (process_section_header(&reader, "wanted-refs", 1))
1761 receive_wanted_refs(&reader, sought, nr_sought);
1763 /* get the pack(s) */
1764 if (git_env_bool("GIT_TRACE_REDACT", 1))
1765 reader.options |= PACKET_READ_REDACT_URI_PATH;
1766 if (process_section_header(&reader, "packfile-uris", 1))
1767 receive_packfile_uris(&reader, &packfile_uris);
1768 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1769 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1771 process_section_header(&reader, "packfile", 0);
1774 * this is the final request we'll make of the server;
1775 * do a half-duplex shutdown to indicate that they can
1776 * hang up as soon as the pack is sent.
1778 close(fd[1]);
1779 fd[1] = -1;
1781 if (get_pack(args, fd, pack_lockfiles,
1782 packfile_uris.nr ? &index_pack_args : NULL,
1783 sought, nr_sought, &fsck_options.gitmodules_found))
1784 die(_("git fetch-pack: fetch failed."));
1785 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1787 state = FETCH_DONE;
1788 break;
1789 case FETCH_DONE:
1790 continue;
1794 for (i = 0; i < packfile_uris.nr; i++) {
1795 int j;
1796 struct child_process cmd = CHILD_PROCESS_INIT;
1797 char packname[GIT_MAX_HEXSZ + 1];
1798 const char *uri = packfile_uris.items[i].string +
1799 the_hash_algo->hexsz + 1;
1801 strvec_push(&cmd.args, "http-fetch");
1802 strvec_pushf(&cmd.args, "--packfile=%.*s",
1803 (int) the_hash_algo->hexsz,
1804 packfile_uris.items[i].string);
1805 for (j = 0; j < index_pack_args.nr; j++)
1806 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1807 index_pack_args.v[j]);
1808 strvec_push(&cmd.args, uri);
1809 cmd.git_cmd = 1;
1810 cmd.no_stdin = 1;
1811 cmd.out = -1;
1812 if (start_command(&cmd))
1813 die("fetch-pack: unable to spawn http-fetch");
1815 if (read_in_full(cmd.out, packname, 5) < 0 ||
1816 memcmp(packname, "keep\t", 5))
1817 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1819 if (read_in_full(cmd.out, packname,
1820 the_hash_algo->hexsz + 1) < 0 ||
1821 packname[the_hash_algo->hexsz] != '\n')
1822 die("fetch-pack: expected hash then LF at end of http-fetch output");
1824 packname[the_hash_algo->hexsz] = '\0';
1826 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1828 close(cmd.out);
1830 if (finish_command(&cmd))
1831 die("fetch-pack: unable to finish http-fetch");
1833 if (memcmp(packfile_uris.items[i].string, packname,
1834 the_hash_algo->hexsz))
1835 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1836 uri, (int) the_hash_algo->hexsz,
1837 packfile_uris.items[i].string);
1839 string_list_append_nodup(pack_lockfiles,
1840 xstrfmt("%s/pack/pack-%s.keep",
1841 get_object_directory(),
1842 packname));
1844 string_list_clear(&packfile_uris, 0);
1845 strvec_clear(&index_pack_args);
1847 if (fsck_finish(&fsck_options))
1848 die("fsck failed");
1850 if (negotiator)
1851 negotiator->release(negotiator);
1853 oidset_clear(&common);
1854 return ref;
1857 static int fetch_pack_config_cb(const char *var, const char *value,
1858 const struct config_context *ctx, void *cb)
1860 const char *msg_id;
1862 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1863 const char *path;
1865 if (git_config_pathname(&path, var, value))
1866 return 1;
1867 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1868 fsck_msg_types.len ? ',' : '=', path);
1869 free((char *)path);
1870 return 0;
1873 if (skip_prefix(var, "fetch.fsck.", &msg_id)) {
1874 if (!value)
1875 return config_error_nonbool(var);
1876 if (is_valid_msg_type(msg_id, value))
1877 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1878 fsck_msg_types.len ? ',' : '=', msg_id, value);
1879 else
1880 warning("Skipping unknown msg id '%s'", msg_id);
1881 return 0;
1884 return git_default_config(var, value, ctx, cb);
1887 static void fetch_pack_config(void)
1889 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1890 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1891 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1892 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1893 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1894 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1895 if (!uri_protocols.nr) {
1896 char *str;
1898 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1899 string_list_split(&uri_protocols, str, ',', -1);
1900 free(str);
1904 git_config(fetch_pack_config_cb, NULL);
1907 static void fetch_pack_setup(void)
1909 static int did_setup;
1910 if (did_setup)
1911 return;
1912 fetch_pack_config();
1913 if (0 <= fetch_unpack_limit)
1914 unpack_limit = fetch_unpack_limit;
1915 else if (0 <= transfer_unpack_limit)
1916 unpack_limit = transfer_unpack_limit;
1917 did_setup = 1;
1920 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1922 struct string_list names = STRING_LIST_INIT_NODUP;
1923 int src, dst;
1925 for (src = dst = 0; src < nr; src++) {
1926 struct string_list_item *item;
1927 item = string_list_insert(&names, ref[src]->name);
1928 if (item->util)
1929 continue; /* already have it */
1930 item->util = ref[src];
1931 if (src != dst)
1932 ref[dst] = ref[src];
1933 dst++;
1935 for (src = dst; src < nr; src++)
1936 ref[src] = NULL;
1937 string_list_clear(&names, 0);
1938 return dst;
1941 static void update_shallow(struct fetch_pack_args *args,
1942 struct ref **sought, int nr_sought,
1943 struct shallow_info *si)
1945 struct oid_array ref = OID_ARRAY_INIT;
1946 int *status;
1947 int i;
1949 if (args->deepen && alternate_shallow_file) {
1950 if (*alternate_shallow_file == '\0') { /* --unshallow */
1951 unlink_or_warn(git_path_shallow(the_repository));
1952 rollback_shallow_file(the_repository, &shallow_lock);
1953 } else
1954 commit_shallow_file(the_repository, &shallow_lock);
1955 alternate_shallow_file = NULL;
1956 return;
1959 if (!si->shallow || !si->shallow->nr)
1960 return;
1962 if (args->cloning) {
1964 * remote is shallow, but this is a clone, there are
1965 * no objects in repo to worry about. Accept any
1966 * shallow points that exist in the pack (iow in repo
1967 * after get_pack() and reprepare_packed_git())
1969 struct oid_array extra = OID_ARRAY_INIT;
1970 struct object_id *oid = si->shallow->oid;
1971 for (i = 0; i < si->shallow->nr; i++)
1972 if (repo_has_object_file(the_repository, &oid[i]))
1973 oid_array_append(&extra, &oid[i]);
1974 if (extra.nr) {
1975 setup_alternate_shallow(&shallow_lock,
1976 &alternate_shallow_file,
1977 &extra);
1978 commit_shallow_file(the_repository, &shallow_lock);
1979 alternate_shallow_file = NULL;
1981 oid_array_clear(&extra);
1982 return;
1985 if (!si->nr_ours && !si->nr_theirs)
1986 return;
1988 remove_nonexistent_theirs_shallow(si);
1989 if (!si->nr_ours && !si->nr_theirs)
1990 return;
1991 for (i = 0; i < nr_sought; i++)
1992 oid_array_append(&ref, &sought[i]->old_oid);
1993 si->ref = &ref;
1995 if (args->update_shallow) {
1997 * remote is also shallow, .git/shallow may be updated
1998 * so all refs can be accepted. Make sure we only add
1999 * shallow roots that are actually reachable from new
2000 * refs.
2002 struct oid_array extra = OID_ARRAY_INIT;
2003 struct object_id *oid = si->shallow->oid;
2004 assign_shallow_commits_to_refs(si, NULL, NULL);
2005 if (!si->nr_ours && !si->nr_theirs) {
2006 oid_array_clear(&ref);
2007 return;
2009 for (i = 0; i < si->nr_ours; i++)
2010 oid_array_append(&extra, &oid[si->ours[i]]);
2011 for (i = 0; i < si->nr_theirs; i++)
2012 oid_array_append(&extra, &oid[si->theirs[i]]);
2013 setup_alternate_shallow(&shallow_lock,
2014 &alternate_shallow_file,
2015 &extra);
2016 commit_shallow_file(the_repository, &shallow_lock);
2017 oid_array_clear(&extra);
2018 oid_array_clear(&ref);
2019 alternate_shallow_file = NULL;
2020 return;
2024 * remote is also shallow, check what ref is safe to update
2025 * without updating .git/shallow
2027 CALLOC_ARRAY(status, nr_sought);
2028 assign_shallow_commits_to_refs(si, NULL, status);
2029 if (si->nr_ours || si->nr_theirs) {
2030 for (i = 0; i < nr_sought; i++)
2031 if (status[i])
2032 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
2034 free(status);
2035 oid_array_clear(&ref);
2038 static const struct object_id *iterate_ref_map(void *cb_data)
2040 struct ref **rm = cb_data;
2041 struct ref *ref = *rm;
2043 if (!ref)
2044 return NULL;
2045 *rm = ref->next;
2046 return &ref->old_oid;
2049 struct ref *fetch_pack(struct fetch_pack_args *args,
2050 int fd[],
2051 const struct ref *ref,
2052 struct ref **sought, int nr_sought,
2053 struct oid_array *shallow,
2054 struct string_list *pack_lockfiles,
2055 enum protocol_version version)
2057 struct ref *ref_cpy;
2058 struct shallow_info si;
2059 struct oid_array shallows_scratch = OID_ARRAY_INIT;
2061 fetch_pack_setup();
2062 if (nr_sought)
2063 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
2065 if (version != protocol_v2 && !ref) {
2066 packet_flush(fd[1]);
2067 die(_("no matching remote head"));
2069 if (version == protocol_v2) {
2070 if (shallow->nr)
2071 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2072 memset(&si, 0, sizeof(si));
2073 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
2074 &shallows_scratch, &si,
2075 pack_lockfiles);
2076 } else {
2077 prepare_shallow_info(&si, shallow);
2078 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
2079 &si, pack_lockfiles);
2081 reprepare_packed_git(the_repository);
2083 if (!args->cloning && args->deepen) {
2084 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2085 struct ref *iterator = ref_cpy;
2086 opt.shallow_file = alternate_shallow_file;
2087 if (args->deepen)
2088 opt.is_deepening_fetch = 1;
2089 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2090 error(_("remote did not send all necessary objects"));
2091 free_refs(ref_cpy);
2092 ref_cpy = NULL;
2093 rollback_shallow_file(the_repository, &shallow_lock);
2094 goto cleanup;
2096 args->connectivity_checked = 1;
2099 update_shallow(args, sought, nr_sought, &si);
2100 cleanup:
2101 clear_shallow_info(&si);
2102 oid_array_clear(&shallows_scratch);
2103 return ref_cpy;
2106 static int add_to_object_array(const struct object_id *oid, void *data)
2108 struct object_array *a = data;
2110 add_object_array(lookup_object(the_repository, oid), "", a);
2111 return 0;
2114 static void clear_common_flag(struct oidset *s)
2116 struct oidset_iter iter;
2117 const struct object_id *oid;
2118 oidset_iter_init(s, &iter);
2120 while ((oid = oidset_iter_next(&iter))) {
2121 struct object *obj = lookup_object(the_repository, oid);
2122 obj->flags &= ~COMMON;
2126 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2127 const struct string_list *server_options,
2128 int stateless_rpc,
2129 int fd[],
2130 struct oidset *acked_commits)
2132 struct fetch_negotiator negotiator;
2133 struct packet_reader reader;
2134 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2135 struct strbuf req_buf = STRBUF_INIT;
2136 int haves_to_send = INITIAL_FLUSH;
2137 int in_vain = 0;
2138 int seen_ack = 0;
2139 int last_iteration = 0;
2140 int negotiation_round = 0;
2141 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2143 fetch_negotiator_init(the_repository, &negotiator);
2144 mark_tips(&negotiator, negotiation_tips);
2146 packet_reader_init(&reader, fd[0], NULL, 0,
2147 PACKET_READ_CHOMP_NEWLINE |
2148 PACKET_READ_DIE_ON_ERR_PACKET);
2150 oid_array_for_each((struct oid_array *) negotiation_tips,
2151 add_to_object_array,
2152 &nt_object_array);
2154 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2155 while (!last_iteration) {
2156 int haves_added;
2157 struct object_id common_oid;
2158 int received_ready = 0;
2160 negotiation_round++;
2162 trace2_region_enter_printf("negotiate_using_fetch", "round",
2163 the_repository, "%d",
2164 negotiation_round);
2165 strbuf_reset(&req_buf);
2166 write_fetch_command_and_capabilities(&req_buf, server_options);
2168 packet_buf_write(&req_buf, "wait-for-done");
2170 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2171 in_vain += haves_added;
2172 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2173 last_iteration = 1;
2175 trace2_data_intmax("negotiate_using_fetch", the_repository,
2176 "haves_added", haves_added);
2177 trace2_data_intmax("negotiate_using_fetch", the_repository,
2178 "in_vain", in_vain);
2180 /* Send request */
2181 packet_buf_flush(&req_buf);
2182 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2183 die_errno(_("unable to write request to remote"));
2185 /* Process ACKs/NAKs */
2186 process_section_header(&reader, "acknowledgments", 0);
2187 while (process_ack(&negotiator, &reader, &common_oid,
2188 &received_ready)) {
2189 struct commit *commit = lookup_commit(the_repository,
2190 &common_oid);
2191 if (commit) {
2192 timestamp_t generation;
2194 parse_commit_or_die(commit);
2195 commit->object.flags |= COMMON;
2196 generation = commit_graph_generation(commit);
2197 if (generation < min_generation)
2198 min_generation = generation;
2200 in_vain = 0;
2201 seen_ack = 1;
2202 oidset_insert(acked_commits, &common_oid);
2204 if (received_ready)
2205 die(_("unexpected 'ready' from remote"));
2206 else
2207 do_check_stateless_delimiter(stateless_rpc, &reader);
2208 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2209 REACH_SCRATCH, 0,
2210 min_generation))
2211 last_iteration = 1;
2212 trace2_region_leave_printf("negotiation", "round",
2213 the_repository, "%d",
2214 negotiation_round);
2216 trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository);
2217 trace2_data_intmax("negotiate_using_fetch", the_repository,
2218 "total_rounds", negotiation_round);
2219 clear_common_flag(acked_commits);
2220 strbuf_release(&req_buf);
2223 int report_unmatched_refs(struct ref **sought, int nr_sought)
2225 int i, ret = 0;
2227 for (i = 0; i < nr_sought; i++) {
2228 if (!sought[i])
2229 continue;
2230 switch (sought[i]->match_status) {
2231 case REF_MATCHED:
2232 continue;
2233 case REF_NOT_MATCHED:
2234 error(_("no such remote ref %s"), sought[i]->name);
2235 break;
2236 case REF_UNADVERTISED_NOT_ALLOWED:
2237 error(_("Server does not allow request for unadvertised object %s"),
2238 sought[i]->name);
2239 break;
2241 ret = 1;
2243 return ret;