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