notes doc: split up run-on sentences
[git.git] / fetch-pack.c
blob368f2ed25a1c40f6ab55f0b1098a8722ce930c18
1 #include "cache.h"
2 #include "alloc.h"
3 #include "repository.h"
4 #include "config.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 "exec-cmd.h"
14 #include "pack.h"
15 #include "sideband.h"
16 #include "fetch-pack.h"
17 #include "remote.h"
18 #include "run-command.h"
19 #include "connect.h"
20 #include "transport.h"
21 #include "version.h"
22 #include "oid-array.h"
23 #include "oidset.h"
24 #include "packfile.h"
25 #include "object-store.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"
34 #include "wrapper.h"
36 static int transfer_unpack_limit = -1;
37 static int fetch_unpack_limit = -1;
38 static int unpack_limit = 100;
39 static int prefer_ofs_delta = 1;
40 static int no_done;
41 static int deepen_since_ok;
42 static int deepen_not_ok;
43 static int fetch_fsck_objects = -1;
44 static int transfer_fsck_objects = -1;
45 static int agent_supported;
46 static int server_supports_filtering;
47 static int advertise_sid;
48 static struct shallow_lock shallow_lock;
49 static const char *alternate_shallow_file;
50 static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
51 static struct strbuf fsck_msg_types = STRBUF_INIT;
52 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
54 /* Remember to update object flag allocation in object.h */
55 #define COMPLETE (1U << 0)
56 #define ALTERNATE (1U << 1)
57 #define COMMON (1U << 6)
58 #define REACH_SCRATCH (1U << 7)
61 * After sending this many "have"s if we do not get any new ACK , we
62 * give up traversing our history.
64 #define MAX_IN_VAIN 256
66 static int multi_ack, use_sideband;
67 /* Allow specifying sha1 if it is a ref tip. */
68 #define ALLOW_TIP_SHA1 01
69 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
70 #define ALLOW_REACHABLE_SHA1 02
71 static unsigned int allow_unadvertised_object_request;
73 __attribute__((format (printf, 2, 3)))
74 static inline void print_verbose(const struct fetch_pack_args *args,
75 const char *fmt, ...)
77 va_list params;
79 if (!args->verbose)
80 return;
82 va_start(params, fmt);
83 vfprintf(stderr, fmt, params);
84 va_end(params);
85 fputc('\n', stderr);
88 struct alternate_object_cache {
89 struct object **items;
90 size_t nr, alloc;
93 static void cache_one_alternate(const struct object_id *oid,
94 void *vcache)
96 struct alternate_object_cache *cache = vcache;
97 struct object *obj = parse_object(the_repository, oid);
99 if (!obj || (obj->flags & ALTERNATE))
100 return;
102 obj->flags |= ALTERNATE;
103 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
104 cache->items[cache->nr++] = obj;
107 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
108 void (*cb)(struct fetch_negotiator *,
109 struct object *))
111 static int initialized;
112 static struct alternate_object_cache cache;
113 size_t i;
115 if (!initialized) {
116 for_each_alternate_ref(cache_one_alternate, &cache);
117 initialized = 1;
120 for (i = 0; i < cache.nr; i++)
121 cb(negotiator, cache.items[i]);
124 static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid,
125 int mark_tags_complete,
126 enum object_type *type,
127 unsigned int oi_flags)
129 struct object_info info = { .typep = type };
130 struct commit *commit;
132 commit = lookup_commit_in_graph(the_repository, oid);
133 if (commit)
134 return commit;
136 while (1) {
137 if (oid_object_info_extended(the_repository, oid, &info,
138 oi_flags))
139 return NULL;
140 if (*type == OBJ_TAG) {
141 struct tag *tag = (struct tag *)
142 parse_object(the_repository, oid);
144 if (!tag->tagged)
145 return NULL;
146 if (mark_tags_complete)
147 tag->object.flags |= COMPLETE;
148 oid = &tag->tagged->oid;
149 } else {
150 break;
154 if (*type == OBJ_COMMIT) {
155 struct commit *commit = lookup_commit(the_repository, oid);
156 if (!commit || repo_parse_commit(the_repository, commit))
157 return NULL;
158 return commit;
161 return NULL;
165 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
166 int mark_tags_complete)
168 enum object_type type;
169 unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK;
170 return deref_without_lazy_fetch_extended(oid, mark_tags_complete,
171 &type, flags);
174 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
175 const struct object_id *oid)
177 struct commit *c = deref_without_lazy_fetch(oid, 0);
179 if (c)
180 negotiator->add_tip(negotiator, c);
181 return 0;
184 static int rev_list_insert_ref_oid(const char *refname UNUSED,
185 const struct object_id *oid,
186 int flag UNUSED,
187 void *cb_data)
189 return rev_list_insert_ref(cb_data, oid);
192 enum ack_type {
193 NAK = 0,
194 ACK,
195 ACK_continue,
196 ACK_common,
197 ACK_ready
200 static void consume_shallow_list(struct fetch_pack_args *args,
201 struct packet_reader *reader)
203 if (args->stateless_rpc && args->deepen) {
204 /* If we sent a depth we will get back "duplicate"
205 * shallow and unshallow commands every time there
206 * is a block of have lines exchanged.
208 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
209 if (starts_with(reader->line, "shallow "))
210 continue;
211 if (starts_with(reader->line, "unshallow "))
212 continue;
213 die(_("git fetch-pack: expected shallow list"));
215 if (reader->status != PACKET_READ_FLUSH)
216 die(_("git fetch-pack: expected a flush packet after shallow list"));
220 static enum ack_type get_ack(struct packet_reader *reader,
221 struct object_id *result_oid)
223 int len;
224 const char *arg;
226 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
227 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
228 len = reader->pktlen;
230 if (!strcmp(reader->line, "NAK"))
231 return NAK;
232 if (skip_prefix(reader->line, "ACK ", &arg)) {
233 const char *p;
234 if (!parse_oid_hex(arg, result_oid, &p)) {
235 len -= p - reader->line;
236 if (len < 1)
237 return ACK;
238 if (strstr(p, "continue"))
239 return ACK_continue;
240 if (strstr(p, "common"))
241 return ACK_common;
242 if (strstr(p, "ready"))
243 return ACK_ready;
244 return ACK;
247 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
250 static void send_request(struct fetch_pack_args *args,
251 int fd, struct strbuf *buf)
253 if (args->stateless_rpc) {
254 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
255 packet_flush(fd);
256 } else {
257 if (write_in_full(fd, buf->buf, buf->len) < 0)
258 die_errno(_("unable to write to remote"));
262 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
263 struct object *obj)
265 rev_list_insert_ref(negotiator, &obj->oid);
268 #define INITIAL_FLUSH 16
269 #define PIPESAFE_FLUSH 32
270 #define LARGE_FLUSH 16384
272 static int next_flush(int stateless_rpc, int count)
274 if (stateless_rpc) {
275 if (count < LARGE_FLUSH)
276 count <<= 1;
277 else
278 count = count * 11 / 10;
279 } else {
280 if (count < PIPESAFE_FLUSH)
281 count <<= 1;
282 else
283 count += PIPESAFE_FLUSH;
285 return count;
288 static void mark_tips(struct fetch_negotiator *negotiator,
289 const struct oid_array *negotiation_tips)
291 int i;
293 if (!negotiation_tips) {
294 for_each_rawref(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 for_each_rawref(mark_complete_oid, NULL);
798 for_each_cached_alternate(NULL, mark_alternate_complete);
799 commit_list_sort_by_date(&complete);
800 if (cutoff)
801 mark_recent_complete_commits(args, cutoff);
803 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
806 * Mark all complete remote refs as common refs.
807 * Don't mark them common yet; the server has to be told so first.
809 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
810 for (ref = *refs; ref; ref = ref->next) {
811 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
813 if (!c || !(c->object.flags & COMPLETE))
814 continue;
816 negotiator->known_common(negotiator, c);
818 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
820 save_commit_buffer = old_save_commit_buffer;
824 * Returns 1 if every object pointed to by the given remote refs is available
825 * locally and reachable from a local ref, and 0 otherwise.
827 static int everything_local(struct fetch_pack_args *args,
828 struct ref **refs)
830 struct ref *ref;
831 int retval;
833 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
834 const struct object_id *remote = &ref->old_oid;
835 struct object *o;
837 o = lookup_object(the_repository, remote);
838 if (!o || !(o->flags & COMPLETE)) {
839 retval = 0;
840 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
841 ref->name);
842 continue;
844 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
845 ref->name);
848 return retval;
851 static int sideband_demux(int in UNUSED, int out, void *data)
853 int *xd = data;
854 int ret;
856 ret = recv_sideband("fetch-pack", xd[0], out);
857 close(out);
858 return ret;
861 static void create_promisor_file(const char *keep_name,
862 struct ref **sought, int nr_sought)
864 struct strbuf promisor_name = STRBUF_INIT;
865 int suffix_stripped;
867 strbuf_addstr(&promisor_name, keep_name);
868 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
869 if (!suffix_stripped)
870 BUG("name of pack lockfile should end with .keep (was '%s')",
871 keep_name);
872 strbuf_addstr(&promisor_name, ".promisor");
874 write_promisor_file(promisor_name.buf, sought, nr_sought);
876 strbuf_release(&promisor_name);
879 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
881 int len = the_hash_algo->hexsz + 1; /* hash + NL */
883 do {
884 char hex_hash[GIT_MAX_HEXSZ + 1];
885 int read_len = read_in_full(fd, hex_hash, len);
886 struct object_id oid;
887 const char *end;
889 if (!read_len)
890 return;
891 if (read_len != len)
892 die("invalid length read %d", read_len);
893 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
894 die("invalid hash");
895 oidset_insert(gitmodules_oids, &oid);
896 } while (1);
899 static void add_index_pack_keep_option(struct strvec *args)
901 char hostname[HOST_NAME_MAX + 1];
903 if (xgethostname(hostname, sizeof(hostname)))
904 xsnprintf(hostname, sizeof(hostname), "localhost");
905 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
906 (uintmax_t)getpid(), hostname);
910 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
911 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
912 * stored there. (It must be freed by the caller.)
914 static int get_pack(struct fetch_pack_args *args,
915 int xd[2], struct string_list *pack_lockfiles,
916 struct strvec *index_pack_args,
917 struct ref **sought, int nr_sought,
918 struct oidset *gitmodules_oids)
920 struct async demux;
921 int do_keep = args->keep_pack;
922 const char *cmd_name;
923 struct pack_header header;
924 int pass_header = 0;
925 struct child_process cmd = CHILD_PROCESS_INIT;
926 int fsck_objects = 0;
927 int ret;
929 memset(&demux, 0, sizeof(demux));
930 if (use_sideband) {
931 /* xd[] is talking with upload-pack; subprocess reads from
932 * xd[0], spits out band#2 to stderr, and feeds us band#1
933 * through demux->out.
935 demux.proc = sideband_demux;
936 demux.data = xd;
937 demux.out = -1;
938 demux.isolate_sigpipe = 1;
939 if (start_async(&demux))
940 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
942 else
943 demux.out = xd[0];
945 if (!args->keep_pack && unpack_limit && !index_pack_args) {
947 if (read_pack_header(demux.out, &header))
948 die(_("protocol error: bad pack header"));
949 pass_header = 1;
950 if (ntohl(header.hdr_entries) < unpack_limit)
951 do_keep = 0;
952 else
953 do_keep = 1;
956 if (alternate_shallow_file) {
957 strvec_push(&cmd.args, "--shallow-file");
958 strvec_push(&cmd.args, alternate_shallow_file);
961 if (fetch_fsck_objects >= 0
962 ? fetch_fsck_objects
963 : transfer_fsck_objects >= 0
964 ? transfer_fsck_objects
965 : 0)
966 fsck_objects = 1;
968 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
969 if (pack_lockfiles || fsck_objects)
970 cmd.out = -1;
971 cmd_name = "index-pack";
972 strvec_push(&cmd.args, cmd_name);
973 strvec_push(&cmd.args, "--stdin");
974 if (!args->quiet && !args->no_progress)
975 strvec_push(&cmd.args, "-v");
976 if (args->use_thin_pack)
977 strvec_push(&cmd.args, "--fix-thin");
978 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
979 add_index_pack_keep_option(&cmd.args);
980 if (!index_pack_args && args->check_self_contained_and_connected)
981 strvec_push(&cmd.args, "--check-self-contained-and-connected");
982 else
984 * We cannot perform any connectivity checks because
985 * not all packs have been downloaded; let the caller
986 * have this responsibility.
988 args->check_self_contained_and_connected = 0;
990 if (args->from_promisor)
992 * create_promisor_file() may be called afterwards but
993 * we still need index-pack to know that this is a
994 * promisor pack. For example, if transfer.fsckobjects
995 * is true, index-pack needs to know that .gitmodules
996 * is a promisor object (so that it won't complain if
997 * it is missing).
999 strvec_push(&cmd.args, "--promisor");
1001 else {
1002 cmd_name = "unpack-objects";
1003 strvec_push(&cmd.args, cmd_name);
1004 if (args->quiet || args->no_progress)
1005 strvec_push(&cmd.args, "-q");
1006 args->check_self_contained_and_connected = 0;
1009 if (pass_header)
1010 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
1011 ntohl(header.hdr_version),
1012 ntohl(header.hdr_entries));
1013 if (fsck_objects) {
1014 if (args->from_promisor || index_pack_args)
1016 * We cannot use --strict in index-pack because it
1017 * checks both broken objects and links, but we only
1018 * want to check for broken objects.
1020 strvec_push(&cmd.args, "--fsck-objects");
1021 else
1022 strvec_pushf(&cmd.args, "--strict%s",
1023 fsck_msg_types.buf);
1026 if (index_pack_args) {
1027 int i;
1029 for (i = 0; i < cmd.args.nr; i++)
1030 strvec_push(index_pack_args, cmd.args.v[i]);
1033 sigchain_push(SIGPIPE, SIG_IGN);
1035 cmd.in = demux.out;
1036 cmd.git_cmd = 1;
1037 if (start_command(&cmd))
1038 die(_("fetch-pack: unable to fork off %s"), cmd_name);
1039 if (do_keep && (pack_lockfiles || fsck_objects)) {
1040 int is_well_formed;
1041 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1043 if (!is_well_formed)
1044 die(_("fetch-pack: invalid index-pack output"));
1045 if (pack_lockfile)
1046 string_list_append_nodup(pack_lockfiles, pack_lockfile);
1047 parse_gitmodules_oids(cmd.out, gitmodules_oids);
1048 close(cmd.out);
1051 if (!use_sideband)
1052 /* Closed by start_command() */
1053 xd[0] = -1;
1055 ret = finish_command(&cmd);
1056 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1057 args->self_contained_and_connected =
1058 args->check_self_contained_and_connected &&
1059 ret == 0;
1060 else
1061 die(_("%s failed"), cmd_name);
1062 if (use_sideband && finish_async(&demux))
1063 die(_("error in sideband demultiplexer"));
1065 sigchain_pop(SIGPIPE);
1068 * Now that index-pack has succeeded, write the promisor file using the
1069 * obtained .keep filename if necessary
1071 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
1072 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
1074 return 0;
1077 static int ref_compare_name(const struct ref *a, const struct ref *b)
1079 return strcmp(a->name, b->name);
1082 DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1084 static int cmp_ref_by_name(const void *a_, const void *b_)
1086 const struct ref *a = *((const struct ref **)a_);
1087 const struct ref *b = *((const struct ref **)b_);
1088 return strcmp(a->name, b->name);
1091 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1092 int fd[2],
1093 const struct ref *orig_ref,
1094 struct ref **sought, int nr_sought,
1095 struct shallow_info *si,
1096 struct string_list *pack_lockfiles)
1098 struct repository *r = the_repository;
1099 struct ref *ref = copy_ref_list(orig_ref);
1100 struct object_id oid;
1101 const char *agent_feature;
1102 int agent_len;
1103 struct fetch_negotiator negotiator_alloc;
1104 struct fetch_negotiator *negotiator;
1106 negotiator = &negotiator_alloc;
1107 if (args->refetch) {
1108 fetch_negotiator_init_noop(negotiator);
1109 } else {
1110 fetch_negotiator_init(r, negotiator);
1113 sort_ref_list(&ref, ref_compare_name);
1114 QSORT(sought, nr_sought, cmp_ref_by_name);
1116 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1117 agent_supported = 1;
1118 if (agent_len)
1119 print_verbose(args, _("Server version is %.*s"),
1120 agent_len, agent_feature);
1123 if (!server_supports("session-id"))
1124 advertise_sid = 0;
1126 if (server_supports("shallow"))
1127 print_verbose(args, _("Server supports %s"), "shallow");
1128 else if (args->depth > 0 || is_repository_shallow(r))
1129 die(_("Server does not support shallow clients"));
1130 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1131 args->deepen = 1;
1132 if (server_supports("multi_ack_detailed")) {
1133 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1134 multi_ack = 2;
1135 if (server_supports("no-done")) {
1136 print_verbose(args, _("Server supports %s"), "no-done");
1137 if (args->stateless_rpc)
1138 no_done = 1;
1141 else if (server_supports("multi_ack")) {
1142 print_verbose(args, _("Server supports %s"), "multi_ack");
1143 multi_ack = 1;
1145 if (server_supports("side-band-64k")) {
1146 print_verbose(args, _("Server supports %s"), "side-band-64k");
1147 use_sideband = 2;
1149 else if (server_supports("side-band")) {
1150 print_verbose(args, _("Server supports %s"), "side-band");
1151 use_sideband = 1;
1153 if (server_supports("allow-tip-sha1-in-want")) {
1154 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1155 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1157 if (server_supports("allow-reachable-sha1-in-want")) {
1158 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1159 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1161 if (server_supports("thin-pack"))
1162 print_verbose(args, _("Server supports %s"), "thin-pack");
1163 else
1164 args->use_thin_pack = 0;
1165 if (server_supports("no-progress"))
1166 print_verbose(args, _("Server supports %s"), "no-progress");
1167 else
1168 args->no_progress = 0;
1169 if (server_supports("include-tag"))
1170 print_verbose(args, _("Server supports %s"), "include-tag");
1171 else
1172 args->include_tag = 0;
1173 if (server_supports("ofs-delta"))
1174 print_verbose(args, _("Server supports %s"), "ofs-delta");
1175 else
1176 prefer_ofs_delta = 0;
1178 if (server_supports("filter")) {
1179 server_supports_filtering = 1;
1180 print_verbose(args, _("Server supports %s"), "filter");
1181 } else if (args->filter_options.choice) {
1182 warning("filtering not recognized by server, ignoring");
1185 if (server_supports("deepen-since")) {
1186 print_verbose(args, _("Server supports %s"), "deepen-since");
1187 deepen_since_ok = 1;
1188 } else if (args->deepen_since)
1189 die(_("Server does not support --shallow-since"));
1190 if (server_supports("deepen-not")) {
1191 print_verbose(args, _("Server supports %s"), "deepen-not");
1192 deepen_not_ok = 1;
1193 } else if (args->deepen_not)
1194 die(_("Server does not support --shallow-exclude"));
1195 if (server_supports("deepen-relative"))
1196 print_verbose(args, _("Server supports %s"), "deepen-relative");
1197 else if (args->deepen_relative)
1198 die(_("Server does not support --deepen"));
1199 if (!server_supports_hash(the_hash_algo->name, NULL))
1200 die(_("Server does not support this repository's object format"));
1202 mark_complete_and_common_ref(negotiator, args, &ref);
1203 filter_refs(args, &ref, sought, nr_sought);
1204 if (!args->refetch && everything_local(args, &ref)) {
1205 packet_flush(fd[1]);
1206 goto all_done;
1208 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1209 if (!args->keep_pack)
1210 /* When cloning, it is not unusual to have
1211 * no common commit.
1213 warning(_("no common commits"));
1215 if (args->stateless_rpc)
1216 packet_flush(fd[1]);
1217 if (args->deepen)
1218 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1219 NULL);
1220 else if (si->nr_ours || si->nr_theirs) {
1221 if (args->reject_shallow_remote)
1222 die(_("source repository is shallow, reject to clone."));
1223 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1224 } else
1225 alternate_shallow_file = NULL;
1226 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1227 &fsck_options.gitmodules_found))
1228 die(_("git fetch-pack: fetch failed."));
1229 if (fsck_finish(&fsck_options))
1230 die("fsck failed");
1232 all_done:
1233 if (negotiator)
1234 negotiator->release(negotiator);
1235 return ref;
1238 static void add_shallow_requests(struct strbuf *req_buf,
1239 const struct fetch_pack_args *args)
1241 if (is_repository_shallow(the_repository))
1242 write_shallow_commits(req_buf, 1, NULL);
1243 if (args->depth > 0)
1244 packet_buf_write(req_buf, "deepen %d", args->depth);
1245 if (args->deepen_since) {
1246 timestamp_t max_age = approxidate(args->deepen_since);
1247 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1249 if (args->deepen_not) {
1250 int i;
1251 for (i = 0; i < args->deepen_not->nr; i++) {
1252 struct string_list_item *s = args->deepen_not->items + i;
1253 packet_buf_write(req_buf, "deepen-not %s", s->string);
1256 if (args->deepen_relative)
1257 packet_buf_write(req_buf, "deepen-relative\n");
1260 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1262 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1264 for ( ; wants ; wants = wants->next) {
1265 const struct object_id *remote = &wants->old_oid;
1266 struct object *o;
1269 * If that object is complete (i.e. it is an ancestor of a
1270 * local ref), we tell them we have it but do not have to
1271 * tell them about its ancestors, which they already know
1272 * about.
1274 * We use lookup_object here because we are only
1275 * interested in the case we *know* the object is
1276 * reachable and we have already scanned it.
1278 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1279 (o->flags & COMPLETE)) {
1280 continue;
1283 if (!use_ref_in_want || wants->exact_oid)
1284 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1285 else
1286 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1290 static void add_common(struct strbuf *req_buf, struct oidset *common)
1292 struct oidset_iter iter;
1293 const struct object_id *oid;
1294 oidset_iter_init(common, &iter);
1296 while ((oid = oidset_iter_next(&iter))) {
1297 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1301 static int add_haves(struct fetch_negotiator *negotiator,
1302 struct strbuf *req_buf,
1303 int *haves_to_send)
1305 int haves_added = 0;
1306 const struct object_id *oid;
1308 while ((oid = negotiator->next(negotiator))) {
1309 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1310 if (++haves_added >= *haves_to_send)
1311 break;
1314 /* Increase haves to send on next round */
1315 *haves_to_send = next_flush(1, *haves_to_send);
1317 return haves_added;
1320 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1321 const struct string_list *server_options)
1323 const char *hash_name;
1325 ensure_server_supports_v2("fetch");
1326 packet_buf_write(req_buf, "command=fetch");
1327 if (server_supports_v2("agent"))
1328 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1329 if (advertise_sid && server_supports_v2("session-id"))
1330 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1331 if (server_options && server_options->nr) {
1332 int i;
1333 ensure_server_supports_v2("server-option");
1334 for (i = 0; i < server_options->nr; i++)
1335 packet_buf_write(req_buf, "server-option=%s",
1336 server_options->items[i].string);
1339 if (server_feature_v2("object-format", &hash_name)) {
1340 int hash_algo = hash_algo_by_name(hash_name);
1341 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1342 die(_("mismatched algorithms: client %s; server %s"),
1343 the_hash_algo->name, hash_name);
1344 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1345 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1346 die(_("the server does not support algorithm '%s'"),
1347 the_hash_algo->name);
1349 packet_buf_delim(req_buf);
1352 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1353 struct fetch_pack_args *args,
1354 const struct ref *wants, struct oidset *common,
1355 int *haves_to_send, int *in_vain,
1356 int sideband_all, int seen_ack)
1358 int haves_added;
1359 int done_sent = 0;
1360 struct strbuf req_buf = STRBUF_INIT;
1362 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1364 if (args->use_thin_pack)
1365 packet_buf_write(&req_buf, "thin-pack");
1366 if (args->no_progress)
1367 packet_buf_write(&req_buf, "no-progress");
1368 if (args->include_tag)
1369 packet_buf_write(&req_buf, "include-tag");
1370 if (prefer_ofs_delta)
1371 packet_buf_write(&req_buf, "ofs-delta");
1372 if (sideband_all)
1373 packet_buf_write(&req_buf, "sideband-all");
1375 /* Add shallow-info and deepen request */
1376 if (server_supports_feature("fetch", "shallow", 0))
1377 add_shallow_requests(&req_buf, args);
1378 else if (is_repository_shallow(the_repository) || args->deepen)
1379 die(_("Server does not support shallow requests"));
1381 /* Add filter */
1382 send_filter(args, &req_buf,
1383 server_supports_feature("fetch", "filter", 0));
1385 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1386 int i;
1387 struct strbuf to_send = STRBUF_INIT;
1389 for (i = 0; i < uri_protocols.nr; i++) {
1390 const char *s = uri_protocols.items[i].string;
1392 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1393 if (to_send.len)
1394 strbuf_addch(&to_send, ',');
1395 strbuf_addstr(&to_send, s);
1398 if (to_send.len) {
1399 packet_buf_write(&req_buf, "packfile-uris %s",
1400 to_send.buf);
1401 strbuf_release(&to_send);
1405 /* add wants */
1406 add_wants(wants, &req_buf);
1408 /* Add all of the common commits we've found in previous rounds */
1409 add_common(&req_buf, common);
1411 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1412 *in_vain += haves_added;
1413 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1414 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
1415 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1416 /* Send Done */
1417 packet_buf_write(&req_buf, "done\n");
1418 done_sent = 1;
1421 /* Send request */
1422 packet_buf_flush(&req_buf);
1423 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1424 die_errno(_("unable to write request to remote"));
1426 strbuf_release(&req_buf);
1427 return done_sent;
1431 * Processes a section header in a server's response and checks if it matches
1432 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1433 * not consumed); if 0, the line will be consumed and the function will die if
1434 * the section header doesn't match what was expected.
1436 static int process_section_header(struct packet_reader *reader,
1437 const char *section, int peek)
1439 int ret = 0;
1441 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1442 !strcmp(reader->line, section))
1443 ret = 1;
1445 if (!peek) {
1446 if (!ret) {
1447 if (reader->line)
1448 die(_("expected '%s', received '%s'"),
1449 section, reader->line);
1450 else
1451 die(_("expected '%s'"), section);
1453 packet_reader_read(reader);
1456 return ret;
1459 static int process_ack(struct fetch_negotiator *negotiator,
1460 struct packet_reader *reader,
1461 struct object_id *common_oid,
1462 int *received_ready)
1464 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1465 const char *arg;
1467 if (!strcmp(reader->line, "NAK"))
1468 continue;
1470 if (skip_prefix(reader->line, "ACK ", &arg)) {
1471 if (!get_oid_hex(arg, common_oid)) {
1472 struct commit *commit;
1473 commit = lookup_commit(the_repository, common_oid);
1474 if (negotiator)
1475 negotiator->ack(negotiator, commit);
1477 return 1;
1480 if (!strcmp(reader->line, "ready")) {
1481 *received_ready = 1;
1482 continue;
1485 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1488 if (reader->status != PACKET_READ_FLUSH &&
1489 reader->status != PACKET_READ_DELIM)
1490 die(_("error processing acks: %d"), reader->status);
1493 * If an "acknowledgments" section is sent, a packfile is sent if and
1494 * only if "ready" was sent in this section. The other sections
1495 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1496 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1497 * otherwise.
1499 if (*received_ready && reader->status != PACKET_READ_DELIM)
1501 * TRANSLATORS: The parameter will be 'ready', a protocol
1502 * keyword.
1504 die(_("expected packfile to be sent after '%s'"), "ready");
1505 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1507 * TRANSLATORS: The parameter will be 'ready', a protocol
1508 * keyword.
1510 die(_("expected no other sections to be sent after no '%s'"), "ready");
1512 return 0;
1515 static void receive_shallow_info(struct fetch_pack_args *args,
1516 struct packet_reader *reader,
1517 struct oid_array *shallows,
1518 struct shallow_info *si)
1520 int unshallow_received = 0;
1522 process_section_header(reader, "shallow-info", 0);
1523 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1524 const char *arg;
1525 struct object_id oid;
1527 if (skip_prefix(reader->line, "shallow ", &arg)) {
1528 if (get_oid_hex(arg, &oid))
1529 die(_("invalid shallow line: %s"), reader->line);
1530 oid_array_append(shallows, &oid);
1531 continue;
1533 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1534 if (get_oid_hex(arg, &oid))
1535 die(_("invalid unshallow line: %s"), reader->line);
1536 if (!lookup_object(the_repository, &oid))
1537 die(_("object not found: %s"), reader->line);
1538 /* make sure that it is parsed as shallow */
1539 if (!parse_object(the_repository, &oid))
1540 die(_("error in object: %s"), reader->line);
1541 if (unregister_shallow(&oid))
1542 die(_("no shallow found: %s"), reader->line);
1543 unshallow_received = 1;
1544 continue;
1546 die(_("expected shallow/unshallow, got %s"), reader->line);
1549 if (reader->status != PACKET_READ_FLUSH &&
1550 reader->status != PACKET_READ_DELIM)
1551 die(_("error processing shallow info: %d"), reader->status);
1553 if (args->deepen || unshallow_received) {
1555 * Treat these as shallow lines caused by our depth settings.
1556 * In v0, these lines cannot cause refs to be rejected; do the
1557 * same.
1559 int i;
1561 for (i = 0; i < shallows->nr; i++)
1562 register_shallow(the_repository, &shallows->oid[i]);
1563 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1564 NULL);
1565 args->deepen = 1;
1566 } else if (shallows->nr) {
1568 * Treat these as shallow lines caused by the remote being
1569 * shallow. In v0, remote refs that reach these objects are
1570 * rejected (unless --update-shallow is set); do the same.
1572 prepare_shallow_info(si, shallows);
1573 if (si->nr_ours || si->nr_theirs) {
1574 if (args->reject_shallow_remote)
1575 die(_("source repository is shallow, reject to clone."));
1576 alternate_shallow_file =
1577 setup_temporary_shallow(si->shallow);
1578 } else
1579 alternate_shallow_file = NULL;
1580 } else {
1581 alternate_shallow_file = NULL;
1585 static int cmp_name_ref(const void *name, const void *ref)
1587 return strcmp(name, (*(struct ref **)ref)->name);
1590 static void receive_wanted_refs(struct packet_reader *reader,
1591 struct ref **sought, int nr_sought)
1593 process_section_header(reader, "wanted-refs", 0);
1594 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1595 struct object_id oid;
1596 const char *end;
1597 struct ref **found;
1599 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1600 die(_("expected wanted-ref, got '%s'"), reader->line);
1602 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1603 cmp_name_ref);
1604 if (!found)
1605 die(_("unexpected wanted-ref: '%s'"), reader->line);
1606 oidcpy(&(*found)->old_oid, &oid);
1609 if (reader->status != PACKET_READ_DELIM)
1610 die(_("error processing wanted refs: %d"), reader->status);
1613 static void receive_packfile_uris(struct packet_reader *reader,
1614 struct string_list *uris)
1616 process_section_header(reader, "packfile-uris", 0);
1617 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1618 if (reader->pktlen < the_hash_algo->hexsz ||
1619 reader->line[the_hash_algo->hexsz] != ' ')
1620 die("expected '<hash> <uri>', got: %s\n", reader->line);
1622 string_list_append(uris, reader->line);
1624 if (reader->status != PACKET_READ_DELIM)
1625 die("expected DELIM");
1628 enum fetch_state {
1629 FETCH_CHECK_LOCAL = 0,
1630 FETCH_SEND_REQUEST,
1631 FETCH_PROCESS_ACKS,
1632 FETCH_GET_PACK,
1633 FETCH_DONE,
1636 static void do_check_stateless_delimiter(int stateless_rpc,
1637 struct packet_reader *reader)
1639 check_stateless_delimiter(stateless_rpc, reader,
1640 _("git fetch-pack: expected response end packet"));
1643 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1644 int fd[2],
1645 const struct ref *orig_ref,
1646 struct ref **sought, int nr_sought,
1647 struct oid_array *shallows,
1648 struct shallow_info *si,
1649 struct string_list *pack_lockfiles)
1651 struct repository *r = the_repository;
1652 struct ref *ref = copy_ref_list(orig_ref);
1653 enum fetch_state state = FETCH_CHECK_LOCAL;
1654 struct oidset common = OIDSET_INIT;
1655 struct packet_reader reader;
1656 int in_vain = 0, negotiation_started = 0;
1657 int negotiation_round = 0;
1658 int haves_to_send = INITIAL_FLUSH;
1659 struct fetch_negotiator negotiator_alloc;
1660 struct fetch_negotiator *negotiator;
1661 int seen_ack = 0;
1662 struct object_id common_oid;
1663 int received_ready = 0;
1664 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1665 int i;
1666 struct strvec index_pack_args = STRVEC_INIT;
1668 negotiator = &negotiator_alloc;
1669 if (args->refetch)
1670 fetch_negotiator_init_noop(negotiator);
1671 else
1672 fetch_negotiator_init(r, negotiator);
1674 packet_reader_init(&reader, fd[0], NULL, 0,
1675 PACKET_READ_CHOMP_NEWLINE |
1676 PACKET_READ_DIE_ON_ERR_PACKET);
1677 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1678 server_supports_feature("fetch", "sideband-all", 0)) {
1679 reader.use_sideband = 1;
1680 reader.me = "fetch-pack";
1683 while (state != FETCH_DONE) {
1684 switch (state) {
1685 case FETCH_CHECK_LOCAL:
1686 sort_ref_list(&ref, ref_compare_name);
1687 QSORT(sought, nr_sought, cmp_ref_by_name);
1689 /* v2 supports these by default */
1690 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1691 use_sideband = 2;
1692 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1693 args->deepen = 1;
1695 /* Filter 'ref' by 'sought' and those that aren't local */
1696 mark_complete_and_common_ref(negotiator, args, &ref);
1697 filter_refs(args, &ref, sought, nr_sought);
1698 if (!args->refetch && everything_local(args, &ref))
1699 state = FETCH_DONE;
1700 else
1701 state = FETCH_SEND_REQUEST;
1703 mark_tips(negotiator, args->negotiation_tips);
1704 for_each_cached_alternate(negotiator,
1705 insert_one_alternate_object);
1706 break;
1707 case FETCH_SEND_REQUEST:
1708 if (!negotiation_started) {
1709 negotiation_started = 1;
1710 trace2_region_enter("fetch-pack",
1711 "negotiation_v2",
1712 the_repository);
1714 negotiation_round++;
1715 trace2_region_enter_printf("negotiation_v2", "round",
1716 the_repository, "%d",
1717 negotiation_round);
1718 if (send_fetch_request(negotiator, fd[1], args, ref,
1719 &common,
1720 &haves_to_send, &in_vain,
1721 reader.use_sideband,
1722 seen_ack)) {
1723 trace2_region_leave_printf("negotiation_v2", "round",
1724 the_repository, "%d",
1725 negotiation_round);
1726 state = FETCH_GET_PACK;
1728 else
1729 state = FETCH_PROCESS_ACKS;
1730 break;
1731 case FETCH_PROCESS_ACKS:
1732 /* Process ACKs/NAKs */
1733 process_section_header(&reader, "acknowledgments", 0);
1734 while (process_ack(negotiator, &reader, &common_oid,
1735 &received_ready)) {
1736 in_vain = 0;
1737 seen_ack = 1;
1738 oidset_insert(&common, &common_oid);
1740 trace2_region_leave_printf("negotiation_v2", "round",
1741 the_repository, "%d",
1742 negotiation_round);
1743 if (received_ready) {
1745 * Don't check for response delimiter; get_pack() will
1746 * read the rest of this response.
1748 state = FETCH_GET_PACK;
1749 } else {
1750 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1751 state = FETCH_SEND_REQUEST;
1753 break;
1754 case FETCH_GET_PACK:
1755 trace2_region_leave("fetch-pack",
1756 "negotiation_v2",
1757 the_repository);
1758 trace2_data_intmax("negotiation_v2", the_repository,
1759 "total_rounds", negotiation_round);
1760 /* Check for shallow-info section */
1761 if (process_section_header(&reader, "shallow-info", 1))
1762 receive_shallow_info(args, &reader, shallows, si);
1764 if (process_section_header(&reader, "wanted-refs", 1))
1765 receive_wanted_refs(&reader, sought, nr_sought);
1767 /* get the pack(s) */
1768 if (git_env_bool("GIT_TRACE_REDACT", 1))
1769 reader.options |= PACKET_READ_REDACT_URI_PATH;
1770 if (process_section_header(&reader, "packfile-uris", 1))
1771 receive_packfile_uris(&reader, &packfile_uris);
1772 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1773 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1775 process_section_header(&reader, "packfile", 0);
1778 * this is the final request we'll make of the server;
1779 * do a half-duplex shutdown to indicate that they can
1780 * hang up as soon as the pack is sent.
1782 close(fd[1]);
1783 fd[1] = -1;
1785 if (get_pack(args, fd, pack_lockfiles,
1786 packfile_uris.nr ? &index_pack_args : NULL,
1787 sought, nr_sought, &fsck_options.gitmodules_found))
1788 die(_("git fetch-pack: fetch failed."));
1789 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1791 state = FETCH_DONE;
1792 break;
1793 case FETCH_DONE:
1794 continue;
1798 for (i = 0; i < packfile_uris.nr; i++) {
1799 int j;
1800 struct child_process cmd = CHILD_PROCESS_INIT;
1801 char packname[GIT_MAX_HEXSZ + 1];
1802 const char *uri = packfile_uris.items[i].string +
1803 the_hash_algo->hexsz + 1;
1805 strvec_push(&cmd.args, "http-fetch");
1806 strvec_pushf(&cmd.args, "--packfile=%.*s",
1807 (int) the_hash_algo->hexsz,
1808 packfile_uris.items[i].string);
1809 for (j = 0; j < index_pack_args.nr; j++)
1810 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1811 index_pack_args.v[j]);
1812 strvec_push(&cmd.args, uri);
1813 cmd.git_cmd = 1;
1814 cmd.no_stdin = 1;
1815 cmd.out = -1;
1816 if (start_command(&cmd))
1817 die("fetch-pack: unable to spawn http-fetch");
1819 if (read_in_full(cmd.out, packname, 5) < 0 ||
1820 memcmp(packname, "keep\t", 5))
1821 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1823 if (read_in_full(cmd.out, packname,
1824 the_hash_algo->hexsz + 1) < 0 ||
1825 packname[the_hash_algo->hexsz] != '\n')
1826 die("fetch-pack: expected hash then LF at end of http-fetch output");
1828 packname[the_hash_algo->hexsz] = '\0';
1830 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1832 close(cmd.out);
1834 if (finish_command(&cmd))
1835 die("fetch-pack: unable to finish http-fetch");
1837 if (memcmp(packfile_uris.items[i].string, packname,
1838 the_hash_algo->hexsz))
1839 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1840 uri, (int) the_hash_algo->hexsz,
1841 packfile_uris.items[i].string);
1843 string_list_append_nodup(pack_lockfiles,
1844 xstrfmt("%s/pack/pack-%s.keep",
1845 get_object_directory(),
1846 packname));
1848 string_list_clear(&packfile_uris, 0);
1849 strvec_clear(&index_pack_args);
1851 if (fsck_finish(&fsck_options))
1852 die("fsck failed");
1854 if (negotiator)
1855 negotiator->release(negotiator);
1857 oidset_clear(&common);
1858 return ref;
1861 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1863 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1864 const char *path;
1866 if (git_config_pathname(&path, var, value))
1867 return 1;
1868 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1869 fsck_msg_types.len ? ',' : '=', path);
1870 free((char *)path);
1871 return 0;
1874 if (skip_prefix(var, "fetch.fsck.", &var)) {
1875 if (is_valid_msg_type(var, value))
1876 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1877 fsck_msg_types.len ? ',' : '=', var, value);
1878 else
1879 warning("Skipping unknown msg id '%s'", var);
1880 return 0;
1883 return git_default_config(var, value, cb);
1886 static void fetch_pack_config(void)
1888 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1889 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1890 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1891 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1892 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1893 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1894 if (!uri_protocols.nr) {
1895 char *str;
1897 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1898 string_list_split(&uri_protocols, str, ',', -1);
1899 free(str);
1903 git_config(fetch_pack_config_cb, NULL);
1906 static void fetch_pack_setup(void)
1908 static int did_setup;
1909 if (did_setup)
1910 return;
1911 fetch_pack_config();
1912 if (0 <= transfer_unpack_limit)
1913 unpack_limit = transfer_unpack_limit;
1914 else if (0 <= fetch_unpack_limit)
1915 unpack_limit = fetch_unpack_limit;
1916 did_setup = 1;
1919 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1921 struct string_list names = STRING_LIST_INIT_NODUP;
1922 int src, dst;
1924 for (src = dst = 0; src < nr; src++) {
1925 struct string_list_item *item;
1926 item = string_list_insert(&names, ref[src]->name);
1927 if (item->util)
1928 continue; /* already have it */
1929 item->util = ref[src];
1930 if (src != dst)
1931 ref[dst] = ref[src];
1932 dst++;
1934 for (src = dst; src < nr; src++)
1935 ref[src] = NULL;
1936 string_list_clear(&names, 0);
1937 return dst;
1940 static void update_shallow(struct fetch_pack_args *args,
1941 struct ref **sought, int nr_sought,
1942 struct shallow_info *si)
1944 struct oid_array ref = OID_ARRAY_INIT;
1945 int *status;
1946 int i;
1948 if (args->deepen && alternate_shallow_file) {
1949 if (*alternate_shallow_file == '\0') { /* --unshallow */
1950 unlink_or_warn(git_path_shallow(the_repository));
1951 rollback_shallow_file(the_repository, &shallow_lock);
1952 } else
1953 commit_shallow_file(the_repository, &shallow_lock);
1954 alternate_shallow_file = NULL;
1955 return;
1958 if (!si->shallow || !si->shallow->nr)
1959 return;
1961 if (args->cloning) {
1963 * remote is shallow, but this is a clone, there are
1964 * no objects in repo to worry about. Accept any
1965 * shallow points that exist in the pack (iow in repo
1966 * after get_pack() and reprepare_packed_git())
1968 struct oid_array extra = OID_ARRAY_INIT;
1969 struct object_id *oid = si->shallow->oid;
1970 for (i = 0; i < si->shallow->nr; i++)
1971 if (repo_has_object_file(the_repository, &oid[i]))
1972 oid_array_append(&extra, &oid[i]);
1973 if (extra.nr) {
1974 setup_alternate_shallow(&shallow_lock,
1975 &alternate_shallow_file,
1976 &extra);
1977 commit_shallow_file(the_repository, &shallow_lock);
1978 alternate_shallow_file = NULL;
1980 oid_array_clear(&extra);
1981 return;
1984 if (!si->nr_ours && !si->nr_theirs)
1985 return;
1987 remove_nonexistent_theirs_shallow(si);
1988 if (!si->nr_ours && !si->nr_theirs)
1989 return;
1990 for (i = 0; i < nr_sought; i++)
1991 oid_array_append(&ref, &sought[i]->old_oid);
1992 si->ref = &ref;
1994 if (args->update_shallow) {
1996 * remote is also shallow, .git/shallow may be updated
1997 * so all refs can be accepted. Make sure we only add
1998 * shallow roots that are actually reachable from new
1999 * refs.
2001 struct oid_array extra = OID_ARRAY_INIT;
2002 struct object_id *oid = si->shallow->oid;
2003 assign_shallow_commits_to_refs(si, NULL, NULL);
2004 if (!si->nr_ours && !si->nr_theirs) {
2005 oid_array_clear(&ref);
2006 return;
2008 for (i = 0; i < si->nr_ours; i++)
2009 oid_array_append(&extra, &oid[si->ours[i]]);
2010 for (i = 0; i < si->nr_theirs; i++)
2011 oid_array_append(&extra, &oid[si->theirs[i]]);
2012 setup_alternate_shallow(&shallow_lock,
2013 &alternate_shallow_file,
2014 &extra);
2015 commit_shallow_file(the_repository, &shallow_lock);
2016 oid_array_clear(&extra);
2017 oid_array_clear(&ref);
2018 alternate_shallow_file = NULL;
2019 return;
2023 * remote is also shallow, check what ref is safe to update
2024 * without updating .git/shallow
2026 CALLOC_ARRAY(status, nr_sought);
2027 assign_shallow_commits_to_refs(si, NULL, status);
2028 if (si->nr_ours || si->nr_theirs) {
2029 for (i = 0; i < nr_sought; i++)
2030 if (status[i])
2031 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
2033 free(status);
2034 oid_array_clear(&ref);
2037 static const struct object_id *iterate_ref_map(void *cb_data)
2039 struct ref **rm = cb_data;
2040 struct ref *ref = *rm;
2042 if (!ref)
2043 return NULL;
2044 *rm = ref->next;
2045 return &ref->old_oid;
2048 struct ref *fetch_pack(struct fetch_pack_args *args,
2049 int fd[],
2050 const struct ref *ref,
2051 struct ref **sought, int nr_sought,
2052 struct oid_array *shallow,
2053 struct string_list *pack_lockfiles,
2054 enum protocol_version version)
2056 struct ref *ref_cpy;
2057 struct shallow_info si;
2058 struct oid_array shallows_scratch = OID_ARRAY_INIT;
2060 fetch_pack_setup();
2061 if (nr_sought)
2062 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
2064 if (version != protocol_v2 && !ref) {
2065 packet_flush(fd[1]);
2066 die(_("no matching remote head"));
2068 if (version == protocol_v2) {
2069 if (shallow->nr)
2070 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2071 memset(&si, 0, sizeof(si));
2072 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
2073 &shallows_scratch, &si,
2074 pack_lockfiles);
2075 } else {
2076 prepare_shallow_info(&si, shallow);
2077 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
2078 &si, pack_lockfiles);
2080 reprepare_packed_git(the_repository);
2082 if (!args->cloning && args->deepen) {
2083 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2084 struct ref *iterator = ref_cpy;
2085 opt.shallow_file = alternate_shallow_file;
2086 if (args->deepen)
2087 opt.is_deepening_fetch = 1;
2088 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2089 error(_("remote did not send all necessary objects"));
2090 free_refs(ref_cpy);
2091 ref_cpy = NULL;
2092 rollback_shallow_file(the_repository, &shallow_lock);
2093 goto cleanup;
2095 args->connectivity_checked = 1;
2098 update_shallow(args, sought, nr_sought, &si);
2099 cleanup:
2100 clear_shallow_info(&si);
2101 oid_array_clear(&shallows_scratch);
2102 return ref_cpy;
2105 static int add_to_object_array(const struct object_id *oid, void *data)
2107 struct object_array *a = data;
2109 add_object_array(lookup_object(the_repository, oid), "", a);
2110 return 0;
2113 static void clear_common_flag(struct oidset *s)
2115 struct oidset_iter iter;
2116 const struct object_id *oid;
2117 oidset_iter_init(s, &iter);
2119 while ((oid = oidset_iter_next(&iter))) {
2120 struct object *obj = lookup_object(the_repository, oid);
2121 obj->flags &= ~COMMON;
2125 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2126 const struct string_list *server_options,
2127 int stateless_rpc,
2128 int fd[],
2129 struct oidset *acked_commits)
2131 struct fetch_negotiator negotiator;
2132 struct packet_reader reader;
2133 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2134 struct strbuf req_buf = STRBUF_INIT;
2135 int haves_to_send = INITIAL_FLUSH;
2136 int in_vain = 0;
2137 int seen_ack = 0;
2138 int last_iteration = 0;
2139 int negotiation_round = 0;
2140 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2142 fetch_negotiator_init(the_repository, &negotiator);
2143 mark_tips(&negotiator, negotiation_tips);
2145 packet_reader_init(&reader, fd[0], NULL, 0,
2146 PACKET_READ_CHOMP_NEWLINE |
2147 PACKET_READ_DIE_ON_ERR_PACKET);
2149 oid_array_for_each((struct oid_array *) negotiation_tips,
2150 add_to_object_array,
2151 &nt_object_array);
2153 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2154 while (!last_iteration) {
2155 int haves_added;
2156 struct object_id common_oid;
2157 int received_ready = 0;
2159 negotiation_round++;
2161 trace2_region_enter_printf("negotiate_using_fetch", "round",
2162 the_repository, "%d",
2163 negotiation_round);
2164 strbuf_reset(&req_buf);
2165 write_fetch_command_and_capabilities(&req_buf, server_options);
2167 packet_buf_write(&req_buf, "wait-for-done");
2169 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2170 in_vain += haves_added;
2171 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2172 last_iteration = 1;
2174 trace2_data_intmax("negotiate_using_fetch", the_repository,
2175 "haves_added", haves_added);
2176 trace2_data_intmax("negotiate_using_fetch", the_repository,
2177 "in_vain", in_vain);
2179 /* Send request */
2180 packet_buf_flush(&req_buf);
2181 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2182 die_errno(_("unable to write request to remote"));
2184 /* Process ACKs/NAKs */
2185 process_section_header(&reader, "acknowledgments", 0);
2186 while (process_ack(&negotiator, &reader, &common_oid,
2187 &received_ready)) {
2188 struct commit *commit = lookup_commit(the_repository,
2189 &common_oid);
2190 if (commit) {
2191 timestamp_t generation;
2193 parse_commit_or_die(commit);
2194 commit->object.flags |= COMMON;
2195 generation = commit_graph_generation(commit);
2196 if (generation < min_generation)
2197 min_generation = generation;
2199 in_vain = 0;
2200 seen_ack = 1;
2201 oidset_insert(acked_commits, &common_oid);
2203 if (received_ready)
2204 die(_("unexpected 'ready' from remote"));
2205 else
2206 do_check_stateless_delimiter(stateless_rpc, &reader);
2207 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2208 REACH_SCRATCH, 0,
2209 min_generation))
2210 last_iteration = 1;
2211 trace2_region_leave_printf("negotiation", "round",
2212 the_repository, "%d",
2213 negotiation_round);
2215 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2216 trace2_data_intmax("negotiate_using_fetch", the_repository,
2217 "total_rounds", negotiation_round);
2218 clear_common_flag(acked_commits);
2219 strbuf_release(&req_buf);
2222 int report_unmatched_refs(struct ref **sought, int nr_sought)
2224 int i, ret = 0;
2226 for (i = 0; i < nr_sought; i++) {
2227 if (!sought[i])
2228 continue;
2229 switch (sought[i]->match_status) {
2230 case REF_MATCHED:
2231 continue;
2232 case REF_NOT_MATCHED:
2233 error(_("no such remote ref %s"), sought[i]->name);
2234 break;
2235 case REF_UNADVERTISED_NOT_ALLOWED:
2236 error(_("Server does not allow request for unadvertised object %s"),
2237 sought[i]->name);
2238 break;
2240 ret = 1;
2242 return ret;