2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "sha1-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
26 static int transfer_unpack_limit = -1;
27 static int fetch_unpack_limit = -1;
28 static int unpack_limit = 100;
29 static int prefer_ofs_delta = 1;
31 static int deepen_since_ok;
32 static int deepen_not_ok;
33 static int fetch_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int agent_supported;
36 static int server_supports_filtering;
37 static struct lock_file shallow_lock;
38 static const char *alternate_shallow_file;
39 static char *negotiation_algorithm;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
42 /* Remember to update object flag allocation in object.h */
43 #define COMPLETE (1U << 0)
44 #define ALTERNATE (1U << 1)
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
50 #define MAX_IN_VAIN 256
52 static int multi_ack, use_sideband;
53 /* Allow specifying sha1 if it is a ref tip. */
54 #define ALLOW_TIP_SHA1 01
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 #define ALLOW_REACHABLE_SHA1 02
57 static unsigned int allow_unadvertised_object_request;
59 __attribute__((format (printf, 2, 3)))
60 static inline void print_verbose(const struct fetch_pack_args *args,
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
74 struct alternate_object_cache {
75 struct object **items;
79 static void cache_one_alternate(const struct object_id *oid,
82 struct alternate_object_cache *cache = vcache;
83 struct object *obj = parse_object(the_repository, oid);
85 if (!obj || (obj->flags & ALTERNATE))
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
93 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
97 static int initialized;
98 static struct alternate_object_cache cache;
102 for_each_alternate_ref(cache_one_alternate, &cache);
106 for (i = 0; i < cache.nr; i++)
107 cb(negotiator, cache.items[i]);
110 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
112 const struct object_id *oid)
114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
118 if (o && o->type == OBJ_COMMIT)
119 negotiator->add_tip(negotiator, (struct commit *)o);
124 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
127 return rev_list_insert_ref(cb_data, refname, oid);
138 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
140 if (args->stateless_rpc && args->deepen) {
141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
146 while ((line = packet_read_line(fd, NULL))) {
147 if (starts_with(line, "shallow "))
149 if (starts_with(line, "unshallow "))
151 die(_("git fetch-pack: expected shallow list"));
156 static enum ack_type get_ack(int fd, struct object_id *result_oid)
159 char *line = packet_read_line(fd, &len);
163 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
164 if (!strcmp(line, "NAK"))
166 if (skip_prefix(line, "ACK ", &arg)) {
167 if (!get_oid_hex(arg, result_oid)) {
172 if (strstr(arg, "continue"))
174 if (strstr(arg, "common"))
176 if (strstr(arg, "ready"))
181 if (skip_prefix(line, "ERR ", &arg))
182 die(_("remote error: %s"), arg);
183 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
186 static void send_request(struct fetch_pack_args *args,
187 int fd, struct strbuf *buf)
189 if (args->stateless_rpc) {
190 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
193 write_or_die(fd, buf->buf, buf->len);
196 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
199 rev_list_insert_ref(negotiator, NULL, &obj->oid);
202 #define INITIAL_FLUSH 16
203 #define PIPESAFE_FLUSH 32
204 #define LARGE_FLUSH 16384
206 static int next_flush(int stateless_rpc, int count)
209 if (count < LARGE_FLUSH)
212 count = count * 11 / 10;
214 if (count < PIPESAFE_FLUSH)
217 count += PIPESAFE_FLUSH;
222 static void mark_tips(struct fetch_negotiator *negotiator,
223 const struct oid_array *negotiation_tips)
227 if (!negotiation_tips) {
228 for_each_ref(rev_list_insert_ref_oid, negotiator);
232 for (i = 0; i < negotiation_tips->nr; i++)
233 rev_list_insert_ref(negotiator, NULL,
234 &negotiation_tips->oid[i]);
238 static int find_common(struct fetch_negotiator *negotiator,
239 struct fetch_pack_args *args,
240 int fd[2], struct object_id *result_oid,
244 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
245 const struct object_id *oid;
246 unsigned in_vain = 0;
247 int got_continue = 0;
249 struct strbuf req_buf = STRBUF_INIT;
250 size_t state_len = 0;
252 if (args->stateless_rpc && multi_ack == 1)
253 die(_("--stateless-rpc requires multi_ack_detailed"));
255 if (!args->no_dependents) {
256 mark_tips(negotiator, args->negotiation_tips);
257 for_each_cached_alternate(negotiator, insert_one_alternate_object);
261 for ( ; refs ; refs = refs->next) {
262 struct object_id *remote = &refs->old_oid;
263 const char *remote_hex;
267 * If that object is complete (i.e. it is an ancestor of a
268 * local ref), we tell them we have it but do not have to
269 * tell them about its ancestors, which they already know
272 * We use lookup_object here because we are only
273 * interested in the case we *know* the object is
274 * reachable and we have already scanned it.
276 * Do this only if args->no_dependents is false (if it is true,
277 * we cannot trust the object flags).
279 if (!args->no_dependents &&
280 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
281 (o->flags & COMPLETE)) {
285 remote_hex = oid_to_hex(remote);
287 struct strbuf c = STRBUF_INIT;
288 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
289 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
290 if (no_done) strbuf_addstr(&c, " no-done");
291 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
292 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
293 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
294 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
295 if (args->no_progress) strbuf_addstr(&c, " no-progress");
296 if (args->include_tag) strbuf_addstr(&c, " include-tag");
297 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
298 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
299 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
300 if (agent_supported) strbuf_addf(&c, " agent=%s",
301 git_user_agent_sanitized());
302 if (args->filter_options.choice)
303 strbuf_addstr(&c, " filter");
304 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
307 packet_buf_write(&req_buf, "want %s\n", remote_hex);
312 strbuf_release(&req_buf);
317 if (is_repository_shallow(the_repository))
318 write_shallow_commits(&req_buf, 1, NULL);
320 packet_buf_write(&req_buf, "deepen %d", args->depth);
321 if (args->deepen_since) {
322 timestamp_t max_age = approxidate(args->deepen_since);
323 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
325 if (args->deepen_not) {
327 for (i = 0; i < args->deepen_not->nr; i++) {
328 struct string_list_item *s = args->deepen_not->items + i;
329 packet_buf_write(&req_buf, "deepen-not %s", s->string);
332 if (server_supports_filtering && args->filter_options.choice) {
333 struct strbuf expanded_filter_spec = STRBUF_INIT;
334 expand_list_objects_filter_spec(&args->filter_options,
335 &expanded_filter_spec);
336 packet_buf_write(&req_buf, "filter %s",
337 expanded_filter_spec.buf);
338 strbuf_release(&expanded_filter_spec);
340 packet_buf_flush(&req_buf);
341 state_len = req_buf.len;
346 struct object_id oid;
348 send_request(args, fd[1], &req_buf);
349 while ((line = packet_read_line(fd[0], NULL))) {
350 if (skip_prefix(line, "shallow ", &arg)) {
351 if (get_oid_hex(arg, &oid))
352 die(_("invalid shallow line: %s"), line);
353 register_shallow(the_repository, &oid);
356 if (skip_prefix(line, "unshallow ", &arg)) {
357 if (get_oid_hex(arg, &oid))
358 die(_("invalid unshallow line: %s"), line);
359 if (!lookup_object(the_repository, oid.hash))
360 die(_("object not found: %s"), line);
361 /* make sure that it is parsed as shallow */
362 if (!parse_object(the_repository, &oid))
363 die(_("error in object: %s"), line);
364 if (unregister_shallow(&oid))
365 die(_("no shallow found: %s"), line);
368 die(_("expected shallow/unshallow, got %s"), line);
370 } else if (!args->stateless_rpc)
371 send_request(args, fd[1], &req_buf);
373 if (!args->stateless_rpc) {
374 /* If we aren't using the stateless-rpc interface
375 * we don't need to retain the headers.
377 strbuf_setlen(&req_buf, 0);
383 if (args->no_dependents)
385 while ((oid = negotiator->next(negotiator))) {
386 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
387 print_verbose(args, "have %s", oid_to_hex(oid));
389 if (flush_at <= ++count) {
392 packet_buf_flush(&req_buf);
393 send_request(args, fd[1], &req_buf);
394 strbuf_setlen(&req_buf, state_len);
396 flush_at = next_flush(args->stateless_rpc, count);
399 * We keep one window "ahead" of the other side, and
400 * will wait for an ACK only on the next one
402 if (!args->stateless_rpc && count == INITIAL_FLUSH)
405 consume_shallow_list(args, fd[0]);
407 ack = get_ack(fd[0], result_oid);
409 print_verbose(args, _("got %s %d %s"), "ack",
410 ack, oid_to_hex(result_oid));
420 struct commit *commit =
421 lookup_commit(the_repository,
426 die(_("invalid commit %s"), oid_to_hex(result_oid));
427 was_common = negotiator->ack(negotiator, commit);
428 if (args->stateless_rpc
431 /* We need to replay the have for this object
432 * on the next RPC request so the peer knows
433 * it is in common with us.
435 const char *hex = oid_to_hex(result_oid);
436 packet_buf_write(&req_buf, "have %s\n", hex);
437 state_len = req_buf.len;
439 * Reset in_vain because an ack
440 * for this commit has not been
444 } else if (!args->stateless_rpc
445 || ack != ACK_common)
449 if (ack == ACK_ready)
456 if (got_continue && MAX_IN_VAIN < in_vain) {
457 print_verbose(args, _("giving up"));
465 if (!got_ready || !no_done) {
466 packet_buf_write(&req_buf, "done\n");
467 send_request(args, fd[1], &req_buf);
469 print_verbose(args, _("done"));
474 strbuf_release(&req_buf);
476 if (!got_ready || !no_done)
477 consume_shallow_list(args, fd[0]);
478 while (flushes || multi_ack) {
479 int ack = get_ack(fd[0], result_oid);
481 print_verbose(args, _("got %s (%d) %s"), "ack",
482 ack, oid_to_hex(result_oid));
490 /* it is no error to fetch into a completely empty repo */
491 return count ? retval : 0;
494 static struct commit_list *complete;
496 static int mark_complete(const struct object_id *oid)
498 struct object *o = parse_object(the_repository, oid);
500 while (o && o->type == OBJ_TAG) {
501 struct tag *t = (struct tag *) o;
503 break; /* broken repository */
504 o->flags |= COMPLETE;
505 o = parse_object(the_repository, &t->tagged->oid);
507 if (o && o->type == OBJ_COMMIT) {
508 struct commit *commit = (struct commit *)o;
509 if (!(commit->object.flags & COMPLETE)) {
510 commit->object.flags |= COMPLETE;
511 commit_list_insert(commit, &complete);
517 static int mark_complete_oid(const char *refname, const struct object_id *oid,
518 int flag, void *cb_data)
520 return mark_complete(oid);
523 static void mark_recent_complete_commits(struct fetch_pack_args *args,
526 while (complete && cutoff <= complete->item->date) {
527 print_verbose(args, _("Marking %s as complete"),
528 oid_to_hex(&complete->item->object.oid));
529 pop_most_recent_commit(&complete, COMPLETE);
533 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
535 for (; refs; refs = refs->next)
536 oidset_insert(oids, &refs->old_oid);
539 static int is_unmatched_ref(const struct ref *ref)
541 struct object_id oid;
543 return ref->match_status == REF_NOT_MATCHED &&
544 !parse_oid_hex(ref->name, &oid, &p) &&
546 oideq(&oid, &ref->old_oid);
549 static void filter_refs(struct fetch_pack_args *args,
551 struct ref **sought, int nr_sought)
553 struct ref *newlist = NULL;
554 struct ref **newtail = &newlist;
555 struct ref *unmatched = NULL;
556 struct ref *ref, *next;
557 struct oidset tip_oids = OIDSET_INIT;
559 int strict = !(allow_unadvertised_object_request &
560 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
563 for (ref = *refs; ref; ref = next) {
567 if (starts_with(ref->name, "refs/") &&
568 check_refname_format(ref->name, 0))
571 while (i < nr_sought) {
572 int cmp = strcmp(ref->name, sought[i]->name);
574 break; /* definitely do not have it */
576 keep = 1; /* definitely have it */
577 sought[i]->match_status = REF_MATCHED;
582 if (!keep && args->fetch_all &&
583 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
590 newtail = &ref->next;
592 ref->next = unmatched;
598 for (i = 0; i < nr_sought; i++) {
600 if (!is_unmatched_ref(ref))
603 add_refs_to_oidset(&tip_oids, unmatched);
604 add_refs_to_oidset(&tip_oids, newlist);
609 /* Append unmatched requests to the list */
610 for (i = 0; i < nr_sought; i++) {
612 if (!is_unmatched_ref(ref))
615 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
616 ref->match_status = REF_MATCHED;
617 *newtail = copy_ref(ref);
618 newtail = &(*newtail)->next;
620 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
624 oidset_clear(&tip_oids);
625 for (ref = unmatched; ref; ref = next) {
633 static void mark_alternate_complete(struct fetch_negotiator *unused,
636 mark_complete(&obj->oid);
639 struct loose_object_iter {
640 struct oidset *loose_object_set;
645 * Mark recent commits available locally and reachable from a local ref as
646 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
647 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
648 * thus do not need COMMON_REF marks).
650 * The cutoff time for recency is determined by this heuristic: it is the
651 * earliest commit time of the objects in refs that are commits and that we know
652 * the commit time of.
654 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
655 struct fetch_pack_args *args,
659 int old_save_commit_buffer = save_commit_buffer;
660 timestamp_t cutoff = 0;
662 save_commit_buffer = 0;
664 for (ref = *refs; ref; ref = ref->next) {
667 if (!has_object_file_with_flags(&ref->old_oid,
670 o = parse_object(the_repository, &ref->old_oid);
674 /* We already have it -- which may mean that we were
675 * in sync with the other side at some time after
676 * that (it is OK if we guess wrong here).
678 if (o->type == OBJ_COMMIT) {
679 struct commit *commit = (struct commit *)o;
680 if (!cutoff || cutoff < commit->date)
681 cutoff = commit->date;
686 for_each_ref(mark_complete_oid, NULL);
687 for_each_cached_alternate(NULL, mark_alternate_complete);
688 commit_list_sort_by_date(&complete);
690 mark_recent_complete_commits(args, cutoff);
694 * Mark all complete remote refs as common refs.
695 * Don't mark them common yet; the server has to be told so first.
697 for (ref = *refs; ref; ref = ref->next) {
698 struct object *o = deref_tag(the_repository,
699 lookup_object(the_repository,
703 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
706 negotiator->known_common(negotiator,
710 save_commit_buffer = old_save_commit_buffer;
714 * Returns 1 if every object pointed to by the given remote refs is available
715 * locally and reachable from a local ref, and 0 otherwise.
717 static int everything_local(struct fetch_pack_args *args,
723 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
724 const struct object_id *remote = &ref->old_oid;
727 o = lookup_object(the_repository, remote->hash);
728 if (!o || !(o->flags & COMPLETE)) {
730 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
734 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
741 static int sideband_demux(int in, int out, void *data)
746 ret = recv_sideband("fetch-pack", xd[0], out);
751 static int get_pack(struct fetch_pack_args *args,
752 int xd[2], char **pack_lockfile)
755 int do_keep = args->keep_pack;
756 const char *cmd_name;
757 struct pack_header header;
759 struct child_process cmd = CHILD_PROCESS_INIT;
762 memset(&demux, 0, sizeof(demux));
764 /* xd[] is talking with upload-pack; subprocess reads from
765 * xd[0], spits out band#2 to stderr, and feeds us band#1
766 * through demux->out.
768 demux.proc = sideband_demux;
771 demux.isolate_sigpipe = 1;
772 if (start_async(&demux))
773 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
778 if (!args->keep_pack && unpack_limit) {
780 if (read_pack_header(demux.out, &header))
781 die(_("protocol error: bad pack header"));
783 if (ntohl(header.hdr_entries) < unpack_limit)
789 if (alternate_shallow_file) {
790 argv_array_push(&cmd.args, "--shallow-file");
791 argv_array_push(&cmd.args, alternate_shallow_file);
794 if (do_keep || args->from_promisor) {
797 cmd_name = "index-pack";
798 argv_array_push(&cmd.args, cmd_name);
799 argv_array_push(&cmd.args, "--stdin");
800 if (!args->quiet && !args->no_progress)
801 argv_array_push(&cmd.args, "-v");
802 if (args->use_thin_pack)
803 argv_array_push(&cmd.args, "--fix-thin");
804 if (do_keep && (args->lock_pack || unpack_limit)) {
805 char hostname[HOST_NAME_MAX + 1];
806 if (xgethostname(hostname, sizeof(hostname)))
807 xsnprintf(hostname, sizeof(hostname), "localhost");
808 argv_array_pushf(&cmd.args,
809 "--keep=fetch-pack %"PRIuMAX " on %s",
810 (uintmax_t)getpid(), hostname);
812 if (args->check_self_contained_and_connected)
813 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
814 if (args->from_promisor)
815 argv_array_push(&cmd.args, "--promisor");
818 cmd_name = "unpack-objects";
819 argv_array_push(&cmd.args, cmd_name);
820 if (args->quiet || args->no_progress)
821 argv_array_push(&cmd.args, "-q");
822 args->check_self_contained_and_connected = 0;
826 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
827 ntohl(header.hdr_version),
828 ntohl(header.hdr_entries));
829 if (fetch_fsck_objects >= 0
831 : transfer_fsck_objects >= 0
832 ? transfer_fsck_objects
834 if (args->from_promisor)
836 * We cannot use --strict in index-pack because it
837 * checks both broken objects and links, but we only
838 * want to check for broken objects.
840 argv_array_push(&cmd.args, "--fsck-objects");
842 argv_array_pushf(&cmd.args, "--strict%s",
848 if (start_command(&cmd))
849 die(_("fetch-pack: unable to fork off %s"), cmd_name);
850 if (do_keep && pack_lockfile) {
851 *pack_lockfile = index_pack_lockfile(cmd.out);
856 /* Closed by start_command() */
859 ret = finish_command(&cmd);
860 if (!ret || (args->check_self_contained_and_connected && ret == 1))
861 args->self_contained_and_connected =
862 args->check_self_contained_and_connected &&
865 die(_("%s failed"), cmd_name);
866 if (use_sideband && finish_async(&demux))
867 die(_("error in sideband demultiplexer"));
871 static int cmp_ref_by_name(const void *a_, const void *b_)
873 const struct ref *a = *((const struct ref **)a_);
874 const struct ref *b = *((const struct ref **)b_);
875 return strcmp(a->name, b->name);
878 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
880 const struct ref *orig_ref,
881 struct ref **sought, int nr_sought,
882 struct shallow_info *si,
883 char **pack_lockfile)
885 struct ref *ref = copy_ref_list(orig_ref);
886 struct object_id oid;
887 const char *agent_feature;
889 struct fetch_negotiator negotiator;
890 fetch_negotiator_init(&negotiator, negotiation_algorithm);
892 sort_ref_list(&ref, ref_compare_name);
893 QSORT(sought, nr_sought, cmp_ref_by_name);
895 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
896 die(_("Server does not support shallow clients"));
897 if (args->depth > 0 || args->deepen_since || args->deepen_not)
899 if (server_supports("multi_ack_detailed")) {
900 print_verbose(args, _("Server supports multi_ack_detailed"));
902 if (server_supports("no-done")) {
903 print_verbose(args, _("Server supports no-done"));
904 if (args->stateless_rpc)
908 else if (server_supports("multi_ack")) {
909 print_verbose(args, _("Server supports multi_ack"));
912 if (server_supports("side-band-64k")) {
913 print_verbose(args, _("Server supports side-band-64k"));
916 else if (server_supports("side-band")) {
917 print_verbose(args, _("Server supports side-band"));
920 if (server_supports("allow-tip-sha1-in-want")) {
921 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
922 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
924 if (server_supports("allow-reachable-sha1-in-want")) {
925 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
926 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
928 if (!server_supports("thin-pack"))
929 args->use_thin_pack = 0;
930 if (!server_supports("no-progress"))
931 args->no_progress = 0;
932 if (!server_supports("include-tag"))
933 args->include_tag = 0;
934 if (server_supports("ofs-delta"))
935 print_verbose(args, _("Server supports ofs-delta"));
937 prefer_ofs_delta = 0;
939 if (server_supports("filter")) {
940 server_supports_filtering = 1;
941 print_verbose(args, _("Server supports filter"));
942 } else if (args->filter_options.choice) {
943 warning("filtering not recognized by server, ignoring");
946 if ((agent_feature = server_feature_value("agent", &agent_len))) {
949 print_verbose(args, _("Server version is %.*s"),
950 agent_len, agent_feature);
952 if (server_supports("deepen-since"))
954 else if (args->deepen_since)
955 die(_("Server does not support --shallow-since"));
956 if (server_supports("deepen-not"))
958 else if (args->deepen_not)
959 die(_("Server does not support --shallow-exclude"));
960 if (!server_supports("deepen-relative") && args->deepen_relative)
961 die(_("Server does not support --deepen"));
963 if (!args->no_dependents) {
964 mark_complete_and_common_ref(&negotiator, args, &ref);
965 filter_refs(args, &ref, sought, nr_sought);
966 if (everything_local(args, &ref)) {
971 filter_refs(args, &ref, sought, nr_sought);
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);
1015 if (args->deepen_relative)
1016 packet_buf_write(req_buf, "deepen-relative\n");
1019 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1021 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1023 for ( ; wants ; wants = wants->next) {
1024 const struct object_id *remote = &wants->old_oid;
1028 * If that object is complete (i.e. it is an ancestor of a
1029 * local ref), we tell them we have it but do not have to
1030 * tell them about its ancestors, which they already know
1033 * We use lookup_object here because we are only
1034 * interested in the case we *know* the object is
1035 * reachable and we have already scanned it.
1037 * Do this only if args->no_dependents is false (if it is true,
1038 * we cannot trust the object flags).
1040 if (!no_dependents &&
1041 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1042 (o->flags & COMPLETE)) {
1046 if (!use_ref_in_want || wants->exact_oid)
1047 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1049 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1053 static void add_common(struct strbuf *req_buf, struct oidset *common)
1055 struct oidset_iter iter;
1056 const struct object_id *oid;
1057 oidset_iter_init(common, &iter);
1059 while ((oid = oidset_iter_next(&iter))) {
1060 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1064 static int add_haves(struct fetch_negotiator *negotiator,
1065 struct strbuf *req_buf,
1066 int *haves_to_send, int *in_vain)
1069 int haves_added = 0;
1070 const struct object_id *oid;
1072 while ((oid = negotiator->next(negotiator))) {
1073 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1074 if (++haves_added >= *haves_to_send)
1078 *in_vain += haves_added;
1079 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1081 packet_buf_write(req_buf, "done\n");
1085 /* Increase haves to send on next round */
1086 *haves_to_send = next_flush(1, *haves_to_send);
1091 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1092 const struct fetch_pack_args *args,
1093 const struct ref *wants, struct oidset *common,
1094 int *haves_to_send, int *in_vain)
1097 struct strbuf req_buf = STRBUF_INIT;
1099 if (server_supports_v2("fetch", 1))
1100 packet_buf_write(&req_buf, "command=fetch");
1101 if (server_supports_v2("agent", 0))
1102 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1103 if (args->server_options && args->server_options->nr &&
1104 server_supports_v2("server-option", 1)) {
1106 for (i = 0; i < args->server_options->nr; i++)
1107 packet_write_fmt(fd_out, "server-option=%s",
1108 args->server_options->items[i].string);
1111 packet_buf_delim(&req_buf);
1112 if (args->use_thin_pack)
1113 packet_buf_write(&req_buf, "thin-pack");
1114 if (args->no_progress)
1115 packet_buf_write(&req_buf, "no-progress");
1116 if (args->include_tag)
1117 packet_buf_write(&req_buf, "include-tag");
1118 if (prefer_ofs_delta)
1119 packet_buf_write(&req_buf, "ofs-delta");
1121 /* Add shallow-info and deepen request */
1122 if (server_supports_feature("fetch", "shallow", 0))
1123 add_shallow_requests(&req_buf, args);
1124 else if (is_repository_shallow(the_repository) || args->deepen)
1125 die(_("Server does not support shallow requests"));
1128 if (server_supports_feature("fetch", "filter", 0) &&
1129 args->filter_options.choice) {
1130 struct strbuf expanded_filter_spec = STRBUF_INIT;
1131 print_verbose(args, _("Server supports filter"));
1132 expand_list_objects_filter_spec(&args->filter_options,
1133 &expanded_filter_spec);
1134 packet_buf_write(&req_buf, "filter %s",
1135 expanded_filter_spec.buf);
1136 strbuf_release(&expanded_filter_spec);
1137 } else if (args->filter_options.choice) {
1138 warning("filtering not recognized by server, ignoring");
1142 add_wants(args->no_dependents, wants, &req_buf);
1144 if (args->no_dependents) {
1145 packet_buf_write(&req_buf, "done");
1148 /* Add all of the common commits we've found in previous rounds */
1149 add_common(&req_buf, common);
1151 /* Add initial haves */
1152 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1156 packet_buf_flush(&req_buf);
1157 write_or_die(fd_out, req_buf.buf, req_buf.len);
1159 strbuf_release(&req_buf);
1164 * Processes a section header in a server's response and checks if it matches
1165 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1166 * not consumed); if 0, the line will be consumed and the function will die if
1167 * the section header doesn't match what was expected.
1169 static int process_section_header(struct packet_reader *reader,
1170 const char *section, int peek)
1174 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1175 die(_("error reading section header '%s'"), section);
1177 ret = !strcmp(reader->line, section);
1181 die(_("expected '%s', received '%s'"),
1182 section, reader->line);
1183 packet_reader_read(reader);
1189 static int process_acks(struct fetch_negotiator *negotiator,
1190 struct packet_reader *reader,
1191 struct oidset *common)
1194 int received_ready = 0;
1195 int received_ack = 0;
1197 process_section_header(reader, "acknowledgments", 0);
1198 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1201 if (!strcmp(reader->line, "NAK"))
1204 if (skip_prefix(reader->line, "ACK ", &arg)) {
1205 struct object_id oid;
1206 if (!get_oid_hex(arg, &oid)) {
1207 struct commit *commit;
1208 oidset_insert(common, &oid);
1209 commit = lookup_commit(the_repository, &oid);
1210 negotiator->ack(negotiator, commit);
1215 if (!strcmp(reader->line, "ready")) {
1220 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1223 if (reader->status != PACKET_READ_FLUSH &&
1224 reader->status != PACKET_READ_DELIM)
1225 die(_("error processing acks: %d"), reader->status);
1228 * If an "acknowledgments" section is sent, a packfile is sent if and
1229 * only if "ready" was sent in this section. The other sections
1230 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1231 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1234 if (received_ready && reader->status != PACKET_READ_DELIM)
1235 die(_("expected packfile to be sent after 'ready'"));
1236 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1237 die(_("expected no other sections to be sent after no 'ready'"));
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 int line_received = 0;
1248 process_section_header(reader, "shallow-info", 0);
1249 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1251 struct object_id oid;
1253 if (skip_prefix(reader->line, "shallow ", &arg)) {
1254 if (get_oid_hex(arg, &oid))
1255 die(_("invalid shallow line: %s"), reader->line);
1256 register_shallow(the_repository, &oid);
1260 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1261 if (get_oid_hex(arg, &oid))
1262 die(_("invalid unshallow line: %s"), reader->line);
1263 if (!lookup_object(the_repository, oid.hash))
1264 die(_("object not found: %s"), reader->line);
1265 /* make sure that it is parsed as shallow */
1266 if (!parse_object(the_repository, &oid))
1267 die(_("error in object: %s"), reader->line);
1268 if (unregister_shallow(&oid))
1269 die(_("no shallow found: %s"), reader->line);
1273 die(_("expected shallow/unshallow, got %s"), reader->line);
1276 if (reader->status != PACKET_READ_FLUSH &&
1277 reader->status != PACKET_READ_DELIM)
1278 die(_("error processing shallow info: %d"), reader->status);
1280 if (line_received) {
1281 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1287 static void receive_wanted_refs(struct packet_reader *reader,
1288 struct ref **sought, int nr_sought)
1290 process_section_header(reader, "wanted-refs", 0);
1291 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1292 struct object_id oid;
1296 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1297 die(_("expected wanted-ref, got '%s'"), reader->line);
1299 for (i = 0; i < nr_sought; i++) {
1300 if (!strcmp(end, sought[i]->name)) {
1301 oidcpy(&sought[i]->old_oid, &oid);
1307 die(_("unexpected wanted-ref: '%s'"), reader->line);
1310 if (reader->status != PACKET_READ_DELIM)
1311 die(_("error processing wanted refs: %d"), reader->status);
1315 FETCH_CHECK_LOCAL = 0,
1322 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1324 const struct ref *orig_ref,
1325 struct ref **sought, int nr_sought,
1326 char **pack_lockfile)
1328 struct ref *ref = copy_ref_list(orig_ref);
1329 enum fetch_state state = FETCH_CHECK_LOCAL;
1330 struct oidset common = OIDSET_INIT;
1331 struct packet_reader reader;
1333 int haves_to_send = INITIAL_FLUSH;
1334 struct fetch_negotiator negotiator;
1335 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1336 packet_reader_init(&reader, fd[0], NULL, 0,
1337 PACKET_READ_CHOMP_NEWLINE);
1339 while (state != FETCH_DONE) {
1341 case FETCH_CHECK_LOCAL:
1342 sort_ref_list(&ref, ref_compare_name);
1343 QSORT(sought, nr_sought, cmp_ref_by_name);
1345 /* v2 supports these by default */
1346 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1348 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1351 /* Filter 'ref' by 'sought' and those that aren't local */
1352 if (!args->no_dependents) {
1353 mark_complete_and_common_ref(&negotiator, args, &ref);
1354 filter_refs(args, &ref, sought, nr_sought);
1355 if (everything_local(args, &ref))
1358 state = FETCH_SEND_REQUEST;
1360 mark_tips(&negotiator, args->negotiation_tips);
1361 for_each_cached_alternate(&negotiator,
1362 insert_one_alternate_object);
1364 filter_refs(args, &ref, sought, nr_sought);
1365 state = FETCH_SEND_REQUEST;
1368 case FETCH_SEND_REQUEST:
1369 if (send_fetch_request(&negotiator, fd[1], args, ref,
1371 &haves_to_send, &in_vain))
1372 state = FETCH_GET_PACK;
1374 state = FETCH_PROCESS_ACKS;
1376 case FETCH_PROCESS_ACKS:
1377 /* Process ACKs/NAKs */
1378 switch (process_acks(&negotiator, &reader, &common)) {
1380 state = FETCH_GET_PACK;
1386 state = FETCH_SEND_REQUEST;
1390 case FETCH_GET_PACK:
1391 /* Check for shallow-info section */
1392 if (process_section_header(&reader, "shallow-info", 1))
1393 receive_shallow_info(args, &reader);
1395 if (process_section_header(&reader, "wanted-refs", 1))
1396 receive_wanted_refs(&reader, sought, nr_sought);
1399 process_section_header(&reader, "packfile", 0);
1400 if (get_pack(args, fd, pack_lockfile))
1401 die(_("git fetch-pack: fetch failed."));
1410 negotiator.release(&negotiator);
1411 oidset_clear(&common);
1415 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1417 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1420 if (git_config_pathname(&path, var, value))
1422 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1423 fsck_msg_types.len ? ',' : '=', path);
1428 if (skip_prefix(var, "fetch.fsck.", &var)) {
1429 if (is_valid_msg_type(var, value))
1430 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1431 fsck_msg_types.len ? ',' : '=', var, value);
1433 warning("Skipping unknown msg id '%s'", var);
1437 return git_default_config(var, value, cb);
1440 static void fetch_pack_config(void)
1442 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1443 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1444 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1445 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1446 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1447 git_config_get_string("fetch.negotiationalgorithm",
1448 &negotiation_algorithm);
1450 git_config(fetch_pack_config_cb, NULL);
1453 static void fetch_pack_setup(void)
1455 static int did_setup;
1458 fetch_pack_config();
1459 if (0 <= transfer_unpack_limit)
1460 unpack_limit = transfer_unpack_limit;
1461 else if (0 <= fetch_unpack_limit)
1462 unpack_limit = fetch_unpack_limit;
1466 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1468 struct string_list names = STRING_LIST_INIT_NODUP;
1471 for (src = dst = 0; src < nr; src++) {
1472 struct string_list_item *item;
1473 item = string_list_insert(&names, ref[src]->name);
1475 continue; /* already have it */
1476 item->util = ref[src];
1478 ref[dst] = ref[src];
1481 for (src = dst; src < nr; src++)
1483 string_list_clear(&names, 0);
1487 static void update_shallow(struct fetch_pack_args *args,
1488 struct ref **sought, int nr_sought,
1489 struct shallow_info *si)
1491 struct oid_array ref = OID_ARRAY_INIT;
1495 if (args->deepen && alternate_shallow_file) {
1496 if (*alternate_shallow_file == '\0') { /* --unshallow */
1497 unlink_or_warn(git_path_shallow(the_repository));
1498 rollback_lock_file(&shallow_lock);
1500 commit_lock_file(&shallow_lock);
1504 if (!si->shallow || !si->shallow->nr)
1507 if (args->cloning) {
1509 * remote is shallow, but this is a clone, there are
1510 * no objects in repo to worry about. Accept any
1511 * shallow points that exist in the pack (iow in repo
1512 * after get_pack() and reprepare_packed_git())
1514 struct oid_array extra = OID_ARRAY_INIT;
1515 struct object_id *oid = si->shallow->oid;
1516 for (i = 0; i < si->shallow->nr; i++)
1517 if (has_object_file(&oid[i]))
1518 oid_array_append(&extra, &oid[i]);
1520 setup_alternate_shallow(&shallow_lock,
1521 &alternate_shallow_file,
1523 commit_lock_file(&shallow_lock);
1525 oid_array_clear(&extra);
1529 if (!si->nr_ours && !si->nr_theirs)
1532 remove_nonexistent_theirs_shallow(si);
1533 if (!si->nr_ours && !si->nr_theirs)
1535 for (i = 0; i < nr_sought; i++)
1536 oid_array_append(&ref, &sought[i]->old_oid);
1539 if (args->update_shallow) {
1541 * remote is also shallow, .git/shallow may be updated
1542 * so all refs can be accepted. Make sure we only add
1543 * shallow roots that are actually reachable from new
1546 struct oid_array extra = OID_ARRAY_INIT;
1547 struct object_id *oid = si->shallow->oid;
1548 assign_shallow_commits_to_refs(si, NULL, NULL);
1549 if (!si->nr_ours && !si->nr_theirs) {
1550 oid_array_clear(&ref);
1553 for (i = 0; i < si->nr_ours; i++)
1554 oid_array_append(&extra, &oid[si->ours[i]]);
1555 for (i = 0; i < si->nr_theirs; i++)
1556 oid_array_append(&extra, &oid[si->theirs[i]]);
1557 setup_alternate_shallow(&shallow_lock,
1558 &alternate_shallow_file,
1560 commit_lock_file(&shallow_lock);
1561 oid_array_clear(&extra);
1562 oid_array_clear(&ref);
1567 * remote is also shallow, check what ref is safe to update
1568 * without updating .git/shallow
1570 status = xcalloc(nr_sought, sizeof(*status));
1571 assign_shallow_commits_to_refs(si, NULL, status);
1572 if (si->nr_ours || si->nr_theirs) {
1573 for (i = 0; i < nr_sought; i++)
1575 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1578 oid_array_clear(&ref);
1581 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1583 struct ref **rm = cb_data;
1584 struct ref *ref = *rm;
1587 return -1; /* end of the list */
1589 oidcpy(oid, &ref->old_oid);
1593 struct ref *fetch_pack(struct fetch_pack_args *args,
1594 int fd[], struct child_process *conn,
1595 const struct ref *ref,
1597 struct ref **sought, int nr_sought,
1598 struct oid_array *shallow,
1599 char **pack_lockfile,
1600 enum protocol_version version)
1602 struct ref *ref_cpy;
1603 struct shallow_info si;
1607 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1609 if (args->no_dependents && !args->filter_options.choice) {
1611 * The protocol does not support requesting that only the
1612 * wanted objects be sent, so approximate this by setting a
1613 * "blob:none" filter if no filter is already set. This works
1614 * for all object types: note that wanted blobs will still be
1615 * sent because they are directly specified as a "want".
1617 * NEEDSWORK: Add an option in the protocol to request that
1618 * only the wanted objects be sent, and implement it.
1620 parse_list_objects_filter(&args->filter_options, "blob:none");
1623 if (version != protocol_v2 && !ref) {
1624 packet_flush(fd[1]);
1625 die(_("no matching remote head"));
1627 prepare_shallow_info(&si, shallow);
1628 if (version == protocol_v2)
1629 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1632 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1633 &si, pack_lockfile);
1634 reprepare_packed_git(the_repository);
1636 if (!args->cloning && args->deepen) {
1637 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1638 struct ref *iterator = ref_cpy;
1639 opt.shallow_file = alternate_shallow_file;
1641 opt.is_deepening_fetch = 1;
1642 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1643 error(_("remote did not send all necessary objects"));
1646 rollback_lock_file(&shallow_lock);
1649 args->connectivity_checked = 1;
1652 update_shallow(args, sought, nr_sought, &si);
1654 clear_shallow_info(&si);
1658 int report_unmatched_refs(struct ref **sought, int nr_sought)
1662 for (i = 0; i < nr_sought; i++) {
1665 switch (sought[i]->match_status) {
1668 case REF_NOT_MATCHED:
1669 error(_("no such remote ref %s"), sought[i]->name);
1671 case REF_UNADVERTISED_NOT_ALLOWED:
1672 error(_("Server does not allow request for unadvertised object %s"),