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 if (write_in_full(fd, buf->buf, buf->len) < 0)
196 die_errno(_("unable to write to remote"));
200 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
203 rev_list_insert_ref(negotiator, NULL, &obj->oid);
206 #define INITIAL_FLUSH 16
207 #define PIPESAFE_FLUSH 32
208 #define LARGE_FLUSH 16384
210 static int next_flush(int stateless_rpc, int count)
213 if (count < LARGE_FLUSH)
216 count = count * 11 / 10;
218 if (count < PIPESAFE_FLUSH)
221 count += PIPESAFE_FLUSH;
226 static void mark_tips(struct fetch_negotiator *negotiator,
227 const struct oid_array *negotiation_tips)
231 if (!negotiation_tips) {
232 for_each_ref(rev_list_insert_ref_oid, negotiator);
236 for (i = 0; i < negotiation_tips->nr; i++)
237 rev_list_insert_ref(negotiator, NULL,
238 &negotiation_tips->oid[i]);
242 static int find_common(struct fetch_negotiator *negotiator,
243 struct fetch_pack_args *args,
244 int fd[2], struct object_id *result_oid,
248 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
249 const struct object_id *oid;
250 unsigned in_vain = 0;
251 int got_continue = 0;
253 struct strbuf req_buf = STRBUF_INIT;
254 size_t state_len = 0;
255 struct packet_reader reader;
257 if (args->stateless_rpc && multi_ack == 1)
258 die(_("--stateless-rpc requires multi_ack_detailed"));
260 packet_reader_init(&reader, fd[0], NULL, 0,
261 PACKET_READ_CHOMP_NEWLINE |
262 PACKET_READ_DIE_ON_ERR_PACKET);
264 if (!args->no_dependents) {
265 mark_tips(negotiator, args->negotiation_tips);
266 for_each_cached_alternate(negotiator, insert_one_alternate_object);
270 for ( ; refs ; refs = refs->next) {
271 struct object_id *remote = &refs->old_oid;
272 const char *remote_hex;
276 * If that object is complete (i.e. it is an ancestor of a
277 * local ref), we tell them we have it but do not have to
278 * tell them about its ancestors, which they already know
281 * We use lookup_object here because we are only
282 * interested in the case we *know* the object is
283 * reachable and we have already scanned it.
285 * Do this only if args->no_dependents is false (if it is true,
286 * we cannot trust the object flags).
288 if (!args->no_dependents &&
289 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
290 (o->flags & COMPLETE)) {
294 remote_hex = oid_to_hex(remote);
296 struct strbuf c = STRBUF_INIT;
297 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
298 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
299 if (no_done) strbuf_addstr(&c, " no-done");
300 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
301 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
302 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
303 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
304 if (args->no_progress) strbuf_addstr(&c, " no-progress");
305 if (args->include_tag) strbuf_addstr(&c, " include-tag");
306 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
307 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
308 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
309 if (agent_supported) strbuf_addf(&c, " agent=%s",
310 git_user_agent_sanitized());
311 if (args->filter_options.choice)
312 strbuf_addstr(&c, " filter");
313 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
316 packet_buf_write(&req_buf, "want %s\n", remote_hex);
321 strbuf_release(&req_buf);
326 if (is_repository_shallow(the_repository))
327 write_shallow_commits(&req_buf, 1, NULL);
329 packet_buf_write(&req_buf, "deepen %d", args->depth);
330 if (args->deepen_since) {
331 timestamp_t max_age = approxidate(args->deepen_since);
332 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
334 if (args->deepen_not) {
336 for (i = 0; i < args->deepen_not->nr; i++) {
337 struct string_list_item *s = args->deepen_not->items + i;
338 packet_buf_write(&req_buf, "deepen-not %s", s->string);
341 if (server_supports_filtering && args->filter_options.choice) {
342 struct strbuf expanded_filter_spec = STRBUF_INIT;
343 expand_list_objects_filter_spec(&args->filter_options,
344 &expanded_filter_spec);
345 packet_buf_write(&req_buf, "filter %s",
346 expanded_filter_spec.buf);
347 strbuf_release(&expanded_filter_spec);
349 packet_buf_flush(&req_buf);
350 state_len = req_buf.len;
354 struct object_id oid;
356 send_request(args, fd[1], &req_buf);
357 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
358 if (skip_prefix(reader.line, "shallow ", &arg)) {
359 if (get_oid_hex(arg, &oid))
360 die(_("invalid shallow line: %s"), reader.line);
361 register_shallow(the_repository, &oid);
364 if (skip_prefix(reader.line, "unshallow ", &arg)) {
365 if (get_oid_hex(arg, &oid))
366 die(_("invalid unshallow line: %s"), reader.line);
367 if (!lookup_object(the_repository, oid.hash))
368 die(_("object not found: %s"), reader.line);
369 /* make sure that it is parsed as shallow */
370 if (!parse_object(the_repository, &oid))
371 die(_("error in object: %s"), reader.line);
372 if (unregister_shallow(&oid))
373 die(_("no shallow found: %s"), reader.line);
376 die(_("expected shallow/unshallow, got %s"), reader.line);
378 } else if (!args->stateless_rpc)
379 send_request(args, fd[1], &req_buf);
381 if (!args->stateless_rpc) {
382 /* If we aren't using the stateless-rpc interface
383 * we don't need to retain the headers.
385 strbuf_setlen(&req_buf, 0);
391 if (args->no_dependents)
393 while ((oid = negotiator->next(negotiator))) {
394 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
395 print_verbose(args, "have %s", oid_to_hex(oid));
397 if (flush_at <= ++count) {
400 packet_buf_flush(&req_buf);
401 send_request(args, fd[1], &req_buf);
402 strbuf_setlen(&req_buf, state_len);
404 flush_at = next_flush(args->stateless_rpc, count);
407 * We keep one window "ahead" of the other side, and
408 * will wait for an ACK only on the next one
410 if (!args->stateless_rpc && count == INITIAL_FLUSH)
413 consume_shallow_list(args, &reader);
415 ack = get_ack(&reader, result_oid);
417 print_verbose(args, _("got %s %d %s"), "ack",
418 ack, oid_to_hex(result_oid));
428 struct commit *commit =
429 lookup_commit(the_repository,
434 die(_("invalid commit %s"), oid_to_hex(result_oid));
435 was_common = negotiator->ack(negotiator, commit);
436 if (args->stateless_rpc
439 /* We need to replay the have for this object
440 * on the next RPC request so the peer knows
441 * it is in common with us.
443 const char *hex = oid_to_hex(result_oid);
444 packet_buf_write(&req_buf, "have %s\n", hex);
445 state_len = req_buf.len;
447 * Reset in_vain because an ack
448 * for this commit has not been
452 } else if (!args->stateless_rpc
453 || ack != ACK_common)
457 if (ack == ACK_ready)
464 if (got_continue && MAX_IN_VAIN < in_vain) {
465 print_verbose(args, _("giving up"));
473 if (!got_ready || !no_done) {
474 packet_buf_write(&req_buf, "done\n");
475 send_request(args, fd[1], &req_buf);
477 print_verbose(args, _("done"));
482 strbuf_release(&req_buf);
484 if (!got_ready || !no_done)
485 consume_shallow_list(args, &reader);
486 while (flushes || multi_ack) {
487 int ack = get_ack(&reader, result_oid);
489 print_verbose(args, _("got %s (%d) %s"), "ack",
490 ack, oid_to_hex(result_oid));
498 /* it is no error to fetch into a completely empty repo */
499 return count ? retval : 0;
502 static struct commit_list *complete;
504 static int mark_complete(const struct object_id *oid)
506 struct object *o = parse_object(the_repository, oid);
508 while (o && o->type == OBJ_TAG) {
509 struct tag *t = (struct tag *) o;
511 break; /* broken repository */
512 o->flags |= COMPLETE;
513 o = parse_object(the_repository, &t->tagged->oid);
515 if (o && o->type == OBJ_COMMIT) {
516 struct commit *commit = (struct commit *)o;
517 if (!(commit->object.flags & COMPLETE)) {
518 commit->object.flags |= COMPLETE;
519 commit_list_insert(commit, &complete);
525 static int mark_complete_oid(const char *refname, const struct object_id *oid,
526 int flag, void *cb_data)
528 return mark_complete(oid);
531 static void mark_recent_complete_commits(struct fetch_pack_args *args,
534 while (complete && cutoff <= complete->item->date) {
535 print_verbose(args, _("Marking %s as complete"),
536 oid_to_hex(&complete->item->object.oid));
537 pop_most_recent_commit(&complete, COMPLETE);
541 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
543 for (; refs; refs = refs->next)
544 oidset_insert(oids, &refs->old_oid);
547 static int is_unmatched_ref(const struct ref *ref)
549 struct object_id oid;
551 return ref->match_status == REF_NOT_MATCHED &&
552 !parse_oid_hex(ref->name, &oid, &p) &&
554 oideq(&oid, &ref->old_oid);
557 static void filter_refs(struct fetch_pack_args *args,
559 struct ref **sought, int nr_sought)
561 struct ref *newlist = NULL;
562 struct ref **newtail = &newlist;
563 struct ref *unmatched = NULL;
564 struct ref *ref, *next;
565 struct oidset tip_oids = OIDSET_INIT;
567 int strict = !(allow_unadvertised_object_request &
568 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
571 for (ref = *refs; ref; ref = next) {
575 if (starts_with(ref->name, "refs/") &&
576 check_refname_format(ref->name, 0))
579 while (i < nr_sought) {
580 int cmp = strcmp(ref->name, sought[i]->name);
582 break; /* definitely do not have it */
584 keep = 1; /* definitely have it */
585 sought[i]->match_status = REF_MATCHED;
590 if (!keep && args->fetch_all &&
591 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
598 newtail = &ref->next;
600 ref->next = unmatched;
606 for (i = 0; i < nr_sought; i++) {
608 if (!is_unmatched_ref(ref))
611 add_refs_to_oidset(&tip_oids, unmatched);
612 add_refs_to_oidset(&tip_oids, newlist);
617 /* Append unmatched requests to the list */
618 for (i = 0; i < nr_sought; i++) {
620 if (!is_unmatched_ref(ref))
623 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
624 ref->match_status = REF_MATCHED;
625 *newtail = copy_ref(ref);
626 newtail = &(*newtail)->next;
628 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
632 oidset_clear(&tip_oids);
633 for (ref = unmatched; ref; ref = next) {
641 static void mark_alternate_complete(struct fetch_negotiator *unused,
644 mark_complete(&obj->oid);
647 struct loose_object_iter {
648 struct oidset *loose_object_set;
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,
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) {
675 if (!has_object_file_with_flags(&ref->old_oid,
678 o = parse_object(the_repository, &ref->old_oid);
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;
694 for_each_ref(mark_complete_oid, NULL);
695 for_each_cached_alternate(NULL, mark_alternate_complete);
696 commit_list_sort_by_date(&complete);
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,
711 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
714 negotiator->known_common(negotiator,
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,
731 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
732 const struct object_id *remote = &ref->old_oid;
735 o = lookup_object(the_repository, remote->hash);
736 if (!o || !(o->flags & COMPLETE)) {
738 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
742 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
749 static int sideband_demux(int in, int out, void *data)
754 ret = recv_sideband("fetch-pack", xd[0], out);
759 static int get_pack(struct fetch_pack_args *args,
760 int xd[2], char **pack_lockfile)
763 int do_keep = args->keep_pack;
764 const char *cmd_name;
765 struct pack_header header;
767 struct child_process cmd = CHILD_PROCESS_INIT;
770 memset(&demux, 0, sizeof(demux));
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;
779 demux.isolate_sigpipe = 1;
780 if (start_async(&demux))
781 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
786 if (!args->keep_pack && unpack_limit) {
788 if (read_pack_header(demux.out, &header))
789 die(_("protocol error: bad pack header"));
791 if (ntohl(header.hdr_entries) < unpack_limit)
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) {
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");
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;
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
839 : transfer_fsck_objects >= 0
840 ? transfer_fsck_objects
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");
850 argv_array_pushf(&cmd.args, "--strict%s",
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);
864 /* Closed by start_command() */
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 &&
873 die(_("%s failed"), cmd_name);
874 if (use_sideband && finish_async(&demux))
875 die(_("error in sideband demultiplexer"));
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,
888 const struct ref *orig_ref,
889 struct ref **sought, int nr_sought,
890 struct shallow_info *si,
891 char **pack_lockfile)
893 struct ref *ref = copy_ref_list(orig_ref);
894 struct object_id oid;
895 const char *agent_feature;
897 struct fetch_negotiator negotiator;
898 fetch_negotiator_init(&negotiator, negotiation_algorithm);
900 sort_ref_list(&ref, ref_compare_name);
901 QSORT(sought, nr_sought, cmp_ref_by_name);
903 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
904 die(_("Server does not support shallow clients"));
905 if (args->depth > 0 || args->deepen_since || args->deepen_not)
907 if (server_supports("multi_ack_detailed")) {
908 print_verbose(args, _("Server supports multi_ack_detailed"));
910 if (server_supports("no-done")) {
911 print_verbose(args, _("Server supports no-done"));
912 if (args->stateless_rpc)
916 else if (server_supports("multi_ack")) {
917 print_verbose(args, _("Server supports multi_ack"));
920 if (server_supports("side-band-64k")) {
921 print_verbose(args, _("Server supports side-band-64k"));
924 else if (server_supports("side-band")) {
925 print_verbose(args, _("Server supports side-band"));
928 if (server_supports("allow-tip-sha1-in-want")) {
929 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
930 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
932 if (server_supports("allow-reachable-sha1-in-want")) {
933 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
934 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
936 if (!server_supports("thin-pack"))
937 args->use_thin_pack = 0;
938 if (!server_supports("no-progress"))
939 args->no_progress = 0;
940 if (!server_supports("include-tag"))
941 args->include_tag = 0;
942 if (server_supports("ofs-delta"))
943 print_verbose(args, _("Server supports ofs-delta"));
945 prefer_ofs_delta = 0;
947 if (server_supports("filter")) {
948 server_supports_filtering = 1;
949 print_verbose(args, _("Server supports filter"));
950 } else if (args->filter_options.choice) {
951 warning("filtering not recognized by server, ignoring");
954 if ((agent_feature = server_feature_value("agent", &agent_len))) {
957 print_verbose(args, _("Server version is %.*s"),
958 agent_len, agent_feature);
960 if (server_supports("deepen-since"))
962 else if (args->deepen_since)
963 die(_("Server does not support --shallow-since"));
964 if (server_supports("deepen-not"))
966 else if (args->deepen_not)
967 die(_("Server does not support --shallow-exclude"));
968 if (!server_supports("deepen-relative") && args->deepen_relative)
969 die(_("Server does not support --deepen"));
971 if (!args->no_dependents) {
972 mark_complete_and_common_ref(&negotiator, args, &ref);
973 filter_refs(args, &ref, sought, nr_sought);
974 if (everything_local(args, &ref)) {
979 filter_refs(args, &ref, sought, nr_sought);
981 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
982 if (!args->keep_pack)
983 /* When cloning, it is not unusual to have
986 warning(_("no common commits"));
988 if (args->stateless_rpc)
991 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
993 else if (si->nr_ours || si->nr_theirs)
994 alternate_shallow_file = setup_temporary_shallow(si->shallow);
996 alternate_shallow_file = NULL;
997 if (get_pack(args, fd, pack_lockfile))
998 die(_("git fetch-pack: fetch failed."));
1001 negotiator.release(&negotiator);
1005 static void add_shallow_requests(struct strbuf *req_buf,
1006 const struct fetch_pack_args *args)
1008 if (is_repository_shallow(the_repository))
1009 write_shallow_commits(req_buf, 1, NULL);
1010 if (args->depth > 0)
1011 packet_buf_write(req_buf, "deepen %d", args->depth);
1012 if (args->deepen_since) {
1013 timestamp_t max_age = approxidate(args->deepen_since);
1014 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1016 if (args->deepen_not) {
1018 for (i = 0; i < args->deepen_not->nr; i++) {
1019 struct string_list_item *s = args->deepen_not->items + i;
1020 packet_buf_write(req_buf, "deepen-not %s", s->string);
1023 if (args->deepen_relative)
1024 packet_buf_write(req_buf, "deepen-relative\n");
1027 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1029 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1031 for ( ; wants ; wants = wants->next) {
1032 const struct object_id *remote = &wants->old_oid;
1036 * If that object is complete (i.e. it is an ancestor of a
1037 * local ref), we tell them we have it but do not have to
1038 * tell them about its ancestors, which they already know
1041 * We use lookup_object here because we are only
1042 * interested in the case we *know* the object is
1043 * reachable and we have already scanned it.
1045 * Do this only if args->no_dependents is false (if it is true,
1046 * we cannot trust the object flags).
1048 if (!no_dependents &&
1049 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1050 (o->flags & COMPLETE)) {
1054 if (!use_ref_in_want || wants->exact_oid)
1055 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1057 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1061 static void add_common(struct strbuf *req_buf, struct oidset *common)
1063 struct oidset_iter iter;
1064 const struct object_id *oid;
1065 oidset_iter_init(common, &iter);
1067 while ((oid = oidset_iter_next(&iter))) {
1068 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1072 static int add_haves(struct fetch_negotiator *negotiator,
1073 struct strbuf *req_buf,
1074 int *haves_to_send, int *in_vain)
1077 int haves_added = 0;
1078 const struct object_id *oid;
1080 while ((oid = negotiator->next(negotiator))) {
1081 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1082 if (++haves_added >= *haves_to_send)
1086 *in_vain += haves_added;
1087 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1089 packet_buf_write(req_buf, "done\n");
1093 /* Increase haves to send on next round */
1094 *haves_to_send = next_flush(1, *haves_to_send);
1099 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1100 const struct fetch_pack_args *args,
1101 const struct ref *wants, struct oidset *common,
1102 int *haves_to_send, int *in_vain,
1106 struct strbuf req_buf = STRBUF_INIT;
1108 if (server_supports_v2("fetch", 1))
1109 packet_buf_write(&req_buf, "command=fetch");
1110 if (server_supports_v2("agent", 0))
1111 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1112 if (args->server_options && args->server_options->nr &&
1113 server_supports_v2("server-option", 1)) {
1115 for (i = 0; i < args->server_options->nr; i++)
1116 packet_write_fmt(fd_out, "server-option=%s",
1117 args->server_options->items[i].string);
1120 packet_buf_delim(&req_buf);
1121 if (args->use_thin_pack)
1122 packet_buf_write(&req_buf, "thin-pack");
1123 if (args->no_progress)
1124 packet_buf_write(&req_buf, "no-progress");
1125 if (args->include_tag)
1126 packet_buf_write(&req_buf, "include-tag");
1127 if (prefer_ofs_delta)
1128 packet_buf_write(&req_buf, "ofs-delta");
1130 packet_buf_write(&req_buf, "sideband-all");
1132 /* Add shallow-info and deepen request */
1133 if (server_supports_feature("fetch", "shallow", 0))
1134 add_shallow_requests(&req_buf, args);
1135 else if (is_repository_shallow(the_repository) || args->deepen)
1136 die(_("Server does not support shallow requests"));
1139 if (server_supports_feature("fetch", "filter", 0) &&
1140 args->filter_options.choice) {
1141 struct strbuf expanded_filter_spec = STRBUF_INIT;
1142 print_verbose(args, _("Server supports filter"));
1143 expand_list_objects_filter_spec(&args->filter_options,
1144 &expanded_filter_spec);
1145 packet_buf_write(&req_buf, "filter %s",
1146 expanded_filter_spec.buf);
1147 strbuf_release(&expanded_filter_spec);
1148 } else if (args->filter_options.choice) {
1149 warning("filtering not recognized by server, ignoring");
1153 add_wants(args->no_dependents, wants, &req_buf);
1155 if (args->no_dependents) {
1156 packet_buf_write(&req_buf, "done");
1159 /* Add all of the common commits we've found in previous rounds */
1160 add_common(&req_buf, common);
1162 /* Add initial haves */
1163 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1167 packet_buf_flush(&req_buf);
1168 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1169 die_errno(_("unable to write request to remote"));
1171 strbuf_release(&req_buf);
1176 * Processes a section header in a server's response and checks if it matches
1177 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1178 * not consumed); if 0, the line will be consumed and the function will die if
1179 * the section header doesn't match what was expected.
1181 static int process_section_header(struct packet_reader *reader,
1182 const char *section, int peek)
1186 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1187 die(_("error reading section header '%s'"), section);
1189 ret = !strcmp(reader->line, section);
1193 die(_("expected '%s', received '%s'"),
1194 section, reader->line);
1195 packet_reader_read(reader);
1201 static int process_acks(struct fetch_negotiator *negotiator,
1202 struct packet_reader *reader,
1203 struct oidset *common)
1206 int received_ready = 0;
1207 int received_ack = 0;
1209 process_section_header(reader, "acknowledgments", 0);
1210 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1213 if (!strcmp(reader->line, "NAK"))
1216 if (skip_prefix(reader->line, "ACK ", &arg)) {
1217 struct object_id oid;
1218 if (!get_oid_hex(arg, &oid)) {
1219 struct commit *commit;
1220 oidset_insert(common, &oid);
1221 commit = lookup_commit(the_repository, &oid);
1222 negotiator->ack(negotiator, commit);
1227 if (!strcmp(reader->line, "ready")) {
1232 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1235 if (reader->status != PACKET_READ_FLUSH &&
1236 reader->status != PACKET_READ_DELIM)
1237 die(_("error processing acks: %d"), reader->status);
1240 * If an "acknowledgments" section is sent, a packfile is sent if and
1241 * only if "ready" was sent in this section. The other sections
1242 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1243 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1246 if (received_ready && reader->status != PACKET_READ_DELIM)
1247 die(_("expected packfile to be sent after 'ready'"));
1248 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1249 die(_("expected no other sections to be sent after no 'ready'"));
1251 /* return 0 if no common, 1 if there are common, or 2 if ready */
1252 return received_ready ? 2 : (received_ack ? 1 : 0);
1255 static void receive_shallow_info(struct fetch_pack_args *args,
1256 struct packet_reader *reader)
1258 int line_received = 0;
1260 process_section_header(reader, "shallow-info", 0);
1261 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1263 struct object_id oid;
1265 if (skip_prefix(reader->line, "shallow ", &arg)) {
1266 if (get_oid_hex(arg, &oid))
1267 die(_("invalid shallow line: %s"), reader->line);
1268 register_shallow(the_repository, &oid);
1272 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1273 if (get_oid_hex(arg, &oid))
1274 die(_("invalid unshallow line: %s"), reader->line);
1275 if (!lookup_object(the_repository, oid.hash))
1276 die(_("object not found: %s"), reader->line);
1277 /* make sure that it is parsed as shallow */
1278 if (!parse_object(the_repository, &oid))
1279 die(_("error in object: %s"), reader->line);
1280 if (unregister_shallow(&oid))
1281 die(_("no shallow found: %s"), reader->line);
1285 die(_("expected shallow/unshallow, got %s"), reader->line);
1288 if (reader->status != PACKET_READ_FLUSH &&
1289 reader->status != PACKET_READ_DELIM)
1290 die(_("error processing shallow info: %d"), reader->status);
1292 if (line_received) {
1293 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1297 alternate_shallow_file = NULL;
1301 static void receive_wanted_refs(struct packet_reader *reader,
1302 struct ref **sought, int nr_sought)
1304 process_section_header(reader, "wanted-refs", 0);
1305 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1306 struct object_id oid;
1310 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1311 die(_("expected wanted-ref, got '%s'"), reader->line);
1313 for (i = 0; i < nr_sought; i++) {
1314 if (!strcmp(end, sought[i]->name)) {
1315 oidcpy(&sought[i]->old_oid, &oid);
1321 die(_("unexpected wanted-ref: '%s'"), reader->line);
1324 if (reader->status != PACKET_READ_DELIM)
1325 die(_("error processing wanted refs: %d"), reader->status);
1329 FETCH_CHECK_LOCAL = 0,
1336 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1338 const struct ref *orig_ref,
1339 struct ref **sought, int nr_sought,
1340 char **pack_lockfile)
1342 struct ref *ref = copy_ref_list(orig_ref);
1343 enum fetch_state state = FETCH_CHECK_LOCAL;
1344 struct oidset common = OIDSET_INIT;
1345 struct packet_reader reader;
1347 int haves_to_send = INITIAL_FLUSH;
1348 struct fetch_negotiator negotiator;
1349 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1350 packet_reader_init(&reader, fd[0], NULL, 0,
1351 PACKET_READ_CHOMP_NEWLINE |
1352 PACKET_READ_DIE_ON_ERR_PACKET);
1353 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1354 server_supports_feature("fetch", "sideband-all", 0)) {
1355 reader.use_sideband = 1;
1356 reader.me = "fetch-pack";
1359 while (state != FETCH_DONE) {
1361 case FETCH_CHECK_LOCAL:
1362 sort_ref_list(&ref, ref_compare_name);
1363 QSORT(sought, nr_sought, cmp_ref_by_name);
1365 /* v2 supports these by default */
1366 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1368 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1371 /* Filter 'ref' by 'sought' and those that aren't local */
1372 if (!args->no_dependents) {
1373 mark_complete_and_common_ref(&negotiator, args, &ref);
1374 filter_refs(args, &ref, sought, nr_sought);
1375 if (everything_local(args, &ref))
1378 state = FETCH_SEND_REQUEST;
1380 mark_tips(&negotiator, args->negotiation_tips);
1381 for_each_cached_alternate(&negotiator,
1382 insert_one_alternate_object);
1384 filter_refs(args, &ref, sought, nr_sought);
1385 state = FETCH_SEND_REQUEST;
1388 case FETCH_SEND_REQUEST:
1389 if (send_fetch_request(&negotiator, fd[1], args, ref,
1391 &haves_to_send, &in_vain,
1392 reader.use_sideband))
1393 state = FETCH_GET_PACK;
1395 state = FETCH_PROCESS_ACKS;
1397 case FETCH_PROCESS_ACKS:
1398 /* Process ACKs/NAKs */
1399 switch (process_acks(&negotiator, &reader, &common)) {
1401 state = FETCH_GET_PACK;
1407 state = FETCH_SEND_REQUEST;
1411 case FETCH_GET_PACK:
1412 /* Check for shallow-info section */
1413 if (process_section_header(&reader, "shallow-info", 1))
1414 receive_shallow_info(args, &reader);
1416 if (process_section_header(&reader, "wanted-refs", 1))
1417 receive_wanted_refs(&reader, sought, nr_sought);
1420 process_section_header(&reader, "packfile", 0);
1421 if (get_pack(args, fd, pack_lockfile))
1422 die(_("git fetch-pack: fetch failed."));
1431 negotiator.release(&negotiator);
1432 oidset_clear(&common);
1436 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1438 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1441 if (git_config_pathname(&path, var, value))
1443 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1444 fsck_msg_types.len ? ',' : '=', path);
1449 if (skip_prefix(var, "fetch.fsck.", &var)) {
1450 if (is_valid_msg_type(var, value))
1451 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1452 fsck_msg_types.len ? ',' : '=', var, value);
1454 warning("Skipping unknown msg id '%s'", var);
1458 return git_default_config(var, value, cb);
1461 static void fetch_pack_config(void)
1463 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1464 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1465 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1466 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1467 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1468 git_config_get_string("fetch.negotiationalgorithm",
1469 &negotiation_algorithm);
1471 git_config(fetch_pack_config_cb, NULL);
1474 static void fetch_pack_setup(void)
1476 static int did_setup;
1479 fetch_pack_config();
1480 if (0 <= transfer_unpack_limit)
1481 unpack_limit = transfer_unpack_limit;
1482 else if (0 <= fetch_unpack_limit)
1483 unpack_limit = fetch_unpack_limit;
1487 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1489 struct string_list names = STRING_LIST_INIT_NODUP;
1492 for (src = dst = 0; src < nr; src++) {
1493 struct string_list_item *item;
1494 item = string_list_insert(&names, ref[src]->name);
1496 continue; /* already have it */
1497 item->util = ref[src];
1499 ref[dst] = ref[src];
1502 for (src = dst; src < nr; src++)
1504 string_list_clear(&names, 0);
1508 static void update_shallow(struct fetch_pack_args *args,
1509 struct ref **sought, int nr_sought,
1510 struct shallow_info *si)
1512 struct oid_array ref = OID_ARRAY_INIT;
1516 if (args->deepen && alternate_shallow_file) {
1517 if (*alternate_shallow_file == '\0') { /* --unshallow */
1518 unlink_or_warn(git_path_shallow(the_repository));
1519 rollback_lock_file(&shallow_lock);
1521 commit_lock_file(&shallow_lock);
1522 alternate_shallow_file = NULL;
1526 if (!si->shallow || !si->shallow->nr)
1529 if (args->cloning) {
1531 * remote is shallow, but this is a clone, there are
1532 * no objects in repo to worry about. Accept any
1533 * shallow points that exist in the pack (iow in repo
1534 * after get_pack() and reprepare_packed_git())
1536 struct oid_array extra = OID_ARRAY_INIT;
1537 struct object_id *oid = si->shallow->oid;
1538 for (i = 0; i < si->shallow->nr; i++)
1539 if (has_object_file(&oid[i]))
1540 oid_array_append(&extra, &oid[i]);
1542 setup_alternate_shallow(&shallow_lock,
1543 &alternate_shallow_file,
1545 commit_lock_file(&shallow_lock);
1546 alternate_shallow_file = NULL;
1548 oid_array_clear(&extra);
1552 if (!si->nr_ours && !si->nr_theirs)
1555 remove_nonexistent_theirs_shallow(si);
1556 if (!si->nr_ours && !si->nr_theirs)
1558 for (i = 0; i < nr_sought; i++)
1559 oid_array_append(&ref, &sought[i]->old_oid);
1562 if (args->update_shallow) {
1564 * remote is also shallow, .git/shallow may be updated
1565 * so all refs can be accepted. Make sure we only add
1566 * shallow roots that are actually reachable from new
1569 struct oid_array extra = OID_ARRAY_INIT;
1570 struct object_id *oid = si->shallow->oid;
1571 assign_shallow_commits_to_refs(si, NULL, NULL);
1572 if (!si->nr_ours && !si->nr_theirs) {
1573 oid_array_clear(&ref);
1576 for (i = 0; i < si->nr_ours; i++)
1577 oid_array_append(&extra, &oid[si->ours[i]]);
1578 for (i = 0; i < si->nr_theirs; i++)
1579 oid_array_append(&extra, &oid[si->theirs[i]]);
1580 setup_alternate_shallow(&shallow_lock,
1581 &alternate_shallow_file,
1583 commit_lock_file(&shallow_lock);
1584 oid_array_clear(&extra);
1585 oid_array_clear(&ref);
1586 alternate_shallow_file = NULL;
1591 * remote is also shallow, check what ref is safe to update
1592 * without updating .git/shallow
1594 status = xcalloc(nr_sought, sizeof(*status));
1595 assign_shallow_commits_to_refs(si, NULL, status);
1596 if (si->nr_ours || si->nr_theirs) {
1597 for (i = 0; i < nr_sought; i++)
1599 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1602 oid_array_clear(&ref);
1605 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1607 struct ref **rm = cb_data;
1608 struct ref *ref = *rm;
1611 return -1; /* end of the list */
1613 oidcpy(oid, &ref->old_oid);
1617 struct ref *fetch_pack(struct fetch_pack_args *args,
1618 int fd[], struct child_process *conn,
1619 const struct ref *ref,
1621 struct ref **sought, int nr_sought,
1622 struct oid_array *shallow,
1623 char **pack_lockfile,
1624 enum protocol_version version)
1626 struct ref *ref_cpy;
1627 struct shallow_info si;
1631 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1633 if (args->no_dependents && !args->filter_options.choice) {
1635 * The protocol does not support requesting that only the
1636 * wanted objects be sent, so approximate this by setting a
1637 * "blob:none" filter if no filter is already set. This works
1638 * for all object types: note that wanted blobs will still be
1639 * sent because they are directly specified as a "want".
1641 * NEEDSWORK: Add an option in the protocol to request that
1642 * only the wanted objects be sent, and implement it.
1644 parse_list_objects_filter(&args->filter_options, "blob:none");
1647 if (version != protocol_v2 && !ref) {
1648 packet_flush(fd[1]);
1649 die(_("no matching remote head"));
1651 prepare_shallow_info(&si, shallow);
1652 if (version == protocol_v2)
1653 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1656 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1657 &si, pack_lockfile);
1658 reprepare_packed_git(the_repository);
1660 if (!args->cloning && args->deepen) {
1661 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1662 struct ref *iterator = ref_cpy;
1663 opt.shallow_file = alternate_shallow_file;
1665 opt.is_deepening_fetch = 1;
1666 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1667 error(_("remote did not send all necessary objects"));
1670 rollback_lock_file(&shallow_lock);
1673 args->connectivity_checked = 1;
1676 update_shallow(args, sought, nr_sought, &si);
1678 clear_shallow_info(&si);
1682 int report_unmatched_refs(struct ref **sought, int nr_sought)
1686 for (i = 0; i < nr_sought; i++) {
1689 switch (sought[i]->match_status) {
1692 case REF_NOT_MATCHED:
1693 error(_("no such remote ref %s"), sought[i]->name);
1695 case REF_UNADVERTISED_NOT_ALLOWED:
1696 error(_("Server does not allow request for unadvertised object %s"),