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(the_repository, 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(the_repository,
113 parse_object(the_repository, oid),
116 if (o && o->type == OBJ_COMMIT)
117 negotiator->add_tip(negotiator, (struct commit *)o);
122 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
123 int flag, void *cb_data)
125 return rev_list_insert_ref(cb_data, refname, oid);
136 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
138 if (args->stateless_rpc && args->deepen) {
139 /* If we sent a depth we will get back "duplicate"
140 * shallow and unshallow commands every time there
141 * is a block of have lines exchanged.
144 while ((line = packet_read_line(fd, NULL))) {
145 if (starts_with(line, "shallow "))
147 if (starts_with(line, "unshallow "))
149 die(_("git fetch-pack: expected shallow list"));
154 static enum ack_type get_ack(int fd, struct object_id *result_oid)
157 char *line = packet_read_line(fd, &len);
161 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
162 if (!strcmp(line, "NAK"))
164 if (skip_prefix(line, "ACK ", &arg)) {
165 if (!get_oid_hex(arg, result_oid)) {
170 if (strstr(arg, "continue"))
172 if (strstr(arg, "common"))
174 if (strstr(arg, "ready"))
179 if (skip_prefix(line, "ERR ", &arg))
180 die(_("remote error: %s"), arg);
181 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
184 static void send_request(struct fetch_pack_args *args,
185 int fd, struct strbuf *buf)
187 if (args->stateless_rpc) {
188 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
191 write_or_die(fd, buf->buf, buf->len);
194 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
197 rev_list_insert_ref(negotiator, NULL, &obj->oid);
200 #define INITIAL_FLUSH 16
201 #define PIPESAFE_FLUSH 32
202 #define LARGE_FLUSH 16384
204 static int next_flush(int stateless_rpc, int count)
207 if (count < LARGE_FLUSH)
210 count = count * 11 / 10;
212 if (count < PIPESAFE_FLUSH)
215 count += PIPESAFE_FLUSH;
220 static void mark_tips(struct fetch_negotiator *negotiator,
221 const struct oid_array *negotiation_tips)
225 if (!negotiation_tips) {
226 for_each_ref(rev_list_insert_ref_oid, negotiator);
230 for (i = 0; i < negotiation_tips->nr; i++)
231 rev_list_insert_ref(negotiator, NULL,
232 &negotiation_tips->oid[i]);
236 static int find_common(struct fetch_negotiator *negotiator,
237 struct fetch_pack_args *args,
238 int fd[2], struct object_id *result_oid,
242 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
243 const struct object_id *oid;
244 unsigned in_vain = 0;
245 int got_continue = 0;
247 struct strbuf req_buf = STRBUF_INIT;
248 size_t state_len = 0;
250 if (args->stateless_rpc && multi_ack == 1)
251 die(_("--stateless-rpc requires multi_ack_detailed"));
253 mark_tips(negotiator, args->negotiation_tips);
254 for_each_cached_alternate(negotiator, insert_one_alternate_object);
257 for ( ; refs ; refs = refs->next) {
258 struct object_id *remote = &refs->old_oid;
259 const char *remote_hex;
263 * If that object is complete (i.e. it is an ancestor of a
264 * local ref), we tell them we have it but do not have to
265 * tell them about its ancestors, which they already know
268 * We use lookup_object here because we are only
269 * interested in the case we *know* the object is
270 * reachable and we have already scanned it.
272 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
273 (o->flags & COMPLETE)) {
277 remote_hex = oid_to_hex(remote);
279 struct strbuf c = STRBUF_INIT;
280 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
281 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
282 if (no_done) strbuf_addstr(&c, " no-done");
283 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
284 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
285 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
286 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
287 if (args->no_progress) strbuf_addstr(&c, " no-progress");
288 if (args->include_tag) strbuf_addstr(&c, " include-tag");
289 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
290 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
291 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
292 if (agent_supported) strbuf_addf(&c, " agent=%s",
293 git_user_agent_sanitized());
294 if (args->filter_options.choice)
295 strbuf_addstr(&c, " filter");
296 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
299 packet_buf_write(&req_buf, "want %s\n", remote_hex);
304 strbuf_release(&req_buf);
309 if (is_repository_shallow(the_repository))
310 write_shallow_commits(&req_buf, 1, NULL);
312 packet_buf_write(&req_buf, "deepen %d", args->depth);
313 if (args->deepen_since) {
314 timestamp_t max_age = approxidate(args->deepen_since);
315 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
317 if (args->deepen_not) {
319 for (i = 0; i < args->deepen_not->nr; i++) {
320 struct string_list_item *s = args->deepen_not->items + i;
321 packet_buf_write(&req_buf, "deepen-not %s", s->string);
324 if (server_supports_filtering && args->filter_options.choice)
325 packet_buf_write(&req_buf, "filter %s",
326 args->filter_options.filter_spec);
327 packet_buf_flush(&req_buf);
328 state_len = req_buf.len;
333 struct object_id oid;
335 send_request(args, fd[1], &req_buf);
336 while ((line = packet_read_line(fd[0], NULL))) {
337 if (skip_prefix(line, "shallow ", &arg)) {
338 if (get_oid_hex(arg, &oid))
339 die(_("invalid shallow line: %s"), line);
340 register_shallow(the_repository, &oid);
343 if (skip_prefix(line, "unshallow ", &arg)) {
344 if (get_oid_hex(arg, &oid))
345 die(_("invalid unshallow line: %s"), line);
346 if (!lookup_object(the_repository, oid.hash))
347 die(_("object not found: %s"), line);
348 /* make sure that it is parsed as shallow */
349 if (!parse_object(the_repository, &oid))
350 die(_("error in object: %s"), line);
351 if (unregister_shallow(&oid))
352 die(_("no shallow found: %s"), line);
355 die(_("expected shallow/unshallow, got %s"), line);
357 } else if (!args->stateless_rpc)
358 send_request(args, fd[1], &req_buf);
360 if (!args->stateless_rpc) {
361 /* If we aren't using the stateless-rpc interface
362 * we don't need to retain the headers.
364 strbuf_setlen(&req_buf, 0);
370 if (args->no_dependents)
372 while ((oid = negotiator->next(negotiator))) {
373 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
374 print_verbose(args, "have %s", oid_to_hex(oid));
376 if (flush_at <= ++count) {
379 packet_buf_flush(&req_buf);
380 send_request(args, fd[1], &req_buf);
381 strbuf_setlen(&req_buf, state_len);
383 flush_at = next_flush(args->stateless_rpc, count);
386 * We keep one window "ahead" of the other side, and
387 * will wait for an ACK only on the next one
389 if (!args->stateless_rpc && count == INITIAL_FLUSH)
392 consume_shallow_list(args, fd[0]);
394 ack = get_ack(fd[0], result_oid);
396 print_verbose(args, _("got %s %d %s"), "ack",
397 ack, oid_to_hex(result_oid));
407 struct commit *commit =
408 lookup_commit(the_repository,
413 die(_("invalid commit %s"), oid_to_hex(result_oid));
414 was_common = negotiator->ack(negotiator, commit);
415 if (args->stateless_rpc
418 /* We need to replay the have for this object
419 * on the next RPC request so the peer knows
420 * it is in common with us.
422 const char *hex = oid_to_hex(result_oid);
423 packet_buf_write(&req_buf, "have %s\n", hex);
424 state_len = req_buf.len;
426 * Reset in_vain because an ack
427 * for this commit has not been
431 } else if (!args->stateless_rpc
432 || ack != ACK_common)
436 if (ack == ACK_ready)
443 if (got_continue && MAX_IN_VAIN < in_vain) {
444 print_verbose(args, _("giving up"));
452 if (!got_ready || !no_done) {
453 packet_buf_write(&req_buf, "done\n");
454 send_request(args, fd[1], &req_buf);
456 print_verbose(args, _("done"));
461 strbuf_release(&req_buf);
463 if (!got_ready || !no_done)
464 consume_shallow_list(args, fd[0]);
465 while (flushes || multi_ack) {
466 int ack = get_ack(fd[0], result_oid);
468 print_verbose(args, _("got %s (%d) %s"), "ack",
469 ack, oid_to_hex(result_oid));
477 /* it is no error to fetch into a completely empty repo */
478 return count ? retval : 0;
481 static struct commit_list *complete;
483 static int mark_complete(const struct object_id *oid)
485 struct object *o = parse_object(the_repository, oid);
487 while (o && o->type == OBJ_TAG) {
488 struct tag *t = (struct tag *) o;
490 break; /* broken repository */
491 o->flags |= COMPLETE;
492 o = parse_object(the_repository, &t->tagged->oid);
494 if (o && o->type == OBJ_COMMIT) {
495 struct commit *commit = (struct commit *)o;
496 if (!(commit->object.flags & COMPLETE)) {
497 commit->object.flags |= COMPLETE;
498 commit_list_insert(commit, &complete);
504 static int mark_complete_oid(const char *refname, const struct object_id *oid,
505 int flag, void *cb_data)
507 return mark_complete(oid);
510 static void mark_recent_complete_commits(struct fetch_pack_args *args,
513 while (complete && cutoff <= complete->item->date) {
514 print_verbose(args, _("Marking %s as complete"),
515 oid_to_hex(&complete->item->object.oid));
516 pop_most_recent_commit(&complete, COMPLETE);
520 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
522 for (; refs; refs = refs->next)
523 oidset_insert(oids, &refs->old_oid);
526 static int tip_oids_contain(struct oidset *tip_oids,
527 struct ref *unmatched, struct ref *newlist,
528 const struct object_id *id)
531 * Note that this only looks at the ref lists the first time it's
532 * called. This works out in filter_refs() because even though it may
533 * add to "newlist" between calls, the additions will always be for
534 * oids that are already in the set.
536 if (!tip_oids->map.map.tablesize) {
537 add_refs_to_oidset(tip_oids, unmatched);
538 add_refs_to_oidset(tip_oids, newlist);
540 return oidset_contains(tip_oids, id);
543 static void filter_refs(struct fetch_pack_args *args,
545 struct ref **sought, int nr_sought)
547 struct ref *newlist = NULL;
548 struct ref **newtail = &newlist;
549 struct ref *unmatched = NULL;
550 struct ref *ref, *next;
551 struct oidset tip_oids = OIDSET_INIT;
555 for (ref = *refs; ref; ref = next) {
559 if (starts_with(ref->name, "refs/") &&
560 check_refname_format(ref->name, 0))
563 while (i < nr_sought) {
564 int cmp = strcmp(ref->name, sought[i]->name);
566 break; /* definitely do not have it */
568 keep = 1; /* definitely have it */
569 sought[i]->match_status = REF_MATCHED;
574 if (!keep && args->fetch_all &&
575 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
582 newtail = &ref->next;
584 ref->next = unmatched;
589 /* Append unmatched requests to the list */
590 for (i = 0; i < nr_sought; i++) {
591 struct object_id oid;
595 if (ref->match_status != REF_NOT_MATCHED)
597 if (parse_oid_hex(ref->name, &oid, &p) ||
599 oidcmp(&oid, &ref->old_oid))
602 if ((allow_unadvertised_object_request &
603 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
604 tip_oids_contain(&tip_oids, unmatched, newlist,
606 ref->match_status = REF_MATCHED;
607 *newtail = copy_ref(ref);
608 newtail = &(*newtail)->next;
610 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
614 oidset_clear(&tip_oids);
615 for (ref = unmatched; ref; ref = next) {
623 static void mark_alternate_complete(struct fetch_negotiator *unused,
626 mark_complete(&obj->oid);
629 struct loose_object_iter {
630 struct oidset *loose_object_set;
635 * If the number of refs is not larger than the number of loose objects,
636 * this function stops inserting.
638 static int add_loose_objects_to_set(const struct object_id *oid,
642 struct loose_object_iter *iter = data;
643 oidset_insert(iter->loose_object_set, oid);
644 if (iter->refs == NULL)
647 iter->refs = iter->refs->next;
652 * Mark recent commits available locally and reachable from a local ref as
653 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
654 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
655 * thus do not need COMMON_REF marks).
657 * The cutoff time for recency is determined by this heuristic: it is the
658 * earliest commit time of the objects in refs that are commits and that we know
659 * the commit time of.
661 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
662 struct fetch_pack_args *args,
666 int old_save_commit_buffer = save_commit_buffer;
667 timestamp_t cutoff = 0;
668 struct oidset loose_oid_set = OIDSET_INIT;
670 struct loose_object_iter iter = {&loose_oid_set, *refs};
672 /* Enumerate all loose objects or know refs are not so many. */
673 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
676 save_commit_buffer = 0;
678 for (ref = *refs; ref; ref = ref->next) {
680 unsigned int flags = OBJECT_INFO_QUICK;
683 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
685 * I know this does not exist in the loose form,
686 * so check if it exists in a non-loose form.
688 flags |= OBJECT_INFO_IGNORE_LOOSE;
691 if (!has_object_file_with_flags(&ref->old_oid, flags))
693 o = parse_object(the_repository, &ref->old_oid);
697 /* We already have it -- which may mean that we were
698 * in sync with the other side at some time after
699 * that (it is OK if we guess wrong here).
701 if (o->type == OBJ_COMMIT) {
702 struct commit *commit = (struct commit *)o;
703 if (!cutoff || cutoff < commit->date)
704 cutoff = commit->date;
708 oidset_clear(&loose_oid_set);
710 if (!args->no_dependents) {
712 for_each_ref(mark_complete_oid, NULL);
713 for_each_cached_alternate(NULL, mark_alternate_complete);
714 commit_list_sort_by_date(&complete);
716 mark_recent_complete_commits(args, cutoff);
720 * Mark all complete remote refs as common refs.
721 * Don't mark them common yet; the server has to be told so first.
723 for (ref = *refs; ref; ref = ref->next) {
724 struct object *o = deref_tag(the_repository,
725 lookup_object(the_repository,
729 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
732 negotiator->known_common(negotiator,
737 save_commit_buffer = old_save_commit_buffer;
741 * Returns 1 if every object pointed to by the given remote refs is available
742 * locally and reachable from a local ref, and 0 otherwise.
744 static int everything_local(struct fetch_pack_args *args,
750 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
751 const struct object_id *remote = &ref->old_oid;
754 o = lookup_object(the_repository, remote->hash);
755 if (!o || !(o->flags & COMPLETE)) {
757 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
761 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
768 static int sideband_demux(int in, int out, void *data)
773 ret = recv_sideband("fetch-pack", xd[0], out);
778 static int get_pack(struct fetch_pack_args *args,
779 int xd[2], char **pack_lockfile)
782 int do_keep = args->keep_pack;
783 const char *cmd_name;
784 struct pack_header header;
786 struct child_process cmd = CHILD_PROCESS_INIT;
789 memset(&demux, 0, sizeof(demux));
791 /* xd[] is talking with upload-pack; subprocess reads from
792 * xd[0], spits out band#2 to stderr, and feeds us band#1
793 * through demux->out.
795 demux.proc = sideband_demux;
798 demux.isolate_sigpipe = 1;
799 if (start_async(&demux))
800 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
805 if (!args->keep_pack && unpack_limit) {
807 if (read_pack_header(demux.out, &header))
808 die(_("protocol error: bad pack header"));
810 if (ntohl(header.hdr_entries) < unpack_limit)
816 if (alternate_shallow_file) {
817 argv_array_push(&cmd.args, "--shallow-file");
818 argv_array_push(&cmd.args, alternate_shallow_file);
821 if (do_keep || args->from_promisor) {
824 cmd_name = "index-pack";
825 argv_array_push(&cmd.args, cmd_name);
826 argv_array_push(&cmd.args, "--stdin");
827 if (!args->quiet && !args->no_progress)
828 argv_array_push(&cmd.args, "-v");
829 if (args->use_thin_pack)
830 argv_array_push(&cmd.args, "--fix-thin");
831 if (do_keep && (args->lock_pack || unpack_limit)) {
832 char hostname[HOST_NAME_MAX + 1];
833 if (xgethostname(hostname, sizeof(hostname)))
834 xsnprintf(hostname, sizeof(hostname), "localhost");
835 argv_array_pushf(&cmd.args,
836 "--keep=fetch-pack %"PRIuMAX " on %s",
837 (uintmax_t)getpid(), hostname);
839 if (args->check_self_contained_and_connected)
840 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
841 if (args->from_promisor)
842 argv_array_push(&cmd.args, "--promisor");
845 cmd_name = "unpack-objects";
846 argv_array_push(&cmd.args, cmd_name);
847 if (args->quiet || args->no_progress)
848 argv_array_push(&cmd.args, "-q");
849 args->check_self_contained_and_connected = 0;
853 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
854 ntohl(header.hdr_version),
855 ntohl(header.hdr_entries));
856 if (fetch_fsck_objects >= 0
858 : transfer_fsck_objects >= 0
859 ? transfer_fsck_objects
861 if (args->from_promisor)
863 * We cannot use --strict in index-pack because it
864 * checks both broken objects and links, but we only
865 * want to check for broken objects.
867 argv_array_push(&cmd.args, "--fsck-objects");
869 argv_array_push(&cmd.args, "--strict");
874 if (start_command(&cmd))
875 die(_("fetch-pack: unable to fork off %s"), cmd_name);
876 if (do_keep && pack_lockfile) {
877 *pack_lockfile = index_pack_lockfile(cmd.out);
882 /* Closed by start_command() */
885 ret = finish_command(&cmd);
886 if (!ret || (args->check_self_contained_and_connected && ret == 1))
887 args->self_contained_and_connected =
888 args->check_self_contained_and_connected &&
891 die(_("%s failed"), cmd_name);
892 if (use_sideband && finish_async(&demux))
893 die(_("error in sideband demultiplexer"));
897 static int cmp_ref_by_name(const void *a_, const void *b_)
899 const struct ref *a = *((const struct ref **)a_);
900 const struct ref *b = *((const struct ref **)b_);
901 return strcmp(a->name, b->name);
904 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
906 const struct ref *orig_ref,
907 struct ref **sought, int nr_sought,
908 struct shallow_info *si,
909 char **pack_lockfile)
911 struct ref *ref = copy_ref_list(orig_ref);
912 struct object_id oid;
913 const char *agent_feature;
915 struct fetch_negotiator negotiator;
916 fetch_negotiator_init(&negotiator);
918 sort_ref_list(&ref, ref_compare_name);
919 QSORT(sought, nr_sought, cmp_ref_by_name);
921 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
922 die(_("Server does not support shallow clients"));
923 if (args->depth > 0 || args->deepen_since || args->deepen_not)
925 if (server_supports("multi_ack_detailed")) {
926 print_verbose(args, _("Server supports multi_ack_detailed"));
928 if (server_supports("no-done")) {
929 print_verbose(args, _("Server supports no-done"));
930 if (args->stateless_rpc)
934 else if (server_supports("multi_ack")) {
935 print_verbose(args, _("Server supports multi_ack"));
938 if (server_supports("side-band-64k")) {
939 print_verbose(args, _("Server supports side-band-64k"));
942 else if (server_supports("side-band")) {
943 print_verbose(args, _("Server supports side-band"));
946 if (server_supports("allow-tip-sha1-in-want")) {
947 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
948 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
950 if (server_supports("allow-reachable-sha1-in-want")) {
951 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
952 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
954 if (!server_supports("thin-pack"))
955 args->use_thin_pack = 0;
956 if (!server_supports("no-progress"))
957 args->no_progress = 0;
958 if (!server_supports("include-tag"))
959 args->include_tag = 0;
960 if (server_supports("ofs-delta"))
961 print_verbose(args, _("Server supports ofs-delta"));
963 prefer_ofs_delta = 0;
965 if (server_supports("filter")) {
966 server_supports_filtering = 1;
967 print_verbose(args, _("Server supports filter"));
968 } else if (args->filter_options.choice) {
969 warning("filtering not recognized by server, ignoring");
972 if ((agent_feature = server_feature_value("agent", &agent_len))) {
975 print_verbose(args, _("Server version is %.*s"),
976 agent_len, agent_feature);
978 if (server_supports("deepen-since"))
980 else if (args->deepen_since)
981 die(_("Server does not support --shallow-since"));
982 if (server_supports("deepen-not"))
984 else if (args->deepen_not)
985 die(_("Server does not support --shallow-exclude"));
986 if (!server_supports("deepen-relative") && args->deepen_relative)
987 die(_("Server does not support --deepen"));
989 mark_complete_and_common_ref(&negotiator, args, &ref);
990 filter_refs(args, &ref, sought, nr_sought);
991 if (everything_local(args, &ref)) {
995 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
996 if (!args->keep_pack)
997 /* When cloning, it is not unusual to have
1000 warning(_("no common commits"));
1002 if (args->stateless_rpc)
1003 packet_flush(fd[1]);
1005 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1007 else if (si->nr_ours || si->nr_theirs)
1008 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1010 alternate_shallow_file = NULL;
1011 if (get_pack(args, fd, pack_lockfile))
1012 die(_("git fetch-pack: fetch failed."));
1015 negotiator.release(&negotiator);
1019 static void add_shallow_requests(struct strbuf *req_buf,
1020 const struct fetch_pack_args *args)
1022 if (is_repository_shallow(the_repository))
1023 write_shallow_commits(req_buf, 1, NULL);
1024 if (args->depth > 0)
1025 packet_buf_write(req_buf, "deepen %d", args->depth);
1026 if (args->deepen_since) {
1027 timestamp_t max_age = approxidate(args->deepen_since);
1028 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1030 if (args->deepen_not) {
1032 for (i = 0; i < args->deepen_not->nr; i++) {
1033 struct string_list_item *s = args->deepen_not->items + i;
1034 packet_buf_write(req_buf, "deepen-not %s", s->string);
1039 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1041 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1043 for ( ; wants ; wants = wants->next) {
1044 const struct object_id *remote = &wants->old_oid;
1048 * If that object is complete (i.e. it is an ancestor of a
1049 * local ref), we tell them we have it but do not have to
1050 * tell them about its ancestors, which they already know
1053 * We use lookup_object here because we are only
1054 * interested in the case we *know* the object is
1055 * reachable and we have already scanned it.
1057 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1058 (o->flags & COMPLETE)) {
1062 if (!use_ref_in_want || wants->exact_oid)
1063 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1065 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1069 static void add_common(struct strbuf *req_buf, struct oidset *common)
1071 struct oidset_iter iter;
1072 const struct object_id *oid;
1073 oidset_iter_init(common, &iter);
1075 while ((oid = oidset_iter_next(&iter))) {
1076 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1080 static int add_haves(struct fetch_negotiator *negotiator,
1081 struct strbuf *req_buf,
1082 int *haves_to_send, int *in_vain)
1085 int haves_added = 0;
1086 const struct object_id *oid;
1088 while ((oid = negotiator->next(negotiator))) {
1089 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1090 if (++haves_added >= *haves_to_send)
1094 *in_vain += haves_added;
1095 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1097 packet_buf_write(req_buf, "done\n");
1101 /* Increase haves to send on next round */
1102 *haves_to_send = next_flush(1, *haves_to_send);
1107 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1108 const struct fetch_pack_args *args,
1109 const struct ref *wants, struct oidset *common,
1110 int *haves_to_send, int *in_vain)
1113 struct strbuf req_buf = STRBUF_INIT;
1115 if (server_supports_v2("fetch", 1))
1116 packet_buf_write(&req_buf, "command=fetch");
1117 if (server_supports_v2("agent", 0))
1118 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1119 if (args->server_options && args->server_options->nr &&
1120 server_supports_v2("server-option", 1)) {
1122 for (i = 0; i < args->server_options->nr; i++)
1123 packet_write_fmt(fd_out, "server-option=%s",
1124 args->server_options->items[i].string);
1127 packet_buf_delim(&req_buf);
1128 if (args->use_thin_pack)
1129 packet_buf_write(&req_buf, "thin-pack");
1130 if (args->no_progress)
1131 packet_buf_write(&req_buf, "no-progress");
1132 if (args->include_tag)
1133 packet_buf_write(&req_buf, "include-tag");
1134 if (prefer_ofs_delta)
1135 packet_buf_write(&req_buf, "ofs-delta");
1137 /* Add shallow-info and deepen request */
1138 if (server_supports_feature("fetch", "shallow", 0))
1139 add_shallow_requests(&req_buf, args);
1140 else if (is_repository_shallow(the_repository) || args->deepen)
1141 die(_("Server does not support shallow requests"));
1144 if (server_supports_feature("fetch", "filter", 0) &&
1145 args->filter_options.choice) {
1146 print_verbose(args, _("Server supports filter"));
1147 packet_buf_write(&req_buf, "filter %s",
1148 args->filter_options.filter_spec);
1149 } else if (args->filter_options.choice) {
1150 warning("filtering not recognized by server, ignoring");
1154 add_wants(wants, &req_buf);
1156 if (args->no_dependents) {
1157 packet_buf_write(&req_buf, "done");
1160 /* Add all of the common commits we've found in previous rounds */
1161 add_common(&req_buf, common);
1163 /* Add initial haves */
1164 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1168 packet_buf_flush(&req_buf);
1169 write_or_die(fd_out, req_buf.buf, req_buf.len);
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);
1239 /* return 0 if no common, 1 if there are common, or 2 if ready */
1240 return received_ready ? 2 : (received_ack ? 1 : 0);
1243 static void receive_shallow_info(struct fetch_pack_args *args,
1244 struct packet_reader *reader)
1246 process_section_header(reader, "shallow-info", 0);
1247 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1249 struct object_id oid;
1251 if (skip_prefix(reader->line, "shallow ", &arg)) {
1252 if (get_oid_hex(arg, &oid))
1253 die(_("invalid shallow line: %s"), reader->line);
1254 register_shallow(the_repository, &oid);
1257 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1258 if (get_oid_hex(arg, &oid))
1259 die(_("invalid unshallow line: %s"), reader->line);
1260 if (!lookup_object(the_repository, oid.hash))
1261 die(_("object not found: %s"), reader->line);
1262 /* make sure that it is parsed as shallow */
1263 if (!parse_object(the_repository, &oid))
1264 die(_("error in object: %s"), reader->line);
1265 if (unregister_shallow(&oid))
1266 die(_("no shallow found: %s"), reader->line);
1269 die(_("expected shallow/unshallow, got %s"), reader->line);
1272 if (reader->status != PACKET_READ_FLUSH &&
1273 reader->status != PACKET_READ_DELIM)
1274 die("error processing shallow info: %d", reader->status);
1276 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1280 static void receive_wanted_refs(struct packet_reader *reader, struct ref *refs)
1282 process_section_header(reader, "wanted-refs", 0);
1283 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1284 struct object_id oid;
1286 struct ref *r = NULL;
1288 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1289 die("expected wanted-ref, got '%s'", reader->line);
1291 for (r = refs; r; r = r->next) {
1292 if (!strcmp(end, r->name)) {
1293 oidcpy(&r->old_oid, &oid);
1299 die("unexpected wanted-ref: '%s'", reader->line);
1302 if (reader->status != PACKET_READ_DELIM)
1303 die("error processing wanted refs: %d", reader->status);
1307 FETCH_CHECK_LOCAL = 0,
1314 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1316 const struct ref *orig_ref,
1317 struct ref **sought, int nr_sought,
1318 char **pack_lockfile)
1320 struct ref *ref = copy_ref_list(orig_ref);
1321 enum fetch_state state = FETCH_CHECK_LOCAL;
1322 struct oidset common = OIDSET_INIT;
1323 struct packet_reader reader;
1325 int haves_to_send = INITIAL_FLUSH;
1326 struct fetch_negotiator negotiator;
1327 fetch_negotiator_init(&negotiator);
1328 packet_reader_init(&reader, fd[0], NULL, 0,
1329 PACKET_READ_CHOMP_NEWLINE);
1331 while (state != FETCH_DONE) {
1333 case FETCH_CHECK_LOCAL:
1334 sort_ref_list(&ref, ref_compare_name);
1335 QSORT(sought, nr_sought, cmp_ref_by_name);
1337 /* v2 supports these by default */
1338 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1340 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1343 /* Filter 'ref' by 'sought' and those that aren't local */
1344 mark_complete_and_common_ref(&negotiator, args, &ref);
1345 filter_refs(args, &ref, sought, nr_sought);
1346 if (everything_local(args, &ref))
1349 state = FETCH_SEND_REQUEST;
1351 mark_tips(&negotiator, args->negotiation_tips);
1352 for_each_cached_alternate(&negotiator,
1353 insert_one_alternate_object);
1355 case FETCH_SEND_REQUEST:
1356 if (send_fetch_request(&negotiator, fd[1], args, ref,
1358 &haves_to_send, &in_vain))
1359 state = FETCH_GET_PACK;
1361 state = FETCH_PROCESS_ACKS;
1363 case FETCH_PROCESS_ACKS:
1364 /* Process ACKs/NAKs */
1365 switch (process_acks(&negotiator, &reader, &common)) {
1367 state = FETCH_GET_PACK;
1373 state = FETCH_SEND_REQUEST;
1377 case FETCH_GET_PACK:
1378 /* Check for shallow-info section */
1379 if (process_section_header(&reader, "shallow-info", 1))
1380 receive_shallow_info(args, &reader);
1382 if (process_section_header(&reader, "wanted-refs", 1))
1383 receive_wanted_refs(&reader, ref);
1386 process_section_header(&reader, "packfile", 0);
1387 if (get_pack(args, fd, pack_lockfile))
1388 die(_("git fetch-pack: fetch failed."));
1397 negotiator.release(&negotiator);
1398 oidset_clear(&common);
1402 static void fetch_pack_config(void)
1404 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1405 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1406 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1407 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1408 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1410 git_config(git_default_config, NULL);
1413 static void fetch_pack_setup(void)
1415 static int did_setup;
1418 fetch_pack_config();
1419 if (0 <= transfer_unpack_limit)
1420 unpack_limit = transfer_unpack_limit;
1421 else if (0 <= fetch_unpack_limit)
1422 unpack_limit = fetch_unpack_limit;
1426 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1428 struct string_list names = STRING_LIST_INIT_NODUP;
1431 for (src = dst = 0; src < nr; src++) {
1432 struct string_list_item *item;
1433 item = string_list_insert(&names, ref[src]->name);
1435 continue; /* already have it */
1436 item->util = ref[src];
1438 ref[dst] = ref[src];
1441 for (src = dst; src < nr; src++)
1443 string_list_clear(&names, 0);
1447 static void update_shallow(struct fetch_pack_args *args,
1449 struct shallow_info *si)
1451 struct oid_array ref = OID_ARRAY_INIT;
1456 if (args->deepen && alternate_shallow_file) {
1457 if (*alternate_shallow_file == '\0') { /* --unshallow */
1458 unlink_or_warn(git_path_shallow(the_repository));
1459 rollback_lock_file(&shallow_lock);
1461 commit_lock_file(&shallow_lock);
1465 if (!si->shallow || !si->shallow->nr)
1468 if (args->cloning) {
1470 * remote is shallow, but this is a clone, there are
1471 * no objects in repo to worry about. Accept any
1472 * shallow points that exist in the pack (iow in repo
1473 * after get_pack() and reprepare_packed_git())
1475 struct oid_array extra = OID_ARRAY_INIT;
1476 struct object_id *oid = si->shallow->oid;
1477 for (i = 0; i < si->shallow->nr; i++)
1478 if (has_object_file(&oid[i]))
1479 oid_array_append(&extra, &oid[i]);
1481 setup_alternate_shallow(&shallow_lock,
1482 &alternate_shallow_file,
1484 commit_lock_file(&shallow_lock);
1486 oid_array_clear(&extra);
1490 if (!si->nr_ours && !si->nr_theirs)
1493 remove_nonexistent_theirs_shallow(si);
1494 if (!si->nr_ours && !si->nr_theirs)
1496 for (r = refs; r; r = r->next)
1497 oid_array_append(&ref, &r->old_oid);
1500 if (args->update_shallow) {
1502 * remote is also shallow, .git/shallow may be updated
1503 * so all refs can be accepted. Make sure we only add
1504 * shallow roots that are actually reachable from new
1507 struct oid_array extra = OID_ARRAY_INIT;
1508 struct object_id *oid = si->shallow->oid;
1509 assign_shallow_commits_to_refs(si, NULL, NULL);
1510 if (!si->nr_ours && !si->nr_theirs) {
1511 oid_array_clear(&ref);
1514 for (i = 0; i < si->nr_ours; i++)
1515 oid_array_append(&extra, &oid[si->ours[i]]);
1516 for (i = 0; i < si->nr_theirs; i++)
1517 oid_array_append(&extra, &oid[si->theirs[i]]);
1518 setup_alternate_shallow(&shallow_lock,
1519 &alternate_shallow_file,
1521 commit_lock_file(&shallow_lock);
1522 oid_array_clear(&extra);
1523 oid_array_clear(&ref);
1528 * remote is also shallow, check what ref is safe to update
1529 * without updating .git/shallow
1531 status = xcalloc(ref.nr, sizeof(*status));
1532 assign_shallow_commits_to_refs(si, NULL, status);
1533 if (si->nr_ours || si->nr_theirs) {
1534 for (r = refs, i = 0; r; r = r->next, i++)
1536 r->status = REF_STATUS_REJECT_SHALLOW;
1539 oid_array_clear(&ref);
1542 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1544 struct ref **rm = cb_data;
1545 struct ref *ref = *rm;
1548 return -1; /* end of the list */
1550 oidcpy(oid, &ref->old_oid);
1554 struct ref *fetch_pack(struct fetch_pack_args *args,
1555 int fd[], struct child_process *conn,
1556 const struct ref *ref,
1558 struct ref **sought, int nr_sought,
1559 struct oid_array *shallow,
1560 char **pack_lockfile,
1561 enum protocol_version version)
1563 struct ref *ref_cpy;
1564 struct shallow_info si;
1568 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1571 packet_flush(fd[1]);
1572 die(_("no matching remote head"));
1574 prepare_shallow_info(&si, shallow);
1575 if (version == protocol_v2)
1576 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1579 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1580 &si, pack_lockfile);
1581 reprepare_packed_git(the_repository);
1583 if (!args->cloning && args->deepen) {
1584 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1585 struct ref *iterator = ref_cpy;
1586 opt.shallow_file = alternate_shallow_file;
1588 opt.is_deepening_fetch = 1;
1589 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1590 error(_("remote did not send all necessary objects"));
1593 rollback_lock_file(&shallow_lock);
1596 args->connectivity_checked = 1;
1599 update_shallow(args, ref_cpy, &si);
1601 clear_shallow_info(&si);
1605 int report_unmatched_refs(struct ref **sought, int nr_sought)
1609 for (i = 0; i < nr_sought; i++) {
1612 switch (sought[i]->match_status) {
1615 case REF_NOT_MATCHED:
1616 error(_("no such remote ref %s"), sought[i]->name);
1618 case REF_UNADVERTISED_NOT_ALLOWED:
1619 error(_("Server does not allow request for unadvertised object %s"),