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"
25 static int transfer_unpack_limit = -1;
26 static int fetch_unpack_limit = -1;
27 static int unpack_limit = 100;
28 static int prefer_ofs_delta = 1;
30 static int deepen_since_ok;
31 static int deepen_not_ok;
32 static int fetch_fsck_objects = -1;
33 static int transfer_fsck_objects = -1;
34 static int agent_supported;
35 static int server_supports_filtering;
36 static struct lock_file shallow_lock;
37 static const char *alternate_shallow_file;
39 /* Remember to update object flag allocation in object.h */
40 #define COMPLETE (1U << 0)
41 #define ALTERNATE (1U << 1)
44 * After sending this many "have"s if we do not get any new ACK , we
45 * give up traversing our history.
47 #define MAX_IN_VAIN 256
49 static int multi_ack, use_sideband;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 static unsigned int allow_unadvertised_object_request;
56 __attribute__((format (printf, 2, 3)))
57 static inline void print_verbose(const struct fetch_pack_args *args,
65 va_start(params, fmt);
66 vfprintf(stderr, fmt, params);
71 struct alternate_object_cache {
72 struct object **items;
76 static void cache_one_alternate(const char *refname,
77 const struct object_id *oid,
80 struct alternate_object_cache *cache = vcache;
81 struct object *obj = parse_object(oid);
83 if (!obj || (obj->flags & ALTERNATE))
86 obj->flags |= ALTERNATE;
87 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
88 cache->items[cache->nr++] = obj;
91 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
92 void (*cb)(struct fetch_negotiator *,
95 static int initialized;
96 static struct alternate_object_cache cache;
100 for_each_alternate_ref(cache_one_alternate, &cache);
104 for (i = 0; i < cache.nr; i++)
105 cb(negotiator, cache.items[i]);
108 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
110 const struct object_id *oid)
112 struct object *o = deref_tag(parse_object(oid), refname, 0);
114 if (o && o->type == OBJ_COMMIT)
115 negotiator->add_tip(negotiator, (struct commit *)o);
120 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
121 int flag, void *cb_data)
123 return rev_list_insert_ref(cb_data, refname, oid);
134 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
136 if (args->stateless_rpc && args->deepen) {
137 /* If we sent a depth we will get back "duplicate"
138 * shallow and unshallow commands every time there
139 * is a block of have lines exchanged.
142 while ((line = packet_read_line(fd, NULL))) {
143 if (starts_with(line, "shallow "))
145 if (starts_with(line, "unshallow "))
147 die(_("git fetch-pack: expected shallow list"));
152 static enum ack_type get_ack(int fd, struct object_id *result_oid)
155 char *line = packet_read_line(fd, &len);
159 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
160 if (!strcmp(line, "NAK"))
162 if (skip_prefix(line, "ACK ", &arg)) {
163 if (!get_oid_hex(arg, result_oid)) {
168 if (strstr(arg, "continue"))
170 if (strstr(arg, "common"))
172 if (strstr(arg, "ready"))
177 if (skip_prefix(line, "ERR ", &arg))
178 die(_("remote error: %s"), arg);
179 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
182 static void send_request(struct fetch_pack_args *args,
183 int fd, struct strbuf *buf)
185 if (args->stateless_rpc) {
186 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
189 write_or_die(fd, buf->buf, buf->len);
192 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
195 rev_list_insert_ref(negotiator, NULL, &obj->oid);
198 #define INITIAL_FLUSH 16
199 #define PIPESAFE_FLUSH 32
200 #define LARGE_FLUSH 16384
202 static int next_flush(int stateless_rpc, int count)
205 if (count < LARGE_FLUSH)
208 count = count * 11 / 10;
210 if (count < PIPESAFE_FLUSH)
213 count += PIPESAFE_FLUSH;
218 static int find_common(struct fetch_negotiator *negotiator,
219 struct fetch_pack_args *args,
220 int fd[2], struct object_id *result_oid,
224 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
225 const struct object_id *oid;
226 unsigned in_vain = 0;
227 int got_continue = 0;
229 struct strbuf req_buf = STRBUF_INIT;
230 size_t state_len = 0;
232 if (args->stateless_rpc && multi_ack == 1)
233 die(_("--stateless-rpc requires multi_ack_detailed"));
235 for_each_ref(rev_list_insert_ref_oid, negotiator);
236 for_each_cached_alternate(negotiator, insert_one_alternate_object);
239 for ( ; refs ; refs = refs->next) {
240 struct object_id *remote = &refs->old_oid;
241 const char *remote_hex;
245 * If that object is complete (i.e. it is an ancestor of a
246 * local ref), we tell them we have it but do not have to
247 * tell them about its ancestors, which they already know
250 * We use lookup_object here because we are only
251 * interested in the case we *know* the object is
252 * reachable and we have already scanned it.
254 if (((o = lookup_object(remote->hash)) != NULL) &&
255 (o->flags & COMPLETE)) {
259 remote_hex = oid_to_hex(remote);
261 struct strbuf c = STRBUF_INIT;
262 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
263 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
264 if (no_done) strbuf_addstr(&c, " no-done");
265 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
266 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
267 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
268 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
269 if (args->no_progress) strbuf_addstr(&c, " no-progress");
270 if (args->include_tag) strbuf_addstr(&c, " include-tag");
271 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
272 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
273 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
274 if (agent_supported) strbuf_addf(&c, " agent=%s",
275 git_user_agent_sanitized());
276 if (args->filter_options.choice)
277 strbuf_addstr(&c, " filter");
278 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
281 packet_buf_write(&req_buf, "want %s\n", remote_hex);
286 strbuf_release(&req_buf);
291 if (is_repository_shallow(the_repository))
292 write_shallow_commits(&req_buf, 1, NULL);
294 packet_buf_write(&req_buf, "deepen %d", args->depth);
295 if (args->deepen_since) {
296 timestamp_t max_age = approxidate(args->deepen_since);
297 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
299 if (args->deepen_not) {
301 for (i = 0; i < args->deepen_not->nr; i++) {
302 struct string_list_item *s = args->deepen_not->items + i;
303 packet_buf_write(&req_buf, "deepen-not %s", s->string);
306 if (server_supports_filtering && args->filter_options.choice)
307 packet_buf_write(&req_buf, "filter %s",
308 args->filter_options.filter_spec);
309 packet_buf_flush(&req_buf);
310 state_len = req_buf.len;
315 struct object_id oid;
317 send_request(args, fd[1], &req_buf);
318 while ((line = packet_read_line(fd[0], NULL))) {
319 if (skip_prefix(line, "shallow ", &arg)) {
320 if (get_oid_hex(arg, &oid))
321 die(_("invalid shallow line: %s"), line);
322 register_shallow(the_repository, &oid);
325 if (skip_prefix(line, "unshallow ", &arg)) {
326 if (get_oid_hex(arg, &oid))
327 die(_("invalid unshallow line: %s"), line);
328 if (!lookup_object(oid.hash))
329 die(_("object not found: %s"), line);
330 /* make sure that it is parsed as shallow */
331 if (!parse_object(&oid))
332 die(_("error in object: %s"), line);
333 if (unregister_shallow(&oid))
334 die(_("no shallow found: %s"), line);
337 die(_("expected shallow/unshallow, got %s"), line);
339 } else if (!args->stateless_rpc)
340 send_request(args, fd[1], &req_buf);
342 if (!args->stateless_rpc) {
343 /* If we aren't using the stateless-rpc interface
344 * we don't need to retain the headers.
346 strbuf_setlen(&req_buf, 0);
352 if (args->no_dependents)
354 while ((oid = negotiator->next(negotiator))) {
355 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
356 print_verbose(args, "have %s", oid_to_hex(oid));
358 if (flush_at <= ++count) {
361 packet_buf_flush(&req_buf);
362 send_request(args, fd[1], &req_buf);
363 strbuf_setlen(&req_buf, state_len);
365 flush_at = next_flush(args->stateless_rpc, count);
368 * We keep one window "ahead" of the other side, and
369 * will wait for an ACK only on the next one
371 if (!args->stateless_rpc && count == INITIAL_FLUSH)
374 consume_shallow_list(args, fd[0]);
376 ack = get_ack(fd[0], result_oid);
378 print_verbose(args, _("got %s %d %s"), "ack",
379 ack, oid_to_hex(result_oid));
389 struct commit *commit =
390 lookup_commit(result_oid);
393 die(_("invalid commit %s"), oid_to_hex(result_oid));
394 was_common = negotiator->ack(negotiator, commit);
395 if (args->stateless_rpc
398 /* We need to replay the have for this object
399 * on the next RPC request so the peer knows
400 * it is in common with us.
402 const char *hex = oid_to_hex(result_oid);
403 packet_buf_write(&req_buf, "have %s\n", hex);
404 state_len = req_buf.len;
406 * Reset in_vain because an ack
407 * for this commit has not been
411 } else if (!args->stateless_rpc
412 || ack != ACK_common)
416 if (ack == ACK_ready)
423 if (got_continue && MAX_IN_VAIN < in_vain) {
424 print_verbose(args, _("giving up"));
432 if (!got_ready || !no_done) {
433 packet_buf_write(&req_buf, "done\n");
434 send_request(args, fd[1], &req_buf);
436 print_verbose(args, _("done"));
441 strbuf_release(&req_buf);
443 if (!got_ready || !no_done)
444 consume_shallow_list(args, fd[0]);
445 while (flushes || multi_ack) {
446 int ack = get_ack(fd[0], result_oid);
448 print_verbose(args, _("got %s (%d) %s"), "ack",
449 ack, oid_to_hex(result_oid));
457 /* it is no error to fetch into a completely empty repo */
458 return count ? retval : 0;
461 static struct commit_list *complete;
463 static int mark_complete(const struct object_id *oid)
465 struct object *o = parse_object(oid);
467 while (o && o->type == OBJ_TAG) {
468 struct tag *t = (struct tag *) o;
470 break; /* broken repository */
471 o->flags |= COMPLETE;
472 o = parse_object(&t->tagged->oid);
474 if (o && o->type == OBJ_COMMIT) {
475 struct commit *commit = (struct commit *)o;
476 if (!(commit->object.flags & COMPLETE)) {
477 commit->object.flags |= COMPLETE;
478 commit_list_insert(commit, &complete);
484 static int mark_complete_oid(const char *refname, const struct object_id *oid,
485 int flag, void *cb_data)
487 return mark_complete(oid);
490 static void mark_recent_complete_commits(struct fetch_pack_args *args,
493 while (complete && cutoff <= complete->item->date) {
494 print_verbose(args, _("Marking %s as complete"),
495 oid_to_hex(&complete->item->object.oid));
496 pop_most_recent_commit(&complete, COMPLETE);
500 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
502 for (; refs; refs = refs->next)
503 oidset_insert(oids, &refs->old_oid);
506 static int tip_oids_contain(struct oidset *tip_oids,
507 struct ref *unmatched, struct ref *newlist,
508 const struct object_id *id)
511 * Note that this only looks at the ref lists the first time it's
512 * called. This works out in filter_refs() because even though it may
513 * add to "newlist" between calls, the additions will always be for
514 * oids that are already in the set.
516 if (!tip_oids->map.map.tablesize) {
517 add_refs_to_oidset(tip_oids, unmatched);
518 add_refs_to_oidset(tip_oids, newlist);
520 return oidset_contains(tip_oids, id);
523 static void filter_refs(struct fetch_pack_args *args,
525 struct ref **sought, int nr_sought)
527 struct ref *newlist = NULL;
528 struct ref **newtail = &newlist;
529 struct ref *unmatched = NULL;
530 struct ref *ref, *next;
531 struct oidset tip_oids = OIDSET_INIT;
535 for (ref = *refs; ref; ref = next) {
539 if (starts_with(ref->name, "refs/") &&
540 check_refname_format(ref->name, 0))
543 while (i < nr_sought) {
544 int cmp = strcmp(ref->name, sought[i]->name);
546 break; /* definitely do not have it */
548 keep = 1; /* definitely have it */
549 sought[i]->match_status = REF_MATCHED;
554 if (!keep && args->fetch_all &&
555 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
562 newtail = &ref->next;
564 ref->next = unmatched;
569 /* Append unmatched requests to the list */
570 for (i = 0; i < nr_sought; i++) {
571 struct object_id oid;
575 if (ref->match_status != REF_NOT_MATCHED)
577 if (parse_oid_hex(ref->name, &oid, &p) ||
579 oidcmp(&oid, &ref->old_oid))
582 if ((allow_unadvertised_object_request &
583 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
584 tip_oids_contain(&tip_oids, unmatched, newlist,
586 ref->match_status = REF_MATCHED;
587 *newtail = copy_ref(ref);
588 newtail = &(*newtail)->next;
590 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
594 oidset_clear(&tip_oids);
595 for (ref = unmatched; ref; ref = next) {
603 static void mark_alternate_complete(struct fetch_negotiator *unused,
606 mark_complete(&obj->oid);
609 struct loose_object_iter {
610 struct oidset *loose_object_set;
615 * If the number of refs is not larger than the number of loose objects,
616 * this function stops inserting.
618 static int add_loose_objects_to_set(const struct object_id *oid,
622 struct loose_object_iter *iter = data;
623 oidset_insert(iter->loose_object_set, oid);
624 if (iter->refs == NULL)
627 iter->refs = iter->refs->next;
632 * Mark recent commits available locally and reachable from a local ref as
633 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
634 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
635 * thus do not need COMMON_REF marks).
637 * The cutoff time for recency is determined by this heuristic: it is the
638 * earliest commit time of the objects in refs that are commits and that we know
639 * the commit time of.
641 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
642 struct fetch_pack_args *args,
646 int old_save_commit_buffer = save_commit_buffer;
647 timestamp_t cutoff = 0;
648 struct oidset loose_oid_set = OIDSET_INIT;
650 struct loose_object_iter iter = {&loose_oid_set, *refs};
652 /* Enumerate all loose objects or know refs are not so many. */
653 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
656 save_commit_buffer = 0;
658 for (ref = *refs; ref; ref = ref->next) {
660 unsigned int flags = OBJECT_INFO_QUICK;
663 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
665 * I know this does not exist in the loose form,
666 * so check if it exists in a non-loose form.
668 flags |= OBJECT_INFO_IGNORE_LOOSE;
671 if (!has_object_file_with_flags(&ref->old_oid, flags))
673 o = parse_object(&ref->old_oid);
677 /* We already have it -- which may mean that we were
678 * in sync with the other side at some time after
679 * that (it is OK if we guess wrong here).
681 if (o->type == OBJ_COMMIT) {
682 struct commit *commit = (struct commit *)o;
683 if (!cutoff || cutoff < commit->date)
684 cutoff = commit->date;
688 oidset_clear(&loose_oid_set);
690 if (!args->no_dependents) {
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(lookup_object(ref->old_oid.hash),
707 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
710 negotiator->known_common(negotiator,
715 save_commit_buffer = old_save_commit_buffer;
719 * Returns 1 if every object pointed to by the given remote refs is available
720 * locally and reachable from a local ref, and 0 otherwise.
722 static int everything_local(struct fetch_pack_args *args,
728 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
729 const struct object_id *remote = &ref->old_oid;
732 o = lookup_object(remote->hash);
733 if (!o || !(o->flags & COMPLETE)) {
735 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
739 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
746 static int sideband_demux(int in, int out, void *data)
751 ret = recv_sideband("fetch-pack", xd[0], out);
756 static int get_pack(struct fetch_pack_args *args,
757 int xd[2], char **pack_lockfile)
760 int do_keep = args->keep_pack;
761 const char *cmd_name;
762 struct pack_header header;
764 struct child_process cmd = CHILD_PROCESS_INIT;
767 memset(&demux, 0, sizeof(demux));
769 /* xd[] is talking with upload-pack; subprocess reads from
770 * xd[0], spits out band#2 to stderr, and feeds us band#1
771 * through demux->out.
773 demux.proc = sideband_demux;
776 demux.isolate_sigpipe = 1;
777 if (start_async(&demux))
778 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
783 if (!args->keep_pack && unpack_limit) {
785 if (read_pack_header(demux.out, &header))
786 die(_("protocol error: bad pack header"));
788 if (ntohl(header.hdr_entries) < unpack_limit)
794 if (alternate_shallow_file) {
795 argv_array_push(&cmd.args, "--shallow-file");
796 argv_array_push(&cmd.args, alternate_shallow_file);
799 if (do_keep || args->from_promisor) {
802 cmd_name = "index-pack";
803 argv_array_push(&cmd.args, cmd_name);
804 argv_array_push(&cmd.args, "--stdin");
805 if (!args->quiet && !args->no_progress)
806 argv_array_push(&cmd.args, "-v");
807 if (args->use_thin_pack)
808 argv_array_push(&cmd.args, "--fix-thin");
809 if (do_keep && (args->lock_pack || unpack_limit)) {
810 char hostname[HOST_NAME_MAX + 1];
811 if (xgethostname(hostname, sizeof(hostname)))
812 xsnprintf(hostname, sizeof(hostname), "localhost");
813 argv_array_pushf(&cmd.args,
814 "--keep=fetch-pack %"PRIuMAX " on %s",
815 (uintmax_t)getpid(), hostname);
817 if (args->check_self_contained_and_connected)
818 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
819 if (args->from_promisor)
820 argv_array_push(&cmd.args, "--promisor");
823 cmd_name = "unpack-objects";
824 argv_array_push(&cmd.args, cmd_name);
825 if (args->quiet || args->no_progress)
826 argv_array_push(&cmd.args, "-q");
827 args->check_self_contained_and_connected = 0;
831 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
832 ntohl(header.hdr_version),
833 ntohl(header.hdr_entries));
834 if (fetch_fsck_objects >= 0
836 : transfer_fsck_objects >= 0
837 ? transfer_fsck_objects
839 if (args->from_promisor)
841 * We cannot use --strict in index-pack because it
842 * checks both broken objects and links, but we only
843 * want to check for broken objects.
845 argv_array_push(&cmd.args, "--fsck-objects");
847 argv_array_push(&cmd.args, "--strict");
852 if (start_command(&cmd))
853 die(_("fetch-pack: unable to fork off %s"), cmd_name);
854 if (do_keep && pack_lockfile) {
855 *pack_lockfile = index_pack_lockfile(cmd.out);
860 /* Closed by start_command() */
863 ret = finish_command(&cmd);
864 if (!ret || (args->check_self_contained_and_connected && ret == 1))
865 args->self_contained_and_connected =
866 args->check_self_contained_and_connected &&
869 die(_("%s failed"), cmd_name);
870 if (use_sideband && finish_async(&demux))
871 die(_("error in sideband demultiplexer"));
875 static int cmp_ref_by_name(const void *a_, const void *b_)
877 const struct ref *a = *((const struct ref **)a_);
878 const struct ref *b = *((const struct ref **)b_);
879 return strcmp(a->name, b->name);
882 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
884 const struct ref *orig_ref,
885 struct ref **sought, int nr_sought,
886 struct shallow_info *si,
887 char **pack_lockfile)
889 struct ref *ref = copy_ref_list(orig_ref);
890 struct object_id oid;
891 const char *agent_feature;
893 struct fetch_negotiator negotiator;
894 fetch_negotiator_init(&negotiator);
896 sort_ref_list(&ref, ref_compare_name);
897 QSORT(sought, nr_sought, cmp_ref_by_name);
899 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
900 die(_("Server does not support shallow clients"));
901 if (args->depth > 0 || args->deepen_since || args->deepen_not)
903 if (server_supports("multi_ack_detailed")) {
904 print_verbose(args, _("Server supports multi_ack_detailed"));
906 if (server_supports("no-done")) {
907 print_verbose(args, _("Server supports no-done"));
908 if (args->stateless_rpc)
912 else if (server_supports("multi_ack")) {
913 print_verbose(args, _("Server supports multi_ack"));
916 if (server_supports("side-band-64k")) {
917 print_verbose(args, _("Server supports side-band-64k"));
920 else if (server_supports("side-band")) {
921 print_verbose(args, _("Server supports side-band"));
924 if (server_supports("allow-tip-sha1-in-want")) {
925 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
926 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
928 if (server_supports("allow-reachable-sha1-in-want")) {
929 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
930 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
932 if (!server_supports("thin-pack"))
933 args->use_thin_pack = 0;
934 if (!server_supports("no-progress"))
935 args->no_progress = 0;
936 if (!server_supports("include-tag"))
937 args->include_tag = 0;
938 if (server_supports("ofs-delta"))
939 print_verbose(args, _("Server supports ofs-delta"));
941 prefer_ofs_delta = 0;
943 if (server_supports("filter")) {
944 server_supports_filtering = 1;
945 print_verbose(args, _("Server supports filter"));
946 } else if (args->filter_options.choice) {
947 warning("filtering not recognized by server, ignoring");
950 if ((agent_feature = server_feature_value("agent", &agent_len))) {
953 print_verbose(args, _("Server version is %.*s"),
954 agent_len, agent_feature);
956 if (server_supports("deepen-since"))
958 else if (args->deepen_since)
959 die(_("Server does not support --shallow-since"));
960 if (server_supports("deepen-not"))
962 else if (args->deepen_not)
963 die(_("Server does not support --shallow-exclude"));
964 if (!server_supports("deepen-relative") && args->deepen_relative)
965 die(_("Server does not support --deepen"));
967 mark_complete_and_common_ref(&negotiator, args, &ref);
968 filter_refs(args, &ref, sought, nr_sought);
969 if (everything_local(args, &ref)) {
973 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
974 if (!args->keep_pack)
975 /* When cloning, it is not unusual to have
978 warning(_("no common commits"));
980 if (args->stateless_rpc)
983 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
985 else if (si->nr_ours || si->nr_theirs)
986 alternate_shallow_file = setup_temporary_shallow(si->shallow);
988 alternate_shallow_file = NULL;
989 if (get_pack(args, fd, pack_lockfile))
990 die(_("git fetch-pack: fetch failed."));
993 negotiator.release(&negotiator);
997 static void add_shallow_requests(struct strbuf *req_buf,
998 const struct fetch_pack_args *args)
1000 if (is_repository_shallow(the_repository))
1001 write_shallow_commits(req_buf, 1, NULL);
1002 if (args->depth > 0)
1003 packet_buf_write(req_buf, "deepen %d", args->depth);
1004 if (args->deepen_since) {
1005 timestamp_t max_age = approxidate(args->deepen_since);
1006 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1008 if (args->deepen_not) {
1010 for (i = 0; i < args->deepen_not->nr; i++) {
1011 struct string_list_item *s = args->deepen_not->items + i;
1012 packet_buf_write(req_buf, "deepen-not %s", s->string);
1017 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1019 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1021 for ( ; wants ; wants = wants->next) {
1022 const struct object_id *remote = &wants->old_oid;
1026 * If that object is complete (i.e. it is an ancestor of a
1027 * local ref), we tell them we have it but do not have to
1028 * tell them about its ancestors, which they already know
1031 * We use lookup_object here because we are only
1032 * interested in the case we *know* the object is
1033 * reachable and we have already scanned it.
1035 if (((o = lookup_object(remote->hash)) != NULL) &&
1036 (o->flags & COMPLETE)) {
1040 if (!use_ref_in_want || wants->exact_oid)
1041 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1043 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1047 static void add_common(struct strbuf *req_buf, struct oidset *common)
1049 struct oidset_iter iter;
1050 const struct object_id *oid;
1051 oidset_iter_init(common, &iter);
1053 while ((oid = oidset_iter_next(&iter))) {
1054 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1058 static int add_haves(struct fetch_negotiator *negotiator,
1059 struct strbuf *req_buf,
1060 int *haves_to_send, int *in_vain)
1063 int haves_added = 0;
1064 const struct object_id *oid;
1066 while ((oid = negotiator->next(negotiator))) {
1067 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1068 if (++haves_added >= *haves_to_send)
1072 *in_vain += haves_added;
1073 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1075 packet_buf_write(req_buf, "done\n");
1079 /* Increase haves to send on next round */
1080 *haves_to_send = next_flush(1, *haves_to_send);
1085 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1086 const struct fetch_pack_args *args,
1087 const struct ref *wants, struct oidset *common,
1088 int *haves_to_send, int *in_vain)
1091 struct strbuf req_buf = STRBUF_INIT;
1093 if (server_supports_v2("fetch", 1))
1094 packet_buf_write(&req_buf, "command=fetch");
1095 if (server_supports_v2("agent", 0))
1096 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1097 if (args->server_options && args->server_options->nr &&
1098 server_supports_v2("server-option", 1)) {
1100 for (i = 0; i < args->server_options->nr; i++)
1101 packet_write_fmt(fd_out, "server-option=%s",
1102 args->server_options->items[i].string);
1105 packet_buf_delim(&req_buf);
1106 if (args->use_thin_pack)
1107 packet_buf_write(&req_buf, "thin-pack");
1108 if (args->no_progress)
1109 packet_buf_write(&req_buf, "no-progress");
1110 if (args->include_tag)
1111 packet_buf_write(&req_buf, "include-tag");
1112 if (prefer_ofs_delta)
1113 packet_buf_write(&req_buf, "ofs-delta");
1115 /* Add shallow-info and deepen request */
1116 if (server_supports_feature("fetch", "shallow", 0))
1117 add_shallow_requests(&req_buf, args);
1118 else if (is_repository_shallow(the_repository) || args->deepen)
1119 die(_("Server does not support shallow requests"));
1122 if (server_supports_feature("fetch", "filter", 0) &&
1123 args->filter_options.choice) {
1124 print_verbose(args, _("Server supports filter"));
1125 packet_buf_write(&req_buf, "filter %s",
1126 args->filter_options.filter_spec);
1127 } else if (args->filter_options.choice) {
1128 warning("filtering not recognized by server, ignoring");
1132 add_wants(wants, &req_buf);
1134 if (args->no_dependents) {
1135 packet_buf_write(&req_buf, "done");
1138 /* Add all of the common commits we've found in previous rounds */
1139 add_common(&req_buf, common);
1141 /* Add initial haves */
1142 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1146 packet_buf_flush(&req_buf);
1147 write_or_die(fd_out, req_buf.buf, req_buf.len);
1149 strbuf_release(&req_buf);
1154 * Processes a section header in a server's response and checks if it matches
1155 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1156 * not consumed); if 0, the line will be consumed and the function will die if
1157 * the section header doesn't match what was expected.
1159 static int process_section_header(struct packet_reader *reader,
1160 const char *section, int peek)
1164 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1165 die("error reading section header '%s'", section);
1167 ret = !strcmp(reader->line, section);
1171 die("expected '%s', received '%s'",
1172 section, reader->line);
1173 packet_reader_read(reader);
1179 static int process_acks(struct fetch_negotiator *negotiator,
1180 struct packet_reader *reader,
1181 struct oidset *common)
1184 int received_ready = 0;
1185 int received_ack = 0;
1187 process_section_header(reader, "acknowledgments", 0);
1188 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1191 if (!strcmp(reader->line, "NAK"))
1194 if (skip_prefix(reader->line, "ACK ", &arg)) {
1195 struct object_id oid;
1196 if (!get_oid_hex(arg, &oid)) {
1197 struct commit *commit;
1198 oidset_insert(common, &oid);
1199 commit = lookup_commit(&oid);
1200 negotiator->ack(negotiator, commit);
1205 if (!strcmp(reader->line, "ready")) {
1210 die("unexpected acknowledgment line: '%s'", reader->line);
1213 if (reader->status != PACKET_READ_FLUSH &&
1214 reader->status != PACKET_READ_DELIM)
1215 die("error processing acks: %d", reader->status);
1217 /* return 0 if no common, 1 if there are common, or 2 if ready */
1218 return received_ready ? 2 : (received_ack ? 1 : 0);
1221 static void receive_shallow_info(struct fetch_pack_args *args,
1222 struct packet_reader *reader)
1224 process_section_header(reader, "shallow-info", 0);
1225 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1227 struct object_id oid;
1229 if (skip_prefix(reader->line, "shallow ", &arg)) {
1230 if (get_oid_hex(arg, &oid))
1231 die(_("invalid shallow line: %s"), reader->line);
1232 register_shallow(the_repository, &oid);
1235 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1236 if (get_oid_hex(arg, &oid))
1237 die(_("invalid unshallow line: %s"), reader->line);
1238 if (!lookup_object(oid.hash))
1239 die(_("object not found: %s"), reader->line);
1240 /* make sure that it is parsed as shallow */
1241 if (!parse_object(&oid))
1242 die(_("error in object: %s"), reader->line);
1243 if (unregister_shallow(&oid))
1244 die(_("no shallow found: %s"), reader->line);
1247 die(_("expected shallow/unshallow, got %s"), reader->line);
1250 if (reader->status != PACKET_READ_FLUSH &&
1251 reader->status != PACKET_READ_DELIM)
1252 die("error processing shallow info: %d", reader->status);
1254 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1258 static void receive_wanted_refs(struct packet_reader *reader, struct ref *refs)
1260 process_section_header(reader, "wanted-refs", 0);
1261 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1262 struct object_id oid;
1264 struct ref *r = NULL;
1266 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1267 die("expected wanted-ref, got '%s'", reader->line);
1269 for (r = refs; r; r = r->next) {
1270 if (!strcmp(end, r->name)) {
1271 oidcpy(&r->old_oid, &oid);
1277 die("unexpected wanted-ref: '%s'", reader->line);
1280 if (reader->status != PACKET_READ_DELIM)
1281 die("error processing wanted refs: %d", reader->status);
1285 FETCH_CHECK_LOCAL = 0,
1292 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1294 const struct ref *orig_ref,
1295 struct ref **sought, int nr_sought,
1296 char **pack_lockfile)
1298 struct ref *ref = copy_ref_list(orig_ref);
1299 enum fetch_state state = FETCH_CHECK_LOCAL;
1300 struct oidset common = OIDSET_INIT;
1301 struct packet_reader reader;
1303 int haves_to_send = INITIAL_FLUSH;
1304 struct fetch_negotiator negotiator;
1305 fetch_negotiator_init(&negotiator);
1306 packet_reader_init(&reader, fd[0], NULL, 0,
1307 PACKET_READ_CHOMP_NEWLINE);
1309 while (state != FETCH_DONE) {
1311 case FETCH_CHECK_LOCAL:
1312 sort_ref_list(&ref, ref_compare_name);
1313 QSORT(sought, nr_sought, cmp_ref_by_name);
1315 /* v2 supports these by default */
1316 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1318 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1321 /* Filter 'ref' by 'sought' and those that aren't local */
1322 mark_complete_and_common_ref(&negotiator, args, &ref);
1323 filter_refs(args, &ref, sought, nr_sought);
1324 if (everything_local(args, &ref))
1327 state = FETCH_SEND_REQUEST;
1329 for_each_ref(rev_list_insert_ref_oid, &negotiator);
1330 for_each_cached_alternate(&negotiator,
1331 insert_one_alternate_object);
1333 case FETCH_SEND_REQUEST:
1334 if (send_fetch_request(&negotiator, fd[1], args, ref,
1336 &haves_to_send, &in_vain))
1337 state = FETCH_GET_PACK;
1339 state = FETCH_PROCESS_ACKS;
1341 case FETCH_PROCESS_ACKS:
1342 /* Process ACKs/NAKs */
1343 switch (process_acks(&negotiator, &reader, &common)) {
1345 state = FETCH_GET_PACK;
1351 state = FETCH_SEND_REQUEST;
1355 case FETCH_GET_PACK:
1356 /* Check for shallow-info section */
1357 if (process_section_header(&reader, "shallow-info", 1))
1358 receive_shallow_info(args, &reader);
1360 if (process_section_header(&reader, "wanted-refs", 1))
1361 receive_wanted_refs(&reader, ref);
1364 process_section_header(&reader, "packfile", 0);
1365 if (get_pack(args, fd, pack_lockfile))
1366 die(_("git fetch-pack: fetch failed."));
1375 negotiator.release(&negotiator);
1376 oidset_clear(&common);
1380 static void fetch_pack_config(void)
1382 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1383 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1384 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1385 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1386 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1388 git_config(git_default_config, NULL);
1391 static void fetch_pack_setup(void)
1393 static int did_setup;
1396 fetch_pack_config();
1397 if (0 <= transfer_unpack_limit)
1398 unpack_limit = transfer_unpack_limit;
1399 else if (0 <= fetch_unpack_limit)
1400 unpack_limit = fetch_unpack_limit;
1404 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1406 struct string_list names = STRING_LIST_INIT_NODUP;
1409 for (src = dst = 0; src < nr; src++) {
1410 struct string_list_item *item;
1411 item = string_list_insert(&names, ref[src]->name);
1413 continue; /* already have it */
1414 item->util = ref[src];
1416 ref[dst] = ref[src];
1419 for (src = dst; src < nr; src++)
1421 string_list_clear(&names, 0);
1425 static void update_shallow(struct fetch_pack_args *args,
1427 struct shallow_info *si)
1429 struct oid_array ref = OID_ARRAY_INIT;
1434 if (args->deepen && alternate_shallow_file) {
1435 if (*alternate_shallow_file == '\0') { /* --unshallow */
1436 unlink_or_warn(git_path_shallow(the_repository));
1437 rollback_lock_file(&shallow_lock);
1439 commit_lock_file(&shallow_lock);
1443 if (!si->shallow || !si->shallow->nr)
1446 if (args->cloning) {
1448 * remote is shallow, but this is a clone, there are
1449 * no objects in repo to worry about. Accept any
1450 * shallow points that exist in the pack (iow in repo
1451 * after get_pack() and reprepare_packed_git())
1453 struct oid_array extra = OID_ARRAY_INIT;
1454 struct object_id *oid = si->shallow->oid;
1455 for (i = 0; i < si->shallow->nr; i++)
1456 if (has_object_file(&oid[i]))
1457 oid_array_append(&extra, &oid[i]);
1459 setup_alternate_shallow(&shallow_lock,
1460 &alternate_shallow_file,
1462 commit_lock_file(&shallow_lock);
1464 oid_array_clear(&extra);
1468 if (!si->nr_ours && !si->nr_theirs)
1471 remove_nonexistent_theirs_shallow(si);
1472 if (!si->nr_ours && !si->nr_theirs)
1474 for (r = refs; r; r = r->next)
1475 oid_array_append(&ref, &r->old_oid);
1478 if (args->update_shallow) {
1480 * remote is also shallow, .git/shallow may be updated
1481 * so all refs can be accepted. Make sure we only add
1482 * shallow roots that are actually reachable from new
1485 struct oid_array extra = OID_ARRAY_INIT;
1486 struct object_id *oid = si->shallow->oid;
1487 assign_shallow_commits_to_refs(si, NULL, NULL);
1488 if (!si->nr_ours && !si->nr_theirs) {
1489 oid_array_clear(&ref);
1492 for (i = 0; i < si->nr_ours; i++)
1493 oid_array_append(&extra, &oid[si->ours[i]]);
1494 for (i = 0; i < si->nr_theirs; i++)
1495 oid_array_append(&extra, &oid[si->theirs[i]]);
1496 setup_alternate_shallow(&shallow_lock,
1497 &alternate_shallow_file,
1499 commit_lock_file(&shallow_lock);
1500 oid_array_clear(&extra);
1501 oid_array_clear(&ref);
1506 * remote is also shallow, check what ref is safe to update
1507 * without updating .git/shallow
1509 status = xcalloc(ref.nr, sizeof(*status));
1510 assign_shallow_commits_to_refs(si, NULL, status);
1511 if (si->nr_ours || si->nr_theirs) {
1512 for (r = refs, i = 0; r; r = r->next, i++)
1514 r->status = REF_STATUS_REJECT_SHALLOW;
1517 oid_array_clear(&ref);
1520 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1522 struct ref **rm = cb_data;
1523 struct ref *ref = *rm;
1526 return -1; /* end of the list */
1528 oidcpy(oid, &ref->old_oid);
1532 struct ref *fetch_pack(struct fetch_pack_args *args,
1533 int fd[], struct child_process *conn,
1534 const struct ref *ref,
1536 struct ref **sought, int nr_sought,
1537 struct oid_array *shallow,
1538 char **pack_lockfile,
1539 enum protocol_version version)
1541 struct ref *ref_cpy;
1542 struct shallow_info si;
1546 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1549 packet_flush(fd[1]);
1550 die(_("no matching remote head"));
1552 prepare_shallow_info(&si, shallow);
1553 if (version == protocol_v2)
1554 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1557 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1558 &si, pack_lockfile);
1559 reprepare_packed_git(the_repository);
1561 if (!args->cloning && args->deepen) {
1562 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1563 struct ref *iterator = ref_cpy;
1564 opt.shallow_file = alternate_shallow_file;
1566 opt.is_deepening_fetch = 1;
1567 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1568 error(_("remote did not send all necessary objects"));
1571 rollback_lock_file(&shallow_lock);
1574 args->connectivity_checked = 1;
1577 update_shallow(args, ref_cpy, &si);
1579 clear_shallow_info(&si);
1583 int report_unmatched_refs(struct ref **sought, int nr_sought)
1587 for (i = 0; i < nr_sought; i++) {
1590 switch (sought[i]->match_status) {
1593 case REF_NOT_MATCHED:
1594 error(_("no such remote ref %s"), sought[i]->name);
1596 case REF_UNADVERTISED_NOT_ALLOWED:
1597 error(_("Server does not allow request for unadvertised object %s"),