fetch: add trace2 instrumentation
[git/debian.git] / fetch-pack.c
blobc016eeab1d862224c812ba47ab1832e692181b73
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 "sha1-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"
26 static int transfer_unpack_limit = -1;
27 static int fetch_unpack_limit = -1;
28 static int unpack_limit = 100;
29 static int prefer_ofs_delta = 1;
30 static int no_done;
31 static int deepen_since_ok;
32 static int deepen_not_ok;
33 static int fetch_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int agent_supported;
36 static int server_supports_filtering;
37 static struct lock_file shallow_lock;
38 static const char *alternate_shallow_file;
39 static struct strbuf fsck_msg_types = STRBUF_INIT;
41 /* Remember to update object flag allocation in object.h */
42 #define COMPLETE (1U << 0)
43 #define ALTERNATE (1U << 1)
46 * After sending this many "have"s if we do not get any new ACK , we
47 * give up traversing our history.
49 #define MAX_IN_VAIN 256
51 static int multi_ack, use_sideband;
52 /* Allow specifying sha1 if it is a ref tip. */
53 #define ALLOW_TIP_SHA1 01
54 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
55 #define ALLOW_REACHABLE_SHA1 02
56 static unsigned int allow_unadvertised_object_request;
58 __attribute__((format (printf, 2, 3)))
59 static inline void print_verbose(const struct fetch_pack_args *args,
60 const char *fmt, ...)
62 va_list params;
64 if (!args->verbose)
65 return;
67 va_start(params, fmt);
68 vfprintf(stderr, fmt, params);
69 va_end(params);
70 fputc('\n', stderr);
73 struct alternate_object_cache {
74 struct object **items;
75 size_t nr, alloc;
78 static void cache_one_alternate(const struct object_id *oid,
79 void *vcache)
81 struct alternate_object_cache *cache = vcache;
82 struct object *obj = parse_object(the_repository, oid);
84 if (!obj || (obj->flags & ALTERNATE))
85 return;
87 obj->flags |= ALTERNATE;
88 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
89 cache->items[cache->nr++] = obj;
92 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
93 void (*cb)(struct fetch_negotiator *,
94 struct object *))
96 static int initialized;
97 static struct alternate_object_cache cache;
98 size_t i;
100 if (!initialized) {
101 for_each_alternate_ref(cache_one_alternate, &cache);
102 initialized = 1;
105 for (i = 0; i < cache.nr; i++)
106 cb(negotiator, cache.items[i]);
109 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
110 const char *refname,
111 const struct object_id *oid)
113 struct object *o = deref_tag(the_repository,
114 parse_object(the_repository, oid),
115 refname, 0);
117 if (o && o->type == OBJ_COMMIT)
118 negotiator->add_tip(negotiator, (struct commit *)o);
120 return 0;
123 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
124 int flag, void *cb_data)
126 return rev_list_insert_ref(cb_data, refname, oid);
129 enum ack_type {
130 NAK = 0,
131 ACK,
132 ACK_continue,
133 ACK_common,
134 ACK_ready
137 static void consume_shallow_list(struct fetch_pack_args *args,
138 struct packet_reader *reader)
140 if (args->stateless_rpc && args->deepen) {
141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
145 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
146 if (starts_with(reader->line, "shallow "))
147 continue;
148 if (starts_with(reader->line, "unshallow "))
149 continue;
150 die(_("git fetch-pack: expected shallow list"));
152 if (reader->status != PACKET_READ_FLUSH)
153 die(_("git fetch-pack: expected a flush packet after shallow list"));
157 static enum ack_type get_ack(struct packet_reader *reader,
158 struct object_id *result_oid)
160 int len;
161 const char *arg;
163 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
165 len = reader->pktlen;
167 if (!strcmp(reader->line, "NAK"))
168 return NAK;
169 if (skip_prefix(reader->line, "ACK ", &arg)) {
170 if (!get_oid_hex(arg, result_oid)) {
171 arg += 40;
172 len -= arg - reader->line;
173 if (len < 1)
174 return ACK;
175 if (strstr(arg, "continue"))
176 return ACK_continue;
177 if (strstr(arg, "common"))
178 return ACK_common;
179 if (strstr(arg, "ready"))
180 return ACK_ready;
181 return ACK;
184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
187 static void send_request(struct fetch_pack_args *args,
188 int fd, struct strbuf *buf)
190 if (args->stateless_rpc) {
191 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
192 packet_flush(fd);
193 } else {
194 if (write_in_full(fd, buf->buf, buf->len) < 0)
195 die_errno(_("unable to write to remote"));
199 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
200 struct object *obj)
202 rev_list_insert_ref(negotiator, NULL, &obj->oid);
205 #define INITIAL_FLUSH 16
206 #define PIPESAFE_FLUSH 32
207 #define LARGE_FLUSH 16384
209 static int next_flush(int stateless_rpc, int count)
211 if (stateless_rpc) {
212 if (count < LARGE_FLUSH)
213 count <<= 1;
214 else
215 count = count * 11 / 10;
216 } else {
217 if (count < PIPESAFE_FLUSH)
218 count <<= 1;
219 else
220 count += PIPESAFE_FLUSH;
222 return count;
225 static void mark_tips(struct fetch_negotiator *negotiator,
226 const struct oid_array *negotiation_tips)
228 int i;
230 if (!negotiation_tips) {
231 for_each_ref(rev_list_insert_ref_oid, negotiator);
232 return;
235 for (i = 0; i < negotiation_tips->nr; i++)
236 rev_list_insert_ref(negotiator, NULL,
237 &negotiation_tips->oid[i]);
238 return;
241 static int find_common(struct fetch_negotiator *negotiator,
242 struct fetch_pack_args *args,
243 int fd[2], struct object_id *result_oid,
244 struct ref *refs)
246 int fetching;
247 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
248 const struct object_id *oid;
249 unsigned in_vain = 0;
250 int got_continue = 0;
251 int got_ready = 0;
252 struct strbuf req_buf = STRBUF_INIT;
253 size_t state_len = 0;
254 struct packet_reader reader;
256 if (args->stateless_rpc && multi_ack == 1)
257 die(_("--stateless-rpc requires multi_ack_detailed"));
259 packet_reader_init(&reader, fd[0], NULL, 0,
260 PACKET_READ_CHOMP_NEWLINE |
261 PACKET_READ_DIE_ON_ERR_PACKET);
263 if (!args->no_dependents) {
264 mark_tips(negotiator, args->negotiation_tips);
265 for_each_cached_alternate(negotiator, insert_one_alternate_object);
268 fetching = 0;
269 for ( ; refs ; refs = refs->next) {
270 struct object_id *remote = &refs->old_oid;
271 const char *remote_hex;
272 struct object *o;
275 * If that object is complete (i.e. it is an ancestor of a
276 * local ref), we tell them we have it but do not have to
277 * tell them about its ancestors, which they already know
278 * about.
280 * We use lookup_object here because we are only
281 * interested in the case we *know* the object is
282 * reachable and we have already scanned it.
284 * Do this only if args->no_dependents is false (if it is true,
285 * we cannot trust the object flags).
287 if (!args->no_dependents &&
288 ((o = lookup_object(the_repository, remote)) != NULL) &&
289 (o->flags & COMPLETE)) {
290 continue;
293 remote_hex = oid_to_hex(remote);
294 if (!fetching) {
295 struct strbuf c = STRBUF_INIT;
296 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
297 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
298 if (no_done) strbuf_addstr(&c, " no-done");
299 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
300 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
301 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
302 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
303 if (args->no_progress) strbuf_addstr(&c, " no-progress");
304 if (args->include_tag) strbuf_addstr(&c, " include-tag");
305 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
306 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
307 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
308 if (agent_supported) strbuf_addf(&c, " agent=%s",
309 git_user_agent_sanitized());
310 if (args->filter_options.choice)
311 strbuf_addstr(&c, " filter");
312 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
313 strbuf_release(&c);
314 } else
315 packet_buf_write(&req_buf, "want %s\n", remote_hex);
316 fetching++;
319 if (!fetching) {
320 strbuf_release(&req_buf);
321 packet_flush(fd[1]);
322 return 1;
325 if (is_repository_shallow(the_repository))
326 write_shallow_commits(&req_buf, 1, NULL);
327 if (args->depth > 0)
328 packet_buf_write(&req_buf, "deepen %d", args->depth);
329 if (args->deepen_since) {
330 timestamp_t max_age = approxidate(args->deepen_since);
331 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
333 if (args->deepen_not) {
334 int i;
335 for (i = 0; i < args->deepen_not->nr; i++) {
336 struct string_list_item *s = args->deepen_not->items + i;
337 packet_buf_write(&req_buf, "deepen-not %s", s->string);
340 if (server_supports_filtering && args->filter_options.choice) {
341 const char *spec =
342 expand_list_objects_filter_spec(&args->filter_options);
343 packet_buf_write(&req_buf, "filter %s", spec);
345 packet_buf_flush(&req_buf);
346 state_len = req_buf.len;
348 if (args->deepen) {
349 const char *arg;
350 struct object_id oid;
352 send_request(args, fd[1], &req_buf);
353 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
354 if (skip_prefix(reader.line, "shallow ", &arg)) {
355 if (get_oid_hex(arg, &oid))
356 die(_("invalid shallow line: %s"), reader.line);
357 register_shallow(the_repository, &oid);
358 continue;
360 if (skip_prefix(reader.line, "unshallow ", &arg)) {
361 if (get_oid_hex(arg, &oid))
362 die(_("invalid unshallow line: %s"), reader.line);
363 if (!lookup_object(the_repository, &oid))
364 die(_("object not found: %s"), reader.line);
365 /* make sure that it is parsed as shallow */
366 if (!parse_object(the_repository, &oid))
367 die(_("error in object: %s"), reader.line);
368 if (unregister_shallow(&oid))
369 die(_("no shallow found: %s"), reader.line);
370 continue;
372 die(_("expected shallow/unshallow, got %s"), reader.line);
374 } else if (!args->stateless_rpc)
375 send_request(args, fd[1], &req_buf);
377 if (!args->stateless_rpc) {
378 /* If we aren't using the stateless-rpc interface
379 * we don't need to retain the headers.
381 strbuf_setlen(&req_buf, 0);
382 state_len = 0;
385 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
386 flushes = 0;
387 retval = -1;
388 if (args->no_dependents)
389 goto done;
390 while ((oid = negotiator->next(negotiator))) {
391 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
392 print_verbose(args, "have %s", oid_to_hex(oid));
393 in_vain++;
394 if (flush_at <= ++count) {
395 int ack;
397 packet_buf_flush(&req_buf);
398 send_request(args, fd[1], &req_buf);
399 strbuf_setlen(&req_buf, state_len);
400 flushes++;
401 flush_at = next_flush(args->stateless_rpc, count);
404 * We keep one window "ahead" of the other side, and
405 * will wait for an ACK only on the next one
407 if (!args->stateless_rpc && count == INITIAL_FLUSH)
408 continue;
410 consume_shallow_list(args, &reader);
411 do {
412 ack = get_ack(&reader, result_oid);
413 if (ack)
414 print_verbose(args, _("got %s %d %s"), "ack",
415 ack, oid_to_hex(result_oid));
416 switch (ack) {
417 case ACK:
418 flushes = 0;
419 multi_ack = 0;
420 retval = 0;
421 goto done;
422 case ACK_common:
423 case ACK_ready:
424 case ACK_continue: {
425 struct commit *commit =
426 lookup_commit(the_repository,
427 result_oid);
428 int was_common;
430 if (!commit)
431 die(_("invalid commit %s"), oid_to_hex(result_oid));
432 was_common = negotiator->ack(negotiator, commit);
433 if (args->stateless_rpc
434 && ack == ACK_common
435 && !was_common) {
436 /* We need to replay the have for this object
437 * on the next RPC request so the peer knows
438 * it is in common with us.
440 const char *hex = oid_to_hex(result_oid);
441 packet_buf_write(&req_buf, "have %s\n", hex);
442 state_len = req_buf.len;
444 * Reset in_vain because an ack
445 * for this commit has not been
446 * seen.
448 in_vain = 0;
449 } else if (!args->stateless_rpc
450 || ack != ACK_common)
451 in_vain = 0;
452 retval = 0;
453 got_continue = 1;
454 if (ack == ACK_ready)
455 got_ready = 1;
456 break;
459 } while (ack);
460 flushes--;
461 if (got_continue && MAX_IN_VAIN < in_vain) {
462 print_verbose(args, _("giving up"));
463 break; /* give up */
465 if (got_ready)
466 break;
469 done:
470 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
471 if (!got_ready || !no_done) {
472 packet_buf_write(&req_buf, "done\n");
473 send_request(args, fd[1], &req_buf);
475 print_verbose(args, _("done"));
476 if (retval != 0) {
477 multi_ack = 0;
478 flushes++;
480 strbuf_release(&req_buf);
482 if (!got_ready || !no_done)
483 consume_shallow_list(args, &reader);
484 while (flushes || multi_ack) {
485 int ack = get_ack(&reader, result_oid);
486 if (ack) {
487 print_verbose(args, _("got %s (%d) %s"), "ack",
488 ack, oid_to_hex(result_oid));
489 if (ack == ACK)
490 return 0;
491 multi_ack = 1;
492 continue;
494 flushes--;
496 /* it is no error to fetch into a completely empty repo */
497 return count ? retval : 0;
500 static struct commit_list *complete;
502 static int mark_complete(const struct object_id *oid)
504 struct object *o = parse_object(the_repository, oid);
506 while (o && o->type == OBJ_TAG) {
507 struct tag *t = (struct tag *) o;
508 if (!t->tagged)
509 break; /* broken repository */
510 o->flags |= COMPLETE;
511 o = parse_object(the_repository, &t->tagged->oid);
513 if (o && o->type == OBJ_COMMIT) {
514 struct commit *commit = (struct commit *)o;
515 if (!(commit->object.flags & COMPLETE)) {
516 commit->object.flags |= COMPLETE;
517 commit_list_insert(commit, &complete);
520 return 0;
523 static int mark_complete_oid(const char *refname, const struct object_id *oid,
524 int flag, void *cb_data)
526 return mark_complete(oid);
529 static void mark_recent_complete_commits(struct fetch_pack_args *args,
530 timestamp_t cutoff)
532 while (complete && cutoff <= complete->item->date) {
533 print_verbose(args, _("Marking %s as complete"),
534 oid_to_hex(&complete->item->object.oid));
535 pop_most_recent_commit(&complete, COMPLETE);
539 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
541 for (; refs; refs = refs->next)
542 oidset_insert(oids, &refs->old_oid);
545 static int is_unmatched_ref(const struct ref *ref)
547 struct object_id oid;
548 const char *p;
549 return ref->match_status == REF_NOT_MATCHED &&
550 !parse_oid_hex(ref->name, &oid, &p) &&
551 *p == '\0' &&
552 oideq(&oid, &ref->old_oid);
555 static void filter_refs(struct fetch_pack_args *args,
556 struct ref **refs,
557 struct ref **sought, int nr_sought)
559 struct ref *newlist = NULL;
560 struct ref **newtail = &newlist;
561 struct ref *unmatched = NULL;
562 struct ref *ref, *next;
563 struct oidset tip_oids = OIDSET_INIT;
564 int i;
565 int strict = !(allow_unadvertised_object_request &
566 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
568 i = 0;
569 for (ref = *refs; ref; ref = next) {
570 int keep = 0;
571 next = ref->next;
573 if (starts_with(ref->name, "refs/") &&
574 check_refname_format(ref->name, 0)) {
576 * trash or a peeled value; do not even add it to
577 * unmatched list
579 free_one_ref(ref);
580 continue;
581 } else {
582 while (i < nr_sought) {
583 int cmp = strcmp(ref->name, sought[i]->name);
584 if (cmp < 0)
585 break; /* definitely do not have it */
586 else if (cmp == 0) {
587 keep = 1; /* definitely have it */
588 sought[i]->match_status = REF_MATCHED;
590 i++;
593 if (!keep && args->fetch_all &&
594 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
595 keep = 1;
598 if (keep) {
599 *newtail = ref;
600 ref->next = NULL;
601 newtail = &ref->next;
602 } else {
603 ref->next = unmatched;
604 unmatched = ref;
608 if (strict) {
609 for (i = 0; i < nr_sought; i++) {
610 ref = sought[i];
611 if (!is_unmatched_ref(ref))
612 continue;
614 add_refs_to_oidset(&tip_oids, unmatched);
615 add_refs_to_oidset(&tip_oids, newlist);
616 break;
620 /* Append unmatched requests to the list */
621 for (i = 0; i < nr_sought; i++) {
622 ref = sought[i];
623 if (!is_unmatched_ref(ref))
624 continue;
626 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
627 ref->match_status = REF_MATCHED;
628 *newtail = copy_ref(ref);
629 newtail = &(*newtail)->next;
630 } else {
631 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
635 oidset_clear(&tip_oids);
636 free_refs(unmatched);
638 *refs = newlist;
641 static void mark_alternate_complete(struct fetch_negotiator *unused,
642 struct object *obj)
644 mark_complete(&obj->oid);
647 struct loose_object_iter {
648 struct oidset *loose_object_set;
649 struct ref *refs;
653 * Mark recent commits available locally and reachable from a local ref as
654 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
655 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
656 * thus do not need COMMON_REF marks).
658 * The cutoff time for recency is determined by this heuristic: it is the
659 * earliest commit time of the objects in refs that are commits and that we know
660 * the commit time of.
662 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
663 struct fetch_pack_args *args,
664 struct ref **refs)
666 struct ref *ref;
667 int old_save_commit_buffer = save_commit_buffer;
668 timestamp_t cutoff = 0;
670 save_commit_buffer = 0;
672 for (ref = *refs; ref; ref = ref->next) {
673 struct object *o;
675 if (!has_object_file_with_flags(&ref->old_oid,
676 OBJECT_INFO_QUICK))
677 continue;
678 o = parse_object(the_repository, &ref->old_oid);
679 if (!o)
680 continue;
682 /* We already have it -- which may mean that we were
683 * in sync with the other side at some time after
684 * that (it is OK if we guess wrong here).
686 if (o->type == OBJ_COMMIT) {
687 struct commit *commit = (struct commit *)o;
688 if (!cutoff || cutoff < commit->date)
689 cutoff = commit->date;
693 if (!args->deepen) {
694 for_each_ref(mark_complete_oid, NULL);
695 for_each_cached_alternate(NULL, mark_alternate_complete);
696 commit_list_sort_by_date(&complete);
697 if (cutoff)
698 mark_recent_complete_commits(args, cutoff);
702 * Mark all complete remote refs as common refs.
703 * Don't mark them common yet; the server has to be told so first.
705 for (ref = *refs; ref; ref = ref->next) {
706 struct object *o = deref_tag(the_repository,
707 lookup_object(the_repository,
708 &ref->old_oid),
709 NULL, 0);
711 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
712 continue;
714 negotiator->known_common(negotiator,
715 (struct commit *)o);
718 save_commit_buffer = old_save_commit_buffer;
722 * Returns 1 if every object pointed to by the given remote refs is available
723 * locally and reachable from a local ref, and 0 otherwise.
725 static int everything_local(struct fetch_pack_args *args,
726 struct ref **refs)
728 struct ref *ref;
729 int retval;
731 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
732 const struct object_id *remote = &ref->old_oid;
733 struct object *o;
735 o = lookup_object(the_repository, remote);
736 if (!o || !(o->flags & COMPLETE)) {
737 retval = 0;
738 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
739 ref->name);
740 continue;
742 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
743 ref->name);
746 return retval;
749 static int sideband_demux(int in, int out, void *data)
751 int *xd = data;
752 int ret;
754 ret = recv_sideband("fetch-pack", xd[0], out);
755 close(out);
756 return ret;
759 static int get_pack(struct fetch_pack_args *args,
760 int xd[2], char **pack_lockfile)
762 struct async demux;
763 int do_keep = args->keep_pack;
764 const char *cmd_name;
765 struct pack_header header;
766 int pass_header = 0;
767 struct child_process cmd = CHILD_PROCESS_INIT;
768 int ret;
770 memset(&demux, 0, sizeof(demux));
771 if (use_sideband) {
772 /* xd[] is talking with upload-pack; subprocess reads from
773 * xd[0], spits out band#2 to stderr, and feeds us band#1
774 * through demux->out.
776 demux.proc = sideband_demux;
777 demux.data = xd;
778 demux.out = -1;
779 demux.isolate_sigpipe = 1;
780 if (start_async(&demux))
781 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
783 else
784 demux.out = xd[0];
786 if (!args->keep_pack && unpack_limit) {
788 if (read_pack_header(demux.out, &header))
789 die(_("protocol error: bad pack header"));
790 pass_header = 1;
791 if (ntohl(header.hdr_entries) < unpack_limit)
792 do_keep = 0;
793 else
794 do_keep = 1;
797 if (alternate_shallow_file) {
798 argv_array_push(&cmd.args, "--shallow-file");
799 argv_array_push(&cmd.args, alternate_shallow_file);
802 if (do_keep || args->from_promisor) {
803 if (pack_lockfile)
804 cmd.out = -1;
805 cmd_name = "index-pack";
806 argv_array_push(&cmd.args, cmd_name);
807 argv_array_push(&cmd.args, "--stdin");
808 if (!args->quiet && !args->no_progress)
809 argv_array_push(&cmd.args, "-v");
810 if (args->use_thin_pack)
811 argv_array_push(&cmd.args, "--fix-thin");
812 if (do_keep && (args->lock_pack || unpack_limit)) {
813 char hostname[HOST_NAME_MAX + 1];
814 if (xgethostname(hostname, sizeof(hostname)))
815 xsnprintf(hostname, sizeof(hostname), "localhost");
816 argv_array_pushf(&cmd.args,
817 "--keep=fetch-pack %"PRIuMAX " on %s",
818 (uintmax_t)getpid(), hostname);
820 if (args->check_self_contained_and_connected)
821 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
822 if (args->from_promisor)
823 argv_array_push(&cmd.args, "--promisor");
825 else {
826 cmd_name = "unpack-objects";
827 argv_array_push(&cmd.args, cmd_name);
828 if (args->quiet || args->no_progress)
829 argv_array_push(&cmd.args, "-q");
830 args->check_self_contained_and_connected = 0;
833 if (pass_header)
834 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
835 ntohl(header.hdr_version),
836 ntohl(header.hdr_entries));
837 if (fetch_fsck_objects >= 0
838 ? fetch_fsck_objects
839 : transfer_fsck_objects >= 0
840 ? transfer_fsck_objects
841 : 0) {
842 if (args->from_promisor)
844 * We cannot use --strict in index-pack because it
845 * checks both broken objects and links, but we only
846 * want to check for broken objects.
848 argv_array_push(&cmd.args, "--fsck-objects");
849 else
850 argv_array_pushf(&cmd.args, "--strict%s",
851 fsck_msg_types.buf);
854 cmd.in = demux.out;
855 cmd.git_cmd = 1;
856 if (start_command(&cmd))
857 die(_("fetch-pack: unable to fork off %s"), cmd_name);
858 if (do_keep && pack_lockfile) {
859 *pack_lockfile = index_pack_lockfile(cmd.out);
860 close(cmd.out);
863 if (!use_sideband)
864 /* Closed by start_command() */
865 xd[0] = -1;
867 ret = finish_command(&cmd);
868 if (!ret || (args->check_self_contained_and_connected && ret == 1))
869 args->self_contained_and_connected =
870 args->check_self_contained_and_connected &&
871 ret == 0;
872 else
873 die(_("%s failed"), cmd_name);
874 if (use_sideband && finish_async(&demux))
875 die(_("error in sideband demultiplexer"));
876 return 0;
879 static int cmp_ref_by_name(const void *a_, const void *b_)
881 const struct ref *a = *((const struct ref **)a_);
882 const struct ref *b = *((const struct ref **)b_);
883 return strcmp(a->name, b->name);
886 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
887 int fd[2],
888 const struct ref *orig_ref,
889 struct ref **sought, int nr_sought,
890 struct shallow_info *si,
891 char **pack_lockfile)
893 struct repository *r = the_repository;
894 struct ref *ref = copy_ref_list(orig_ref);
895 struct object_id oid;
896 const char *agent_feature;
897 int agent_len;
898 struct fetch_negotiator negotiator;
899 fetch_negotiator_init(r, &negotiator);
901 sort_ref_list(&ref, ref_compare_name);
902 QSORT(sought, nr_sought, cmp_ref_by_name);
904 if ((agent_feature = server_feature_value("agent", &agent_len))) {
905 agent_supported = 1;
906 if (agent_len)
907 print_verbose(args, _("Server version is %.*s"),
908 agent_len, agent_feature);
911 if (server_supports("shallow"))
912 print_verbose(args, _("Server supports %s"), "shallow");
913 else if (args->depth > 0 || is_repository_shallow(r))
914 die(_("Server does not support shallow clients"));
915 if (args->depth > 0 || args->deepen_since || args->deepen_not)
916 args->deepen = 1;
917 if (server_supports("multi_ack_detailed")) {
918 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
919 multi_ack = 2;
920 if (server_supports("no-done")) {
921 print_verbose(args, _("Server supports %s"), "no-done");
922 if (args->stateless_rpc)
923 no_done = 1;
926 else if (server_supports("multi_ack")) {
927 print_verbose(args, _("Server supports %s"), "multi_ack");
928 multi_ack = 1;
930 if (server_supports("side-band-64k")) {
931 print_verbose(args, _("Server supports %s"), "side-band-64k");
932 use_sideband = 2;
934 else if (server_supports("side-band")) {
935 print_verbose(args, _("Server supports %s"), "side-band");
936 use_sideband = 1;
938 if (server_supports("allow-tip-sha1-in-want")) {
939 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
940 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
942 if (server_supports("allow-reachable-sha1-in-want")) {
943 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
944 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
946 if (server_supports("thin-pack"))
947 print_verbose(args, _("Server supports %s"), "thin-pack");
948 else
949 args->use_thin_pack = 0;
950 if (server_supports("no-progress"))
951 print_verbose(args, _("Server supports %s"), "no-progress");
952 else
953 args->no_progress = 0;
954 if (server_supports("include-tag"))
955 print_verbose(args, _("Server supports %s"), "include-tag");
956 else
957 args->include_tag = 0;
958 if (server_supports("ofs-delta"))
959 print_verbose(args, _("Server supports %s"), "ofs-delta");
960 else
961 prefer_ofs_delta = 0;
963 if (server_supports("filter")) {
964 server_supports_filtering = 1;
965 print_verbose(args, _("Server supports %s"), "filter");
966 } else if (args->filter_options.choice) {
967 warning("filtering not recognized by server, ignoring");
970 if (server_supports("deepen-since")) {
971 print_verbose(args, _("Server supports %s"), "deepen-since");
972 deepen_since_ok = 1;
973 } else if (args->deepen_since)
974 die(_("Server does not support --shallow-since"));
975 if (server_supports("deepen-not")) {
976 print_verbose(args, _("Server supports %s"), "deepen-not");
977 deepen_not_ok = 1;
978 } else if (args->deepen_not)
979 die(_("Server does not support --shallow-exclude"));
980 if (server_supports("deepen-relative"))
981 print_verbose(args, _("Server supports %s"), "deepen-relative");
982 else if (args->deepen_relative)
983 die(_("Server does not support --deepen"));
985 if (!args->no_dependents) {
986 mark_complete_and_common_ref(&negotiator, args, &ref);
987 filter_refs(args, &ref, sought, nr_sought);
988 if (everything_local(args, &ref)) {
989 packet_flush(fd[1]);
990 goto all_done;
992 } else {
993 filter_refs(args, &ref, sought, nr_sought);
995 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
996 if (!args->keep_pack)
997 /* When cloning, it is not unusual to have
998 * no common commit.
1000 warning(_("no common commits"));
1002 if (args->stateless_rpc)
1003 packet_flush(fd[1]);
1004 if (args->deepen)
1005 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1006 NULL);
1007 else if (si->nr_ours || si->nr_theirs)
1008 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1009 else
1010 alternate_shallow_file = NULL;
1011 if (get_pack(args, fd, pack_lockfile))
1012 die(_("git fetch-pack: fetch failed."));
1014 all_done:
1015 negotiator.release(&negotiator);
1016 return ref;
1019 static void add_shallow_requests(struct strbuf *req_buf,
1020 const struct fetch_pack_args *args)
1022 if (is_repository_shallow(the_repository))
1023 write_shallow_commits(req_buf, 1, NULL);
1024 if (args->depth > 0)
1025 packet_buf_write(req_buf, "deepen %d", args->depth);
1026 if (args->deepen_since) {
1027 timestamp_t max_age = approxidate(args->deepen_since);
1028 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1030 if (args->deepen_not) {
1031 int i;
1032 for (i = 0; i < args->deepen_not->nr; i++) {
1033 struct string_list_item *s = args->deepen_not->items + i;
1034 packet_buf_write(req_buf, "deepen-not %s", s->string);
1037 if (args->deepen_relative)
1038 packet_buf_write(req_buf, "deepen-relative\n");
1041 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1043 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1045 for ( ; wants ; wants = wants->next) {
1046 const struct object_id *remote = &wants->old_oid;
1047 struct object *o;
1050 * If that object is complete (i.e. it is an ancestor of a
1051 * local ref), we tell them we have it but do not have to
1052 * tell them about its ancestors, which they already know
1053 * about.
1055 * We use lookup_object here because we are only
1056 * interested in the case we *know* the object is
1057 * reachable and we have already scanned it.
1059 * Do this only if args->no_dependents is false (if it is true,
1060 * we cannot trust the object flags).
1062 if (!no_dependents &&
1063 ((o = lookup_object(the_repository, remote)) != NULL) &&
1064 (o->flags & COMPLETE)) {
1065 continue;
1068 if (!use_ref_in_want || wants->exact_oid)
1069 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1070 else
1071 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1075 static void add_common(struct strbuf *req_buf, struct oidset *common)
1077 struct oidset_iter iter;
1078 const struct object_id *oid;
1079 oidset_iter_init(common, &iter);
1081 while ((oid = oidset_iter_next(&iter))) {
1082 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1086 static int add_haves(struct fetch_negotiator *negotiator,
1087 struct strbuf *req_buf,
1088 int *haves_to_send, int *in_vain)
1090 int ret = 0;
1091 int haves_added = 0;
1092 const struct object_id *oid;
1094 while ((oid = negotiator->next(negotiator))) {
1095 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1096 if (++haves_added >= *haves_to_send)
1097 break;
1100 *in_vain += haves_added;
1101 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1102 /* Send Done */
1103 packet_buf_write(req_buf, "done\n");
1104 ret = 1;
1107 /* Increase haves to send on next round */
1108 *haves_to_send = next_flush(1, *haves_to_send);
1110 return ret;
1113 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1114 struct fetch_pack_args *args,
1115 const struct ref *wants, struct oidset *common,
1116 int *haves_to_send, int *in_vain,
1117 int sideband_all)
1119 int ret = 0;
1120 struct strbuf req_buf = STRBUF_INIT;
1122 if (server_supports_v2("fetch", 1))
1123 packet_buf_write(&req_buf, "command=fetch");
1124 if (server_supports_v2("agent", 0))
1125 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1126 if (args->server_options && args->server_options->nr &&
1127 server_supports_v2("server-option", 1)) {
1128 int i;
1129 for (i = 0; i < args->server_options->nr; i++)
1130 packet_buf_write(&req_buf, "server-option=%s",
1131 args->server_options->items[i].string);
1134 packet_buf_delim(&req_buf);
1135 if (args->use_thin_pack)
1136 packet_buf_write(&req_buf, "thin-pack");
1137 if (args->no_progress)
1138 packet_buf_write(&req_buf, "no-progress");
1139 if (args->include_tag)
1140 packet_buf_write(&req_buf, "include-tag");
1141 if (prefer_ofs_delta)
1142 packet_buf_write(&req_buf, "ofs-delta");
1143 if (sideband_all)
1144 packet_buf_write(&req_buf, "sideband-all");
1146 /* Add shallow-info and deepen request */
1147 if (server_supports_feature("fetch", "shallow", 0))
1148 add_shallow_requests(&req_buf, args);
1149 else if (is_repository_shallow(the_repository) || args->deepen)
1150 die(_("Server does not support shallow requests"));
1152 /* Add filter */
1153 if (server_supports_feature("fetch", "filter", 0) &&
1154 args->filter_options.choice) {
1155 const char *spec =
1156 expand_list_objects_filter_spec(&args->filter_options);
1157 print_verbose(args, _("Server supports filter"));
1158 packet_buf_write(&req_buf, "filter %s", spec);
1159 } else if (args->filter_options.choice) {
1160 warning("filtering not recognized by server, ignoring");
1163 /* add wants */
1164 add_wants(args->no_dependents, wants, &req_buf);
1166 if (args->no_dependents) {
1167 packet_buf_write(&req_buf, "done");
1168 ret = 1;
1169 } else {
1170 /* Add all of the common commits we've found in previous rounds */
1171 add_common(&req_buf, common);
1173 /* Add initial haves */
1174 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1177 /* Send request */
1178 packet_buf_flush(&req_buf);
1179 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1180 die_errno(_("unable to write request to remote"));
1182 strbuf_release(&req_buf);
1183 return ret;
1187 * Processes a section header in a server's response and checks if it matches
1188 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1189 * not consumed); if 0, the line will be consumed and the function will die if
1190 * the section header doesn't match what was expected.
1192 static int process_section_header(struct packet_reader *reader,
1193 const char *section, int peek)
1195 int ret;
1197 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1198 die(_("error reading section header '%s'"), section);
1200 ret = !strcmp(reader->line, section);
1202 if (!peek) {
1203 if (!ret)
1204 die(_("expected '%s', received '%s'"),
1205 section, reader->line);
1206 packet_reader_read(reader);
1209 return ret;
1212 static int process_acks(struct fetch_negotiator *negotiator,
1213 struct packet_reader *reader,
1214 struct oidset *common)
1216 /* received */
1217 int received_ready = 0;
1218 int received_ack = 0;
1220 process_section_header(reader, "acknowledgments", 0);
1221 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1222 const char *arg;
1224 if (!strcmp(reader->line, "NAK"))
1225 continue;
1227 if (skip_prefix(reader->line, "ACK ", &arg)) {
1228 struct object_id oid;
1229 if (!get_oid_hex(arg, &oid)) {
1230 struct commit *commit;
1231 oidset_insert(common, &oid);
1232 commit = lookup_commit(the_repository, &oid);
1233 negotiator->ack(negotiator, commit);
1235 continue;
1238 if (!strcmp(reader->line, "ready")) {
1239 received_ready = 1;
1240 continue;
1243 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1246 if (reader->status != PACKET_READ_FLUSH &&
1247 reader->status != PACKET_READ_DELIM)
1248 die(_("error processing acks: %d"), reader->status);
1251 * If an "acknowledgments" section is sent, a packfile is sent if and
1252 * only if "ready" was sent in this section. The other sections
1253 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1254 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1255 * otherwise.
1257 if (received_ready && reader->status != PACKET_READ_DELIM)
1258 die(_("expected packfile to be sent after 'ready'"));
1259 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1260 die(_("expected no other sections to be sent after no 'ready'"));
1262 /* return 0 if no common, 1 if there are common, or 2 if ready */
1263 return received_ready ? 2 : (received_ack ? 1 : 0);
1266 static void receive_shallow_info(struct fetch_pack_args *args,
1267 struct packet_reader *reader,
1268 struct oid_array *shallows,
1269 struct shallow_info *si)
1271 int unshallow_received = 0;
1273 process_section_header(reader, "shallow-info", 0);
1274 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1275 const char *arg;
1276 struct object_id oid;
1278 if (skip_prefix(reader->line, "shallow ", &arg)) {
1279 if (get_oid_hex(arg, &oid))
1280 die(_("invalid shallow line: %s"), reader->line);
1281 oid_array_append(shallows, &oid);
1282 continue;
1284 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1285 if (get_oid_hex(arg, &oid))
1286 die(_("invalid unshallow line: %s"), reader->line);
1287 if (!lookup_object(the_repository, &oid))
1288 die(_("object not found: %s"), reader->line);
1289 /* make sure that it is parsed as shallow */
1290 if (!parse_object(the_repository, &oid))
1291 die(_("error in object: %s"), reader->line);
1292 if (unregister_shallow(&oid))
1293 die(_("no shallow found: %s"), reader->line);
1294 unshallow_received = 1;
1295 continue;
1297 die(_("expected shallow/unshallow, got %s"), reader->line);
1300 if (reader->status != PACKET_READ_FLUSH &&
1301 reader->status != PACKET_READ_DELIM)
1302 die(_("error processing shallow info: %d"), reader->status);
1304 if (args->deepen || unshallow_received) {
1306 * Treat these as shallow lines caused by our depth settings.
1307 * In v0, these lines cannot cause refs to be rejected; do the
1308 * same.
1310 int i;
1312 for (i = 0; i < shallows->nr; i++)
1313 register_shallow(the_repository, &shallows->oid[i]);
1314 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1315 NULL);
1316 args->deepen = 1;
1317 } else if (shallows->nr) {
1319 * Treat these as shallow lines caused by the remote being
1320 * shallow. In v0, remote refs that reach these objects are
1321 * rejected (unless --update-shallow is set); do the same.
1323 prepare_shallow_info(si, shallows);
1324 if (si->nr_ours || si->nr_theirs)
1325 alternate_shallow_file =
1326 setup_temporary_shallow(si->shallow);
1327 else
1328 alternate_shallow_file = NULL;
1329 } else {
1330 alternate_shallow_file = NULL;
1334 static int cmp_name_ref(const void *name, const void *ref)
1336 return strcmp(name, (*(struct ref **)ref)->name);
1339 static void receive_wanted_refs(struct packet_reader *reader,
1340 struct ref **sought, int nr_sought)
1342 process_section_header(reader, "wanted-refs", 0);
1343 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1344 struct object_id oid;
1345 const char *end;
1346 struct ref **found;
1348 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1349 die(_("expected wanted-ref, got '%s'"), reader->line);
1351 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1352 cmp_name_ref);
1353 if (!found)
1354 die(_("unexpected wanted-ref: '%s'"), reader->line);
1355 oidcpy(&(*found)->old_oid, &oid);
1358 if (reader->status != PACKET_READ_DELIM)
1359 die(_("error processing wanted refs: %d"), reader->status);
1362 enum fetch_state {
1363 FETCH_CHECK_LOCAL = 0,
1364 FETCH_SEND_REQUEST,
1365 FETCH_PROCESS_ACKS,
1366 FETCH_GET_PACK,
1367 FETCH_DONE,
1370 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1371 int fd[2],
1372 const struct ref *orig_ref,
1373 struct ref **sought, int nr_sought,
1374 struct oid_array *shallows,
1375 struct shallow_info *si,
1376 char **pack_lockfile)
1378 struct repository *r = the_repository;
1379 struct ref *ref = copy_ref_list(orig_ref);
1380 enum fetch_state state = FETCH_CHECK_LOCAL;
1381 struct oidset common = OIDSET_INIT;
1382 struct packet_reader reader;
1383 int in_vain = 0, negotiation_started = 0;
1384 int haves_to_send = INITIAL_FLUSH;
1385 struct fetch_negotiator negotiator;
1386 fetch_negotiator_init(r, &negotiator);
1387 packet_reader_init(&reader, fd[0], NULL, 0,
1388 PACKET_READ_CHOMP_NEWLINE |
1389 PACKET_READ_DIE_ON_ERR_PACKET);
1390 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1391 server_supports_feature("fetch", "sideband-all", 0)) {
1392 reader.use_sideband = 1;
1393 reader.me = "fetch-pack";
1396 while (state != FETCH_DONE) {
1397 switch (state) {
1398 case FETCH_CHECK_LOCAL:
1399 sort_ref_list(&ref, ref_compare_name);
1400 QSORT(sought, nr_sought, cmp_ref_by_name);
1402 /* v2 supports these by default */
1403 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1404 use_sideband = 2;
1405 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1406 args->deepen = 1;
1408 /* Filter 'ref' by 'sought' and those that aren't local */
1409 if (!args->no_dependents) {
1410 mark_complete_and_common_ref(&negotiator, args, &ref);
1411 filter_refs(args, &ref, sought, nr_sought);
1412 if (everything_local(args, &ref))
1413 state = FETCH_DONE;
1414 else
1415 state = FETCH_SEND_REQUEST;
1417 mark_tips(&negotiator, args->negotiation_tips);
1418 for_each_cached_alternate(&negotiator,
1419 insert_one_alternate_object);
1420 } else {
1421 filter_refs(args, &ref, sought, nr_sought);
1422 state = FETCH_SEND_REQUEST;
1424 break;
1425 case FETCH_SEND_REQUEST:
1426 if (!negotiation_started) {
1427 negotiation_started = 1;
1428 trace2_region_enter("fetch-pack",
1429 "negotiation_v2",
1430 the_repository);
1432 if (send_fetch_request(&negotiator, fd[1], args, ref,
1433 &common,
1434 &haves_to_send, &in_vain,
1435 reader.use_sideband))
1436 state = FETCH_GET_PACK;
1437 else
1438 state = FETCH_PROCESS_ACKS;
1439 break;
1440 case FETCH_PROCESS_ACKS:
1441 /* Process ACKs/NAKs */
1442 switch (process_acks(&negotiator, &reader, &common)) {
1443 case 2:
1444 state = FETCH_GET_PACK;
1445 break;
1446 case 1:
1447 in_vain = 0;
1448 /* fallthrough */
1449 default:
1450 state = FETCH_SEND_REQUEST;
1451 break;
1453 break;
1454 case FETCH_GET_PACK:
1455 trace2_region_leave("fetch-pack",
1456 "negotiation_v2",
1457 the_repository);
1458 /* Check for shallow-info section */
1459 if (process_section_header(&reader, "shallow-info", 1))
1460 receive_shallow_info(args, &reader, shallows, si);
1462 if (process_section_header(&reader, "wanted-refs", 1))
1463 receive_wanted_refs(&reader, sought, nr_sought);
1465 /* get the pack */
1466 process_section_header(&reader, "packfile", 0);
1467 if (get_pack(args, fd, pack_lockfile))
1468 die(_("git fetch-pack: fetch failed."));
1470 state = FETCH_DONE;
1471 break;
1472 case FETCH_DONE:
1473 continue;
1477 negotiator.release(&negotiator);
1478 oidset_clear(&common);
1479 return ref;
1482 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1484 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1485 const char *path;
1487 if (git_config_pathname(&path, var, value))
1488 return 1;
1489 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1490 fsck_msg_types.len ? ',' : '=', path);
1491 free((char *)path);
1492 return 0;
1495 if (skip_prefix(var, "fetch.fsck.", &var)) {
1496 if (is_valid_msg_type(var, value))
1497 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1498 fsck_msg_types.len ? ',' : '=', var, value);
1499 else
1500 warning("Skipping unknown msg id '%s'", var);
1501 return 0;
1504 return git_default_config(var, value, cb);
1507 static void fetch_pack_config(void)
1509 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1510 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1511 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1512 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1513 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1515 git_config(fetch_pack_config_cb, NULL);
1518 static void fetch_pack_setup(void)
1520 static int did_setup;
1521 if (did_setup)
1522 return;
1523 fetch_pack_config();
1524 if (0 <= transfer_unpack_limit)
1525 unpack_limit = transfer_unpack_limit;
1526 else if (0 <= fetch_unpack_limit)
1527 unpack_limit = fetch_unpack_limit;
1528 did_setup = 1;
1531 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1533 struct string_list names = STRING_LIST_INIT_NODUP;
1534 int src, dst;
1536 for (src = dst = 0; src < nr; src++) {
1537 struct string_list_item *item;
1538 item = string_list_insert(&names, ref[src]->name);
1539 if (item->util)
1540 continue; /* already have it */
1541 item->util = ref[src];
1542 if (src != dst)
1543 ref[dst] = ref[src];
1544 dst++;
1546 for (src = dst; src < nr; src++)
1547 ref[src] = NULL;
1548 string_list_clear(&names, 0);
1549 return dst;
1552 static void update_shallow(struct fetch_pack_args *args,
1553 struct ref **sought, int nr_sought,
1554 struct shallow_info *si)
1556 struct oid_array ref = OID_ARRAY_INIT;
1557 int *status;
1558 int i;
1560 if (args->deepen && alternate_shallow_file) {
1561 if (*alternate_shallow_file == '\0') { /* --unshallow */
1562 unlink_or_warn(git_path_shallow(the_repository));
1563 rollback_lock_file(&shallow_lock);
1564 } else
1565 commit_lock_file(&shallow_lock);
1566 alternate_shallow_file = NULL;
1567 return;
1570 if (!si->shallow || !si->shallow->nr)
1571 return;
1573 if (args->cloning) {
1575 * remote is shallow, but this is a clone, there are
1576 * no objects in repo to worry about. Accept any
1577 * shallow points that exist in the pack (iow in repo
1578 * after get_pack() and reprepare_packed_git())
1580 struct oid_array extra = OID_ARRAY_INIT;
1581 struct object_id *oid = si->shallow->oid;
1582 for (i = 0; i < si->shallow->nr; i++)
1583 if (has_object_file(&oid[i]))
1584 oid_array_append(&extra, &oid[i]);
1585 if (extra.nr) {
1586 setup_alternate_shallow(&shallow_lock,
1587 &alternate_shallow_file,
1588 &extra);
1589 commit_lock_file(&shallow_lock);
1590 alternate_shallow_file = NULL;
1592 oid_array_clear(&extra);
1593 return;
1596 if (!si->nr_ours && !si->nr_theirs)
1597 return;
1599 remove_nonexistent_theirs_shallow(si);
1600 if (!si->nr_ours && !si->nr_theirs)
1601 return;
1602 for (i = 0; i < nr_sought; i++)
1603 oid_array_append(&ref, &sought[i]->old_oid);
1604 si->ref = &ref;
1606 if (args->update_shallow) {
1608 * remote is also shallow, .git/shallow may be updated
1609 * so all refs can be accepted. Make sure we only add
1610 * shallow roots that are actually reachable from new
1611 * refs.
1613 struct oid_array extra = OID_ARRAY_INIT;
1614 struct object_id *oid = si->shallow->oid;
1615 assign_shallow_commits_to_refs(si, NULL, NULL);
1616 if (!si->nr_ours && !si->nr_theirs) {
1617 oid_array_clear(&ref);
1618 return;
1620 for (i = 0; i < si->nr_ours; i++)
1621 oid_array_append(&extra, &oid[si->ours[i]]);
1622 for (i = 0; i < si->nr_theirs; i++)
1623 oid_array_append(&extra, &oid[si->theirs[i]]);
1624 setup_alternate_shallow(&shallow_lock,
1625 &alternate_shallow_file,
1626 &extra);
1627 commit_lock_file(&shallow_lock);
1628 oid_array_clear(&extra);
1629 oid_array_clear(&ref);
1630 alternate_shallow_file = NULL;
1631 return;
1635 * remote is also shallow, check what ref is safe to update
1636 * without updating .git/shallow
1638 status = xcalloc(nr_sought, sizeof(*status));
1639 assign_shallow_commits_to_refs(si, NULL, status);
1640 if (si->nr_ours || si->nr_theirs) {
1641 for (i = 0; i < nr_sought; i++)
1642 if (status[i])
1643 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1645 free(status);
1646 oid_array_clear(&ref);
1649 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1651 struct ref **rm = cb_data;
1652 struct ref *ref = *rm;
1654 if (!ref)
1655 return -1; /* end of the list */
1656 *rm = ref->next;
1657 oidcpy(oid, &ref->old_oid);
1658 return 0;
1661 struct ref *fetch_pack(struct fetch_pack_args *args,
1662 int fd[],
1663 const struct ref *ref,
1664 struct ref **sought, int nr_sought,
1665 struct oid_array *shallow,
1666 char **pack_lockfile,
1667 enum protocol_version version)
1669 struct ref *ref_cpy;
1670 struct shallow_info si;
1671 struct oid_array shallows_scratch = OID_ARRAY_INIT;
1673 fetch_pack_setup();
1674 if (nr_sought)
1675 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1677 if (args->no_dependents && !args->filter_options.choice) {
1679 * The protocol does not support requesting that only the
1680 * wanted objects be sent, so approximate this by setting a
1681 * "blob:none" filter if no filter is already set. This works
1682 * for all object types: note that wanted blobs will still be
1683 * sent because they are directly specified as a "want".
1685 * NEEDSWORK: Add an option in the protocol to request that
1686 * only the wanted objects be sent, and implement it.
1688 parse_list_objects_filter(&args->filter_options, "blob:none");
1691 if (version != protocol_v2 && !ref) {
1692 packet_flush(fd[1]);
1693 die(_("no matching remote head"));
1695 if (version == protocol_v2) {
1696 if (shallow->nr)
1697 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1698 memset(&si, 0, sizeof(si));
1699 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1700 &shallows_scratch, &si,
1701 pack_lockfile);
1702 } else {
1703 prepare_shallow_info(&si, shallow);
1704 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1705 &si, pack_lockfile);
1707 reprepare_packed_git(the_repository);
1709 if (!args->cloning && args->deepen) {
1710 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1711 struct ref *iterator = ref_cpy;
1712 opt.shallow_file = alternate_shallow_file;
1713 if (args->deepen)
1714 opt.is_deepening_fetch = 1;
1715 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1716 error(_("remote did not send all necessary objects"));
1717 free_refs(ref_cpy);
1718 ref_cpy = NULL;
1719 rollback_lock_file(&shallow_lock);
1720 goto cleanup;
1722 args->connectivity_checked = 1;
1725 update_shallow(args, sought, nr_sought, &si);
1726 cleanup:
1727 clear_shallow_info(&si);
1728 oid_array_clear(&shallows_scratch);
1729 return ref_cpy;
1732 int report_unmatched_refs(struct ref **sought, int nr_sought)
1734 int i, ret = 0;
1736 for (i = 0; i < nr_sought; i++) {
1737 if (!sought[i])
1738 continue;
1739 switch (sought[i]->match_status) {
1740 case REF_MATCHED:
1741 continue;
1742 case REF_NOT_MATCHED:
1743 error(_("no such remote ref %s"), sought[i]->name);
1744 break;
1745 case REF_UNADVERTISED_NOT_ALLOWED:
1746 error(_("Server does not allow request for unadvertised object %s"),
1747 sought[i]->name);
1748 break;
1750 ret = 1;
1752 return ret;