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 char *refname,
80 const struct object_id *oid,
83 struct alternate_object_cache *cache = vcache;
84 struct object *obj = parse_object(the_repository, oid);
86 if (!obj || (obj->flags & ALTERNATE))
89 obj->flags |= ALTERNATE;
90 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
91 cache->items[cache->nr++] = obj;
94 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
95 void (*cb)(struct fetch_negotiator *,
98 static int initialized;
99 static struct alternate_object_cache cache;
103 for_each_alternate_ref(cache_one_alternate, &cache);
107 for (i = 0; i < cache.nr; i++)
108 cb(negotiator, cache.items[i]);
111 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
113 const struct object_id *oid)
115 struct object *o = deref_tag(the_repository,
116 parse_object(the_repository, oid),
119 if (o && o->type == OBJ_COMMIT)
120 negotiator->add_tip(negotiator, (struct commit *)o);
125 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
126 int flag, void *cb_data)
128 return rev_list_insert_ref(cb_data, refname, oid);
139 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
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.
147 while ((line = packet_read_line(fd, NULL))) {
148 if (starts_with(line, "shallow "))
150 if (starts_with(line, "unshallow "))
152 die(_("git fetch-pack: expected shallow list"));
157 static enum ack_type get_ack(int fd, struct object_id *result_oid)
160 char *line = packet_read_line(fd, &len);
164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
165 if (!strcmp(line, "NAK"))
167 if (skip_prefix(line, "ACK ", &arg)) {
168 if (!get_oid_hex(arg, result_oid)) {
173 if (strstr(arg, "continue"))
175 if (strstr(arg, "common"))
177 if (strstr(arg, "ready"))
182 if (skip_prefix(line, "ERR ", &arg))
183 die(_("remote error: %s"), arg);
184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), 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);
194 write_or_die(fd, buf->buf, buf->len);
197 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
200 rev_list_insert_ref(negotiator, NULL, &obj->oid);
203 #define INITIAL_FLUSH 16
204 #define PIPESAFE_FLUSH 32
205 #define LARGE_FLUSH 16384
207 static int next_flush(int stateless_rpc, int count)
210 if (count < LARGE_FLUSH)
213 count = count * 11 / 10;
215 if (count < PIPESAFE_FLUSH)
218 count += PIPESAFE_FLUSH;
223 static void mark_tips(struct fetch_negotiator *negotiator,
224 const struct oid_array *negotiation_tips)
228 if (!negotiation_tips) {
229 for_each_ref(rev_list_insert_ref_oid, negotiator);
233 for (i = 0; i < negotiation_tips->nr; i++)
234 rev_list_insert_ref(negotiator, NULL,
235 &negotiation_tips->oid[i]);
239 static int find_common(struct fetch_negotiator *negotiator,
240 struct fetch_pack_args *args,
241 int fd[2], struct object_id *result_oid,
245 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
246 const struct object_id *oid;
247 unsigned in_vain = 0;
248 int got_continue = 0;
250 struct strbuf req_buf = STRBUF_INIT;
251 size_t state_len = 0;
253 if (args->stateless_rpc && multi_ack == 1)
254 die(_("--stateless-rpc requires multi_ack_detailed"));
256 if (!args->no_dependents) {
257 mark_tips(negotiator, args->negotiation_tips);
258 for_each_cached_alternate(negotiator, insert_one_alternate_object);
262 for ( ; refs ; refs = refs->next) {
263 struct object_id *remote = &refs->old_oid;
264 const char *remote_hex;
268 * If that object is complete (i.e. it is an ancestor of a
269 * local ref), we tell them we have it but do not have to
270 * tell them about its ancestors, which they already know
273 * We use lookup_object here because we are only
274 * interested in the case we *know* the object is
275 * reachable and we have already scanned it.
277 * Do this only if args->no_dependents is false (if it is true,
278 * we cannot trust the object flags).
280 if (!args->no_dependents &&
281 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
282 (o->flags & COMPLETE)) {
286 remote_hex = oid_to_hex(remote);
288 struct strbuf c = STRBUF_INIT;
289 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
290 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
291 if (no_done) strbuf_addstr(&c, " no-done");
292 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
293 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
294 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
295 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
296 if (args->no_progress) strbuf_addstr(&c, " no-progress");
297 if (args->include_tag) strbuf_addstr(&c, " include-tag");
298 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
299 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
300 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
301 if (agent_supported) strbuf_addf(&c, " agent=%s",
302 git_user_agent_sanitized());
303 if (args->filter_options.choice)
304 strbuf_addstr(&c, " filter");
305 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
308 packet_buf_write(&req_buf, "want %s\n", remote_hex);
313 strbuf_release(&req_buf);
318 if (is_repository_shallow(the_repository))
319 write_shallow_commits(&req_buf, 1, NULL);
321 packet_buf_write(&req_buf, "deepen %d", args->depth);
322 if (args->deepen_since) {
323 timestamp_t max_age = approxidate(args->deepen_since);
324 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
326 if (args->deepen_not) {
328 for (i = 0; i < args->deepen_not->nr; i++) {
329 struct string_list_item *s = args->deepen_not->items + i;
330 packet_buf_write(&req_buf, "deepen-not %s", s->string);
333 if (server_supports_filtering && args->filter_options.choice)
334 packet_buf_write(&req_buf, "filter %s",
335 args->filter_options.filter_spec);
336 packet_buf_flush(&req_buf);
337 state_len = req_buf.len;
342 struct object_id oid;
344 send_request(args, fd[1], &req_buf);
345 while ((line = packet_read_line(fd[0], NULL))) {
346 if (skip_prefix(line, "shallow ", &arg)) {
347 if (get_oid_hex(arg, &oid))
348 die(_("invalid shallow line: %s"), line);
349 register_shallow(the_repository, &oid);
352 if (skip_prefix(line, "unshallow ", &arg)) {
353 if (get_oid_hex(arg, &oid))
354 die(_("invalid unshallow line: %s"), line);
355 if (!lookup_object(the_repository, oid.hash))
356 die(_("object not found: %s"), line);
357 /* make sure that it is parsed as shallow */
358 if (!parse_object(the_repository, &oid))
359 die(_("error in object: %s"), line);
360 if (unregister_shallow(&oid))
361 die(_("no shallow found: %s"), line);
364 die(_("expected shallow/unshallow, got %s"), line);
366 } else if (!args->stateless_rpc)
367 send_request(args, fd[1], &req_buf);
369 if (!args->stateless_rpc) {
370 /* If we aren't using the stateless-rpc interface
371 * we don't need to retain the headers.
373 strbuf_setlen(&req_buf, 0);
379 if (args->no_dependents)
381 while ((oid = negotiator->next(negotiator))) {
382 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
383 print_verbose(args, "have %s", oid_to_hex(oid));
385 if (flush_at <= ++count) {
388 packet_buf_flush(&req_buf);
389 send_request(args, fd[1], &req_buf);
390 strbuf_setlen(&req_buf, state_len);
392 flush_at = next_flush(args->stateless_rpc, count);
395 * We keep one window "ahead" of the other side, and
396 * will wait for an ACK only on the next one
398 if (!args->stateless_rpc && count == INITIAL_FLUSH)
401 consume_shallow_list(args, fd[0]);
403 ack = get_ack(fd[0], result_oid);
405 print_verbose(args, _("got %s %d %s"), "ack",
406 ack, oid_to_hex(result_oid));
416 struct commit *commit =
417 lookup_commit(the_repository,
422 die(_("invalid commit %s"), oid_to_hex(result_oid));
423 was_common = negotiator->ack(negotiator, commit);
424 if (args->stateless_rpc
427 /* We need to replay the have for this object
428 * on the next RPC request so the peer knows
429 * it is in common with us.
431 const char *hex = oid_to_hex(result_oid);
432 packet_buf_write(&req_buf, "have %s\n", hex);
433 state_len = req_buf.len;
435 * Reset in_vain because an ack
436 * for this commit has not been
440 } else if (!args->stateless_rpc
441 || ack != ACK_common)
445 if (ack == ACK_ready)
452 if (got_continue && MAX_IN_VAIN < in_vain) {
453 print_verbose(args, _("giving up"));
461 if (!got_ready || !no_done) {
462 packet_buf_write(&req_buf, "done\n");
463 send_request(args, fd[1], &req_buf);
465 print_verbose(args, _("done"));
470 strbuf_release(&req_buf);
472 if (!got_ready || !no_done)
473 consume_shallow_list(args, fd[0]);
474 while (flushes || multi_ack) {
475 int ack = get_ack(fd[0], result_oid);
477 print_verbose(args, _("got %s (%d) %s"), "ack",
478 ack, oid_to_hex(result_oid));
486 /* it is no error to fetch into a completely empty repo */
487 return count ? retval : 0;
490 static struct commit_list *complete;
492 static int mark_complete(const struct object_id *oid)
494 struct object *o = parse_object(the_repository, oid);
496 while (o && o->type == OBJ_TAG) {
497 struct tag *t = (struct tag *) o;
499 break; /* broken repository */
500 o->flags |= COMPLETE;
501 o = parse_object(the_repository, &t->tagged->oid);
503 if (o && o->type == OBJ_COMMIT) {
504 struct commit *commit = (struct commit *)o;
505 if (!(commit->object.flags & COMPLETE)) {
506 commit->object.flags |= COMPLETE;
507 commit_list_insert(commit, &complete);
513 static int mark_complete_oid(const char *refname, const struct object_id *oid,
514 int flag, void *cb_data)
516 return mark_complete(oid);
519 static void mark_recent_complete_commits(struct fetch_pack_args *args,
522 while (complete && cutoff <= complete->item->date) {
523 print_verbose(args, _("Marking %s as complete"),
524 oid_to_hex(&complete->item->object.oid));
525 pop_most_recent_commit(&complete, COMPLETE);
529 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
531 for (; refs; refs = refs->next)
532 oidset_insert(oids, &refs->old_oid);
535 static int tip_oids_contain(struct oidset *tip_oids,
536 struct ref *unmatched, struct ref *newlist,
537 const struct object_id *id)
540 * Note that this only looks at the ref lists the first time it's
541 * called. This works out in filter_refs() because even though it may
542 * add to "newlist" between calls, the additions will always be for
543 * oids that are already in the set.
545 if (!tip_oids->map.map.tablesize) {
546 add_refs_to_oidset(tip_oids, unmatched);
547 add_refs_to_oidset(tip_oids, newlist);
549 return oidset_contains(tip_oids, id);
552 static void filter_refs(struct fetch_pack_args *args,
554 struct ref **sought, int nr_sought)
556 struct ref *newlist = NULL;
557 struct ref **newtail = &newlist;
558 struct ref *unmatched = NULL;
559 struct ref *ref, *next;
560 struct oidset tip_oids = OIDSET_INIT;
564 for (ref = *refs; ref; ref = next) {
568 if (starts_with(ref->name, "refs/") &&
569 check_refname_format(ref->name, 0))
572 while (i < nr_sought) {
573 int cmp = strcmp(ref->name, sought[i]->name);
575 break; /* definitely do not have it */
577 keep = 1; /* definitely have it */
578 sought[i]->match_status = REF_MATCHED;
583 if (!keep && args->fetch_all &&
584 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
591 newtail = &ref->next;
593 ref->next = unmatched;
598 /* Append unmatched requests to the list */
599 for (i = 0; i < nr_sought; i++) {
600 struct object_id oid;
604 if (ref->match_status != REF_NOT_MATCHED)
606 if (parse_oid_hex(ref->name, &oid, &p) ||
608 oidcmp(&oid, &ref->old_oid))
611 if ((allow_unadvertised_object_request &
612 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
613 tip_oids_contain(&tip_oids, unmatched, newlist,
615 ref->match_status = REF_MATCHED;
616 *newtail = copy_ref(ref);
617 newtail = &(*newtail)->next;
619 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
623 oidset_clear(&tip_oids);
624 for (ref = unmatched; ref; ref = next) {
632 static void mark_alternate_complete(struct fetch_negotiator *unused,
635 mark_complete(&obj->oid);
638 struct loose_object_iter {
639 struct oidset *loose_object_set;
644 * If the number of refs is not larger than the number of loose objects,
645 * this function stops inserting.
647 static int add_loose_objects_to_set(const struct object_id *oid,
651 struct loose_object_iter *iter = data;
652 oidset_insert(iter->loose_object_set, oid);
653 if (iter->refs == NULL)
656 iter->refs = iter->refs->next;
661 * Mark recent commits available locally and reachable from a local ref as
662 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
663 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
664 * thus do not need COMMON_REF marks).
666 * The cutoff time for recency is determined by this heuristic: it is the
667 * earliest commit time of the objects in refs that are commits and that we know
668 * the commit time of.
670 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
671 struct fetch_pack_args *args,
675 int old_save_commit_buffer = save_commit_buffer;
676 timestamp_t cutoff = 0;
677 struct oidset loose_oid_set = OIDSET_INIT;
679 struct loose_object_iter iter = {&loose_oid_set, *refs};
681 /* Enumerate all loose objects or know refs are not so many. */
682 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
685 save_commit_buffer = 0;
687 for (ref = *refs; ref; ref = ref->next) {
689 unsigned int flags = OBJECT_INFO_QUICK;
692 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
694 * I know this does not exist in the loose form,
695 * so check if it exists in a non-loose form.
697 flags |= OBJECT_INFO_IGNORE_LOOSE;
700 if (!has_object_file_with_flags(&ref->old_oid, flags))
702 o = parse_object(the_repository, &ref->old_oid);
706 /* We already have it -- which may mean that we were
707 * in sync with the other side at some time after
708 * that (it is OK if we guess wrong here).
710 if (o->type == OBJ_COMMIT) {
711 struct commit *commit = (struct commit *)o;
712 if (!cutoff || cutoff < commit->date)
713 cutoff = commit->date;
717 oidset_clear(&loose_oid_set);
720 for_each_ref(mark_complete_oid, NULL);
721 for_each_cached_alternate(NULL, mark_alternate_complete);
722 commit_list_sort_by_date(&complete);
724 mark_recent_complete_commits(args, cutoff);
728 * Mark all complete remote refs as common refs.
729 * Don't mark them common yet; the server has to be told so first.
731 for (ref = *refs; ref; ref = ref->next) {
732 struct object *o = deref_tag(the_repository,
733 lookup_object(the_repository,
737 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
740 negotiator->known_common(negotiator,
744 save_commit_buffer = old_save_commit_buffer;
748 * Returns 1 if every object pointed to by the given remote refs is available
749 * locally and reachable from a local ref, and 0 otherwise.
751 static int everything_local(struct fetch_pack_args *args,
757 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
758 const struct object_id *remote = &ref->old_oid;
761 o = lookup_object(the_repository, remote->hash);
762 if (!o || !(o->flags & COMPLETE)) {
764 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
768 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
775 static int sideband_demux(int in, int out, void *data)
780 ret = recv_sideband("fetch-pack", xd[0], out);
785 static int get_pack(struct fetch_pack_args *args,
786 int xd[2], char **pack_lockfile)
789 int do_keep = args->keep_pack;
790 const char *cmd_name;
791 struct pack_header header;
793 struct child_process cmd = CHILD_PROCESS_INIT;
796 memset(&demux, 0, sizeof(demux));
798 /* xd[] is talking with upload-pack; subprocess reads from
799 * xd[0], spits out band#2 to stderr, and feeds us band#1
800 * through demux->out.
802 demux.proc = sideband_demux;
805 demux.isolate_sigpipe = 1;
806 if (start_async(&demux))
807 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
812 if (!args->keep_pack && unpack_limit) {
814 if (read_pack_header(demux.out, &header))
815 die(_("protocol error: bad pack header"));
817 if (ntohl(header.hdr_entries) < unpack_limit)
823 if (alternate_shallow_file) {
824 argv_array_push(&cmd.args, "--shallow-file");
825 argv_array_push(&cmd.args, alternate_shallow_file);
828 if (do_keep || args->from_promisor) {
831 cmd_name = "index-pack";
832 argv_array_push(&cmd.args, cmd_name);
833 argv_array_push(&cmd.args, "--stdin");
834 if (!args->quiet && !args->no_progress)
835 argv_array_push(&cmd.args, "-v");
836 if (args->use_thin_pack)
837 argv_array_push(&cmd.args, "--fix-thin");
838 if (do_keep && (args->lock_pack || unpack_limit)) {
839 char hostname[HOST_NAME_MAX + 1];
840 if (xgethostname(hostname, sizeof(hostname)))
841 xsnprintf(hostname, sizeof(hostname), "localhost");
842 argv_array_pushf(&cmd.args,
843 "--keep=fetch-pack %"PRIuMAX " on %s",
844 (uintmax_t)getpid(), hostname);
846 if (args->check_self_contained_and_connected)
847 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
848 if (args->from_promisor)
849 argv_array_push(&cmd.args, "--promisor");
852 cmd_name = "unpack-objects";
853 argv_array_push(&cmd.args, cmd_name);
854 if (args->quiet || args->no_progress)
855 argv_array_push(&cmd.args, "-q");
856 args->check_self_contained_and_connected = 0;
860 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
861 ntohl(header.hdr_version),
862 ntohl(header.hdr_entries));
863 if (fetch_fsck_objects >= 0
865 : transfer_fsck_objects >= 0
866 ? transfer_fsck_objects
868 if (args->from_promisor)
870 * We cannot use --strict in index-pack because it
871 * checks both broken objects and links, but we only
872 * want to check for broken objects.
874 argv_array_push(&cmd.args, "--fsck-objects");
876 argv_array_pushf(&cmd.args, "--strict%s",
882 if (start_command(&cmd))
883 die(_("fetch-pack: unable to fork off %s"), cmd_name);
884 if (do_keep && pack_lockfile) {
885 *pack_lockfile = index_pack_lockfile(cmd.out);
890 /* Closed by start_command() */
893 ret = finish_command(&cmd);
894 if (!ret || (args->check_self_contained_and_connected && ret == 1))
895 args->self_contained_and_connected =
896 args->check_self_contained_and_connected &&
899 die(_("%s failed"), cmd_name);
900 if (use_sideband && finish_async(&demux))
901 die(_("error in sideband demultiplexer"));
905 static int cmp_ref_by_name(const void *a_, const void *b_)
907 const struct ref *a = *((const struct ref **)a_);
908 const struct ref *b = *((const struct ref **)b_);
909 return strcmp(a->name, b->name);
912 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
914 const struct ref *orig_ref,
915 struct ref **sought, int nr_sought,
916 struct shallow_info *si,
917 char **pack_lockfile)
919 struct ref *ref = copy_ref_list(orig_ref);
920 struct object_id oid;
921 const char *agent_feature;
923 struct fetch_negotiator negotiator;
924 fetch_negotiator_init(&negotiator, negotiation_algorithm);
926 sort_ref_list(&ref, ref_compare_name);
927 QSORT(sought, nr_sought, cmp_ref_by_name);
929 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
930 die(_("Server does not support shallow clients"));
931 if (args->depth > 0 || args->deepen_since || args->deepen_not)
933 if (server_supports("multi_ack_detailed")) {
934 print_verbose(args, _("Server supports multi_ack_detailed"));
936 if (server_supports("no-done")) {
937 print_verbose(args, _("Server supports no-done"));
938 if (args->stateless_rpc)
942 else if (server_supports("multi_ack")) {
943 print_verbose(args, _("Server supports multi_ack"));
946 if (server_supports("side-band-64k")) {
947 print_verbose(args, _("Server supports side-band-64k"));
950 else if (server_supports("side-band")) {
951 print_verbose(args, _("Server supports side-band"));
954 if (server_supports("allow-tip-sha1-in-want")) {
955 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
956 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
958 if (server_supports("allow-reachable-sha1-in-want")) {
959 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
960 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
962 if (!server_supports("thin-pack"))
963 args->use_thin_pack = 0;
964 if (!server_supports("no-progress"))
965 args->no_progress = 0;
966 if (!server_supports("include-tag"))
967 args->include_tag = 0;
968 if (server_supports("ofs-delta"))
969 print_verbose(args, _("Server supports ofs-delta"));
971 prefer_ofs_delta = 0;
973 if (server_supports("filter")) {
974 server_supports_filtering = 1;
975 print_verbose(args, _("Server supports filter"));
976 } else if (args->filter_options.choice) {
977 warning("filtering not recognized by server, ignoring");
980 if ((agent_feature = server_feature_value("agent", &agent_len))) {
983 print_verbose(args, _("Server version is %.*s"),
984 agent_len, agent_feature);
986 if (server_supports("deepen-since"))
988 else if (args->deepen_since)
989 die(_("Server does not support --shallow-since"));
990 if (server_supports("deepen-not"))
992 else if (args->deepen_not)
993 die(_("Server does not support --shallow-exclude"));
994 if (!server_supports("deepen-relative") && args->deepen_relative)
995 die(_("Server does not support --deepen"));
997 if (!args->no_dependents) {
998 mark_complete_and_common_ref(&negotiator, args, &ref);
999 filter_refs(args, &ref, sought, nr_sought);
1000 if (everything_local(args, &ref)) {
1001 packet_flush(fd[1]);
1005 filter_refs(args, &ref, sought, nr_sought);
1007 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
1008 if (!args->keep_pack)
1009 /* When cloning, it is not unusual to have
1012 warning(_("no common commits"));
1014 if (args->stateless_rpc)
1015 packet_flush(fd[1]);
1017 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1019 else if (si->nr_ours || si->nr_theirs)
1020 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1022 alternate_shallow_file = NULL;
1023 if (get_pack(args, fd, pack_lockfile))
1024 die(_("git fetch-pack: fetch failed."));
1027 negotiator.release(&negotiator);
1031 static void add_shallow_requests(struct strbuf *req_buf,
1032 const struct fetch_pack_args *args)
1034 if (is_repository_shallow(the_repository))
1035 write_shallow_commits(req_buf, 1, NULL);
1036 if (args->depth > 0)
1037 packet_buf_write(req_buf, "deepen %d", args->depth);
1038 if (args->deepen_since) {
1039 timestamp_t max_age = approxidate(args->deepen_since);
1040 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1042 if (args->deepen_not) {
1044 for (i = 0; i < args->deepen_not->nr; i++) {
1045 struct string_list_item *s = args->deepen_not->items + i;
1046 packet_buf_write(req_buf, "deepen-not %s", s->string);
1051 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1053 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1055 for ( ; wants ; wants = wants->next) {
1056 const struct object_id *remote = &wants->old_oid;
1060 * If that object is complete (i.e. it is an ancestor of a
1061 * local ref), we tell them we have it but do not have to
1062 * tell them about its ancestors, which they already know
1065 * We use lookup_object here because we are only
1066 * interested in the case we *know* the object is
1067 * reachable and we have already scanned it.
1069 * Do this only if args->no_dependents is false (if it is true,
1070 * we cannot trust the object flags).
1072 if (!no_dependents &&
1073 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1074 (o->flags & COMPLETE)) {
1078 if (!use_ref_in_want || wants->exact_oid)
1079 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1081 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1085 static void add_common(struct strbuf *req_buf, struct oidset *common)
1087 struct oidset_iter iter;
1088 const struct object_id *oid;
1089 oidset_iter_init(common, &iter);
1091 while ((oid = oidset_iter_next(&iter))) {
1092 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1096 static int add_haves(struct fetch_negotiator *negotiator,
1097 struct strbuf *req_buf,
1098 int *haves_to_send, int *in_vain)
1101 int haves_added = 0;
1102 const struct object_id *oid;
1104 while ((oid = negotiator->next(negotiator))) {
1105 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1106 if (++haves_added >= *haves_to_send)
1110 *in_vain += haves_added;
1111 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1113 packet_buf_write(req_buf, "done\n");
1117 /* Increase haves to send on next round */
1118 *haves_to_send = next_flush(1, *haves_to_send);
1123 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1124 const struct fetch_pack_args *args,
1125 const struct ref *wants, struct oidset *common,
1126 int *haves_to_send, int *in_vain)
1129 struct strbuf req_buf = STRBUF_INIT;
1131 if (server_supports_v2("fetch", 1))
1132 packet_buf_write(&req_buf, "command=fetch");
1133 if (server_supports_v2("agent", 0))
1134 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1135 if (args->server_options && args->server_options->nr &&
1136 server_supports_v2("server-option", 1)) {
1138 for (i = 0; i < args->server_options->nr; i++)
1139 packet_write_fmt(fd_out, "server-option=%s",
1140 args->server_options->items[i].string);
1143 packet_buf_delim(&req_buf);
1144 if (args->use_thin_pack)
1145 packet_buf_write(&req_buf, "thin-pack");
1146 if (args->no_progress)
1147 packet_buf_write(&req_buf, "no-progress");
1148 if (args->include_tag)
1149 packet_buf_write(&req_buf, "include-tag");
1150 if (prefer_ofs_delta)
1151 packet_buf_write(&req_buf, "ofs-delta");
1153 /* Add shallow-info and deepen request */
1154 if (server_supports_feature("fetch", "shallow", 0))
1155 add_shallow_requests(&req_buf, args);
1156 else if (is_repository_shallow(the_repository) || args->deepen)
1157 die(_("Server does not support shallow requests"));
1160 if (server_supports_feature("fetch", "filter", 0) &&
1161 args->filter_options.choice) {
1162 print_verbose(args, _("Server supports filter"));
1163 packet_buf_write(&req_buf, "filter %s",
1164 args->filter_options.filter_spec);
1165 } else if (args->filter_options.choice) {
1166 warning("filtering not recognized by server, ignoring");
1170 add_wants(args->no_dependents, wants, &req_buf);
1172 if (args->no_dependents) {
1173 packet_buf_write(&req_buf, "done");
1176 /* Add all of the common commits we've found in previous rounds */
1177 add_common(&req_buf, common);
1179 /* Add initial haves */
1180 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1184 packet_buf_flush(&req_buf);
1185 write_or_die(fd_out, req_buf.buf, req_buf.len);
1187 strbuf_release(&req_buf);
1192 * Processes a section header in a server's response and checks if it matches
1193 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1194 * not consumed); if 0, the line will be consumed and the function will die if
1195 * the section header doesn't match what was expected.
1197 static int process_section_header(struct packet_reader *reader,
1198 const char *section, int peek)
1202 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1203 die(_("error reading section header '%s'"), section);
1205 ret = !strcmp(reader->line, section);
1209 die(_("expected '%s', received '%s'"),
1210 section, reader->line);
1211 packet_reader_read(reader);
1217 static int process_acks(struct fetch_negotiator *negotiator,
1218 struct packet_reader *reader,
1219 struct oidset *common)
1222 int received_ready = 0;
1223 int received_ack = 0;
1225 process_section_header(reader, "acknowledgments", 0);
1226 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1229 if (!strcmp(reader->line, "NAK"))
1232 if (skip_prefix(reader->line, "ACK ", &arg)) {
1233 struct object_id oid;
1234 if (!get_oid_hex(arg, &oid)) {
1235 struct commit *commit;
1236 oidset_insert(common, &oid);
1237 commit = lookup_commit(the_repository, &oid);
1238 negotiator->ack(negotiator, commit);
1243 if (!strcmp(reader->line, "ready")) {
1248 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1251 if (reader->status != PACKET_READ_FLUSH &&
1252 reader->status != PACKET_READ_DELIM)
1253 die(_("error processing acks: %d"), reader->status);
1255 /* return 0 if no common, 1 if there are common, or 2 if ready */
1256 return received_ready ? 2 : (received_ack ? 1 : 0);
1259 static void receive_shallow_info(struct fetch_pack_args *args,
1260 struct packet_reader *reader)
1262 process_section_header(reader, "shallow-info", 0);
1263 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1265 struct object_id oid;
1267 if (skip_prefix(reader->line, "shallow ", &arg)) {
1268 if (get_oid_hex(arg, &oid))
1269 die(_("invalid shallow line: %s"), reader->line);
1270 register_shallow(the_repository, &oid);
1273 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1274 if (get_oid_hex(arg, &oid))
1275 die(_("invalid unshallow line: %s"), reader->line);
1276 if (!lookup_object(the_repository, oid.hash))
1277 die(_("object not found: %s"), reader->line);
1278 /* make sure that it is parsed as shallow */
1279 if (!parse_object(the_repository, &oid))
1280 die(_("error in object: %s"), reader->line);
1281 if (unregister_shallow(&oid))
1282 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 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1296 static void receive_wanted_refs(struct packet_reader *reader,
1297 struct ref **sought, int nr_sought)
1299 process_section_header(reader, "wanted-refs", 0);
1300 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1301 struct object_id oid;
1305 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1306 die(_("expected wanted-ref, got '%s'"), reader->line);
1308 for (i = 0; i < nr_sought; i++) {
1309 if (!strcmp(end, sought[i]->name)) {
1310 oidcpy(&sought[i]->old_oid, &oid);
1316 die(_("unexpected wanted-ref: '%s'"), reader->line);
1319 if (reader->status != PACKET_READ_DELIM)
1320 die(_("error processing wanted refs: %d"), reader->status);
1324 FETCH_CHECK_LOCAL = 0,
1331 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1333 const struct ref *orig_ref,
1334 struct ref **sought, int nr_sought,
1335 char **pack_lockfile)
1337 struct ref *ref = copy_ref_list(orig_ref);
1338 enum fetch_state state = FETCH_CHECK_LOCAL;
1339 struct oidset common = OIDSET_INIT;
1340 struct packet_reader reader;
1342 int haves_to_send = INITIAL_FLUSH;
1343 struct fetch_negotiator negotiator;
1344 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1345 packet_reader_init(&reader, fd[0], NULL, 0,
1346 PACKET_READ_CHOMP_NEWLINE);
1348 while (state != FETCH_DONE) {
1350 case FETCH_CHECK_LOCAL:
1351 sort_ref_list(&ref, ref_compare_name);
1352 QSORT(sought, nr_sought, cmp_ref_by_name);
1354 /* v2 supports these by default */
1355 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1357 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1360 /* Filter 'ref' by 'sought' and those that aren't local */
1361 if (!args->no_dependents) {
1362 mark_complete_and_common_ref(&negotiator, args, &ref);
1363 filter_refs(args, &ref, sought, nr_sought);
1364 if (everything_local(args, &ref))
1367 state = FETCH_SEND_REQUEST;
1369 mark_tips(&negotiator, args->negotiation_tips);
1370 for_each_cached_alternate(&negotiator,
1371 insert_one_alternate_object);
1373 filter_refs(args, &ref, sought, nr_sought);
1374 state = FETCH_SEND_REQUEST;
1377 case FETCH_SEND_REQUEST:
1378 if (send_fetch_request(&negotiator, fd[1], args, ref,
1380 &haves_to_send, &in_vain))
1381 state = FETCH_GET_PACK;
1383 state = FETCH_PROCESS_ACKS;
1385 case FETCH_PROCESS_ACKS:
1386 /* Process ACKs/NAKs */
1387 switch (process_acks(&negotiator, &reader, &common)) {
1389 state = FETCH_GET_PACK;
1395 state = FETCH_SEND_REQUEST;
1399 case FETCH_GET_PACK:
1400 /* Check for shallow-info section */
1401 if (process_section_header(&reader, "shallow-info", 1))
1402 receive_shallow_info(args, &reader);
1404 if (process_section_header(&reader, "wanted-refs", 1))
1405 receive_wanted_refs(&reader, sought, nr_sought);
1408 process_section_header(&reader, "packfile", 0);
1409 if (get_pack(args, fd, pack_lockfile))
1410 die(_("git fetch-pack: fetch failed."));
1419 negotiator.release(&negotiator);
1420 oidset_clear(&common);
1424 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1426 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1429 if (git_config_pathname(&path, var, value))
1431 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1432 fsck_msg_types.len ? ',' : '=', path);
1437 if (skip_prefix(var, "fetch.fsck.", &var)) {
1438 if (is_valid_msg_type(var, value))
1439 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1440 fsck_msg_types.len ? ',' : '=', var, value);
1442 warning("Skipping unknown msg id '%s'", var);
1446 return git_default_config(var, value, cb);
1449 static void fetch_pack_config(void)
1451 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1452 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1453 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1454 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1455 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1456 git_config_get_string("fetch.negotiationalgorithm",
1457 &negotiation_algorithm);
1459 git_config(fetch_pack_config_cb, NULL);
1462 static void fetch_pack_setup(void)
1464 static int did_setup;
1467 fetch_pack_config();
1468 if (0 <= transfer_unpack_limit)
1469 unpack_limit = transfer_unpack_limit;
1470 else if (0 <= fetch_unpack_limit)
1471 unpack_limit = fetch_unpack_limit;
1475 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1477 struct string_list names = STRING_LIST_INIT_NODUP;
1480 for (src = dst = 0; src < nr; src++) {
1481 struct string_list_item *item;
1482 item = string_list_insert(&names, ref[src]->name);
1484 continue; /* already have it */
1485 item->util = ref[src];
1487 ref[dst] = ref[src];
1490 for (src = dst; src < nr; src++)
1492 string_list_clear(&names, 0);
1496 static void update_shallow(struct fetch_pack_args *args,
1497 struct ref **sought, int nr_sought,
1498 struct shallow_info *si)
1500 struct oid_array ref = OID_ARRAY_INIT;
1504 if (args->deepen && alternate_shallow_file) {
1505 if (*alternate_shallow_file == '\0') { /* --unshallow */
1506 unlink_or_warn(git_path_shallow(the_repository));
1507 rollback_lock_file(&shallow_lock);
1509 commit_lock_file(&shallow_lock);
1513 if (!si->shallow || !si->shallow->nr)
1516 if (args->cloning) {
1518 * remote is shallow, but this is a clone, there are
1519 * no objects in repo to worry about. Accept any
1520 * shallow points that exist in the pack (iow in repo
1521 * after get_pack() and reprepare_packed_git())
1523 struct oid_array extra = OID_ARRAY_INIT;
1524 struct object_id *oid = si->shallow->oid;
1525 for (i = 0; i < si->shallow->nr; i++)
1526 if (has_object_file(&oid[i]))
1527 oid_array_append(&extra, &oid[i]);
1529 setup_alternate_shallow(&shallow_lock,
1530 &alternate_shallow_file,
1532 commit_lock_file(&shallow_lock);
1534 oid_array_clear(&extra);
1538 if (!si->nr_ours && !si->nr_theirs)
1541 remove_nonexistent_theirs_shallow(si);
1542 if (!si->nr_ours && !si->nr_theirs)
1544 for (i = 0; i < nr_sought; i++)
1545 oid_array_append(&ref, &sought[i]->old_oid);
1548 if (args->update_shallow) {
1550 * remote is also shallow, .git/shallow may be updated
1551 * so all refs can be accepted. Make sure we only add
1552 * shallow roots that are actually reachable from new
1555 struct oid_array extra = OID_ARRAY_INIT;
1556 struct object_id *oid = si->shallow->oid;
1557 assign_shallow_commits_to_refs(si, NULL, NULL);
1558 if (!si->nr_ours && !si->nr_theirs) {
1559 oid_array_clear(&ref);
1562 for (i = 0; i < si->nr_ours; i++)
1563 oid_array_append(&extra, &oid[si->ours[i]]);
1564 for (i = 0; i < si->nr_theirs; i++)
1565 oid_array_append(&extra, &oid[si->theirs[i]]);
1566 setup_alternate_shallow(&shallow_lock,
1567 &alternate_shallow_file,
1569 commit_lock_file(&shallow_lock);
1570 oid_array_clear(&extra);
1571 oid_array_clear(&ref);
1576 * remote is also shallow, check what ref is safe to update
1577 * without updating .git/shallow
1579 status = xcalloc(nr_sought, sizeof(*status));
1580 assign_shallow_commits_to_refs(si, NULL, status);
1581 if (si->nr_ours || si->nr_theirs) {
1582 for (i = 0; i < nr_sought; i++)
1584 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1587 oid_array_clear(&ref);
1590 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1592 struct ref **rm = cb_data;
1593 struct ref *ref = *rm;
1596 return -1; /* end of the list */
1598 oidcpy(oid, &ref->old_oid);
1602 struct ref *fetch_pack(struct fetch_pack_args *args,
1603 int fd[], struct child_process *conn,
1604 const struct ref *ref,
1606 struct ref **sought, int nr_sought,
1607 struct oid_array *shallow,
1608 char **pack_lockfile,
1609 enum protocol_version version)
1611 struct ref *ref_cpy;
1612 struct shallow_info si;
1616 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1618 if (args->no_dependents && !args->filter_options.choice) {
1620 * The protocol does not support requesting that only the
1621 * wanted objects be sent, so approximate this by setting a
1622 * "blob:none" filter if no filter is already set. This works
1623 * for all object types: note that wanted blobs will still be
1624 * sent because they are directly specified as a "want".
1626 * NEEDSWORK: Add an option in the protocol to request that
1627 * only the wanted objects be sent, and implement it.
1629 parse_list_objects_filter(&args->filter_options, "blob:none");
1633 packet_flush(fd[1]);
1634 die(_("no matching remote head"));
1636 prepare_shallow_info(&si, shallow);
1637 if (version == protocol_v2)
1638 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1641 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1642 &si, pack_lockfile);
1643 reprepare_packed_git(the_repository);
1645 if (!args->cloning && args->deepen) {
1646 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1647 struct ref *iterator = ref_cpy;
1648 opt.shallow_file = alternate_shallow_file;
1650 opt.is_deepening_fetch = 1;
1651 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1652 error(_("remote did not send all necessary objects"));
1655 rollback_lock_file(&shallow_lock);
1658 args->connectivity_checked = 1;
1661 update_shallow(args, sought, nr_sought, &si);
1663 clear_shallow_info(&si);
1667 int report_unmatched_refs(struct ref **sought, int nr_sought)
1671 for (i = 0; i < nr_sought; i++) {
1674 switch (sought[i]->match_status) {
1677 case REF_NOT_MATCHED:
1678 error(_("no such remote ref %s"), sought[i]->name);
1680 case REF_UNADVERTISED_NOT_ALLOWED:
1681 error(_("Server does not allow request for unadvertised object %s"),