Merge branch 'fr_v2.34.0_rnd3' of github.com:jnavila/git
[alt-git.git] / fetch-pack.c
bloba9604f35a3ea9055732d48e39b63a39f041f18f3
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"
29 static int transfer_unpack_limit = -1;
30 static int fetch_unpack_limit = -1;
31 static int unpack_limit = 100;
32 static int prefer_ofs_delta = 1;
33 static int no_done;
34 static int deepen_since_ok;
35 static int deepen_not_ok;
36 static int fetch_fsck_objects = -1;
37 static int transfer_fsck_objects = -1;
38 static int agent_supported;
39 static int server_supports_filtering;
40 static int advertise_sid;
41 static struct shallow_lock shallow_lock;
42 static const char *alternate_shallow_file;
43 static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
44 static struct strbuf fsck_msg_types = STRBUF_INIT;
45 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
47 /* Remember to update object flag allocation in object.h */
48 #define COMPLETE (1U << 0)
49 #define ALTERNATE (1U << 1)
50 #define COMMON (1U << 6)
51 #define REACH_SCRATCH (1U << 7)
54 * After sending this many "have"s if we do not get any new ACK , we
55 * give up traversing our history.
57 #define MAX_IN_VAIN 256
59 static int multi_ack, use_sideband;
60 /* Allow specifying sha1 if it is a ref tip. */
61 #define ALLOW_TIP_SHA1 01
62 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
63 #define ALLOW_REACHABLE_SHA1 02
64 static unsigned int allow_unadvertised_object_request;
66 __attribute__((format (printf, 2, 3)))
67 static inline void print_verbose(const struct fetch_pack_args *args,
68 const char *fmt, ...)
70 va_list params;
72 if (!args->verbose)
73 return;
75 va_start(params, fmt);
76 vfprintf(stderr, fmt, params);
77 va_end(params);
78 fputc('\n', stderr);
81 struct alternate_object_cache {
82 struct object **items;
83 size_t nr, alloc;
86 static void cache_one_alternate(const struct object_id *oid,
87 void *vcache)
89 struct alternate_object_cache *cache = vcache;
90 struct object *obj = parse_object(the_repository, oid);
92 if (!obj || (obj->flags & ALTERNATE))
93 return;
95 obj->flags |= ALTERNATE;
96 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
97 cache->items[cache->nr++] = obj;
100 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
101 void (*cb)(struct fetch_negotiator *,
102 struct object *))
104 static int initialized;
105 static struct alternate_object_cache cache;
106 size_t i;
108 if (!initialized) {
109 for_each_alternate_ref(cache_one_alternate, &cache);
110 initialized = 1;
113 for (i = 0; i < cache.nr; i++)
114 cb(negotiator, cache.items[i]);
117 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
118 int mark_tags_complete)
120 enum object_type type;
121 struct object_info info = { .typep = &type };
122 struct commit *commit;
124 commit = lookup_commit_in_graph(the_repository, oid);
125 if (commit)
126 return commit;
128 while (1) {
129 if (oid_object_info_extended(the_repository, oid, &info,
130 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
131 return NULL;
132 if (type == OBJ_TAG) {
133 struct tag *tag = (struct tag *)
134 parse_object(the_repository, oid);
136 if (!tag->tagged)
137 return NULL;
138 if (mark_tags_complete)
139 tag->object.flags |= COMPLETE;
140 oid = &tag->tagged->oid;
141 } else {
142 break;
146 if (type == OBJ_COMMIT) {
147 struct commit *commit = lookup_commit(the_repository, oid);
148 if (!commit || repo_parse_commit(the_repository, commit))
149 return NULL;
150 return commit;
153 return NULL;
156 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
157 const struct object_id *oid)
159 struct commit *c = deref_without_lazy_fetch(oid, 0);
161 if (c)
162 negotiator->add_tip(negotiator, c);
163 return 0;
166 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
167 int flag, void *cb_data)
169 return rev_list_insert_ref(cb_data, oid);
172 enum ack_type {
173 NAK = 0,
174 ACK,
175 ACK_continue,
176 ACK_common,
177 ACK_ready
180 static void consume_shallow_list(struct fetch_pack_args *args,
181 struct packet_reader *reader)
183 if (args->stateless_rpc && args->deepen) {
184 /* If we sent a depth we will get back "duplicate"
185 * shallow and unshallow commands every time there
186 * is a block of have lines exchanged.
188 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
189 if (starts_with(reader->line, "shallow "))
190 continue;
191 if (starts_with(reader->line, "unshallow "))
192 continue;
193 die(_("git fetch-pack: expected shallow list"));
195 if (reader->status != PACKET_READ_FLUSH)
196 die(_("git fetch-pack: expected a flush packet after shallow list"));
200 static enum ack_type get_ack(struct packet_reader *reader,
201 struct object_id *result_oid)
203 int len;
204 const char *arg;
206 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
207 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
208 len = reader->pktlen;
210 if (!strcmp(reader->line, "NAK"))
211 return NAK;
212 if (skip_prefix(reader->line, "ACK ", &arg)) {
213 const char *p;
214 if (!parse_oid_hex(arg, result_oid, &p)) {
215 len -= p - reader->line;
216 if (len < 1)
217 return ACK;
218 if (strstr(p, "continue"))
219 return ACK_continue;
220 if (strstr(p, "common"))
221 return ACK_common;
222 if (strstr(p, "ready"))
223 return ACK_ready;
224 return ACK;
227 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
230 static void send_request(struct fetch_pack_args *args,
231 int fd, struct strbuf *buf)
233 if (args->stateless_rpc) {
234 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
235 packet_flush(fd);
236 } else {
237 if (write_in_full(fd, buf->buf, buf->len) < 0)
238 die_errno(_("unable to write to remote"));
242 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
243 struct object *obj)
245 rev_list_insert_ref(negotiator, &obj->oid);
248 #define INITIAL_FLUSH 16
249 #define PIPESAFE_FLUSH 32
250 #define LARGE_FLUSH 16384
252 static int next_flush(int stateless_rpc, int count)
254 if (stateless_rpc) {
255 if (count < LARGE_FLUSH)
256 count <<= 1;
257 else
258 count = count * 11 / 10;
259 } else {
260 if (count < PIPESAFE_FLUSH)
261 count <<= 1;
262 else
263 count += PIPESAFE_FLUSH;
265 return count;
268 static void mark_tips(struct fetch_negotiator *negotiator,
269 const struct oid_array *negotiation_tips)
271 int i;
273 if (!negotiation_tips) {
274 for_each_rawref(rev_list_insert_ref_oid, negotiator);
275 return;
278 for (i = 0; i < negotiation_tips->nr; i++)
279 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
280 return;
283 static int find_common(struct fetch_negotiator *negotiator,
284 struct fetch_pack_args *args,
285 int fd[2], struct object_id *result_oid,
286 struct ref *refs)
288 int fetching;
289 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
290 const struct object_id *oid;
291 unsigned in_vain = 0;
292 int got_continue = 0;
293 int got_ready = 0;
294 struct strbuf req_buf = STRBUF_INIT;
295 size_t state_len = 0;
296 struct packet_reader reader;
298 if (args->stateless_rpc && multi_ack == 1)
299 die(_("--stateless-rpc requires multi_ack_detailed"));
301 packet_reader_init(&reader, fd[0], NULL, 0,
302 PACKET_READ_CHOMP_NEWLINE |
303 PACKET_READ_DIE_ON_ERR_PACKET);
305 mark_tips(negotiator, args->negotiation_tips);
306 for_each_cached_alternate(negotiator, insert_one_alternate_object);
308 fetching = 0;
309 for ( ; refs ; refs = refs->next) {
310 struct object_id *remote = &refs->old_oid;
311 const char *remote_hex;
312 struct object *o;
315 * If that object is complete (i.e. it is an ancestor of a
316 * local ref), we tell them we have it but do not have to
317 * tell them about its ancestors, which they already know
318 * about.
320 * We use lookup_object here because we are only
321 * interested in the case we *know* the object is
322 * reachable and we have already scanned it.
324 if (((o = lookup_object(the_repository, remote)) != NULL) &&
325 (o->flags & COMPLETE)) {
326 continue;
329 remote_hex = oid_to_hex(remote);
330 if (!fetching) {
331 struct strbuf c = STRBUF_INIT;
332 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
333 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
334 if (no_done) strbuf_addstr(&c, " no-done");
335 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
336 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
337 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
338 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
339 if (args->no_progress) strbuf_addstr(&c, " no-progress");
340 if (args->include_tag) strbuf_addstr(&c, " include-tag");
341 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
342 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
343 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
344 if (agent_supported) strbuf_addf(&c, " agent=%s",
345 git_user_agent_sanitized());
346 if (advertise_sid)
347 strbuf_addf(&c, " session-id=%s", trace2_session_id());
348 if (args->filter_options.choice)
349 strbuf_addstr(&c, " filter");
350 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
351 strbuf_release(&c);
352 } else
353 packet_buf_write(&req_buf, "want %s\n", remote_hex);
354 fetching++;
357 if (!fetching) {
358 strbuf_release(&req_buf);
359 packet_flush(fd[1]);
360 return 1;
363 if (is_repository_shallow(the_repository))
364 write_shallow_commits(&req_buf, 1, NULL);
365 if (args->depth > 0)
366 packet_buf_write(&req_buf, "deepen %d", args->depth);
367 if (args->deepen_since) {
368 timestamp_t max_age = approxidate(args->deepen_since);
369 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
371 if (args->deepen_not) {
372 int i;
373 for (i = 0; i < args->deepen_not->nr; i++) {
374 struct string_list_item *s = args->deepen_not->items + i;
375 packet_buf_write(&req_buf, "deepen-not %s", s->string);
378 if (server_supports_filtering && args->filter_options.choice) {
379 const char *spec =
380 expand_list_objects_filter_spec(&args->filter_options);
381 packet_buf_write(&req_buf, "filter %s", spec);
383 packet_buf_flush(&req_buf);
384 state_len = req_buf.len;
386 if (args->deepen) {
387 const char *arg;
388 struct object_id oid;
390 send_request(args, fd[1], &req_buf);
391 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
392 if (skip_prefix(reader.line, "shallow ", &arg)) {
393 if (get_oid_hex(arg, &oid))
394 die(_("invalid shallow line: %s"), reader.line);
395 register_shallow(the_repository, &oid);
396 continue;
398 if (skip_prefix(reader.line, "unshallow ", &arg)) {
399 if (get_oid_hex(arg, &oid))
400 die(_("invalid unshallow line: %s"), reader.line);
401 if (!lookup_object(the_repository, &oid))
402 die(_("object not found: %s"), reader.line);
403 /* make sure that it is parsed as shallow */
404 if (!parse_object(the_repository, &oid))
405 die(_("error in object: %s"), reader.line);
406 if (unregister_shallow(&oid))
407 die(_("no shallow found: %s"), reader.line);
408 continue;
410 die(_("expected shallow/unshallow, got %s"), reader.line);
412 } else if (!args->stateless_rpc)
413 send_request(args, fd[1], &req_buf);
415 if (!args->stateless_rpc) {
416 /* If we aren't using the stateless-rpc interface
417 * we don't need to retain the headers.
419 strbuf_setlen(&req_buf, 0);
420 state_len = 0;
423 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
424 flushes = 0;
425 retval = -1;
426 while ((oid = negotiator->next(negotiator))) {
427 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
428 print_verbose(args, "have %s", oid_to_hex(oid));
429 in_vain++;
430 if (flush_at <= ++count) {
431 int ack;
433 packet_buf_flush(&req_buf);
434 send_request(args, fd[1], &req_buf);
435 strbuf_setlen(&req_buf, state_len);
436 flushes++;
437 flush_at = next_flush(args->stateless_rpc, count);
440 * We keep one window "ahead" of the other side, and
441 * will wait for an ACK only on the next one
443 if (!args->stateless_rpc && count == INITIAL_FLUSH)
444 continue;
446 consume_shallow_list(args, &reader);
447 do {
448 ack = get_ack(&reader, result_oid);
449 if (ack)
450 print_verbose(args, _("got %s %d %s"), "ack",
451 ack, oid_to_hex(result_oid));
452 switch (ack) {
453 case ACK:
454 flushes = 0;
455 multi_ack = 0;
456 retval = 0;
457 goto done;
458 case ACK_common:
459 case ACK_ready:
460 case ACK_continue: {
461 struct commit *commit =
462 lookup_commit(the_repository,
463 result_oid);
464 int was_common;
466 if (!commit)
467 die(_("invalid commit %s"), oid_to_hex(result_oid));
468 was_common = negotiator->ack(negotiator, commit);
469 if (args->stateless_rpc
470 && ack == ACK_common
471 && !was_common) {
472 /* We need to replay the have for this object
473 * on the next RPC request so the peer knows
474 * it is in common with us.
476 const char *hex = oid_to_hex(result_oid);
477 packet_buf_write(&req_buf, "have %s\n", hex);
478 state_len = req_buf.len;
480 * Reset in_vain because an ack
481 * for this commit has not been
482 * seen.
484 in_vain = 0;
485 } else if (!args->stateless_rpc
486 || ack != ACK_common)
487 in_vain = 0;
488 retval = 0;
489 got_continue = 1;
490 if (ack == ACK_ready)
491 got_ready = 1;
492 break;
495 } while (ack);
496 flushes--;
497 if (got_continue && MAX_IN_VAIN < in_vain) {
498 print_verbose(args, _("giving up"));
499 break; /* give up */
501 if (got_ready)
502 break;
505 done:
506 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
507 if (!got_ready || !no_done) {
508 packet_buf_write(&req_buf, "done\n");
509 send_request(args, fd[1], &req_buf);
511 print_verbose(args, _("done"));
512 if (retval != 0) {
513 multi_ack = 0;
514 flushes++;
516 strbuf_release(&req_buf);
518 if (!got_ready || !no_done)
519 consume_shallow_list(args, &reader);
520 while (flushes || multi_ack) {
521 int ack = get_ack(&reader, result_oid);
522 if (ack) {
523 print_verbose(args, _("got %s (%d) %s"), "ack",
524 ack, oid_to_hex(result_oid));
525 if (ack == ACK)
526 return 0;
527 multi_ack = 1;
528 continue;
530 flushes--;
532 /* it is no error to fetch into a completely empty repo */
533 return count ? retval : 0;
536 static struct commit_list *complete;
538 static int mark_complete(const struct object_id *oid)
540 struct commit *commit = deref_without_lazy_fetch(oid, 1);
542 if (commit && !(commit->object.flags & COMPLETE)) {
543 commit->object.flags |= COMPLETE;
544 commit_list_insert(commit, &complete);
546 return 0;
549 static int mark_complete_oid(const char *refname, const struct object_id *oid,
550 int flag, void *cb_data)
552 return mark_complete(oid);
555 static void mark_recent_complete_commits(struct fetch_pack_args *args,
556 timestamp_t cutoff)
558 while (complete && cutoff <= complete->item->date) {
559 print_verbose(args, _("Marking %s as complete"),
560 oid_to_hex(&complete->item->object.oid));
561 pop_most_recent_commit(&complete, COMPLETE);
565 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
567 for (; refs; refs = refs->next)
568 oidset_insert(oids, &refs->old_oid);
571 static int is_unmatched_ref(const struct ref *ref)
573 struct object_id oid;
574 const char *p;
575 return ref->match_status == REF_NOT_MATCHED &&
576 !parse_oid_hex(ref->name, &oid, &p) &&
577 *p == '\0' &&
578 oideq(&oid, &ref->old_oid);
581 static void filter_refs(struct fetch_pack_args *args,
582 struct ref **refs,
583 struct ref **sought, int nr_sought)
585 struct ref *newlist = NULL;
586 struct ref **newtail = &newlist;
587 struct ref *unmatched = NULL;
588 struct ref *ref, *next;
589 struct oidset tip_oids = OIDSET_INIT;
590 int i;
591 int strict = !(allow_unadvertised_object_request &
592 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
594 i = 0;
595 for (ref = *refs; ref; ref = next) {
596 int keep = 0;
597 next = ref->next;
599 if (starts_with(ref->name, "refs/") &&
600 check_refname_format(ref->name, 0)) {
602 * trash or a peeled value; do not even add it to
603 * unmatched list
605 free_one_ref(ref);
606 continue;
607 } else {
608 while (i < nr_sought) {
609 int cmp = strcmp(ref->name, sought[i]->name);
610 if (cmp < 0)
611 break; /* definitely do not have it */
612 else if (cmp == 0) {
613 keep = 1; /* definitely have it */
614 sought[i]->match_status = REF_MATCHED;
616 i++;
619 if (!keep && args->fetch_all &&
620 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
621 keep = 1;
624 if (keep) {
625 *newtail = ref;
626 ref->next = NULL;
627 newtail = &ref->next;
628 } else {
629 ref->next = unmatched;
630 unmatched = ref;
634 if (strict) {
635 for (i = 0; i < nr_sought; i++) {
636 ref = sought[i];
637 if (!is_unmatched_ref(ref))
638 continue;
640 add_refs_to_oidset(&tip_oids, unmatched);
641 add_refs_to_oidset(&tip_oids, newlist);
642 break;
646 /* Append unmatched requests to the list */
647 for (i = 0; i < nr_sought; i++) {
648 ref = sought[i];
649 if (!is_unmatched_ref(ref))
650 continue;
652 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
653 ref->match_status = REF_MATCHED;
654 *newtail = copy_ref(ref);
655 newtail = &(*newtail)->next;
656 } else {
657 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
661 oidset_clear(&tip_oids);
662 free_refs(unmatched);
664 *refs = newlist;
667 static void mark_alternate_complete(struct fetch_negotiator *unused,
668 struct object *obj)
670 mark_complete(&obj->oid);
673 struct loose_object_iter {
674 struct oidset *loose_object_set;
675 struct ref *refs;
679 * Mark recent commits available locally and reachable from a local ref as
680 * COMPLETE.
682 * The cutoff time for recency is determined by this heuristic: it is the
683 * earliest commit time of the objects in refs that are commits and that we know
684 * the commit time of.
686 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
687 struct fetch_pack_args *args,
688 struct ref **refs)
690 struct ref *ref;
691 int old_save_commit_buffer = save_commit_buffer;
692 timestamp_t cutoff = 0;
694 save_commit_buffer = 0;
696 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
697 for (ref = *refs; ref; ref = ref->next) {
698 struct object *o;
700 if (!has_object_file_with_flags(&ref->old_oid,
701 OBJECT_INFO_QUICK |
702 OBJECT_INFO_SKIP_FETCH_OBJECT))
703 continue;
704 o = parse_object(the_repository, &ref->old_oid);
705 if (!o)
706 continue;
709 * We already have it -- which may mean that we were
710 * in sync with the other side at some time after
711 * that (it is OK if we guess wrong here).
713 if (o->type == OBJ_COMMIT) {
714 struct commit *commit = (struct commit *)o;
715 if (!cutoff || cutoff < commit->date)
716 cutoff = commit->date;
719 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
722 * This block marks all local refs as COMPLETE, and then recursively marks all
723 * parents of those refs as COMPLETE.
725 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
726 if (!args->deepen) {
727 for_each_rawref(mark_complete_oid, NULL);
728 for_each_cached_alternate(NULL, mark_alternate_complete);
729 commit_list_sort_by_date(&complete);
730 if (cutoff)
731 mark_recent_complete_commits(args, cutoff);
733 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
736 * Mark all complete remote refs as common refs.
737 * Don't mark them common yet; the server has to be told so first.
739 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
740 for (ref = *refs; ref; ref = ref->next) {
741 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
743 if (!c || !(c->object.flags & COMPLETE))
744 continue;
746 negotiator->known_common(negotiator, c);
748 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
750 save_commit_buffer = old_save_commit_buffer;
754 * Returns 1 if every object pointed to by the given remote refs is available
755 * locally and reachable from a local ref, and 0 otherwise.
757 static int everything_local(struct fetch_pack_args *args,
758 struct ref **refs)
760 struct ref *ref;
761 int retval;
763 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
764 const struct object_id *remote = &ref->old_oid;
765 struct object *o;
767 o = lookup_object(the_repository, remote);
768 if (!o || !(o->flags & COMPLETE)) {
769 retval = 0;
770 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
771 ref->name);
772 continue;
774 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
775 ref->name);
778 return retval;
781 static int sideband_demux(int in, int out, void *data)
783 int *xd = data;
784 int ret;
786 ret = recv_sideband("fetch-pack", xd[0], out);
787 close(out);
788 return ret;
791 static void create_promisor_file(const char *keep_name,
792 struct ref **sought, int nr_sought)
794 struct strbuf promisor_name = STRBUF_INIT;
795 int suffix_stripped;
797 strbuf_addstr(&promisor_name, keep_name);
798 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
799 if (!suffix_stripped)
800 BUG("name of pack lockfile should end with .keep (was '%s')",
801 keep_name);
802 strbuf_addstr(&promisor_name, ".promisor");
804 write_promisor_file(promisor_name.buf, sought, nr_sought);
806 strbuf_release(&promisor_name);
809 static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
811 int len = the_hash_algo->hexsz + 1; /* hash + NL */
813 do {
814 char hex_hash[GIT_MAX_HEXSZ + 1];
815 int read_len = read_in_full(fd, hex_hash, len);
816 struct object_id oid;
817 const char *end;
819 if (!read_len)
820 return;
821 if (read_len != len)
822 die("invalid length read %d", read_len);
823 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
824 die("invalid hash");
825 oidset_insert(gitmodules_oids, &oid);
826 } while (1);
830 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
831 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
832 * stored there. (It must be freed by the caller.)
834 static int get_pack(struct fetch_pack_args *args,
835 int xd[2], struct string_list *pack_lockfiles,
836 struct strvec *index_pack_args,
837 struct ref **sought, int nr_sought,
838 struct oidset *gitmodules_oids)
840 struct async demux;
841 int do_keep = args->keep_pack;
842 const char *cmd_name;
843 struct pack_header header;
844 int pass_header = 0;
845 struct child_process cmd = CHILD_PROCESS_INIT;
846 int fsck_objects = 0;
847 int ret;
849 memset(&demux, 0, sizeof(demux));
850 if (use_sideband) {
851 /* xd[] is talking with upload-pack; subprocess reads from
852 * xd[0], spits out band#2 to stderr, and feeds us band#1
853 * through demux->out.
855 demux.proc = sideband_demux;
856 demux.data = xd;
857 demux.out = -1;
858 demux.isolate_sigpipe = 1;
859 if (start_async(&demux))
860 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
862 else
863 demux.out = xd[0];
865 if (!args->keep_pack && unpack_limit && !index_pack_args) {
867 if (read_pack_header(demux.out, &header))
868 die(_("protocol error: bad pack header"));
869 pass_header = 1;
870 if (ntohl(header.hdr_entries) < unpack_limit)
871 do_keep = 0;
872 else
873 do_keep = 1;
876 if (alternate_shallow_file) {
877 strvec_push(&cmd.args, "--shallow-file");
878 strvec_push(&cmd.args, alternate_shallow_file);
881 if (fetch_fsck_objects >= 0
882 ? fetch_fsck_objects
883 : transfer_fsck_objects >= 0
884 ? transfer_fsck_objects
885 : 0)
886 fsck_objects = 1;
888 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
889 if (pack_lockfiles || fsck_objects)
890 cmd.out = -1;
891 cmd_name = "index-pack";
892 strvec_push(&cmd.args, cmd_name);
893 strvec_push(&cmd.args, "--stdin");
894 if (!args->quiet && !args->no_progress)
895 strvec_push(&cmd.args, "-v");
896 if (args->use_thin_pack)
897 strvec_push(&cmd.args, "--fix-thin");
898 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) {
899 char hostname[HOST_NAME_MAX + 1];
900 if (xgethostname(hostname, sizeof(hostname)))
901 xsnprintf(hostname, sizeof(hostname), "localhost");
902 strvec_pushf(&cmd.args,
903 "--keep=fetch-pack %"PRIuMAX " on %s",
904 (uintmax_t)getpid(), hostname);
906 if (!index_pack_args && args->check_self_contained_and_connected)
907 strvec_push(&cmd.args, "--check-self-contained-and-connected");
908 else
910 * We cannot perform any connectivity checks because
911 * not all packs have been downloaded; let the caller
912 * have this responsibility.
914 args->check_self_contained_and_connected = 0;
916 if (args->from_promisor)
918 * create_promisor_file() may be called afterwards but
919 * we still need index-pack to know that this is a
920 * promisor pack. For example, if transfer.fsckobjects
921 * is true, index-pack needs to know that .gitmodules
922 * is a promisor object (so that it won't complain if
923 * it is missing).
925 strvec_push(&cmd.args, "--promisor");
927 else {
928 cmd_name = "unpack-objects";
929 strvec_push(&cmd.args, cmd_name);
930 if (args->quiet || args->no_progress)
931 strvec_push(&cmd.args, "-q");
932 args->check_self_contained_and_connected = 0;
935 if (pass_header)
936 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
937 ntohl(header.hdr_version),
938 ntohl(header.hdr_entries));
939 if (fsck_objects) {
940 if (args->from_promisor || index_pack_args)
942 * We cannot use --strict in index-pack because it
943 * checks both broken objects and links, but we only
944 * want to check for broken objects.
946 strvec_push(&cmd.args, "--fsck-objects");
947 else
948 strvec_pushf(&cmd.args, "--strict%s",
949 fsck_msg_types.buf);
952 if (index_pack_args) {
953 int i;
955 for (i = 0; i < cmd.args.nr; i++)
956 strvec_push(index_pack_args, cmd.args.v[i]);
959 cmd.in = demux.out;
960 cmd.git_cmd = 1;
961 if (start_command(&cmd))
962 die(_("fetch-pack: unable to fork off %s"), cmd_name);
963 if (do_keep && (pack_lockfiles || fsck_objects)) {
964 int is_well_formed;
965 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
967 if (!is_well_formed)
968 die(_("fetch-pack: invalid index-pack output"));
969 if (pack_lockfile)
970 string_list_append_nodup(pack_lockfiles, pack_lockfile);
971 parse_gitmodules_oids(cmd.out, gitmodules_oids);
972 close(cmd.out);
975 if (!use_sideband)
976 /* Closed by start_command() */
977 xd[0] = -1;
979 ret = finish_command(&cmd);
980 if (!ret || (args->check_self_contained_and_connected && ret == 1))
981 args->self_contained_and_connected =
982 args->check_self_contained_and_connected &&
983 ret == 0;
984 else
985 die(_("%s failed"), cmd_name);
986 if (use_sideband && finish_async(&demux))
987 die(_("error in sideband demultiplexer"));
990 * Now that index-pack has succeeded, write the promisor file using the
991 * obtained .keep filename if necessary
993 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
994 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
996 return 0;
999 static int cmp_ref_by_name(const void *a_, const void *b_)
1001 const struct ref *a = *((const struct ref **)a_);
1002 const struct ref *b = *((const struct ref **)b_);
1003 return strcmp(a->name, b->name);
1006 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1007 int fd[2],
1008 const struct ref *orig_ref,
1009 struct ref **sought, int nr_sought,
1010 struct shallow_info *si,
1011 struct string_list *pack_lockfiles)
1013 struct repository *r = the_repository;
1014 struct ref *ref = copy_ref_list(orig_ref);
1015 struct object_id oid;
1016 const char *agent_feature;
1017 int agent_len;
1018 struct fetch_negotiator negotiator_alloc;
1019 struct fetch_negotiator *negotiator;
1021 negotiator = &negotiator_alloc;
1022 fetch_negotiator_init(r, negotiator);
1024 sort_ref_list(&ref, ref_compare_name);
1025 QSORT(sought, nr_sought, cmp_ref_by_name);
1027 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1028 agent_supported = 1;
1029 if (agent_len)
1030 print_verbose(args, _("Server version is %.*s"),
1031 agent_len, agent_feature);
1034 if (!server_supports("session-id"))
1035 advertise_sid = 0;
1037 if (server_supports("shallow"))
1038 print_verbose(args, _("Server supports %s"), "shallow");
1039 else if (args->depth > 0 || is_repository_shallow(r))
1040 die(_("Server does not support shallow clients"));
1041 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1042 args->deepen = 1;
1043 if (server_supports("multi_ack_detailed")) {
1044 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
1045 multi_ack = 2;
1046 if (server_supports("no-done")) {
1047 print_verbose(args, _("Server supports %s"), "no-done");
1048 if (args->stateless_rpc)
1049 no_done = 1;
1052 else if (server_supports("multi_ack")) {
1053 print_verbose(args, _("Server supports %s"), "multi_ack");
1054 multi_ack = 1;
1056 if (server_supports("side-band-64k")) {
1057 print_verbose(args, _("Server supports %s"), "side-band-64k");
1058 use_sideband = 2;
1060 else if (server_supports("side-band")) {
1061 print_verbose(args, _("Server supports %s"), "side-band");
1062 use_sideband = 1;
1064 if (server_supports("allow-tip-sha1-in-want")) {
1065 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1066 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1068 if (server_supports("allow-reachable-sha1-in-want")) {
1069 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1070 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1072 if (server_supports("thin-pack"))
1073 print_verbose(args, _("Server supports %s"), "thin-pack");
1074 else
1075 args->use_thin_pack = 0;
1076 if (server_supports("no-progress"))
1077 print_verbose(args, _("Server supports %s"), "no-progress");
1078 else
1079 args->no_progress = 0;
1080 if (server_supports("include-tag"))
1081 print_verbose(args, _("Server supports %s"), "include-tag");
1082 else
1083 args->include_tag = 0;
1084 if (server_supports("ofs-delta"))
1085 print_verbose(args, _("Server supports %s"), "ofs-delta");
1086 else
1087 prefer_ofs_delta = 0;
1089 if (server_supports("filter")) {
1090 server_supports_filtering = 1;
1091 print_verbose(args, _("Server supports %s"), "filter");
1092 } else if (args->filter_options.choice) {
1093 warning("filtering not recognized by server, ignoring");
1096 if (server_supports("deepen-since")) {
1097 print_verbose(args, _("Server supports %s"), "deepen-since");
1098 deepen_since_ok = 1;
1099 } else if (args->deepen_since)
1100 die(_("Server does not support --shallow-since"));
1101 if (server_supports("deepen-not")) {
1102 print_verbose(args, _("Server supports %s"), "deepen-not");
1103 deepen_not_ok = 1;
1104 } else if (args->deepen_not)
1105 die(_("Server does not support --shallow-exclude"));
1106 if (server_supports("deepen-relative"))
1107 print_verbose(args, _("Server supports %s"), "deepen-relative");
1108 else if (args->deepen_relative)
1109 die(_("Server does not support --deepen"));
1110 if (!server_supports_hash(the_hash_algo->name, NULL))
1111 die(_("Server does not support this repository's object format"));
1113 mark_complete_and_common_ref(negotiator, args, &ref);
1114 filter_refs(args, &ref, sought, nr_sought);
1115 if (everything_local(args, &ref)) {
1116 packet_flush(fd[1]);
1117 goto all_done;
1119 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1120 if (!args->keep_pack)
1121 /* When cloning, it is not unusual to have
1122 * no common commit.
1124 warning(_("no common commits"));
1126 if (args->stateless_rpc)
1127 packet_flush(fd[1]);
1128 if (args->deepen)
1129 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1130 NULL);
1131 else if (si->nr_ours || si->nr_theirs) {
1132 if (args->reject_shallow_remote)
1133 die(_("source repository is shallow, reject to clone."));
1134 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1135 } else
1136 alternate_shallow_file = NULL;
1137 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1138 &fsck_options.gitmodules_found))
1139 die(_("git fetch-pack: fetch failed."));
1140 if (fsck_finish(&fsck_options))
1141 die("fsck failed");
1143 all_done:
1144 if (negotiator)
1145 negotiator->release(negotiator);
1146 return ref;
1149 static void add_shallow_requests(struct strbuf *req_buf,
1150 const struct fetch_pack_args *args)
1152 if (is_repository_shallow(the_repository))
1153 write_shallow_commits(req_buf, 1, NULL);
1154 if (args->depth > 0)
1155 packet_buf_write(req_buf, "deepen %d", args->depth);
1156 if (args->deepen_since) {
1157 timestamp_t max_age = approxidate(args->deepen_since);
1158 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1160 if (args->deepen_not) {
1161 int i;
1162 for (i = 0; i < args->deepen_not->nr; i++) {
1163 struct string_list_item *s = args->deepen_not->items + i;
1164 packet_buf_write(req_buf, "deepen-not %s", s->string);
1167 if (args->deepen_relative)
1168 packet_buf_write(req_buf, "deepen-relative\n");
1171 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1173 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1175 for ( ; wants ; wants = wants->next) {
1176 const struct object_id *remote = &wants->old_oid;
1177 struct object *o;
1180 * If that object is complete (i.e. it is an ancestor of a
1181 * local ref), we tell them we have it but do not have to
1182 * tell them about its ancestors, which they already know
1183 * about.
1185 * We use lookup_object here because we are only
1186 * interested in the case we *know* the object is
1187 * reachable and we have already scanned it.
1189 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1190 (o->flags & COMPLETE)) {
1191 continue;
1194 if (!use_ref_in_want || wants->exact_oid)
1195 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1196 else
1197 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1201 static void add_common(struct strbuf *req_buf, struct oidset *common)
1203 struct oidset_iter iter;
1204 const struct object_id *oid;
1205 oidset_iter_init(common, &iter);
1207 while ((oid = oidset_iter_next(&iter))) {
1208 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1212 static int add_haves(struct fetch_negotiator *negotiator,
1213 struct strbuf *req_buf,
1214 int *haves_to_send)
1216 int haves_added = 0;
1217 const struct object_id *oid;
1219 while ((oid = negotiator->next(negotiator))) {
1220 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1221 if (++haves_added >= *haves_to_send)
1222 break;
1225 /* Increase haves to send on next round */
1226 *haves_to_send = next_flush(1, *haves_to_send);
1228 return haves_added;
1231 static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1232 const struct string_list *server_options)
1234 const char *hash_name;
1236 if (server_supports_v2("fetch", 1))
1237 packet_buf_write(req_buf, "command=fetch");
1238 if (server_supports_v2("agent", 0))
1239 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1240 if (advertise_sid && server_supports_v2("session-id", 0))
1241 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1242 if (server_options && server_options->nr &&
1243 server_supports_v2("server-option", 1)) {
1244 int i;
1245 for (i = 0; i < server_options->nr; i++)
1246 packet_buf_write(req_buf, "server-option=%s",
1247 server_options->items[i].string);
1250 if (server_feature_v2("object-format", &hash_name)) {
1251 int hash_algo = hash_algo_by_name(hash_name);
1252 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1253 die(_("mismatched algorithms: client %s; server %s"),
1254 the_hash_algo->name, hash_name);
1255 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1256 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1257 die(_("the server does not support algorithm '%s'"),
1258 the_hash_algo->name);
1260 packet_buf_delim(req_buf);
1263 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1264 struct fetch_pack_args *args,
1265 const struct ref *wants, struct oidset *common,
1266 int *haves_to_send, int *in_vain,
1267 int sideband_all, int seen_ack)
1269 int haves_added;
1270 int done_sent = 0;
1271 struct strbuf req_buf = STRBUF_INIT;
1273 write_fetch_command_and_capabilities(&req_buf, args->server_options);
1275 if (args->use_thin_pack)
1276 packet_buf_write(&req_buf, "thin-pack");
1277 if (args->no_progress)
1278 packet_buf_write(&req_buf, "no-progress");
1279 if (args->include_tag)
1280 packet_buf_write(&req_buf, "include-tag");
1281 if (prefer_ofs_delta)
1282 packet_buf_write(&req_buf, "ofs-delta");
1283 if (sideband_all)
1284 packet_buf_write(&req_buf, "sideband-all");
1286 /* Add shallow-info and deepen request */
1287 if (server_supports_feature("fetch", "shallow", 0))
1288 add_shallow_requests(&req_buf, args);
1289 else if (is_repository_shallow(the_repository) || args->deepen)
1290 die(_("Server does not support shallow requests"));
1292 /* Add filter */
1293 if (server_supports_feature("fetch", "filter", 0) &&
1294 args->filter_options.choice) {
1295 const char *spec =
1296 expand_list_objects_filter_spec(&args->filter_options);
1297 print_verbose(args, _("Server supports filter"));
1298 packet_buf_write(&req_buf, "filter %s", spec);
1299 } else if (args->filter_options.choice) {
1300 warning("filtering not recognized by server, ignoring");
1303 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1304 int i;
1305 struct strbuf to_send = STRBUF_INIT;
1307 for (i = 0; i < uri_protocols.nr; i++) {
1308 const char *s = uri_protocols.items[i].string;
1310 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1311 if (to_send.len)
1312 strbuf_addch(&to_send, ',');
1313 strbuf_addstr(&to_send, s);
1316 if (to_send.len) {
1317 packet_buf_write(&req_buf, "packfile-uris %s",
1318 to_send.buf);
1319 strbuf_release(&to_send);
1323 /* add wants */
1324 add_wants(wants, &req_buf);
1326 /* Add all of the common commits we've found in previous rounds */
1327 add_common(&req_buf, common);
1329 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1330 *in_vain += haves_added;
1331 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1332 /* Send Done */
1333 packet_buf_write(&req_buf, "done\n");
1334 done_sent = 1;
1337 /* Send request */
1338 packet_buf_flush(&req_buf);
1339 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1340 die_errno(_("unable to write request to remote"));
1342 strbuf_release(&req_buf);
1343 return done_sent;
1347 * Processes a section header in a server's response and checks if it matches
1348 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1349 * not consumed); if 0, the line will be consumed and the function will die if
1350 * the section header doesn't match what was expected.
1352 static int process_section_header(struct packet_reader *reader,
1353 const char *section, int peek)
1355 int ret;
1357 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1358 die(_("error reading section header '%s'"), section);
1360 ret = !strcmp(reader->line, section);
1362 if (!peek) {
1363 if (!ret)
1364 die(_("expected '%s', received '%s'"),
1365 section, reader->line);
1366 packet_reader_read(reader);
1369 return ret;
1372 static int process_ack(struct fetch_negotiator *negotiator,
1373 struct packet_reader *reader,
1374 struct object_id *common_oid,
1375 int *received_ready)
1377 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1378 const char *arg;
1380 if (!strcmp(reader->line, "NAK"))
1381 continue;
1383 if (skip_prefix(reader->line, "ACK ", &arg)) {
1384 if (!get_oid_hex(arg, common_oid)) {
1385 struct commit *commit;
1386 commit = lookup_commit(the_repository, common_oid);
1387 if (negotiator)
1388 negotiator->ack(negotiator, commit);
1390 return 1;
1393 if (!strcmp(reader->line, "ready")) {
1394 *received_ready = 1;
1395 continue;
1398 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1401 if (reader->status != PACKET_READ_FLUSH &&
1402 reader->status != PACKET_READ_DELIM)
1403 die(_("error processing acks: %d"), reader->status);
1406 * If an "acknowledgments" section is sent, a packfile is sent if and
1407 * only if "ready" was sent in this section. The other sections
1408 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1409 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1410 * otherwise.
1412 if (*received_ready && reader->status != PACKET_READ_DELIM)
1413 die(_("expected packfile to be sent after 'ready'"));
1414 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1415 die(_("expected no other sections to be sent after no 'ready'"));
1417 return 0;
1420 static void receive_shallow_info(struct fetch_pack_args *args,
1421 struct packet_reader *reader,
1422 struct oid_array *shallows,
1423 struct shallow_info *si)
1425 int unshallow_received = 0;
1427 process_section_header(reader, "shallow-info", 0);
1428 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1429 const char *arg;
1430 struct object_id oid;
1432 if (skip_prefix(reader->line, "shallow ", &arg)) {
1433 if (get_oid_hex(arg, &oid))
1434 die(_("invalid shallow line: %s"), reader->line);
1435 oid_array_append(shallows, &oid);
1436 continue;
1438 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1439 if (get_oid_hex(arg, &oid))
1440 die(_("invalid unshallow line: %s"), reader->line);
1441 if (!lookup_object(the_repository, &oid))
1442 die(_("object not found: %s"), reader->line);
1443 /* make sure that it is parsed as shallow */
1444 if (!parse_object(the_repository, &oid))
1445 die(_("error in object: %s"), reader->line);
1446 if (unregister_shallow(&oid))
1447 die(_("no shallow found: %s"), reader->line);
1448 unshallow_received = 1;
1449 continue;
1451 die(_("expected shallow/unshallow, got %s"), reader->line);
1454 if (reader->status != PACKET_READ_FLUSH &&
1455 reader->status != PACKET_READ_DELIM)
1456 die(_("error processing shallow info: %d"), reader->status);
1458 if (args->deepen || unshallow_received) {
1460 * Treat these as shallow lines caused by our depth settings.
1461 * In v0, these lines cannot cause refs to be rejected; do the
1462 * same.
1464 int i;
1466 for (i = 0; i < shallows->nr; i++)
1467 register_shallow(the_repository, &shallows->oid[i]);
1468 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1469 NULL);
1470 args->deepen = 1;
1471 } else if (shallows->nr) {
1473 * Treat these as shallow lines caused by the remote being
1474 * shallow. In v0, remote refs that reach these objects are
1475 * rejected (unless --update-shallow is set); do the same.
1477 prepare_shallow_info(si, shallows);
1478 if (si->nr_ours || si->nr_theirs) {
1479 if (args->reject_shallow_remote)
1480 die(_("source repository is shallow, reject to clone."));
1481 alternate_shallow_file =
1482 setup_temporary_shallow(si->shallow);
1483 } else
1484 alternate_shallow_file = NULL;
1485 } else {
1486 alternate_shallow_file = NULL;
1490 static int cmp_name_ref(const void *name, const void *ref)
1492 return strcmp(name, (*(struct ref **)ref)->name);
1495 static void receive_wanted_refs(struct packet_reader *reader,
1496 struct ref **sought, int nr_sought)
1498 process_section_header(reader, "wanted-refs", 0);
1499 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1500 struct object_id oid;
1501 const char *end;
1502 struct ref **found;
1504 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1505 die(_("expected wanted-ref, got '%s'"), reader->line);
1507 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1508 cmp_name_ref);
1509 if (!found)
1510 die(_("unexpected wanted-ref: '%s'"), reader->line);
1511 oidcpy(&(*found)->old_oid, &oid);
1514 if (reader->status != PACKET_READ_DELIM)
1515 die(_("error processing wanted refs: %d"), reader->status);
1518 static void receive_packfile_uris(struct packet_reader *reader,
1519 struct string_list *uris)
1521 process_section_header(reader, "packfile-uris", 0);
1522 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1523 if (reader->pktlen < the_hash_algo->hexsz ||
1524 reader->line[the_hash_algo->hexsz] != ' ')
1525 die("expected '<hash> <uri>', got: %s\n", reader->line);
1527 string_list_append(uris, reader->line);
1529 if (reader->status != PACKET_READ_DELIM)
1530 die("expected DELIM");
1533 enum fetch_state {
1534 FETCH_CHECK_LOCAL = 0,
1535 FETCH_SEND_REQUEST,
1536 FETCH_PROCESS_ACKS,
1537 FETCH_GET_PACK,
1538 FETCH_DONE,
1541 static void do_check_stateless_delimiter(int stateless_rpc,
1542 struct packet_reader *reader)
1544 check_stateless_delimiter(stateless_rpc, reader,
1545 _("git fetch-pack: expected response end packet"));
1548 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1549 int fd[2],
1550 const struct ref *orig_ref,
1551 struct ref **sought, int nr_sought,
1552 struct oid_array *shallows,
1553 struct shallow_info *si,
1554 struct string_list *pack_lockfiles)
1556 struct repository *r = the_repository;
1557 struct ref *ref = copy_ref_list(orig_ref);
1558 enum fetch_state state = FETCH_CHECK_LOCAL;
1559 struct oidset common = OIDSET_INIT;
1560 struct packet_reader reader;
1561 int in_vain = 0, negotiation_started = 0;
1562 int haves_to_send = INITIAL_FLUSH;
1563 struct fetch_negotiator negotiator_alloc;
1564 struct fetch_negotiator *negotiator;
1565 int seen_ack = 0;
1566 struct object_id common_oid;
1567 int received_ready = 0;
1568 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1569 int i;
1570 struct strvec index_pack_args = STRVEC_INIT;
1572 negotiator = &negotiator_alloc;
1573 fetch_negotiator_init(r, negotiator);
1575 packet_reader_init(&reader, fd[0], NULL, 0,
1576 PACKET_READ_CHOMP_NEWLINE |
1577 PACKET_READ_DIE_ON_ERR_PACKET);
1578 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1579 server_supports_feature("fetch", "sideband-all", 0)) {
1580 reader.use_sideband = 1;
1581 reader.me = "fetch-pack";
1584 while (state != FETCH_DONE) {
1585 switch (state) {
1586 case FETCH_CHECK_LOCAL:
1587 sort_ref_list(&ref, ref_compare_name);
1588 QSORT(sought, nr_sought, cmp_ref_by_name);
1590 /* v2 supports these by default */
1591 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1592 use_sideband = 2;
1593 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1594 args->deepen = 1;
1596 /* Filter 'ref' by 'sought' and those that aren't local */
1597 mark_complete_and_common_ref(negotiator, args, &ref);
1598 filter_refs(args, &ref, sought, nr_sought);
1599 if (everything_local(args, &ref))
1600 state = FETCH_DONE;
1601 else
1602 state = FETCH_SEND_REQUEST;
1604 mark_tips(negotiator, args->negotiation_tips);
1605 for_each_cached_alternate(negotiator,
1606 insert_one_alternate_object);
1607 break;
1608 case FETCH_SEND_REQUEST:
1609 if (!negotiation_started) {
1610 negotiation_started = 1;
1611 trace2_region_enter("fetch-pack",
1612 "negotiation_v2",
1613 the_repository);
1615 if (send_fetch_request(negotiator, fd[1], args, ref,
1616 &common,
1617 &haves_to_send, &in_vain,
1618 reader.use_sideband,
1619 seen_ack))
1620 state = FETCH_GET_PACK;
1621 else
1622 state = FETCH_PROCESS_ACKS;
1623 break;
1624 case FETCH_PROCESS_ACKS:
1625 /* Process ACKs/NAKs */
1626 process_section_header(&reader, "acknowledgments", 0);
1627 while (process_ack(negotiator, &reader, &common_oid,
1628 &received_ready)) {
1629 in_vain = 0;
1630 seen_ack = 1;
1631 oidset_insert(&common, &common_oid);
1633 if (received_ready) {
1635 * Don't check for response delimiter; get_pack() will
1636 * read the rest of this response.
1638 state = FETCH_GET_PACK;
1639 } else {
1640 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1641 state = FETCH_SEND_REQUEST;
1643 break;
1644 case FETCH_GET_PACK:
1645 trace2_region_leave("fetch-pack",
1646 "negotiation_v2",
1647 the_repository);
1648 /* Check for shallow-info section */
1649 if (process_section_header(&reader, "shallow-info", 1))
1650 receive_shallow_info(args, &reader, shallows, si);
1652 if (process_section_header(&reader, "wanted-refs", 1))
1653 receive_wanted_refs(&reader, sought, nr_sought);
1655 /* get the pack(s) */
1656 if (process_section_header(&reader, "packfile-uris", 1))
1657 receive_packfile_uris(&reader, &packfile_uris);
1658 process_section_header(&reader, "packfile", 0);
1661 * this is the final request we'll make of the server;
1662 * do a half-duplex shutdown to indicate that they can
1663 * hang up as soon as the pack is sent.
1665 close(fd[1]);
1666 fd[1] = -1;
1668 if (get_pack(args, fd, pack_lockfiles,
1669 packfile_uris.nr ? &index_pack_args : NULL,
1670 sought, nr_sought, &fsck_options.gitmodules_found))
1671 die(_("git fetch-pack: fetch failed."));
1672 do_check_stateless_delimiter(args->stateless_rpc, &reader);
1674 state = FETCH_DONE;
1675 break;
1676 case FETCH_DONE:
1677 continue;
1681 for (i = 0; i < packfile_uris.nr; i++) {
1682 int j;
1683 struct child_process cmd = CHILD_PROCESS_INIT;
1684 char packname[GIT_MAX_HEXSZ + 1];
1685 const char *uri = packfile_uris.items[i].string +
1686 the_hash_algo->hexsz + 1;
1688 strvec_push(&cmd.args, "http-fetch");
1689 strvec_pushf(&cmd.args, "--packfile=%.*s",
1690 (int) the_hash_algo->hexsz,
1691 packfile_uris.items[i].string);
1692 for (j = 0; j < index_pack_args.nr; j++)
1693 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1694 index_pack_args.v[j]);
1695 strvec_push(&cmd.args, uri);
1696 cmd.git_cmd = 1;
1697 cmd.no_stdin = 1;
1698 cmd.out = -1;
1699 if (start_command(&cmd))
1700 die("fetch-pack: unable to spawn http-fetch");
1702 if (read_in_full(cmd.out, packname, 5) < 0 ||
1703 memcmp(packname, "keep\t", 5))
1704 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1706 if (read_in_full(cmd.out, packname,
1707 the_hash_algo->hexsz + 1) < 0 ||
1708 packname[the_hash_algo->hexsz] != '\n')
1709 die("fetch-pack: expected hash then LF at end of http-fetch output");
1711 packname[the_hash_algo->hexsz] = '\0';
1713 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1715 close(cmd.out);
1717 if (finish_command(&cmd))
1718 die("fetch-pack: unable to finish http-fetch");
1720 if (memcmp(packfile_uris.items[i].string, packname,
1721 the_hash_algo->hexsz))
1722 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1723 uri, (int) the_hash_algo->hexsz,
1724 packfile_uris.items[i].string);
1726 string_list_append_nodup(pack_lockfiles,
1727 xstrfmt("%s/pack/pack-%s.keep",
1728 get_object_directory(),
1729 packname));
1731 string_list_clear(&packfile_uris, 0);
1732 strvec_clear(&index_pack_args);
1734 if (fsck_finish(&fsck_options))
1735 die("fsck failed");
1737 if (negotiator)
1738 negotiator->release(negotiator);
1740 oidset_clear(&common);
1741 return ref;
1744 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1746 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1747 const char *path;
1749 if (git_config_pathname(&path, var, value))
1750 return 1;
1751 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1752 fsck_msg_types.len ? ',' : '=', path);
1753 free((char *)path);
1754 return 0;
1757 if (skip_prefix(var, "fetch.fsck.", &var)) {
1758 if (is_valid_msg_type(var, value))
1759 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1760 fsck_msg_types.len ? ',' : '=', var, value);
1761 else
1762 warning("Skipping unknown msg id '%s'", var);
1763 return 0;
1766 return git_default_config(var, value, cb);
1769 static void fetch_pack_config(void)
1771 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1772 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1773 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1774 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1775 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1776 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1777 if (!uri_protocols.nr) {
1778 char *str;
1780 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1781 string_list_split(&uri_protocols, str, ',', -1);
1782 free(str);
1786 git_config(fetch_pack_config_cb, NULL);
1789 static void fetch_pack_setup(void)
1791 static int did_setup;
1792 if (did_setup)
1793 return;
1794 fetch_pack_config();
1795 if (0 <= transfer_unpack_limit)
1796 unpack_limit = transfer_unpack_limit;
1797 else if (0 <= fetch_unpack_limit)
1798 unpack_limit = fetch_unpack_limit;
1799 did_setup = 1;
1802 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1804 struct string_list names = STRING_LIST_INIT_NODUP;
1805 int src, dst;
1807 for (src = dst = 0; src < nr; src++) {
1808 struct string_list_item *item;
1809 item = string_list_insert(&names, ref[src]->name);
1810 if (item->util)
1811 continue; /* already have it */
1812 item->util = ref[src];
1813 if (src != dst)
1814 ref[dst] = ref[src];
1815 dst++;
1817 for (src = dst; src < nr; src++)
1818 ref[src] = NULL;
1819 string_list_clear(&names, 0);
1820 return dst;
1823 static void update_shallow(struct fetch_pack_args *args,
1824 struct ref **sought, int nr_sought,
1825 struct shallow_info *si)
1827 struct oid_array ref = OID_ARRAY_INIT;
1828 int *status;
1829 int i;
1831 if (args->deepen && alternate_shallow_file) {
1832 if (*alternate_shallow_file == '\0') { /* --unshallow */
1833 unlink_or_warn(git_path_shallow(the_repository));
1834 rollback_shallow_file(the_repository, &shallow_lock);
1835 } else
1836 commit_shallow_file(the_repository, &shallow_lock);
1837 alternate_shallow_file = NULL;
1838 return;
1841 if (!si->shallow || !si->shallow->nr)
1842 return;
1844 if (args->cloning) {
1846 * remote is shallow, but this is a clone, there are
1847 * no objects in repo to worry about. Accept any
1848 * shallow points that exist in the pack (iow in repo
1849 * after get_pack() and reprepare_packed_git())
1851 struct oid_array extra = OID_ARRAY_INIT;
1852 struct object_id *oid = si->shallow->oid;
1853 for (i = 0; i < si->shallow->nr; i++)
1854 if (has_object_file(&oid[i]))
1855 oid_array_append(&extra, &oid[i]);
1856 if (extra.nr) {
1857 setup_alternate_shallow(&shallow_lock,
1858 &alternate_shallow_file,
1859 &extra);
1860 commit_shallow_file(the_repository, &shallow_lock);
1861 alternate_shallow_file = NULL;
1863 oid_array_clear(&extra);
1864 return;
1867 if (!si->nr_ours && !si->nr_theirs)
1868 return;
1870 remove_nonexistent_theirs_shallow(si);
1871 if (!si->nr_ours && !si->nr_theirs)
1872 return;
1873 for (i = 0; i < nr_sought; i++)
1874 oid_array_append(&ref, &sought[i]->old_oid);
1875 si->ref = &ref;
1877 if (args->update_shallow) {
1879 * remote is also shallow, .git/shallow may be updated
1880 * so all refs can be accepted. Make sure we only add
1881 * shallow roots that are actually reachable from new
1882 * refs.
1884 struct oid_array extra = OID_ARRAY_INIT;
1885 struct object_id *oid = si->shallow->oid;
1886 assign_shallow_commits_to_refs(si, NULL, NULL);
1887 if (!si->nr_ours && !si->nr_theirs) {
1888 oid_array_clear(&ref);
1889 return;
1891 for (i = 0; i < si->nr_ours; i++)
1892 oid_array_append(&extra, &oid[si->ours[i]]);
1893 for (i = 0; i < si->nr_theirs; i++)
1894 oid_array_append(&extra, &oid[si->theirs[i]]);
1895 setup_alternate_shallow(&shallow_lock,
1896 &alternate_shallow_file,
1897 &extra);
1898 commit_shallow_file(the_repository, &shallow_lock);
1899 oid_array_clear(&extra);
1900 oid_array_clear(&ref);
1901 alternate_shallow_file = NULL;
1902 return;
1906 * remote is also shallow, check what ref is safe to update
1907 * without updating .git/shallow
1909 CALLOC_ARRAY(status, nr_sought);
1910 assign_shallow_commits_to_refs(si, NULL, status);
1911 if (si->nr_ours || si->nr_theirs) {
1912 for (i = 0; i < nr_sought; i++)
1913 if (status[i])
1914 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1916 free(status);
1917 oid_array_clear(&ref);
1920 static const struct object_id *iterate_ref_map(void *cb_data)
1922 struct ref **rm = cb_data;
1923 struct ref *ref = *rm;
1925 if (!ref)
1926 return NULL;
1927 *rm = ref->next;
1928 return &ref->old_oid;
1931 struct ref *fetch_pack(struct fetch_pack_args *args,
1932 int fd[],
1933 const struct ref *ref,
1934 struct ref **sought, int nr_sought,
1935 struct oid_array *shallow,
1936 struct string_list *pack_lockfiles,
1937 enum protocol_version version)
1939 struct ref *ref_cpy;
1940 struct shallow_info si;
1941 struct oid_array shallows_scratch = OID_ARRAY_INIT;
1943 fetch_pack_setup();
1944 if (nr_sought)
1945 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1947 if (version != protocol_v2 && !ref) {
1948 packet_flush(fd[1]);
1949 die(_("no matching remote head"));
1951 if (version == protocol_v2) {
1952 if (shallow->nr)
1953 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1954 memset(&si, 0, sizeof(si));
1955 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1956 &shallows_scratch, &si,
1957 pack_lockfiles);
1958 } else {
1959 prepare_shallow_info(&si, shallow);
1960 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1961 &si, pack_lockfiles);
1963 reprepare_packed_git(the_repository);
1965 if (!args->cloning && args->deepen) {
1966 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1967 struct ref *iterator = ref_cpy;
1968 opt.shallow_file = alternate_shallow_file;
1969 if (args->deepen)
1970 opt.is_deepening_fetch = 1;
1971 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1972 error(_("remote did not send all necessary objects"));
1973 free_refs(ref_cpy);
1974 ref_cpy = NULL;
1975 rollback_shallow_file(the_repository, &shallow_lock);
1976 goto cleanup;
1978 args->connectivity_checked = 1;
1981 update_shallow(args, sought, nr_sought, &si);
1982 cleanup:
1983 clear_shallow_info(&si);
1984 oid_array_clear(&shallows_scratch);
1985 return ref_cpy;
1988 static int add_to_object_array(const struct object_id *oid, void *data)
1990 struct object_array *a = data;
1992 add_object_array(lookup_object(the_repository, oid), "", a);
1993 return 0;
1996 static void clear_common_flag(struct oidset *s)
1998 struct oidset_iter iter;
1999 const struct object_id *oid;
2000 oidset_iter_init(s, &iter);
2002 while ((oid = oidset_iter_next(&iter))) {
2003 struct object *obj = lookup_object(the_repository, oid);
2004 obj->flags &= ~COMMON;
2008 void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2009 const struct string_list *server_options,
2010 int stateless_rpc,
2011 int fd[],
2012 struct oidset *acked_commits)
2014 struct fetch_negotiator negotiator;
2015 struct packet_reader reader;
2016 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2017 struct strbuf req_buf = STRBUF_INIT;
2018 int haves_to_send = INITIAL_FLUSH;
2019 int in_vain = 0;
2020 int seen_ack = 0;
2021 int last_iteration = 0;
2022 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2024 fetch_negotiator_init(the_repository, &negotiator);
2025 mark_tips(&negotiator, negotiation_tips);
2027 packet_reader_init(&reader, fd[0], NULL, 0,
2028 PACKET_READ_CHOMP_NEWLINE |
2029 PACKET_READ_DIE_ON_ERR_PACKET);
2031 oid_array_for_each((struct oid_array *) negotiation_tips,
2032 add_to_object_array,
2033 &nt_object_array);
2035 while (!last_iteration) {
2036 int haves_added;
2037 struct object_id common_oid;
2038 int received_ready = 0;
2040 strbuf_reset(&req_buf);
2041 write_fetch_command_and_capabilities(&req_buf, server_options);
2043 packet_buf_write(&req_buf, "wait-for-done");
2045 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2046 in_vain += haves_added;
2047 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2048 last_iteration = 1;
2050 /* Send request */
2051 packet_buf_flush(&req_buf);
2052 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2053 die_errno(_("unable to write request to remote"));
2055 /* Process ACKs/NAKs */
2056 process_section_header(&reader, "acknowledgments", 0);
2057 while (process_ack(&negotiator, &reader, &common_oid,
2058 &received_ready)) {
2059 struct commit *commit = lookup_commit(the_repository,
2060 &common_oid);
2061 if (commit) {
2062 timestamp_t generation;
2064 parse_commit_or_die(commit);
2065 commit->object.flags |= COMMON;
2066 generation = commit_graph_generation(commit);
2067 if (generation < min_generation)
2068 min_generation = generation;
2070 in_vain = 0;
2071 seen_ack = 1;
2072 oidset_insert(acked_commits, &common_oid);
2074 if (received_ready)
2075 die(_("unexpected 'ready' from remote"));
2076 else
2077 do_check_stateless_delimiter(stateless_rpc, &reader);
2078 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2079 REACH_SCRATCH, 0,
2080 min_generation))
2081 last_iteration = 1;
2083 clear_common_flag(acked_commits);
2084 strbuf_release(&req_buf);
2087 int report_unmatched_refs(struct ref **sought, int nr_sought)
2089 int i, ret = 0;
2091 for (i = 0; i < nr_sought; i++) {
2092 if (!sought[i])
2093 continue;
2094 switch (sought[i]->match_status) {
2095 case REF_MATCHED:
2096 continue;
2097 case REF_NOT_MATCHED:
2098 error(_("no such remote ref %s"), sought[i]->name);
2099 break;
2100 case REF_UNADVERTISED_NOT_ALLOWED:
2101 error(_("Server does not allow request for unadvertised object %s"),
2102 sought[i]->name);
2103 break;
2105 ret = 1;
2107 return ret;