Sync with 'master'
[git.git] / fetch-pack.c
blob8e8f3bba32d33fd8e0f4d24b8dde6c65c102f327
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);
736 struct loose_object_iter {
737 struct oidset *loose_object_set;
738 struct ref *refs;
742 * Mark recent commits available locally and reachable from a local ref as
743 * COMPLETE.
745 * The cutoff time for recency is determined by this heuristic: it is the
746 * earliest commit time of the objects in refs that are commits and that we know
747 * the commit time of.
749 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
750 struct fetch_pack_args *args,
751 struct ref **refs)
753 struct ref *ref;
754 int old_save_commit_buffer = save_commit_buffer;
755 timestamp_t cutoff = 0;
757 if (args->refetch)
758 return;
760 save_commit_buffer = 0;
762 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
763 for (ref = *refs; ref; ref = ref->next) {
764 struct commit *commit;
766 commit = lookup_commit_in_graph(the_repository, &ref->old_oid);
767 if (!commit) {
768 struct object *o;
770 if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
771 OBJECT_INFO_QUICK |
772 OBJECT_INFO_SKIP_FETCH_OBJECT))
773 continue;
774 o = parse_object(the_repository, &ref->old_oid);
775 if (!o || o->type != OBJ_COMMIT)
776 continue;
778 commit = (struct commit *)o;
782 * We already have it -- which may mean that we were
783 * in sync with the other side at some time after
784 * that (it is OK if we guess wrong here).
786 if (!cutoff || cutoff < commit->date)
787 cutoff = commit->date;
789 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
792 * This block marks all local refs as COMPLETE, and then recursively marks all
793 * parents of those refs as COMPLETE.
795 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
796 if (!args->deepen) {
797 refs_for_each_rawref(get_main_ref_store(the_repository),
798 mark_complete_oid, NULL);
799 for_each_cached_alternate(NULL, mark_alternate_complete);
800 commit_list_sort_by_date(&complete);
801 if (cutoff)
802 mark_recent_complete_commits(args, cutoff);
804 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
807 * Mark all complete remote refs as common refs.
808 * Don't mark them common yet; the server has to be told so first.
810 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
811 for (ref = *refs; ref; ref = ref->next) {
812 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
814 if (!c || !(c->object.flags & COMPLETE))
815 continue;
817 negotiator->known_common(negotiator, c);
819 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
821 save_commit_buffer = old_save_commit_buffer;
825 * Returns 1 if every object pointed to by the given remote refs is available
826 * locally and reachable from a local ref, and 0 otherwise.
828 static int everything_local(struct fetch_pack_args *args,
829 struct ref **refs)
831 struct ref *ref;
832 int retval;
834 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
835 const struct object_id *remote = &ref->old_oid;
836 struct object *o;
838 o = lookup_object(the_repository, remote);
839 if (!o || !(o->flags & COMPLETE)) {
840 retval = 0;
841 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
842 ref->name);
843 continue;
845 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
846 ref->name);
849 return retval;
852 static int sideband_demux(int in UNUSED, int out, void *data)
854 int *xd = data;
855 int ret;
857 ret = recv_sideband("fetch-pack", xd[0], out);
858 close(out);
859 return ret;
862 static void create_promisor_file(const char *keep_name,
863 struct ref **sought, int nr_sought)
865 struct strbuf promisor_name = STRBUF_INIT;
866 int suffix_stripped;
868 strbuf_addstr(&promisor_name, keep_name);
869 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
870 if (!suffix_stripped)
871 BUG("name of pack lockfile should end with .keep (was '%s')",
872 keep_name);
873 strbuf_addstr(&promisor_name, ".promisor");
875 write_promisor_file(promisor_name.buf, sought, nr_sought);
877 strbuf_release(&promisor_name);
880 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
882 int len = the_hash_algo->hexsz + 1; /* hash + NL */
884 do {
885 char hex_hash[GIT_MAX_HEXSZ + 1];
886 int read_len = read_in_full(fd, hex_hash, len);
887 struct object_id oid;
888 const char *end;
890 if (!read_len)
891 return;
892 if (read_len != len)
893 die("invalid length read %d", read_len);
894 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
895 die("invalid hash");
896 oidset_insert(gitmodules_oids, &oid);
897 } while (1);
900 static void add_index_pack_keep_option(struct strvec *args)
902 char hostname[HOST_NAME_MAX + 1];
904 if (xgethostname(hostname, sizeof(hostname)))
905 xsnprintf(hostname, sizeof(hostname), "localhost");
906 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
907 (uintmax_t)getpid(), hostname);
911 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
912 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
913 * stored there. (It must be freed by the caller.)
915 static int get_pack(struct fetch_pack_args *args,
916 int xd[2], struct string_list *pack_lockfiles,
917 struct strvec *index_pack_args,
918 struct ref **sought, int nr_sought,
919 struct oidset *gitmodules_oids)
921 struct async demux;
922 int do_keep = args->keep_pack;
923 const char *cmd_name;
924 struct pack_header header;
925 int pass_header = 0;
926 struct child_process cmd = CHILD_PROCESS_INIT;
927 int fsck_objects = 0;
928 int ret;
930 memset(&demux, 0, sizeof(demux));
931 if (use_sideband) {
932 /* xd[] is talking with upload-pack; subprocess reads from
933 * xd[0], spits out band#2 to stderr, and feeds us band#1
934 * through demux->out.
936 demux.proc = sideband_demux;
937 demux.data = xd;
938 demux.out = -1;
939 demux.isolate_sigpipe = 1;
940 if (start_async(&demux))
941 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
943 else
944 demux.out = xd[0];
946 if (!args->keep_pack && unpack_limit && !index_pack_args) {
948 if (read_pack_header(demux.out, &header))
949 die(_("protocol error: bad pack header"));
950 pass_header = 1;
951 if (ntohl(header.hdr_entries) < unpack_limit)
952 do_keep = 0;
953 else
954 do_keep = 1;
957 if (alternate_shallow_file) {
958 strvec_push(&cmd.args, "--shallow-file");
959 strvec_push(&cmd.args, alternate_shallow_file);
962 if (fetch_fsck_objects >= 0
963 ? fetch_fsck_objects
964 : transfer_fsck_objects >= 0
965 ? transfer_fsck_objects
966 : 0)
967 fsck_objects = 1;
969 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
970 if (pack_lockfiles || fsck_objects)
971 cmd.out = -1;
972 cmd_name = "index-pack";
973 strvec_push(&cmd.args, cmd_name);
974 strvec_push(&cmd.args, "--stdin");
975 if (!args->quiet && !args->no_progress)
976 strvec_push(&cmd.args, "-v");
977 if (args->use_thin_pack)
978 strvec_push(&cmd.args, "--fix-thin");
979 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
980 add_index_pack_keep_option(&cmd.args);
981 if (!index_pack_args && args->check_self_contained_and_connected)
982 strvec_push(&cmd.args, "--check-self-contained-and-connected");
983 else
985 * We cannot perform any connectivity checks because
986 * not all packs have been downloaded; let the caller
987 * have this responsibility.
989 args->check_self_contained_and_connected = 0;
991 if (args->from_promisor)
993 * create_promisor_file() may be called afterwards but
994 * we still need index-pack to know that this is a
995 * promisor pack. For example, if transfer.fsckobjects
996 * is true, index-pack needs to know that .gitmodules
997 * is a promisor object (so that it won't complain if
998 * it is missing).
1000 strvec_push(&cmd.args, "--promisor");
1002 else {
1003 cmd_name = "unpack-objects";
1004 strvec_push(&cmd.args, cmd_name);
1005 if (args->quiet || args->no_progress)
1006 strvec_push(&cmd.args, "-q");
1007 args->check_self_contained_and_connected = 0;
1010 if (pass_header)
1011 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
1012 ntohl(header.hdr_version),
1013 ntohl(header.hdr_entries));
1014 if (fsck_objects) {
1015 if (args->from_promisor || index_pack_args)
1017 * We cannot use --strict in index-pack because it
1018 * checks both broken objects and links, but we only
1019 * want to check for broken objects.
1021 strvec_push(&cmd.args, "--fsck-objects");
1022 else
1023 strvec_pushf(&cmd.args, "--strict%s",
1024 fsck_msg_types.buf);
1027 if (index_pack_args) {
1028 int i;
1030 for (i = 0; i < cmd.args.nr; i++)
1031 strvec_push(index_pack_args, cmd.args.v[i]);
1034 sigchain_push(SIGPIPE, SIG_IGN);
1036 cmd.in = demux.out;
1037 cmd.git_cmd = 1;
1038 if (start_command(&cmd))
1039 die(_("fetch-pack: unable to fork off %s"), cmd_name);
1040 if (do_keep && (pack_lockfiles || fsck_objects)) {
1041 int is_well_formed;
1042 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1044 if (!is_well_formed)
1045 die(_("fetch-pack: invalid index-pack output"));
1046 if (pack_lockfile)
1047 string_list_append_nodup(pack_lockfiles, pack_lockfile);
1048 parse_gitmodules_oids(cmd.out, gitmodules_oids);
1049 close(cmd.out);
1052 if (!use_sideband)
1053 /* Closed by start_command() */
1054 xd[0] = -1;
1056 ret = finish_command(&cmd);
1057 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1058 args->self_contained_and_connected =
1059 args->check_self_contained_and_connected &&
1060 ret == 0;
1061 else
1062 die(_("%s failed"), cmd_name);
1063 if (use_sideband && finish_async(&demux))
1064 die(_("error in sideband demultiplexer"));
1066 sigchain_pop(SIGPIPE);
1069 * Now that index-pack has succeeded, write the promisor file using the
1070 * obtained .keep filename if necessary
1072 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
1073 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
1075 return 0;
1078 static int ref_compare_name(const struct ref *a, const struct ref *b)
1080 return strcmp(a->name, b->name);
1083 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1085 static int cmp_ref_by_name(const void *a_, const void *b_)
1087 const struct ref *a = *((const struct ref **)a_);
1088 const struct ref *b = *((const struct ref **)b_);
1089 return strcmp(a->name, b->name);
1092 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1093 int fd[2],
1094 const struct ref *orig_ref,
1095 struct ref **sought, int nr_sought,
1096 struct shallow_info *si,
1097 struct string_list *pack_lockfiles)
1099 struct repository *r = the_repository;
1100 struct ref *ref = copy_ref_list(orig_ref);
1101 struct object_id oid;
1102 const char *agent_feature;
1103 size_t agent_len;
1104 struct fetch_negotiator negotiator_alloc;
1105 struct fetch_negotiator *negotiator;
1107 negotiator = &negotiator_alloc;
1108 if (args->refetch) {
1109 fetch_negotiator_init_noop(negotiator);
1110 } else {
1111 fetch_negotiator_init(r, negotiator);
1114 sort_ref_list(&ref, ref_compare_name);
1115 QSORT(sought, nr_sought, cmp_ref_by_name);
1117 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1118 agent_supported = 1;
1119 if (agent_len)
1120 print_verbose(args, _("Server version is %.*s"),
1121 (int)agent_len, agent_feature);
1124 if (!server_supports("session-id"))
1125 advertise_sid = 0;
1127 if (server_supports("shallow"))
1128 print_verbose(args, _("Server supports %s"), "shallow");
1129 else if (args->depth > 0 || is_repository_shallow(r))
1130 die(_("Server does not support shallow clients"));
1131 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1132 args->deepen = 1;
1133 if (server_supports("multi_ack_detailed")) {
1134 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1135 multi_ack = 2;
1136 if (server_supports("no-done")) {
1137 print_verbose(args, _("Server supports %s"), "no-done");
1138 if (args->stateless_rpc)
1139 no_done = 1;
1142 else if (server_supports("multi_ack")) {
1143 print_verbose(args, _("Server supports %s"), "multi_ack");
1144 multi_ack = 1;
1146 if (server_supports("side-band-64k")) {
1147 print_verbose(args, _("Server supports %s"), "side-band-64k");
1148 use_sideband = 2;
1150 else if (server_supports("side-band")) {
1151 print_verbose(args, _("Server supports %s"), "side-band");
1152 use_sideband = 1;
1154 if (server_supports("allow-tip-sha1-in-want")) {
1155 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1156 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1158 if (server_supports("allow-reachable-sha1-in-want")) {
1159 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1160 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1162 if (server_supports("thin-pack"))
1163 print_verbose(args, _("Server supports %s"), "thin-pack");
1164 else
1165 args->use_thin_pack = 0;
1166 if (server_supports("no-progress"))
1167 print_verbose(args, _("Server supports %s"), "no-progress");
1168 else
1169 args->no_progress = 0;
1170 if (server_supports("include-tag"))
1171 print_verbose(args, _("Server supports %s"), "include-tag");
1172 else
1173 args->include_tag = 0;
1174 if (server_supports("ofs-delta"))
1175 print_verbose(args, _("Server supports %s"), "ofs-delta");
1176 else
1177 prefer_ofs_delta = 0;
1179 if (server_supports("filter")) {
1180 server_supports_filtering = 1;
1181 print_verbose(args, _("Server supports %s"), "filter");
1182 } else if (args->filter_options.choice) {
1183 warning("filtering not recognized by server, ignoring");
1186 if (server_supports("deepen-since")) {
1187 print_verbose(args, _("Server supports %s"), "deepen-since");
1188 deepen_since_ok = 1;
1189 } else if (args->deepen_since)
1190 die(_("Server does not support --shallow-since"));
1191 if (server_supports("deepen-not")) {
1192 print_verbose(args, _("Server supports %s"), "deepen-not");
1193 deepen_not_ok = 1;
1194 } else if (args->deepen_not)
1195 die(_("Server does not support --shallow-exclude"));
1196 if (server_supports("deepen-relative"))
1197 print_verbose(args, _("Server supports %s"), "deepen-relative");
1198 else if (args->deepen_relative)
1199 die(_("Server does not support --deepen"));
1200 if (!server_supports_hash(the_hash_algo->name, NULL))
1201 die(_("Server does not support this repository's object format"));
1203 mark_complete_and_common_ref(negotiator, args, &ref);
1204 filter_refs(args, &ref, sought, nr_sought);
1205 if (!args->refetch && everything_local(args, &ref)) {
1206 packet_flush(fd[1]);
1207 goto all_done;
1209 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1210 if (!args->keep_pack)
1211 /* When cloning, it is not unusual to have
1212 * no common commit.
1214 warning(_("no common commits"));
1216 if (args->stateless_rpc)
1217 packet_flush(fd[1]);
1218 if (args->deepen)
1219 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1220 NULL);
1221 else if (si->nr_ours || si->nr_theirs) {
1222 if (args->reject_shallow_remote)
1223 die(_("source repository is shallow, reject to clone."));
1224 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1225 } else
1226 alternate_shallow_file = NULL;
1227 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1228 &fsck_options.gitmodules_found))
1229 die(_("git fetch-pack: fetch failed."));
1230 if (fsck_finish(&fsck_options))
1231 die("fsck failed");
1233 all_done:
1234 if (negotiator)
1235 negotiator->release(negotiator);
1236 return ref;
1239 static void add_shallow_requests(struct strbuf *req_buf,
1240 const struct fetch_pack_args *args)
1242 if (is_repository_shallow(the_repository))
1243 write_shallow_commits(req_buf, 1, NULL);
1244 if (args->depth > 0)
1245 packet_buf_write(req_buf, "deepen %d", args->depth);
1246 if (args->deepen_since) {
1247 timestamp_t max_age = approxidate(args->deepen_since);
1248 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1250 if (args->deepen_not) {
1251 int i;
1252 for (i = 0; i < args->deepen_not->nr; i++) {
1253 struct string_list_item *s = args->deepen_not->items + i;
1254 packet_buf_write(req_buf, "deepen-not %s", s->string);
1257 if (args->deepen_relative)
1258 packet_buf_write(req_buf, "deepen-relative\n");
1261 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1263 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1265 for ( ; wants ; wants = wants->next) {
1266 const struct object_id *remote = &wants->old_oid;
1267 struct object *o;
1270 * If that object is complete (i.e. it is an ancestor of a
1271 * local ref), we tell them we have it but do not have to
1272 * tell them about its ancestors, which they already know
1273 * about.
1275 * We use lookup_object here because we are only
1276 * interested in the case we *know* the object is
1277 * reachable and we have already scanned it.
1279 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1280 (o->flags & COMPLETE)) {
1281 continue;
1284 if (!use_ref_in_want || wants->exact_oid)
1285 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1286 else
1287 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1291 static void add_common(struct strbuf *req_buf, struct oidset *common)
1293 struct oidset_iter iter;
1294 const struct object_id *oid;
1295 oidset_iter_init(common, &iter);
1297 while ((oid = oidset_iter_next(&iter))) {
1298 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1302 static int add_haves(struct fetch_negotiator *negotiator,
1303 struct strbuf *req_buf,
1304 int *haves_to_send)
1306 int haves_added = 0;
1307 const struct object_id *oid;
1309 while ((oid = negotiator->next(negotiator))) {
1310 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1311 if (++haves_added >= *haves_to_send)
1312 break;
1315 /* Increase haves to send on next round */
1316 *haves_to_send = next_flush(1, *haves_to_send);
1318 return haves_added;
1321 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1322 const struct string_list *server_options)
1324 const char *hash_name;
1326 ensure_server_supports_v2("fetch");
1327 packet_buf_write(req_buf, "command=fetch");
1328 if (server_supports_v2("agent"))
1329 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1330 if (advertise_sid && server_supports_v2("session-id"))
1331 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1332 if (server_options && server_options->nr) {
1333 int i;
1334 ensure_server_supports_v2("server-option");
1335 for (i = 0; i < server_options->nr; i++)
1336 packet_buf_write(req_buf, "server-option=%s",
1337 server_options->items[i].string);
1340 if (server_feature_v2("object-format", &hash_name)) {
1341 int hash_algo = hash_algo_by_name(hash_name);
1342 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1343 die(_("mismatched algorithms: client %s; server %s"),
1344 the_hash_algo->name, hash_name);
1345 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1346 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1347 die(_("the server does not support algorithm '%s'"),
1348 the_hash_algo->name);
1350 packet_buf_delim(req_buf);
1353 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1354 struct fetch_pack_args *args,
1355 const struct ref *wants, struct oidset *common,
1356 int *haves_to_send, int *in_vain,
1357 int sideband_all, int seen_ack)
1359 int haves_added;
1360 int done_sent = 0;
1361 struct strbuf req_buf = STRBUF_INIT;
1363 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1365 if (args->use_thin_pack)
1366 packet_buf_write(&req_buf, "thin-pack");
1367 if (args->no_progress)
1368 packet_buf_write(&req_buf, "no-progress");
1369 if (args->include_tag)
1370 packet_buf_write(&req_buf, "include-tag");
1371 if (prefer_ofs_delta)
1372 packet_buf_write(&req_buf, "ofs-delta");
1373 if (sideband_all)
1374 packet_buf_write(&req_buf, "sideband-all");
1376 /* Add shallow-info and deepen request */
1377 if (server_supports_feature("fetch", "shallow", 0))
1378 add_shallow_requests(&req_buf, args);
1379 else if (is_repository_shallow(the_repository) || args->deepen)
1380 die(_("Server does not support shallow requests"));
1382 /* Add filter */
1383 send_filter(args, &req_buf,
1384 server_supports_feature("fetch", "filter", 0));
1386 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1387 int i;
1388 struct strbuf to_send = STRBUF_INIT;
1390 for (i = 0; i < uri_protocols.nr; i++) {
1391 const char *s = uri_protocols.items[i].string;
1393 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1394 if (to_send.len)
1395 strbuf_addch(&to_send, ',');
1396 strbuf_addstr(&to_send, s);
1399 if (to_send.len) {
1400 packet_buf_write(&req_buf, "packfile-uris %s",
1401 to_send.buf);
1402 strbuf_release(&to_send);
1406 /* add wants */
1407 add_wants(wants, &req_buf);
1409 /* Add all of the common commits we've found in previous rounds */
1410 add_common(&req_buf, common);
1412 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1413 *in_vain += haves_added;
1414 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1415 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
1416 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1417 /* Send Done */
1418 packet_buf_write(&req_buf, "done\n");
1419 done_sent = 1;
1422 /* Send request */
1423 packet_buf_flush(&req_buf);
1424 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1425 die_errno(_("unable to write request to remote"));
1427 strbuf_release(&req_buf);
1428 return done_sent;
1432 * Processes a section header in a server's response and checks if it matches
1433 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1434 * not consumed); if 0, the line will be consumed and the function will die if
1435 * the section header doesn't match what was expected.
1437 static int process_section_header(struct packet_reader *reader,
1438 const char *section, int peek)
1440 int ret = 0;
1442 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1443 !strcmp(reader->line, section))
1444 ret = 1;
1446 if (!peek) {
1447 if (!ret) {
1448 if (reader->line)
1449 die(_("expected '%s', received '%s'"),
1450 section, reader->line);
1451 else
1452 die(_("expected '%s'"), section);
1454 packet_reader_read(reader);
1457 return ret;
1460 static int process_ack(struct fetch_negotiator *negotiator,
1461 struct packet_reader *reader,
1462 struct object_id *common_oid,
1463 int *received_ready)
1465 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1466 const char *arg;
1468 if (!strcmp(reader->line, "NAK"))
1469 continue;
1471 if (skip_prefix(reader->line, "ACK ", &arg)) {
1472 if (!get_oid_hex(arg, common_oid)) {
1473 struct commit *commit;
1474 commit = lookup_commit(the_repository, common_oid);
1475 if (negotiator)
1476 negotiator->ack(negotiator, commit);
1478 return 1;
1481 if (!strcmp(reader->line, "ready")) {
1482 *received_ready = 1;
1483 continue;
1486 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1489 if (reader->status != PACKET_READ_FLUSH &&
1490 reader->status != PACKET_READ_DELIM)
1491 die(_("error processing acks: %d"), reader->status);
1494 * If an "acknowledgments" section is sent, a packfile is sent if and
1495 * only if "ready" was sent in this section. The other sections
1496 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1497 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1498 * otherwise.
1500 if (*received_ready && reader->status != PACKET_READ_DELIM)
1502 * TRANSLATORS: The parameter will be 'ready', a protocol
1503 * keyword.
1505 die(_("expected packfile to be sent after '%s'"), "ready");
1506 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1508 * TRANSLATORS: The parameter will be 'ready', a protocol
1509 * keyword.
1511 die(_("expected no other sections to be sent after no '%s'"), "ready");
1513 return 0;
1516 static void receive_shallow_info(struct fetch_pack_args *args,
1517 struct packet_reader *reader,
1518 struct oid_array *shallows,
1519 struct shallow_info *si)
1521 int unshallow_received = 0;
1523 process_section_header(reader, "shallow-info", 0);
1524 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1525 const char *arg;
1526 struct object_id oid;
1528 if (skip_prefix(reader->line, "shallow ", &arg)) {
1529 if (get_oid_hex(arg, &oid))
1530 die(_("invalid shallow line: %s"), reader->line);
1531 oid_array_append(shallows, &oid);
1532 continue;
1534 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1535 if (get_oid_hex(arg, &oid))
1536 die(_("invalid unshallow line: %s"), reader->line);
1537 if (!lookup_object(the_repository, &oid))
1538 die(_("object not found: %s"), reader->line);
1539 /* make sure that it is parsed as shallow */
1540 if (!parse_object(the_repository, &oid))
1541 die(_("error in object: %s"), reader->line);
1542 if (unregister_shallow(&oid))
1543 die(_("no shallow found: %s"), reader->line);
1544 unshallow_received = 1;
1545 continue;
1547 die(_("expected shallow/unshallow, got %s"), reader->line);
1550 if (reader->status != PACKET_READ_FLUSH &&
1551 reader->status != PACKET_READ_DELIM)
1552 die(_("error processing shallow info: %d"), reader->status);
1554 if (args->deepen || unshallow_received) {
1556 * Treat these as shallow lines caused by our depth settings.
1557 * In v0, these lines cannot cause refs to be rejected; do the
1558 * same.
1560 int i;
1562 for (i = 0; i < shallows->nr; i++)
1563 register_shallow(the_repository, &shallows->oid[i]);
1564 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1565 NULL);
1566 args->deepen = 1;
1567 } else if (shallows->nr) {
1569 * Treat these as shallow lines caused by the remote being
1570 * shallow. In v0, remote refs that reach these objects are
1571 * rejected (unless --update-shallow is set); do the same.
1573 prepare_shallow_info(si, shallows);
1574 if (si->nr_ours || si->nr_theirs) {
1575 if (args->reject_shallow_remote)
1576 die(_("source repository is shallow, reject to clone."));
1577 alternate_shallow_file =
1578 setup_temporary_shallow(si->shallow);
1579 } else
1580 alternate_shallow_file = NULL;
1581 } else {
1582 alternate_shallow_file = NULL;
1586 static int cmp_name_ref(const void *name, const void *ref)
1588 return strcmp(name, (*(struct ref **)ref)->name);
1591 static void receive_wanted_refs(struct packet_reader *reader,
1592 struct ref **sought, int nr_sought)
1594 process_section_header(reader, "wanted-refs", 0);
1595 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1596 struct object_id oid;
1597 const char *end;
1598 struct ref **found;
1600 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1601 die(_("expected wanted-ref, got '%s'"), reader->line);
1603 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1604 cmp_name_ref);
1605 if (!found)
1606 die(_("unexpected wanted-ref: '%s'"), reader->line);
1607 oidcpy(&(*found)->old_oid, &oid);
1610 if (reader->status != PACKET_READ_DELIM)
1611 die(_("error processing wanted refs: %d"), reader->status);
1614 static void receive_packfile_uris(struct packet_reader *reader,
1615 struct string_list *uris)
1617 process_section_header(reader, "packfile-uris", 0);
1618 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1619 if (reader->pktlen < the_hash_algo->hexsz ||
1620 reader->line[the_hash_algo->hexsz] != ' ')
1621 die("expected '<hash> <uri>', got: %s\n", reader->line);
1623 string_list_append(uris, reader->line);
1625 if (reader->status != PACKET_READ_DELIM)
1626 die("expected DELIM");
1629 enum fetch_state {
1630 FETCH_CHECK_LOCAL = 0,
1631 FETCH_SEND_REQUEST,
1632 FETCH_PROCESS_ACKS,
1633 FETCH_GET_PACK,
1634 FETCH_DONE,
1637 static void do_check_stateless_delimiter(int stateless_rpc,
1638 struct packet_reader *reader)
1640 check_stateless_delimiter(stateless_rpc, reader,
1641 _("git fetch-pack: expected response end packet"));
1644 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1645 int fd[2],
1646 const struct ref *orig_ref,
1647 struct ref **sought, int nr_sought,
1648 struct oid_array *shallows,
1649 struct shallow_info *si,
1650 struct string_list *pack_lockfiles)
1652 struct repository *r = the_repository;
1653 struct ref *ref = copy_ref_list(orig_ref);
1654 enum fetch_state state = FETCH_CHECK_LOCAL;
1655 struct oidset common = OIDSET_INIT;
1656 struct packet_reader reader;
1657 int in_vain = 0, negotiation_started = 0;
1658 int negotiation_round = 0;
1659 int haves_to_send = INITIAL_FLUSH;
1660 struct fetch_negotiator negotiator_alloc;
1661 struct fetch_negotiator *negotiator;
1662 int seen_ack = 0;
1663 struct object_id common_oid;
1664 int received_ready = 0;
1665 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1666 int i;
1667 struct strvec index_pack_args = STRVEC_INIT;
1669 negotiator = &negotiator_alloc;
1670 if (args->refetch)
1671 fetch_negotiator_init_noop(negotiator);
1672 else
1673 fetch_negotiator_init(r, negotiator);
1675 packet_reader_init(&reader, fd[0], NULL, 0,
1676 PACKET_READ_CHOMP_NEWLINE |
1677 PACKET_READ_DIE_ON_ERR_PACKET);
1678 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1679 server_supports_feature("fetch", "sideband-all", 0)) {
1680 reader.use_sideband = 1;
1681 reader.me = "fetch-pack";
1684 while (state != FETCH_DONE) {
1685 switch (state) {
1686 case FETCH_CHECK_LOCAL:
1687 sort_ref_list(&ref, ref_compare_name);
1688 QSORT(sought, nr_sought, cmp_ref_by_name);
1690 /* v2 supports these by default */
1691 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1692 use_sideband = 2;
1693 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1694 args->deepen = 1;
1696 /* Filter 'ref' by 'sought' and those that aren't local */
1697 mark_complete_and_common_ref(negotiator, args, &ref);
1698 filter_refs(args, &ref, sought, nr_sought);
1699 if (!args->refetch && everything_local(args, &ref))
1700 state = FETCH_DONE;
1701 else
1702 state = FETCH_SEND_REQUEST;
1704 mark_tips(negotiator, args->negotiation_tips);
1705 for_each_cached_alternate(negotiator,
1706 insert_one_alternate_object);
1707 break;
1708 case FETCH_SEND_REQUEST:
1709 if (!negotiation_started) {
1710 negotiation_started = 1;
1711 trace2_region_enter("fetch-pack",
1712 "negotiation_v2",
1713 the_repository);
1715 negotiation_round++;
1716 trace2_region_enter_printf("negotiation_v2", "round",
1717 the_repository, "%d",
1718 negotiation_round);
1719 if (send_fetch_request(negotiator, fd[1], args, ref,
1720 &common,
1721 &haves_to_send, &in_vain,
1722 reader.use_sideband,
1723 seen_ack)) {
1724 trace2_region_leave_printf("negotiation_v2", "round",
1725 the_repository, "%d",
1726 negotiation_round);
1727 state = FETCH_GET_PACK;
1729 else
1730 state = FETCH_PROCESS_ACKS;
1731 break;
1732 case FETCH_PROCESS_ACKS:
1733 /* Process ACKs/NAKs */
1734 process_section_header(&reader, "acknowledgments", 0);
1735 while (process_ack(negotiator, &reader, &common_oid,
1736 &received_ready)) {
1737 in_vain = 0;
1738 seen_ack = 1;
1739 oidset_insert(&common, &common_oid);
1741 trace2_region_leave_printf("negotiation_v2", "round",
1742 the_repository, "%d",
1743 negotiation_round);
1744 if (received_ready) {
1746 * Don't check for response delimiter; get_pack() will
1747 * read the rest of this response.
1749 state = FETCH_GET_PACK;
1750 } else {
1751 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1752 state = FETCH_SEND_REQUEST;
1754 break;
1755 case FETCH_GET_PACK:
1756 trace2_region_leave("fetch-pack",
1757 "negotiation_v2",
1758 the_repository);
1759 trace2_data_intmax("negotiation_v2", the_repository,
1760 "total_rounds", negotiation_round);
1761 /* Check for shallow-info section */
1762 if (process_section_header(&reader, "shallow-info", 1))
1763 receive_shallow_info(args, &reader, shallows, si);
1765 if (process_section_header(&reader, "wanted-refs", 1))
1766 receive_wanted_refs(&reader, sought, nr_sought);
1768 /* get the pack(s) */
1769 if (git_env_bool("GIT_TRACE_REDACT", 1))
1770 reader.options |= PACKET_READ_REDACT_URI_PATH;
1771 if (process_section_header(&reader, "packfile-uris", 1))
1772 receive_packfile_uris(&reader, &packfile_uris);
1773 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1774 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1776 process_section_header(&reader, "packfile", 0);
1779 * this is the final request we'll make of the server;
1780 * do a half-duplex shutdown to indicate that they can
1781 * hang up as soon as the pack is sent.
1783 close(fd[1]);
1784 fd[1] = -1;
1786 if (get_pack(args, fd, pack_lockfiles,
1787 packfile_uris.nr ? &index_pack_args : NULL,
1788 sought, nr_sought, &fsck_options.gitmodules_found))
1789 die(_("git fetch-pack: fetch failed."));
1790 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1792 state = FETCH_DONE;
1793 break;
1794 case FETCH_DONE:
1795 continue;
1799 for (i = 0; i < packfile_uris.nr; i++) {
1800 int j;
1801 struct child_process cmd = CHILD_PROCESS_INIT;
1802 char packname[GIT_MAX_HEXSZ + 1];
1803 const char *uri = packfile_uris.items[i].string +
1804 the_hash_algo->hexsz + 1;
1806 strvec_push(&cmd.args, "http-fetch");
1807 strvec_pushf(&cmd.args, "--packfile=%.*s",
1808 (int) the_hash_algo->hexsz,
1809 packfile_uris.items[i].string);
1810 for (j = 0; j < index_pack_args.nr; j++)
1811 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1812 index_pack_args.v[j]);
1813 strvec_push(&cmd.args, uri);
1814 cmd.git_cmd = 1;
1815 cmd.no_stdin = 1;
1816 cmd.out = -1;
1817 if (start_command(&cmd))
1818 die("fetch-pack: unable to spawn http-fetch");
1820 if (read_in_full(cmd.out, packname, 5) < 0 ||
1821 memcmp(packname, "keep\t", 5))
1822 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1824 if (read_in_full(cmd.out, packname,
1825 the_hash_algo->hexsz + 1) < 0 ||
1826 packname[the_hash_algo->hexsz] != '\n')
1827 die("fetch-pack: expected hash then LF at end of http-fetch output");
1829 packname[the_hash_algo->hexsz] = '\0';
1831 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1833 close(cmd.out);
1835 if (finish_command(&cmd))
1836 die("fetch-pack: unable to finish http-fetch");
1838 if (memcmp(packfile_uris.items[i].string, packname,
1839 the_hash_algo->hexsz))
1840 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1841 uri, (int) the_hash_algo->hexsz,
1842 packfile_uris.items[i].string);
1844 string_list_append_nodup(pack_lockfiles,
1845 xstrfmt("%s/pack/pack-%s.keep",
1846 get_object_directory(),
1847 packname));
1849 string_list_clear(&packfile_uris, 0);
1850 strvec_clear(&index_pack_args);
1852 if (fsck_finish(&fsck_options))
1853 die("fsck failed");
1855 if (negotiator)
1856 negotiator->release(negotiator);
1858 oidset_clear(&common);
1859 return ref;
1862 static int fetch_pack_config_cb(const char *var, const char *value,
1863 const struct config_context *ctx, void *cb)
1865 const char *msg_id;
1867 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1868 const char *path;
1870 if (git_config_pathname(&path, var, value))
1871 return 1;
1872 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1873 fsck_msg_types.len ? ',' : '=', path);
1874 free((char *)path);
1875 return 0;
1878 if (skip_prefix(var, "fetch.fsck.", &msg_id)) {
1879 if (!value)
1880 return config_error_nonbool(var);
1881 if (is_valid_msg_type(msg_id, value))
1882 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1883 fsck_msg_types.len ? ',' : '=', msg_id, value);
1884 else
1885 warning("Skipping unknown msg id '%s'", msg_id);
1886 return 0;
1889 return git_default_config(var, value, ctx, cb);
1892 static void fetch_pack_config(void)
1894 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1895 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1896 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1897 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1898 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1899 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1900 if (!uri_protocols.nr) {
1901 char *str;
1903 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1904 string_list_split(&uri_protocols, str, ',', -1);
1905 free(str);
1909 git_config(fetch_pack_config_cb, NULL);
1912 static void fetch_pack_setup(void)
1914 static int did_setup;
1915 if (did_setup)
1916 return;
1917 fetch_pack_config();
1918 if (0 <= fetch_unpack_limit)
1919 unpack_limit = fetch_unpack_limit;
1920 else if (0 <= transfer_unpack_limit)
1921 unpack_limit = transfer_unpack_limit;
1922 did_setup = 1;
1925 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1927 struct string_list names = STRING_LIST_INIT_NODUP;
1928 int src, dst;
1930 for (src = dst = 0; src < nr; src++) {
1931 struct string_list_item *item;
1932 item = string_list_insert(&names, ref[src]->name);
1933 if (item->util)
1934 continue; /* already have it */
1935 item->util = ref[src];
1936 if (src != dst)
1937 ref[dst] = ref[src];
1938 dst++;
1940 for (src = dst; src < nr; src++)
1941 ref[src] = NULL;
1942 string_list_clear(&names, 0);
1943 return dst;
1946 static void update_shallow(struct fetch_pack_args *args,
1947 struct ref **sought, int nr_sought,
1948 struct shallow_info *si)
1950 struct oid_array ref = OID_ARRAY_INIT;
1951 int *status;
1952 int i;
1954 if (args->deepen && alternate_shallow_file) {
1955 if (*alternate_shallow_file == '\0') { /* --unshallow */
1956 unlink_or_warn(git_path_shallow(the_repository));
1957 rollback_shallow_file(the_repository, &shallow_lock);
1958 } else
1959 commit_shallow_file(the_repository, &shallow_lock);
1960 alternate_shallow_file = NULL;
1961 return;
1964 if (!si->shallow || !si->shallow->nr)
1965 return;
1967 if (args->cloning) {
1969 * remote is shallow, but this is a clone, there are
1970 * no objects in repo to worry about. Accept any
1971 * shallow points that exist in the pack (iow in repo
1972 * after get_pack() and reprepare_packed_git())
1974 struct oid_array extra = OID_ARRAY_INIT;
1975 struct object_id *oid = si->shallow->oid;
1976 for (i = 0; i < si->shallow->nr; i++)
1977 if (repo_has_object_file(the_repository, &oid[i]))
1978 oid_array_append(&extra, &oid[i]);
1979 if (extra.nr) {
1980 setup_alternate_shallow(&shallow_lock,
1981 &alternate_shallow_file,
1982 &extra);
1983 commit_shallow_file(the_repository, &shallow_lock);
1984 alternate_shallow_file = NULL;
1986 oid_array_clear(&extra);
1987 return;
1990 if (!si->nr_ours && !si->nr_theirs)
1991 return;
1993 remove_nonexistent_theirs_shallow(si);
1994 if (!si->nr_ours && !si->nr_theirs)
1995 return;
1996 for (i = 0; i < nr_sought; i++)
1997 oid_array_append(&ref, &sought[i]->old_oid);
1998 si->ref = &ref;
2000 if (args->update_shallow) {
2002 * remote is also shallow, .git/shallow may be updated
2003 * so all refs can be accepted. Make sure we only add
2004 * shallow roots that are actually reachable from new
2005 * refs.
2007 struct oid_array extra = OID_ARRAY_INIT;
2008 struct object_id *oid = si->shallow->oid;
2009 assign_shallow_commits_to_refs(si, NULL, NULL);
2010 if (!si->nr_ours && !si->nr_theirs) {
2011 oid_array_clear(&ref);
2012 return;
2014 for (i = 0; i < si->nr_ours; i++)
2015 oid_array_append(&extra, &oid[si->ours[i]]);
2016 for (i = 0; i < si->nr_theirs; i++)
2017 oid_array_append(&extra, &oid[si->theirs[i]]);
2018 setup_alternate_shallow(&shallow_lock,
2019 &alternate_shallow_file,
2020 &extra);
2021 commit_shallow_file(the_repository, &shallow_lock);
2022 oid_array_clear(&extra);
2023 oid_array_clear(&ref);
2024 alternate_shallow_file = NULL;
2025 return;
2029 * remote is also shallow, check what ref is safe to update
2030 * without updating .git/shallow
2032 CALLOC_ARRAY(status, nr_sought);
2033 assign_shallow_commits_to_refs(si, NULL, status);
2034 if (si->nr_ours || si->nr_theirs) {
2035 for (i = 0; i < nr_sought; i++)
2036 if (status[i])
2037 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
2039 free(status);
2040 oid_array_clear(&ref);
2043 static const struct object_id *iterate_ref_map(void *cb_data)
2045 struct ref **rm = cb_data;
2046 struct ref *ref = *rm;
2048 if (!ref)
2049 return NULL;
2050 *rm = ref->next;
2051 return &ref->old_oid;
2054 struct ref *fetch_pack(struct fetch_pack_args *args,
2055 int fd[],
2056 const struct ref *ref,
2057 struct ref **sought, int nr_sought,
2058 struct oid_array *shallow,
2059 struct string_list *pack_lockfiles,
2060 enum protocol_version version)
2062 struct ref *ref_cpy;
2063 struct shallow_info si;
2064 struct oid_array shallows_scratch = OID_ARRAY_INIT;
2066 fetch_pack_setup();
2067 if (nr_sought)
2068 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
2070 if (version != protocol_v2 && !ref) {
2071 packet_flush(fd[1]);
2072 die(_("no matching remote head"));
2074 if (version == protocol_v2) {
2075 if (shallow->nr)
2076 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2077 memset(&si, 0, sizeof(si));
2078 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
2079 &shallows_scratch, &si,
2080 pack_lockfiles);
2081 } else {
2082 prepare_shallow_info(&si, shallow);
2083 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
2084 &si, pack_lockfiles);
2086 reprepare_packed_git(the_repository);
2088 if (!args->cloning && args->deepen) {
2089 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2090 struct ref *iterator = ref_cpy;
2091 opt.shallow_file = alternate_shallow_file;
2092 if (args->deepen)
2093 opt.is_deepening_fetch = 1;
2094 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2095 error(_("remote did not send all necessary objects"));
2096 free_refs(ref_cpy);
2097 ref_cpy = NULL;
2098 rollback_shallow_file(the_repository, &shallow_lock);
2099 goto cleanup;
2101 args->connectivity_checked = 1;
2104 update_shallow(args, sought, nr_sought, &si);
2105 cleanup:
2106 clear_shallow_info(&si);
2107 oid_array_clear(&shallows_scratch);
2108 return ref_cpy;
2111 static int add_to_object_array(const struct object_id *oid, void *data)
2113 struct object_array *a = data;
2115 add_object_array(lookup_object(the_repository, oid), "", a);
2116 return 0;
2119 static void clear_common_flag(struct oidset *s)
2121 struct oidset_iter iter;
2122 const struct object_id *oid;
2123 oidset_iter_init(s, &iter);
2125 while ((oid = oidset_iter_next(&iter))) {
2126 struct object *obj = lookup_object(the_repository, oid);
2127 obj->flags &= ~COMMON;
2131 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2132 const struct string_list *server_options,
2133 int stateless_rpc,
2134 int fd[],
2135 struct oidset *acked_commits)
2137 struct fetch_negotiator negotiator;
2138 struct packet_reader reader;
2139 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2140 struct strbuf req_buf = STRBUF_INIT;
2141 int haves_to_send = INITIAL_FLUSH;
2142 int in_vain = 0;
2143 int seen_ack = 0;
2144 int last_iteration = 0;
2145 int negotiation_round = 0;
2146 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2148 fetch_negotiator_init(the_repository, &negotiator);
2149 mark_tips(&negotiator, negotiation_tips);
2151 packet_reader_init(&reader, fd[0], NULL, 0,
2152 PACKET_READ_CHOMP_NEWLINE |
2153 PACKET_READ_DIE_ON_ERR_PACKET);
2155 oid_array_for_each((struct oid_array *) negotiation_tips,
2156 add_to_object_array,
2157 &nt_object_array);
2159 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2160 while (!last_iteration) {
2161 int haves_added;
2162 struct object_id common_oid;
2163 int received_ready = 0;
2165 negotiation_round++;
2167 trace2_region_enter_printf("negotiate_using_fetch", "round",
2168 the_repository, "%d",
2169 negotiation_round);
2170 strbuf_reset(&req_buf);
2171 write_fetch_command_and_capabilities(&req_buf, server_options);
2173 packet_buf_write(&req_buf, "wait-for-done");
2175 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2176 in_vain += haves_added;
2177 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2178 last_iteration = 1;
2180 trace2_data_intmax("negotiate_using_fetch", the_repository,
2181 "haves_added", haves_added);
2182 trace2_data_intmax("negotiate_using_fetch", the_repository,
2183 "in_vain", in_vain);
2185 /* Send request */
2186 packet_buf_flush(&req_buf);
2187 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2188 die_errno(_("unable to write request to remote"));
2190 /* Process ACKs/NAKs */
2191 process_section_header(&reader, "acknowledgments", 0);
2192 while (process_ack(&negotiator, &reader, &common_oid,
2193 &received_ready)) {
2194 struct commit *commit = lookup_commit(the_repository,
2195 &common_oid);
2196 if (commit) {
2197 timestamp_t generation;
2199 parse_commit_or_die(commit);
2200 commit->object.flags |= COMMON;
2201 generation = commit_graph_generation(commit);
2202 if (generation < min_generation)
2203 min_generation = generation;
2205 in_vain = 0;
2206 seen_ack = 1;
2207 oidset_insert(acked_commits, &common_oid);
2209 if (received_ready)
2210 die(_("unexpected 'ready' from remote"));
2211 else
2212 do_check_stateless_delimiter(stateless_rpc, &reader);
2213 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2214 REACH_SCRATCH, 0,
2215 min_generation))
2216 last_iteration = 1;
2217 trace2_region_leave_printf("negotiation", "round",
2218 the_repository, "%d",
2219 negotiation_round);
2221 trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository);
2222 trace2_data_intmax("negotiate_using_fetch", the_repository,
2223 "total_rounds", negotiation_round);
2224 clear_common_flag(acked_commits);
2225 strbuf_release(&req_buf);
2228 int report_unmatched_refs(struct ref **sought, int nr_sought)
2230 int i, ret = 0;
2232 for (i = 0; i < nr_sought; i++) {
2233 if (!sought[i])
2234 continue;
2235 switch (sought[i]->match_status) {
2236 case REF_MATCHED:
2237 continue;
2238 case REF_NOT_MATCHED:
2239 error(_("no such remote ref %s"), sought[i]->name);
2240 break;
2241 case REF_UNADVERTISED_NOT_ALLOWED:
2242 error(_("Server does not allow request for unadvertised object %s"),
2243 sought[i]->name);
2244 break;
2246 ret = 1;
2248 return ret;