2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "sha1-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.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;
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 char *negotiation_algorithm;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
42 /* Remember to update object flag allocation in object.h */
43 #define COMPLETE (1U << 0)
44 #define ALTERNATE (1U << 1)
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
50 #define MAX_IN_VAIN 256
52 static int multi_ack, use_sideband;
53 /* Allow specifying sha1 if it is a ref tip. */
54 #define ALLOW_TIP_SHA1 01
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 #define ALLOW_REACHABLE_SHA1 02
57 static unsigned int allow_unadvertised_object_request;
59 __attribute__((format (printf, 2, 3)))
60 static inline void print_verbose(const struct fetch_pack_args *args,
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
74 struct alternate_object_cache {
75 struct object **items;
79 static void cache_one_alternate(const struct object_id *oid,
82 struct alternate_object_cache *cache = vcache;
83 struct object *obj = parse_object(the_repository, oid);
85 if (!obj || (obj->flags & ALTERNATE))
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
93 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
97 static int initialized;
98 static struct alternate_object_cache cache;
102 for_each_alternate_ref(cache_one_alternate, &cache);
106 for (i = 0; i < cache.nr; i++)
107 cb(negotiator, cache.items[i]);
110 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
112 const struct object_id *oid)
114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
118 if (o && o->type == OBJ_COMMIT)
119 negotiator->add_tip(negotiator, (struct commit *)o);
124 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
127 return rev_list_insert_ref(cb_data, refname, oid);
138 static void consume_shallow_list(struct fetch_pack_args *args,
139 struct packet_reader *reader)
141 if (args->stateless_rpc && args->deepen) {
142 /* If we sent a depth we will get back "duplicate"
143 * shallow and unshallow commands every time there
144 * is a block of have lines exchanged.
146 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
147 if (starts_with(reader->line, "shallow "))
149 if (starts_with(reader->line, "unshallow "))
151 die(_("git fetch-pack: expected shallow list"));
153 if (reader->status != PACKET_READ_FLUSH)
154 die(_("git fetch-pack: expected a flush packet after shallow list"));
158 static enum ack_type get_ack(struct packet_reader *reader,
159 struct object_id *result_oid)
164 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
165 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
166 len = reader->pktlen;
168 if (!strcmp(reader->line, "NAK"))
170 if (skip_prefix(reader->line, "ACK ", &arg)) {
171 if (!get_oid_hex(arg, result_oid)) {
173 len -= arg - reader->line;
176 if (strstr(arg, "continue"))
178 if (strstr(arg, "common"))
180 if (strstr(arg, "ready"))
185 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
188 static void send_request(struct fetch_pack_args *args,
189 int fd, struct strbuf *buf)
191 if (args->stateless_rpc) {
192 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
195 write_or_die(fd, buf->buf, buf->len);
198 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
201 rev_list_insert_ref(negotiator, NULL, &obj->oid);
204 #define INITIAL_FLUSH 16
205 #define PIPESAFE_FLUSH 32
206 #define LARGE_FLUSH 16384
208 static int next_flush(int stateless_rpc, int count)
211 if (count < LARGE_FLUSH)
214 count = count * 11 / 10;
216 if (count < PIPESAFE_FLUSH)
219 count += PIPESAFE_FLUSH;
224 static void mark_tips(struct fetch_negotiator *negotiator,
225 const struct oid_array *negotiation_tips)
229 if (!negotiation_tips) {
230 for_each_ref(rev_list_insert_ref_oid, negotiator);
234 for (i = 0; i < negotiation_tips->nr; i++)
235 rev_list_insert_ref(negotiator, NULL,
236 &negotiation_tips->oid[i]);
240 static int find_common(struct fetch_negotiator *negotiator,
241 struct fetch_pack_args *args,
242 int fd[2], struct object_id *result_oid,
246 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
247 const struct object_id *oid;
248 unsigned in_vain = 0;
249 int got_continue = 0;
251 struct strbuf req_buf = STRBUF_INIT;
252 size_t state_len = 0;
253 struct packet_reader reader;
255 if (args->stateless_rpc && multi_ack == 1)
256 die(_("--stateless-rpc requires multi_ack_detailed"));
258 packet_reader_init(&reader, fd[0], NULL, 0,
259 PACKET_READ_CHOMP_NEWLINE |
260 PACKET_READ_DIE_ON_ERR_PACKET);
262 if (!args->no_dependents) {
263 mark_tips(negotiator, args->negotiation_tips);
264 for_each_cached_alternate(negotiator, insert_one_alternate_object);
268 for ( ; refs ; refs = refs->next) {
269 struct object_id *remote = &refs->old_oid;
270 const char *remote_hex;
274 * If that object is complete (i.e. it is an ancestor of a
275 * local ref), we tell them we have it but do not have to
276 * tell them about its ancestors, which they already know
279 * We use lookup_object here because we are only
280 * interested in the case we *know* the object is
281 * reachable and we have already scanned it.
283 * Do this only if args->no_dependents is false (if it is true,
284 * we cannot trust the object flags).
286 if (!args->no_dependents &&
287 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
288 (o->flags & COMPLETE)) {
292 remote_hex = oid_to_hex(remote);
294 struct strbuf c = STRBUF_INIT;
295 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
296 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
297 if (no_done) strbuf_addstr(&c, " no-done");
298 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
299 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
300 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
301 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
302 if (args->no_progress) strbuf_addstr(&c, " no-progress");
303 if (args->include_tag) strbuf_addstr(&c, " include-tag");
304 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
305 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
306 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
307 if (agent_supported) strbuf_addf(&c, " agent=%s",
308 git_user_agent_sanitized());
309 if (args->filter_options.choice)
310 strbuf_addstr(&c, " filter");
311 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
314 packet_buf_write(&req_buf, "want %s\n", remote_hex);
319 strbuf_release(&req_buf);
324 if (is_repository_shallow(the_repository))
325 write_shallow_commits(&req_buf, 1, NULL);
327 packet_buf_write(&req_buf, "deepen %d", args->depth);
328 if (args->deepen_since) {
329 timestamp_t max_age = approxidate(args->deepen_since);
330 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
332 if (args->deepen_not) {
334 for (i = 0; i < args->deepen_not->nr; i++) {
335 struct string_list_item *s = args->deepen_not->items + i;
336 packet_buf_write(&req_buf, "deepen-not %s", s->string);
339 if (server_supports_filtering && args->filter_options.choice) {
340 struct strbuf expanded_filter_spec = STRBUF_INIT;
341 expand_list_objects_filter_spec(&args->filter_options,
342 &expanded_filter_spec);
343 packet_buf_write(&req_buf, "filter %s",
344 expanded_filter_spec.buf);
345 strbuf_release(&expanded_filter_spec);
347 packet_buf_flush(&req_buf);
348 state_len = req_buf.len;
352 struct object_id oid;
354 send_request(args, fd[1], &req_buf);
355 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
356 if (skip_prefix(reader.line, "shallow ", &arg)) {
357 if (get_oid_hex(arg, &oid))
358 die(_("invalid shallow line: %s"), reader.line);
359 register_shallow(the_repository, &oid);
362 if (skip_prefix(reader.line, "unshallow ", &arg)) {
363 if (get_oid_hex(arg, &oid))
364 die(_("invalid unshallow line: %s"), reader.line);
365 if (!lookup_object(the_repository, oid.hash))
366 die(_("object not found: %s"), reader.line);
367 /* make sure that it is parsed as shallow */
368 if (!parse_object(the_repository, &oid))
369 die(_("error in object: %s"), reader.line);
370 if (unregister_shallow(&oid))
371 die(_("no shallow found: %s"), reader.line);
374 die(_("expected shallow/unshallow, got %s"), reader.line);
376 } else if (!args->stateless_rpc)
377 send_request(args, fd[1], &req_buf);
379 if (!args->stateless_rpc) {
380 /* If we aren't using the stateless-rpc interface
381 * we don't need to retain the headers.
383 strbuf_setlen(&req_buf, 0);
389 if (args->no_dependents)
391 while ((oid = negotiator->next(negotiator))) {
392 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
393 print_verbose(args, "have %s", oid_to_hex(oid));
395 if (flush_at <= ++count) {
398 packet_buf_flush(&req_buf);
399 send_request(args, fd[1], &req_buf);
400 strbuf_setlen(&req_buf, state_len);
402 flush_at = next_flush(args->stateless_rpc, count);
405 * We keep one window "ahead" of the other side, and
406 * will wait for an ACK only on the next one
408 if (!args->stateless_rpc && count == INITIAL_FLUSH)
411 consume_shallow_list(args, &reader);
413 ack = get_ack(&reader, result_oid);
415 print_verbose(args, _("got %s %d %s"), "ack",
416 ack, oid_to_hex(result_oid));
426 struct commit *commit =
427 lookup_commit(the_repository,
432 die(_("invalid commit %s"), oid_to_hex(result_oid));
433 was_common = negotiator->ack(negotiator, commit);
434 if (args->stateless_rpc
437 /* We need to replay the have for this object
438 * on the next RPC request so the peer knows
439 * it is in common with us.
441 const char *hex = oid_to_hex(result_oid);
442 packet_buf_write(&req_buf, "have %s\n", hex);
443 state_len = req_buf.len;
445 * Reset in_vain because an ack
446 * for this commit has not been
450 } else if (!args->stateless_rpc
451 || ack != ACK_common)
455 if (ack == ACK_ready)
462 if (got_continue && MAX_IN_VAIN < in_vain) {
463 print_verbose(args, _("giving up"));
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"));
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);
487 print_verbose(args, _("got %s (%d) %s"), "ack",
488 ack, oid_to_hex(result_oid));
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;
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);
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,
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;
549 return ref->match_status == REF_NOT_MATCHED &&
550 !parse_oid_hex(ref->name, &oid, &p) &&
552 oideq(&oid, &ref->old_oid);
555 static void filter_refs(struct fetch_pack_args *args,
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;
565 int strict = !(allow_unadvertised_object_request &
566 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
569 for (ref = *refs; ref; ref = next) {
573 if (starts_with(ref->name, "refs/") &&
574 check_refname_format(ref->name, 0))
577 while (i < nr_sought) {
578 int cmp = strcmp(ref->name, sought[i]->name);
580 break; /* definitely do not have it */
582 keep = 1; /* definitely have it */
583 sought[i]->match_status = REF_MATCHED;
588 if (!keep && args->fetch_all &&
589 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
596 newtail = &ref->next;
598 ref->next = unmatched;
604 for (i = 0; i < nr_sought; i++) {
606 if (!is_unmatched_ref(ref))
609 add_refs_to_oidset(&tip_oids, unmatched);
610 add_refs_to_oidset(&tip_oids, newlist);
615 /* Append unmatched requests to the list */
616 for (i = 0; i < nr_sought; i++) {
618 if (!is_unmatched_ref(ref))
621 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
622 ref->match_status = REF_MATCHED;
623 *newtail = copy_ref(ref);
624 newtail = &(*newtail)->next;
626 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
630 oidset_clear(&tip_oids);
631 for (ref = unmatched; ref; ref = next) {
639 static void mark_alternate_complete(struct fetch_negotiator *unused,
642 mark_complete(&obj->oid);
645 struct loose_object_iter {
646 struct oidset *loose_object_set;
651 * Mark recent commits available locally and reachable from a local ref as
652 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
653 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
654 * thus do not need COMMON_REF marks).
656 * The cutoff time for recency is determined by this heuristic: it is the
657 * earliest commit time of the objects in refs that are commits and that we know
658 * the commit time of.
660 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
661 struct fetch_pack_args *args,
665 int old_save_commit_buffer = save_commit_buffer;
666 timestamp_t cutoff = 0;
668 save_commit_buffer = 0;
670 for (ref = *refs; ref; ref = ref->next) {
673 if (!has_object_file_with_flags(&ref->old_oid,
676 o = parse_object(the_repository, &ref->old_oid);
680 /* We already have it -- which may mean that we were
681 * in sync with the other side at some time after
682 * that (it is OK if we guess wrong here).
684 if (o->type == OBJ_COMMIT) {
685 struct commit *commit = (struct commit *)o;
686 if (!cutoff || cutoff < commit->date)
687 cutoff = commit->date;
692 for_each_ref(mark_complete_oid, NULL);
693 for_each_cached_alternate(NULL, mark_alternate_complete);
694 commit_list_sort_by_date(&complete);
696 mark_recent_complete_commits(args, cutoff);
700 * Mark all complete remote refs as common refs.
701 * Don't mark them common yet; the server has to be told so first.
703 for (ref = *refs; ref; ref = ref->next) {
704 struct object *o = deref_tag(the_repository,
705 lookup_object(the_repository,
709 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
712 negotiator->known_common(negotiator,
716 save_commit_buffer = old_save_commit_buffer;
720 * Returns 1 if every object pointed to by the given remote refs is available
721 * locally and reachable from a local ref, and 0 otherwise.
723 static int everything_local(struct fetch_pack_args *args,
729 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
730 const struct object_id *remote = &ref->old_oid;
733 o = lookup_object(the_repository, remote->hash);
734 if (!o || !(o->flags & COMPLETE)) {
736 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
740 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
747 static int sideband_demux(int in, int out, void *data)
752 ret = recv_sideband("fetch-pack", xd[0], out);
757 static int get_pack(struct fetch_pack_args *args,
758 int xd[2], char **pack_lockfile)
761 int do_keep = args->keep_pack;
762 const char *cmd_name;
763 struct pack_header header;
765 struct child_process cmd = CHILD_PROCESS_INIT;
768 memset(&demux, 0, sizeof(demux));
770 /* xd[] is talking with upload-pack; subprocess reads from
771 * xd[0], spits out band#2 to stderr, and feeds us band#1
772 * through demux->out.
774 demux.proc = sideband_demux;
777 demux.isolate_sigpipe = 1;
778 if (start_async(&demux))
779 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
784 if (!args->keep_pack && unpack_limit) {
786 if (read_pack_header(demux.out, &header))
787 die(_("protocol error: bad pack header"));
789 if (ntohl(header.hdr_entries) < unpack_limit)
795 if (alternate_shallow_file) {
796 argv_array_push(&cmd.args, "--shallow-file");
797 argv_array_push(&cmd.args, alternate_shallow_file);
800 if (do_keep || args->from_promisor) {
803 cmd_name = "index-pack";
804 argv_array_push(&cmd.args, cmd_name);
805 argv_array_push(&cmd.args, "--stdin");
806 if (!args->quiet && !args->no_progress)
807 argv_array_push(&cmd.args, "-v");
808 if (args->use_thin_pack)
809 argv_array_push(&cmd.args, "--fix-thin");
810 if (do_keep && (args->lock_pack || unpack_limit)) {
811 char hostname[HOST_NAME_MAX + 1];
812 if (xgethostname(hostname, sizeof(hostname)))
813 xsnprintf(hostname, sizeof(hostname), "localhost");
814 argv_array_pushf(&cmd.args,
815 "--keep=fetch-pack %"PRIuMAX " on %s",
816 (uintmax_t)getpid(), hostname);
818 if (args->check_self_contained_and_connected)
819 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
820 if (args->from_promisor)
821 argv_array_push(&cmd.args, "--promisor");
824 cmd_name = "unpack-objects";
825 argv_array_push(&cmd.args, cmd_name);
826 if (args->quiet || args->no_progress)
827 argv_array_push(&cmd.args, "-q");
828 args->check_self_contained_and_connected = 0;
832 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
833 ntohl(header.hdr_version),
834 ntohl(header.hdr_entries));
835 if (fetch_fsck_objects >= 0
837 : transfer_fsck_objects >= 0
838 ? transfer_fsck_objects
840 if (args->from_promisor)
842 * We cannot use --strict in index-pack because it
843 * checks both broken objects and links, but we only
844 * want to check for broken objects.
846 argv_array_push(&cmd.args, "--fsck-objects");
848 argv_array_pushf(&cmd.args, "--strict%s",
854 if (start_command(&cmd))
855 die(_("fetch-pack: unable to fork off %s"), cmd_name);
856 if (do_keep && pack_lockfile) {
857 *pack_lockfile = index_pack_lockfile(cmd.out);
862 /* Closed by start_command() */
865 ret = finish_command(&cmd);
866 if (!ret || (args->check_self_contained_and_connected && ret == 1))
867 args->self_contained_and_connected =
868 args->check_self_contained_and_connected &&
871 die(_("%s failed"), cmd_name);
872 if (use_sideband && finish_async(&demux))
873 die(_("error in sideband demultiplexer"));
877 static int cmp_ref_by_name(const void *a_, const void *b_)
879 const struct ref *a = *((const struct ref **)a_);
880 const struct ref *b = *((const struct ref **)b_);
881 return strcmp(a->name, b->name);
884 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
886 const struct ref *orig_ref,
887 struct ref **sought, int nr_sought,
888 struct shallow_info *si,
889 char **pack_lockfile)
891 struct ref *ref = copy_ref_list(orig_ref);
892 struct object_id oid;
893 const char *agent_feature;
895 struct fetch_negotiator negotiator;
896 fetch_negotiator_init(&negotiator, negotiation_algorithm);
898 sort_ref_list(&ref, ref_compare_name);
899 QSORT(sought, nr_sought, cmp_ref_by_name);
901 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
902 die(_("Server does not support shallow clients"));
903 if (args->depth > 0 || args->deepen_since || args->deepen_not)
905 if (server_supports("multi_ack_detailed")) {
906 print_verbose(args, _("Server supports multi_ack_detailed"));
908 if (server_supports("no-done")) {
909 print_verbose(args, _("Server supports no-done"));
910 if (args->stateless_rpc)
914 else if (server_supports("multi_ack")) {
915 print_verbose(args, _("Server supports multi_ack"));
918 if (server_supports("side-band-64k")) {
919 print_verbose(args, _("Server supports side-band-64k"));
922 else if (server_supports("side-band")) {
923 print_verbose(args, _("Server supports side-band"));
926 if (server_supports("allow-tip-sha1-in-want")) {
927 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
928 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
930 if (server_supports("allow-reachable-sha1-in-want")) {
931 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
932 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
934 if (!server_supports("thin-pack"))
935 args->use_thin_pack = 0;
936 if (!server_supports("no-progress"))
937 args->no_progress = 0;
938 if (!server_supports("include-tag"))
939 args->include_tag = 0;
940 if (server_supports("ofs-delta"))
941 print_verbose(args, _("Server supports ofs-delta"));
943 prefer_ofs_delta = 0;
945 if (server_supports("filter")) {
946 server_supports_filtering = 1;
947 print_verbose(args, _("Server supports filter"));
948 } else if (args->filter_options.choice) {
949 warning("filtering not recognized by server, ignoring");
952 if ((agent_feature = server_feature_value("agent", &agent_len))) {
955 print_verbose(args, _("Server version is %.*s"),
956 agent_len, agent_feature);
958 if (server_supports("deepen-since"))
960 else if (args->deepen_since)
961 die(_("Server does not support --shallow-since"));
962 if (server_supports("deepen-not"))
964 else if (args->deepen_not)
965 die(_("Server does not support --shallow-exclude"));
966 if (!server_supports("deepen-relative") && args->deepen_relative)
967 die(_("Server does not support --deepen"));
969 if (!args->no_dependents) {
970 mark_complete_and_common_ref(&negotiator, args, &ref);
971 filter_refs(args, &ref, sought, nr_sought);
972 if (everything_local(args, &ref)) {
977 filter_refs(args, &ref, sought, nr_sought);
979 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
980 if (!args->keep_pack)
981 /* When cloning, it is not unusual to have
984 warning(_("no common commits"));
986 if (args->stateless_rpc)
989 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
991 else if (si->nr_ours || si->nr_theirs)
992 alternate_shallow_file = setup_temporary_shallow(si->shallow);
994 alternate_shallow_file = NULL;
995 if (get_pack(args, fd, pack_lockfile))
996 die(_("git fetch-pack: fetch failed."));
999 negotiator.release(&negotiator);
1003 static void add_shallow_requests(struct strbuf *req_buf,
1004 const struct fetch_pack_args *args)
1006 if (is_repository_shallow(the_repository))
1007 write_shallow_commits(req_buf, 1, NULL);
1008 if (args->depth > 0)
1009 packet_buf_write(req_buf, "deepen %d", args->depth);
1010 if (args->deepen_since) {
1011 timestamp_t max_age = approxidate(args->deepen_since);
1012 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1014 if (args->deepen_not) {
1016 for (i = 0; i < args->deepen_not->nr; i++) {
1017 struct string_list_item *s = args->deepen_not->items + i;
1018 packet_buf_write(req_buf, "deepen-not %s", s->string);
1021 if (args->deepen_relative)
1022 packet_buf_write(req_buf, "deepen-relative\n");
1025 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1027 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1029 for ( ; wants ; wants = wants->next) {
1030 const struct object_id *remote = &wants->old_oid;
1034 * If that object is complete (i.e. it is an ancestor of a
1035 * local ref), we tell them we have it but do not have to
1036 * tell them about its ancestors, which they already know
1039 * We use lookup_object here because we are only
1040 * interested in the case we *know* the object is
1041 * reachable and we have already scanned it.
1043 * Do this only if args->no_dependents is false (if it is true,
1044 * we cannot trust the object flags).
1046 if (!no_dependents &&
1047 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1048 (o->flags & COMPLETE)) {
1052 if (!use_ref_in_want || wants->exact_oid)
1053 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1055 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1059 static void add_common(struct strbuf *req_buf, struct oidset *common)
1061 struct oidset_iter iter;
1062 const struct object_id *oid;
1063 oidset_iter_init(common, &iter);
1065 while ((oid = oidset_iter_next(&iter))) {
1066 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1070 static int add_haves(struct fetch_negotiator *negotiator,
1071 struct strbuf *req_buf,
1072 int *haves_to_send, int *in_vain)
1075 int haves_added = 0;
1076 const struct object_id *oid;
1078 while ((oid = negotiator->next(negotiator))) {
1079 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1080 if (++haves_added >= *haves_to_send)
1084 *in_vain += haves_added;
1085 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1087 packet_buf_write(req_buf, "done\n");
1091 /* Increase haves to send on next round */
1092 *haves_to_send = next_flush(1, *haves_to_send);
1097 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1098 const struct fetch_pack_args *args,
1099 const struct ref *wants, struct oidset *common,
1100 int *haves_to_send, int *in_vain,
1104 struct strbuf req_buf = STRBUF_INIT;
1106 if (server_supports_v2("fetch", 1))
1107 packet_buf_write(&req_buf, "command=fetch");
1108 if (server_supports_v2("agent", 0))
1109 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1110 if (args->server_options && args->server_options->nr &&
1111 server_supports_v2("server-option", 1)) {
1113 for (i = 0; i < args->server_options->nr; i++)
1114 packet_write_fmt(fd_out, "server-option=%s",
1115 args->server_options->items[i].string);
1118 packet_buf_delim(&req_buf);
1119 if (args->use_thin_pack)
1120 packet_buf_write(&req_buf, "thin-pack");
1121 if (args->no_progress)
1122 packet_buf_write(&req_buf, "no-progress");
1123 if (args->include_tag)
1124 packet_buf_write(&req_buf, "include-tag");
1125 if (prefer_ofs_delta)
1126 packet_buf_write(&req_buf, "ofs-delta");
1128 packet_buf_write(&req_buf, "sideband-all");
1130 /* Add shallow-info and deepen request */
1131 if (server_supports_feature("fetch", "shallow", 0))
1132 add_shallow_requests(&req_buf, args);
1133 else if (is_repository_shallow(the_repository) || args->deepen)
1134 die(_("Server does not support shallow requests"));
1137 if (server_supports_feature("fetch", "filter", 0) &&
1138 args->filter_options.choice) {
1139 struct strbuf expanded_filter_spec = STRBUF_INIT;
1140 print_verbose(args, _("Server supports filter"));
1141 expand_list_objects_filter_spec(&args->filter_options,
1142 &expanded_filter_spec);
1143 packet_buf_write(&req_buf, "filter %s",
1144 expanded_filter_spec.buf);
1145 strbuf_release(&expanded_filter_spec);
1146 } else if (args->filter_options.choice) {
1147 warning("filtering not recognized by server, ignoring");
1151 add_wants(args->no_dependents, wants, &req_buf);
1153 if (args->no_dependents) {
1154 packet_buf_write(&req_buf, "done");
1157 /* Add all of the common commits we've found in previous rounds */
1158 add_common(&req_buf, common);
1160 /* Add initial haves */
1161 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1165 packet_buf_flush(&req_buf);
1166 write_or_die(fd_out, req_buf.buf, req_buf.len);
1168 strbuf_release(&req_buf);
1173 * Processes a section header in a server's response and checks if it matches
1174 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1175 * not consumed); if 0, the line will be consumed and the function will die if
1176 * the section header doesn't match what was expected.
1178 static int process_section_header(struct packet_reader *reader,
1179 const char *section, int peek)
1183 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1184 die(_("error reading section header '%s'"), section);
1186 ret = !strcmp(reader->line, section);
1190 die(_("expected '%s', received '%s'"),
1191 section, reader->line);
1192 packet_reader_read(reader);
1198 static int process_acks(struct fetch_negotiator *negotiator,
1199 struct packet_reader *reader,
1200 struct oidset *common)
1203 int received_ready = 0;
1204 int received_ack = 0;
1206 process_section_header(reader, "acknowledgments", 0);
1207 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1210 if (!strcmp(reader->line, "NAK"))
1213 if (skip_prefix(reader->line, "ACK ", &arg)) {
1214 struct object_id oid;
1215 if (!get_oid_hex(arg, &oid)) {
1216 struct commit *commit;
1217 oidset_insert(common, &oid);
1218 commit = lookup_commit(the_repository, &oid);
1219 negotiator->ack(negotiator, commit);
1224 if (!strcmp(reader->line, "ready")) {
1229 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1232 if (reader->status != PACKET_READ_FLUSH &&
1233 reader->status != PACKET_READ_DELIM)
1234 die(_("error processing acks: %d"), reader->status);
1237 * If an "acknowledgments" section is sent, a packfile is sent if and
1238 * only if "ready" was sent in this section. The other sections
1239 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1240 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1243 if (received_ready && reader->status != PACKET_READ_DELIM)
1244 die(_("expected packfile to be sent after 'ready'"));
1245 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1246 die(_("expected no other sections to be sent after no 'ready'"));
1248 /* return 0 if no common, 1 if there are common, or 2 if ready */
1249 return received_ready ? 2 : (received_ack ? 1 : 0);
1252 static void receive_shallow_info(struct fetch_pack_args *args,
1253 struct packet_reader *reader)
1255 int line_received = 0;
1257 process_section_header(reader, "shallow-info", 0);
1258 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1260 struct object_id oid;
1262 if (skip_prefix(reader->line, "shallow ", &arg)) {
1263 if (get_oid_hex(arg, &oid))
1264 die(_("invalid shallow line: %s"), reader->line);
1265 register_shallow(the_repository, &oid);
1269 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1270 if (get_oid_hex(arg, &oid))
1271 die(_("invalid unshallow line: %s"), reader->line);
1272 if (!lookup_object(the_repository, oid.hash))
1273 die(_("object not found: %s"), reader->line);
1274 /* make sure that it is parsed as shallow */
1275 if (!parse_object(the_repository, &oid))
1276 die(_("error in object: %s"), reader->line);
1277 if (unregister_shallow(&oid))
1278 die(_("no shallow found: %s"), reader->line);
1282 die(_("expected shallow/unshallow, got %s"), reader->line);
1285 if (reader->status != PACKET_READ_FLUSH &&
1286 reader->status != PACKET_READ_DELIM)
1287 die(_("error processing shallow info: %d"), reader->status);
1289 if (line_received) {
1290 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1294 alternate_shallow_file = NULL;
1298 static int cmp_name_ref(const void *name, const void *ref)
1300 return strcmp(name, (*(struct ref **)ref)->name);
1303 static void receive_wanted_refs(struct packet_reader *reader,
1304 struct ref **sought, int nr_sought)
1306 process_section_header(reader, "wanted-refs", 0);
1307 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1308 struct object_id oid;
1312 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1313 die(_("expected wanted-ref, got '%s'"), reader->line);
1315 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1318 die(_("unexpected wanted-ref: '%s'"), reader->line);
1319 oidcpy(&(*found)->old_oid, &oid);
1322 if (reader->status != PACKET_READ_DELIM)
1323 die(_("error processing wanted refs: %d"), reader->status);
1327 FETCH_CHECK_LOCAL = 0,
1334 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1336 const struct ref *orig_ref,
1337 struct ref **sought, int nr_sought,
1338 char **pack_lockfile)
1340 struct ref *ref = copy_ref_list(orig_ref);
1341 enum fetch_state state = FETCH_CHECK_LOCAL;
1342 struct oidset common = OIDSET_INIT;
1343 struct packet_reader reader;
1345 int haves_to_send = INITIAL_FLUSH;
1346 struct fetch_negotiator negotiator;
1347 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1348 packet_reader_init(&reader, fd[0], NULL, 0,
1349 PACKET_READ_CHOMP_NEWLINE |
1350 PACKET_READ_DIE_ON_ERR_PACKET);
1351 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1352 server_supports_feature("fetch", "sideband-all", 0)) {
1353 reader.use_sideband = 1;
1354 reader.me = "fetch-pack";
1357 while (state != FETCH_DONE) {
1359 case FETCH_CHECK_LOCAL:
1360 sort_ref_list(&ref, ref_compare_name);
1361 QSORT(sought, nr_sought, cmp_ref_by_name);
1363 /* v2 supports these by default */
1364 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1366 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1369 /* Filter 'ref' by 'sought' and those that aren't local */
1370 if (!args->no_dependents) {
1371 mark_complete_and_common_ref(&negotiator, args, &ref);
1372 filter_refs(args, &ref, sought, nr_sought);
1373 if (everything_local(args, &ref))
1376 state = FETCH_SEND_REQUEST;
1378 mark_tips(&negotiator, args->negotiation_tips);
1379 for_each_cached_alternate(&negotiator,
1380 insert_one_alternate_object);
1382 filter_refs(args, &ref, sought, nr_sought);
1383 state = FETCH_SEND_REQUEST;
1386 case FETCH_SEND_REQUEST:
1387 if (send_fetch_request(&negotiator, fd[1], args, ref,
1389 &haves_to_send, &in_vain,
1390 reader.use_sideband))
1391 state = FETCH_GET_PACK;
1393 state = FETCH_PROCESS_ACKS;
1395 case FETCH_PROCESS_ACKS:
1396 /* Process ACKs/NAKs */
1397 switch (process_acks(&negotiator, &reader, &common)) {
1399 state = FETCH_GET_PACK;
1405 state = FETCH_SEND_REQUEST;
1409 case FETCH_GET_PACK:
1410 /* Check for shallow-info section */
1411 if (process_section_header(&reader, "shallow-info", 1))
1412 receive_shallow_info(args, &reader);
1414 if (process_section_header(&reader, "wanted-refs", 1))
1415 receive_wanted_refs(&reader, sought, nr_sought);
1418 process_section_header(&reader, "packfile", 0);
1419 if (get_pack(args, fd, pack_lockfile))
1420 die(_("git fetch-pack: fetch failed."));
1429 negotiator.release(&negotiator);
1430 oidset_clear(&common);
1434 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1436 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1439 if (git_config_pathname(&path, var, value))
1441 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1442 fsck_msg_types.len ? ',' : '=', path);
1447 if (skip_prefix(var, "fetch.fsck.", &var)) {
1448 if (is_valid_msg_type(var, value))
1449 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1450 fsck_msg_types.len ? ',' : '=', var, value);
1452 warning("Skipping unknown msg id '%s'", var);
1456 return git_default_config(var, value, cb);
1459 static void fetch_pack_config(void)
1461 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1462 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1463 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1464 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1465 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1466 git_config_get_string("fetch.negotiationalgorithm",
1467 &negotiation_algorithm);
1469 git_config(fetch_pack_config_cb, NULL);
1472 static void fetch_pack_setup(void)
1474 static int did_setup;
1477 fetch_pack_config();
1478 if (0 <= transfer_unpack_limit)
1479 unpack_limit = transfer_unpack_limit;
1480 else if (0 <= fetch_unpack_limit)
1481 unpack_limit = fetch_unpack_limit;
1485 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1487 struct string_list names = STRING_LIST_INIT_NODUP;
1490 for (src = dst = 0; src < nr; src++) {
1491 struct string_list_item *item;
1492 item = string_list_insert(&names, ref[src]->name);
1494 continue; /* already have it */
1495 item->util = ref[src];
1497 ref[dst] = ref[src];
1500 for (src = dst; src < nr; src++)
1502 string_list_clear(&names, 0);
1506 static void update_shallow(struct fetch_pack_args *args,
1507 struct ref **sought, int nr_sought,
1508 struct shallow_info *si)
1510 struct oid_array ref = OID_ARRAY_INIT;
1514 if (args->deepen && alternate_shallow_file) {
1515 if (*alternate_shallow_file == '\0') { /* --unshallow */
1516 unlink_or_warn(git_path_shallow(the_repository));
1517 rollback_lock_file(&shallow_lock);
1519 commit_lock_file(&shallow_lock);
1520 alternate_shallow_file = NULL;
1524 if (!si->shallow || !si->shallow->nr)
1527 if (args->cloning) {
1529 * remote is shallow, but this is a clone, there are
1530 * no objects in repo to worry about. Accept any
1531 * shallow points that exist in the pack (iow in repo
1532 * after get_pack() and reprepare_packed_git())
1534 struct oid_array extra = OID_ARRAY_INIT;
1535 struct object_id *oid = si->shallow->oid;
1536 for (i = 0; i < si->shallow->nr; i++)
1537 if (has_object_file(&oid[i]))
1538 oid_array_append(&extra, &oid[i]);
1540 setup_alternate_shallow(&shallow_lock,
1541 &alternate_shallow_file,
1543 commit_lock_file(&shallow_lock);
1544 alternate_shallow_file = NULL;
1546 oid_array_clear(&extra);
1550 if (!si->nr_ours && !si->nr_theirs)
1553 remove_nonexistent_theirs_shallow(si);
1554 if (!si->nr_ours && !si->nr_theirs)
1556 for (i = 0; i < nr_sought; i++)
1557 oid_array_append(&ref, &sought[i]->old_oid);
1560 if (args->update_shallow) {
1562 * remote is also shallow, .git/shallow may be updated
1563 * so all refs can be accepted. Make sure we only add
1564 * shallow roots that are actually reachable from new
1567 struct oid_array extra = OID_ARRAY_INIT;
1568 struct object_id *oid = si->shallow->oid;
1569 assign_shallow_commits_to_refs(si, NULL, NULL);
1570 if (!si->nr_ours && !si->nr_theirs) {
1571 oid_array_clear(&ref);
1574 for (i = 0; i < si->nr_ours; i++)
1575 oid_array_append(&extra, &oid[si->ours[i]]);
1576 for (i = 0; i < si->nr_theirs; i++)
1577 oid_array_append(&extra, &oid[si->theirs[i]]);
1578 setup_alternate_shallow(&shallow_lock,
1579 &alternate_shallow_file,
1581 commit_lock_file(&shallow_lock);
1582 oid_array_clear(&extra);
1583 oid_array_clear(&ref);
1584 alternate_shallow_file = NULL;
1589 * remote is also shallow, check what ref is safe to update
1590 * without updating .git/shallow
1592 status = xcalloc(nr_sought, sizeof(*status));
1593 assign_shallow_commits_to_refs(si, NULL, status);
1594 if (si->nr_ours || si->nr_theirs) {
1595 for (i = 0; i < nr_sought; i++)
1597 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1600 oid_array_clear(&ref);
1603 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1605 struct ref **rm = cb_data;
1606 struct ref *ref = *rm;
1609 return -1; /* end of the list */
1611 oidcpy(oid, &ref->old_oid);
1615 struct ref *fetch_pack(struct fetch_pack_args *args,
1616 int fd[], struct child_process *conn,
1617 const struct ref *ref,
1619 struct ref **sought, int nr_sought,
1620 struct oid_array *shallow,
1621 char **pack_lockfile,
1622 enum protocol_version version)
1624 struct ref *ref_cpy;
1625 struct shallow_info si;
1629 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1631 if (args->no_dependents && !args->filter_options.choice) {
1633 * The protocol does not support requesting that only the
1634 * wanted objects be sent, so approximate this by setting a
1635 * "blob:none" filter if no filter is already set. This works
1636 * for all object types: note that wanted blobs will still be
1637 * sent because they are directly specified as a "want".
1639 * NEEDSWORK: Add an option in the protocol to request that
1640 * only the wanted objects be sent, and implement it.
1642 parse_list_objects_filter(&args->filter_options, "blob:none");
1645 if (version != protocol_v2 && !ref) {
1646 packet_flush(fd[1]);
1647 die(_("no matching remote head"));
1649 prepare_shallow_info(&si, shallow);
1650 if (version == protocol_v2)
1651 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1654 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1655 &si, pack_lockfile);
1656 reprepare_packed_git(the_repository);
1658 if (!args->cloning && args->deepen) {
1659 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1660 struct ref *iterator = ref_cpy;
1661 opt.shallow_file = alternate_shallow_file;
1663 opt.is_deepening_fetch = 1;
1664 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1665 error(_("remote did not send all necessary objects"));
1668 rollback_lock_file(&shallow_lock);
1671 args->connectivity_checked = 1;
1674 update_shallow(args, sought, nr_sought, &si);
1676 clear_shallow_info(&si);
1680 int report_unmatched_refs(struct ref **sought, int nr_sought)
1684 for (i = 0; i < nr_sought; i++) {
1687 switch (sought[i]->match_status) {
1690 case REF_NOT_MATCHED:
1691 error(_("no such remote ref %s"), sought[i]->name);
1693 case REF_UNADVERTISED_NOT_ALLOWED:
1694 error(_("Server does not allow request for unadvertised object %s"),