2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "sha1-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
26 static int transfer_unpack_limit = -1;
27 static int fetch_unpack_limit = -1;
28 static int unpack_limit = 100;
29 static int prefer_ofs_delta = 1;
31 static int deepen_since_ok;
32 static int deepen_not_ok;
33 static int fetch_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int agent_supported;
36 static int server_supports_filtering;
37 static struct lock_file shallow_lock;
38 static const char *alternate_shallow_file;
39 static char *negotiation_algorithm;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
42 /* Remember to update object flag allocation in object.h */
43 #define COMPLETE (1U << 0)
44 #define ALTERNATE (1U << 1)
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
50 #define MAX_IN_VAIN 256
52 static int multi_ack, use_sideband;
53 /* Allow specifying sha1 if it is a ref tip. */
54 #define ALLOW_TIP_SHA1 01
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 #define ALLOW_REACHABLE_SHA1 02
57 static unsigned int allow_unadvertised_object_request;
59 __attribute__((format (printf, 2, 3)))
60 static inline void print_verbose(const struct fetch_pack_args *args,
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
74 struct alternate_object_cache {
75 struct object **items;
79 static void cache_one_alternate(const char *refname,
80 const struct object_id *oid,
83 struct alternate_object_cache *cache = vcache;
84 struct object *obj = parse_object(the_repository, oid);
86 if (!obj || (obj->flags & ALTERNATE))
89 obj->flags |= ALTERNATE;
90 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
91 cache->items[cache->nr++] = obj;
94 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
95 void (*cb)(struct fetch_negotiator *,
98 static int initialized;
99 static struct alternate_object_cache cache;
103 for_each_alternate_ref(cache_one_alternate, &cache);
107 for (i = 0; i < cache.nr; i++)
108 cb(negotiator, cache.items[i]);
111 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
113 const struct object_id *oid)
115 struct object *o = deref_tag(the_repository,
116 parse_object(the_repository, oid),
119 if (o && o->type == OBJ_COMMIT)
120 negotiator->add_tip(negotiator, (struct commit *)o);
125 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
126 int flag, void *cb_data)
128 return rev_list_insert_ref(cb_data, refname, oid);
139 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
141 if (args->stateless_rpc && args->deepen) {
142 /* If we sent a depth we will get back "duplicate"
143 * shallow and unshallow commands every time there
144 * is a block of have lines exchanged.
147 while ((line = packet_read_line(fd, NULL))) {
148 if (starts_with(line, "shallow "))
150 if (starts_with(line, "unshallow "))
152 die(_("git fetch-pack: expected shallow list"));
157 static enum ack_type get_ack(int fd, struct object_id *result_oid)
160 char *line = packet_read_line(fd, &len);
164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
165 if (!strcmp(line, "NAK"))
167 if (skip_prefix(line, "ACK ", &arg)) {
168 if (!get_oid_hex(arg, result_oid)) {
173 if (strstr(arg, "continue"))
175 if (strstr(arg, "common"))
177 if (strstr(arg, "ready"))
182 if (skip_prefix(line, "ERR ", &arg))
183 die(_("remote error: %s"), arg);
184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
187 static void send_request(struct fetch_pack_args *args,
188 int fd, struct strbuf *buf)
190 if (args->stateless_rpc) {
191 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
194 write_or_die(fd, buf->buf, buf->len);
197 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
200 rev_list_insert_ref(negotiator, NULL, &obj->oid);
203 #define INITIAL_FLUSH 16
204 #define PIPESAFE_FLUSH 32
205 #define LARGE_FLUSH 16384
207 static int next_flush(int stateless_rpc, int count)
210 if (count < LARGE_FLUSH)
213 count = count * 11 / 10;
215 if (count < PIPESAFE_FLUSH)
218 count += PIPESAFE_FLUSH;
223 static void mark_tips(struct fetch_negotiator *negotiator,
224 const struct oid_array *negotiation_tips)
228 if (!negotiation_tips) {
229 for_each_ref(rev_list_insert_ref_oid, negotiator);
233 for (i = 0; i < negotiation_tips->nr; i++)
234 rev_list_insert_ref(negotiator, NULL,
235 &negotiation_tips->oid[i]);
239 static int find_common(struct fetch_negotiator *negotiator,
240 struct fetch_pack_args *args,
241 int fd[2], struct object_id *result_oid,
245 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
246 const struct object_id *oid;
247 unsigned in_vain = 0;
248 int got_continue = 0;
250 struct strbuf req_buf = STRBUF_INIT;
251 size_t state_len = 0;
253 if (args->stateless_rpc && multi_ack == 1)
254 die(_("--stateless-rpc requires multi_ack_detailed"));
256 mark_tips(negotiator, args->negotiation_tips);
257 for_each_cached_alternate(negotiator, insert_one_alternate_object);
260 for ( ; refs ; refs = refs->next) {
261 struct object_id *remote = &refs->old_oid;
262 const char *remote_hex;
266 * If that object is complete (i.e. it is an ancestor of a
267 * local ref), we tell them we have it but do not have to
268 * tell them about its ancestors, which they already know
271 * We use lookup_object here because we are only
272 * interested in the case we *know* the object is
273 * reachable and we have already scanned it.
275 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
276 (o->flags & COMPLETE)) {
280 remote_hex = oid_to_hex(remote);
282 struct strbuf c = STRBUF_INIT;
283 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
284 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
285 if (no_done) strbuf_addstr(&c, " no-done");
286 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
287 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
288 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
289 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
290 if (args->no_progress) strbuf_addstr(&c, " no-progress");
291 if (args->include_tag) strbuf_addstr(&c, " include-tag");
292 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
293 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
294 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
295 if (agent_supported) strbuf_addf(&c, " agent=%s",
296 git_user_agent_sanitized());
297 if (args->filter_options.choice)
298 strbuf_addstr(&c, " filter");
299 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
302 packet_buf_write(&req_buf, "want %s\n", remote_hex);
307 strbuf_release(&req_buf);
312 if (is_repository_shallow(the_repository))
313 write_shallow_commits(&req_buf, 1, NULL);
315 packet_buf_write(&req_buf, "deepen %d", args->depth);
316 if (args->deepen_since) {
317 timestamp_t max_age = approxidate(args->deepen_since);
318 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
320 if (args->deepen_not) {
322 for (i = 0; i < args->deepen_not->nr; i++) {
323 struct string_list_item *s = args->deepen_not->items + i;
324 packet_buf_write(&req_buf, "deepen-not %s", s->string);
327 if (server_supports_filtering && args->filter_options.choice)
328 packet_buf_write(&req_buf, "filter %s",
329 args->filter_options.filter_spec);
330 packet_buf_flush(&req_buf);
331 state_len = req_buf.len;
336 struct object_id oid;
338 send_request(args, fd[1], &req_buf);
339 while ((line = packet_read_line(fd[0], NULL))) {
340 if (skip_prefix(line, "shallow ", &arg)) {
341 if (get_oid_hex(arg, &oid))
342 die(_("invalid shallow line: %s"), line);
343 register_shallow(the_repository, &oid);
346 if (skip_prefix(line, "unshallow ", &arg)) {
347 if (get_oid_hex(arg, &oid))
348 die(_("invalid unshallow line: %s"), line);
349 if (!lookup_object(the_repository, oid.hash))
350 die(_("object not found: %s"), line);
351 /* make sure that it is parsed as shallow */
352 if (!parse_object(the_repository, &oid))
353 die(_("error in object: %s"), line);
354 if (unregister_shallow(&oid))
355 die(_("no shallow found: %s"), line);
358 die(_("expected shallow/unshallow, got %s"), line);
360 } else if (!args->stateless_rpc)
361 send_request(args, fd[1], &req_buf);
363 if (!args->stateless_rpc) {
364 /* If we aren't using the stateless-rpc interface
365 * we don't need to retain the headers.
367 strbuf_setlen(&req_buf, 0);
373 if (args->no_dependents)
375 while ((oid = negotiator->next(negotiator))) {
376 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
377 print_verbose(args, "have %s", oid_to_hex(oid));
379 if (flush_at <= ++count) {
382 packet_buf_flush(&req_buf);
383 send_request(args, fd[1], &req_buf);
384 strbuf_setlen(&req_buf, state_len);
386 flush_at = next_flush(args->stateless_rpc, count);
389 * We keep one window "ahead" of the other side, and
390 * will wait for an ACK only on the next one
392 if (!args->stateless_rpc && count == INITIAL_FLUSH)
395 consume_shallow_list(args, fd[0]);
397 ack = get_ack(fd[0], result_oid);
399 print_verbose(args, _("got %s %d %s"), "ack",
400 ack, oid_to_hex(result_oid));
410 struct commit *commit =
411 lookup_commit(the_repository,
416 die(_("invalid commit %s"), oid_to_hex(result_oid));
417 was_common = negotiator->ack(negotiator, commit);
418 if (args->stateless_rpc
421 /* We need to replay the have for this object
422 * on the next RPC request so the peer knows
423 * it is in common with us.
425 const char *hex = oid_to_hex(result_oid);
426 packet_buf_write(&req_buf, "have %s\n", hex);
427 state_len = req_buf.len;
429 * Reset in_vain because an ack
430 * for this commit has not been
434 } else if (!args->stateless_rpc
435 || ack != ACK_common)
439 if (ack == ACK_ready)
446 if (got_continue && MAX_IN_VAIN < in_vain) {
447 print_verbose(args, _("giving up"));
455 if (!got_ready || !no_done) {
456 packet_buf_write(&req_buf, "done\n");
457 send_request(args, fd[1], &req_buf);
459 print_verbose(args, _("done"));
464 strbuf_release(&req_buf);
466 if (!got_ready || !no_done)
467 consume_shallow_list(args, fd[0]);
468 while (flushes || multi_ack) {
469 int ack = get_ack(fd[0], result_oid);
471 print_verbose(args, _("got %s (%d) %s"), "ack",
472 ack, oid_to_hex(result_oid));
480 /* it is no error to fetch into a completely empty repo */
481 return count ? retval : 0;
484 static struct commit_list *complete;
486 static int mark_complete(const struct object_id *oid)
488 struct object *o = parse_object(the_repository, oid);
490 while (o && o->type == OBJ_TAG) {
491 struct tag *t = (struct tag *) o;
493 break; /* broken repository */
494 o->flags |= COMPLETE;
495 o = parse_object(the_repository, &t->tagged->oid);
497 if (o && o->type == OBJ_COMMIT) {
498 struct commit *commit = (struct commit *)o;
499 if (!(commit->object.flags & COMPLETE)) {
500 commit->object.flags |= COMPLETE;
501 commit_list_insert(commit, &complete);
507 static int mark_complete_oid(const char *refname, const struct object_id *oid,
508 int flag, void *cb_data)
510 return mark_complete(oid);
513 static void mark_recent_complete_commits(struct fetch_pack_args *args,
516 while (complete && cutoff <= complete->item->date) {
517 print_verbose(args, _("Marking %s as complete"),
518 oid_to_hex(&complete->item->object.oid));
519 pop_most_recent_commit(&complete, COMPLETE);
523 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
525 for (; refs; refs = refs->next)
526 oidset_insert(oids, &refs->old_oid);
529 static int tip_oids_contain(struct oidset *tip_oids,
530 struct ref *unmatched, struct ref *newlist,
531 const struct object_id *id)
534 * Note that this only looks at the ref lists the first time it's
535 * called. This works out in filter_refs() because even though it may
536 * add to "newlist" between calls, the additions will always be for
537 * oids that are already in the set.
539 if (!tip_oids->map.map.tablesize) {
540 add_refs_to_oidset(tip_oids, unmatched);
541 add_refs_to_oidset(tip_oids, newlist);
543 return oidset_contains(tip_oids, id);
546 static void filter_refs(struct fetch_pack_args *args,
548 struct ref **sought, int nr_sought)
550 struct ref *newlist = NULL;
551 struct ref **newtail = &newlist;
552 struct ref *unmatched = NULL;
553 struct ref *ref, *next;
554 struct oidset tip_oids = OIDSET_INIT;
558 for (ref = *refs; ref; ref = next) {
562 if (starts_with(ref->name, "refs/") &&
563 check_refname_format(ref->name, 0))
566 while (i < nr_sought) {
567 int cmp = strcmp(ref->name, sought[i]->name);
569 break; /* definitely do not have it */
571 keep = 1; /* definitely have it */
572 sought[i]->match_status = REF_MATCHED;
577 if (!keep && args->fetch_all &&
578 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
585 newtail = &ref->next;
587 ref->next = unmatched;
592 /* Append unmatched requests to the list */
593 for (i = 0; i < nr_sought; i++) {
594 struct object_id oid;
598 if (ref->match_status != REF_NOT_MATCHED)
600 if (parse_oid_hex(ref->name, &oid, &p) ||
602 oidcmp(&oid, &ref->old_oid))
605 if ((allow_unadvertised_object_request &
606 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
607 tip_oids_contain(&tip_oids, unmatched, newlist,
609 ref->match_status = REF_MATCHED;
610 *newtail = copy_ref(ref);
611 newtail = &(*newtail)->next;
613 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
617 oidset_clear(&tip_oids);
618 for (ref = unmatched; ref; ref = next) {
626 static void mark_alternate_complete(struct fetch_negotiator *unused,
629 mark_complete(&obj->oid);
632 struct loose_object_iter {
633 struct oidset *loose_object_set;
638 * If the number of refs is not larger than the number of loose objects,
639 * this function stops inserting.
641 static int add_loose_objects_to_set(const struct object_id *oid,
645 struct loose_object_iter *iter = data;
646 oidset_insert(iter->loose_object_set, oid);
647 if (iter->refs == NULL)
650 iter->refs = iter->refs->next;
655 * Mark recent commits available locally and reachable from a local ref as
656 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
657 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
658 * thus do not need COMMON_REF marks).
660 * The cutoff time for recency is determined by this heuristic: it is the
661 * earliest commit time of the objects in refs that are commits and that we know
662 * the commit time of.
664 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
665 struct fetch_pack_args *args,
669 int old_save_commit_buffer = save_commit_buffer;
670 timestamp_t cutoff = 0;
671 struct oidset loose_oid_set = OIDSET_INIT;
673 struct loose_object_iter iter = {&loose_oid_set, *refs};
675 /* Enumerate all loose objects or know refs are not so many. */
676 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
679 save_commit_buffer = 0;
681 for (ref = *refs; ref; ref = ref->next) {
683 unsigned int flags = OBJECT_INFO_QUICK;
686 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
688 * I know this does not exist in the loose form,
689 * so check if it exists in a non-loose form.
691 flags |= OBJECT_INFO_IGNORE_LOOSE;
694 if (!has_object_file_with_flags(&ref->old_oid, flags))
696 o = parse_object(the_repository, &ref->old_oid);
700 /* We already have it -- which may mean that we were
701 * in sync with the other side at some time after
702 * that (it is OK if we guess wrong here).
704 if (o->type == OBJ_COMMIT) {
705 struct commit *commit = (struct commit *)o;
706 if (!cutoff || cutoff < commit->date)
707 cutoff = commit->date;
711 oidset_clear(&loose_oid_set);
713 if (!args->no_dependents) {
715 for_each_ref(mark_complete_oid, NULL);
716 for_each_cached_alternate(NULL, mark_alternate_complete);
717 commit_list_sort_by_date(&complete);
719 mark_recent_complete_commits(args, cutoff);
723 * Mark all complete remote refs as common refs.
724 * Don't mark them common yet; the server has to be told so first.
726 for (ref = *refs; ref; ref = ref->next) {
727 struct object *o = deref_tag(the_repository,
728 lookup_object(the_repository,
732 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
735 negotiator->known_common(negotiator,
740 save_commit_buffer = old_save_commit_buffer;
744 * Returns 1 if every object pointed to by the given remote refs is available
745 * locally and reachable from a local ref, and 0 otherwise.
747 static int everything_local(struct fetch_pack_args *args,
753 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
754 const struct object_id *remote = &ref->old_oid;
757 o = lookup_object(the_repository, remote->hash);
758 if (!o || !(o->flags & COMPLETE)) {
760 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
764 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
771 static int sideband_demux(int in, int out, void *data)
776 ret = recv_sideband("fetch-pack", xd[0], out);
781 static int get_pack(struct fetch_pack_args *args,
782 int xd[2], char **pack_lockfile)
785 int do_keep = args->keep_pack;
786 const char *cmd_name;
787 struct pack_header header;
789 struct child_process cmd = CHILD_PROCESS_INIT;
792 memset(&demux, 0, sizeof(demux));
794 /* xd[] is talking with upload-pack; subprocess reads from
795 * xd[0], spits out band#2 to stderr, and feeds us band#1
796 * through demux->out.
798 demux.proc = sideband_demux;
801 demux.isolate_sigpipe = 1;
802 if (start_async(&demux))
803 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
808 if (!args->keep_pack && unpack_limit) {
810 if (read_pack_header(demux.out, &header))
811 die(_("protocol error: bad pack header"));
813 if (ntohl(header.hdr_entries) < unpack_limit)
819 if (alternate_shallow_file) {
820 argv_array_push(&cmd.args, "--shallow-file");
821 argv_array_push(&cmd.args, alternate_shallow_file);
824 if (do_keep || args->from_promisor) {
827 cmd_name = "index-pack";
828 argv_array_push(&cmd.args, cmd_name);
829 argv_array_push(&cmd.args, "--stdin");
830 if (!args->quiet && !args->no_progress)
831 argv_array_push(&cmd.args, "-v");
832 if (args->use_thin_pack)
833 argv_array_push(&cmd.args, "--fix-thin");
834 if (do_keep && (args->lock_pack || unpack_limit)) {
835 char hostname[HOST_NAME_MAX + 1];
836 if (xgethostname(hostname, sizeof(hostname)))
837 xsnprintf(hostname, sizeof(hostname), "localhost");
838 argv_array_pushf(&cmd.args,
839 "--keep=fetch-pack %"PRIuMAX " on %s",
840 (uintmax_t)getpid(), hostname);
842 if (args->check_self_contained_and_connected)
843 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
844 if (args->from_promisor)
845 argv_array_push(&cmd.args, "--promisor");
848 cmd_name = "unpack-objects";
849 argv_array_push(&cmd.args, cmd_name);
850 if (args->quiet || args->no_progress)
851 argv_array_push(&cmd.args, "-q");
852 args->check_self_contained_and_connected = 0;
856 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
857 ntohl(header.hdr_version),
858 ntohl(header.hdr_entries));
859 if (fetch_fsck_objects >= 0
861 : transfer_fsck_objects >= 0
862 ? transfer_fsck_objects
864 if (args->from_promisor)
866 * We cannot use --strict in index-pack because it
867 * checks both broken objects and links, but we only
868 * want to check for broken objects.
870 argv_array_push(&cmd.args, "--fsck-objects");
872 argv_array_pushf(&cmd.args, "--strict%s",
878 if (start_command(&cmd))
879 die(_("fetch-pack: unable to fork off %s"), cmd_name);
880 if (do_keep && pack_lockfile) {
881 *pack_lockfile = index_pack_lockfile(cmd.out);
886 /* Closed by start_command() */
889 ret = finish_command(&cmd);
890 if (!ret || (args->check_self_contained_and_connected && ret == 1))
891 args->self_contained_and_connected =
892 args->check_self_contained_and_connected &&
895 die(_("%s failed"), cmd_name);
896 if (use_sideband && finish_async(&demux))
897 die(_("error in sideband demultiplexer"));
901 static int cmp_ref_by_name(const void *a_, const void *b_)
903 const struct ref *a = *((const struct ref **)a_);
904 const struct ref *b = *((const struct ref **)b_);
905 return strcmp(a->name, b->name);
908 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
910 const struct ref *orig_ref,
911 struct ref **sought, int nr_sought,
912 struct shallow_info *si,
913 char **pack_lockfile)
915 struct ref *ref = copy_ref_list(orig_ref);
916 struct object_id oid;
917 const char *agent_feature;
919 struct fetch_negotiator negotiator;
920 fetch_negotiator_init(&negotiator, negotiation_algorithm);
922 sort_ref_list(&ref, ref_compare_name);
923 QSORT(sought, nr_sought, cmp_ref_by_name);
925 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
926 die(_("Server does not support shallow clients"));
927 if (args->depth > 0 || args->deepen_since || args->deepen_not)
929 if (server_supports("multi_ack_detailed")) {
930 print_verbose(args, _("Server supports multi_ack_detailed"));
932 if (server_supports("no-done")) {
933 print_verbose(args, _("Server supports no-done"));
934 if (args->stateless_rpc)
938 else if (server_supports("multi_ack")) {
939 print_verbose(args, _("Server supports multi_ack"));
942 if (server_supports("side-band-64k")) {
943 print_verbose(args, _("Server supports side-band-64k"));
946 else if (server_supports("side-band")) {
947 print_verbose(args, _("Server supports side-band"));
950 if (server_supports("allow-tip-sha1-in-want")) {
951 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
952 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
954 if (server_supports("allow-reachable-sha1-in-want")) {
955 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
956 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
958 if (!server_supports("thin-pack"))
959 args->use_thin_pack = 0;
960 if (!server_supports("no-progress"))
961 args->no_progress = 0;
962 if (!server_supports("include-tag"))
963 args->include_tag = 0;
964 if (server_supports("ofs-delta"))
965 print_verbose(args, _("Server supports ofs-delta"));
967 prefer_ofs_delta = 0;
969 if (server_supports("filter")) {
970 server_supports_filtering = 1;
971 print_verbose(args, _("Server supports filter"));
972 } else if (args->filter_options.choice) {
973 warning("filtering not recognized by server, ignoring");
976 if ((agent_feature = server_feature_value("agent", &agent_len))) {
979 print_verbose(args, _("Server version is %.*s"),
980 agent_len, agent_feature);
982 if (server_supports("deepen-since"))
984 else if (args->deepen_since)
985 die(_("Server does not support --shallow-since"));
986 if (server_supports("deepen-not"))
988 else if (args->deepen_not)
989 die(_("Server does not support --shallow-exclude"));
990 if (!server_supports("deepen-relative") && args->deepen_relative)
991 die(_("Server does not support --deepen"));
993 mark_complete_and_common_ref(&negotiator, args, &ref);
994 filter_refs(args, &ref, sought, nr_sought);
995 if (everything_local(args, &ref)) {
999 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
1000 if (!args->keep_pack)
1001 /* When cloning, it is not unusual to have
1004 warning(_("no common commits"));
1006 if (args->stateless_rpc)
1007 packet_flush(fd[1]);
1009 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1011 else if (si->nr_ours || si->nr_theirs)
1012 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1014 alternate_shallow_file = NULL;
1015 if (get_pack(args, fd, pack_lockfile))
1016 die(_("git fetch-pack: fetch failed."));
1019 negotiator.release(&negotiator);
1023 static void add_shallow_requests(struct strbuf *req_buf,
1024 const struct fetch_pack_args *args)
1026 if (is_repository_shallow(the_repository))
1027 write_shallow_commits(req_buf, 1, NULL);
1028 if (args->depth > 0)
1029 packet_buf_write(req_buf, "deepen %d", args->depth);
1030 if (args->deepen_since) {
1031 timestamp_t max_age = approxidate(args->deepen_since);
1032 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1034 if (args->deepen_not) {
1036 for (i = 0; i < args->deepen_not->nr; i++) {
1037 struct string_list_item *s = args->deepen_not->items + i;
1038 packet_buf_write(req_buf, "deepen-not %s", s->string);
1043 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1045 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1047 for ( ; wants ; wants = wants->next) {
1048 const struct object_id *remote = &wants->old_oid;
1052 * If that object is complete (i.e. it is an ancestor of a
1053 * local ref), we tell them we have it but do not have to
1054 * tell them about its ancestors, which they already know
1057 * We use lookup_object here because we are only
1058 * interested in the case we *know* the object is
1059 * reachable and we have already scanned it.
1061 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1062 (o->flags & COMPLETE)) {
1066 if (!use_ref_in_want || wants->exact_oid)
1067 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1069 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1073 static void add_common(struct strbuf *req_buf, struct oidset *common)
1075 struct oidset_iter iter;
1076 const struct object_id *oid;
1077 oidset_iter_init(common, &iter);
1079 while ((oid = oidset_iter_next(&iter))) {
1080 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1084 static int add_haves(struct fetch_negotiator *negotiator,
1085 struct strbuf *req_buf,
1086 int *haves_to_send, int *in_vain)
1089 int haves_added = 0;
1090 const struct object_id *oid;
1092 while ((oid = negotiator->next(negotiator))) {
1093 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1094 if (++haves_added >= *haves_to_send)
1098 *in_vain += haves_added;
1099 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1101 packet_buf_write(req_buf, "done\n");
1105 /* Increase haves to send on next round */
1106 *haves_to_send = next_flush(1, *haves_to_send);
1111 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1112 const struct fetch_pack_args *args,
1113 const struct ref *wants, struct oidset *common,
1114 int *haves_to_send, int *in_vain)
1117 struct strbuf req_buf = STRBUF_INIT;
1119 if (server_supports_v2("fetch", 1))
1120 packet_buf_write(&req_buf, "command=fetch");
1121 if (server_supports_v2("agent", 0))
1122 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1123 if (args->server_options && args->server_options->nr &&
1124 server_supports_v2("server-option", 1)) {
1126 for (i = 0; i < args->server_options->nr; i++)
1127 packet_write_fmt(fd_out, "server-option=%s",
1128 args->server_options->items[i].string);
1131 packet_buf_delim(&req_buf);
1132 if (args->use_thin_pack)
1133 packet_buf_write(&req_buf, "thin-pack");
1134 if (args->no_progress)
1135 packet_buf_write(&req_buf, "no-progress");
1136 if (args->include_tag)
1137 packet_buf_write(&req_buf, "include-tag");
1138 if (prefer_ofs_delta)
1139 packet_buf_write(&req_buf, "ofs-delta");
1141 /* Add shallow-info and deepen request */
1142 if (server_supports_feature("fetch", "shallow", 0))
1143 add_shallow_requests(&req_buf, args);
1144 else if (is_repository_shallow(the_repository) || args->deepen)
1145 die(_("Server does not support shallow requests"));
1148 if (server_supports_feature("fetch", "filter", 0) &&
1149 args->filter_options.choice) {
1150 print_verbose(args, _("Server supports filter"));
1151 packet_buf_write(&req_buf, "filter %s",
1152 args->filter_options.filter_spec);
1153 } else if (args->filter_options.choice) {
1154 warning("filtering not recognized by server, ignoring");
1158 add_wants(wants, &req_buf);
1160 if (args->no_dependents) {
1161 packet_buf_write(&req_buf, "done");
1164 /* Add all of the common commits we've found in previous rounds */
1165 add_common(&req_buf, common);
1167 /* Add initial haves */
1168 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1172 packet_buf_flush(&req_buf);
1173 write_or_die(fd_out, req_buf.buf, req_buf.len);
1175 strbuf_release(&req_buf);
1180 * Processes a section header in a server's response and checks if it matches
1181 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1182 * not consumed); if 0, the line will be consumed and the function will die if
1183 * the section header doesn't match what was expected.
1185 static int process_section_header(struct packet_reader *reader,
1186 const char *section, int peek)
1190 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1191 die(_("error reading section header '%s'"), section);
1193 ret = !strcmp(reader->line, section);
1197 die(_("expected '%s', received '%s'"),
1198 section, reader->line);
1199 packet_reader_read(reader);
1205 static int process_acks(struct fetch_negotiator *negotiator,
1206 struct packet_reader *reader,
1207 struct oidset *common)
1210 int received_ready = 0;
1211 int received_ack = 0;
1213 process_section_header(reader, "acknowledgments", 0);
1214 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1217 if (!strcmp(reader->line, "NAK"))
1220 if (skip_prefix(reader->line, "ACK ", &arg)) {
1221 struct object_id oid;
1222 if (!get_oid_hex(arg, &oid)) {
1223 struct commit *commit;
1224 oidset_insert(common, &oid);
1225 commit = lookup_commit(the_repository, &oid);
1226 negotiator->ack(negotiator, commit);
1231 if (!strcmp(reader->line, "ready")) {
1236 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1239 if (reader->status != PACKET_READ_FLUSH &&
1240 reader->status != PACKET_READ_DELIM)
1241 die(_("error processing acks: %d"), reader->status);
1243 /* return 0 if no common, 1 if there are common, or 2 if ready */
1244 return received_ready ? 2 : (received_ack ? 1 : 0);
1247 static void receive_shallow_info(struct fetch_pack_args *args,
1248 struct packet_reader *reader)
1250 process_section_header(reader, "shallow-info", 0);
1251 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1253 struct object_id oid;
1255 if (skip_prefix(reader->line, "shallow ", &arg)) {
1256 if (get_oid_hex(arg, &oid))
1257 die(_("invalid shallow line: %s"), reader->line);
1258 register_shallow(the_repository, &oid);
1261 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1262 if (get_oid_hex(arg, &oid))
1263 die(_("invalid unshallow line: %s"), reader->line);
1264 if (!lookup_object(the_repository, oid.hash))
1265 die(_("object not found: %s"), reader->line);
1266 /* make sure that it is parsed as shallow */
1267 if (!parse_object(the_repository, &oid))
1268 die(_("error in object: %s"), reader->line);
1269 if (unregister_shallow(&oid))
1270 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 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1284 static void receive_wanted_refs(struct packet_reader *reader,
1285 struct ref **sought, int nr_sought)
1287 process_section_header(reader, "wanted-refs", 0);
1288 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1289 struct object_id oid;
1293 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1294 die(_("expected wanted-ref, got '%s'"), reader->line);
1296 for (i = 0; i < nr_sought; i++) {
1297 if (!strcmp(end, sought[i]->name)) {
1298 oidcpy(&sought[i]->old_oid, &oid);
1304 die(_("unexpected wanted-ref: '%s'"), reader->line);
1307 if (reader->status != PACKET_READ_DELIM)
1308 die(_("error processing wanted refs: %d"), reader->status);
1312 FETCH_CHECK_LOCAL = 0,
1319 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1321 const struct ref *orig_ref,
1322 struct ref **sought, int nr_sought,
1323 char **pack_lockfile)
1325 struct ref *ref = copy_ref_list(orig_ref);
1326 enum fetch_state state = FETCH_CHECK_LOCAL;
1327 struct oidset common = OIDSET_INIT;
1328 struct packet_reader reader;
1330 int haves_to_send = INITIAL_FLUSH;
1331 struct fetch_negotiator negotiator;
1332 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1333 packet_reader_init(&reader, fd[0], NULL, 0,
1334 PACKET_READ_CHOMP_NEWLINE);
1336 while (state != FETCH_DONE) {
1338 case FETCH_CHECK_LOCAL:
1339 sort_ref_list(&ref, ref_compare_name);
1340 QSORT(sought, nr_sought, cmp_ref_by_name);
1342 /* v2 supports these by default */
1343 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1345 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1348 /* Filter 'ref' by 'sought' and those that aren't local */
1349 mark_complete_and_common_ref(&negotiator, args, &ref);
1350 filter_refs(args, &ref, sought, nr_sought);
1351 if (everything_local(args, &ref))
1354 state = FETCH_SEND_REQUEST;
1356 mark_tips(&negotiator, args->negotiation_tips);
1357 for_each_cached_alternate(&negotiator,
1358 insert_one_alternate_object);
1360 case FETCH_SEND_REQUEST:
1361 if (send_fetch_request(&negotiator, fd[1], args, ref,
1363 &haves_to_send, &in_vain))
1364 state = FETCH_GET_PACK;
1366 state = FETCH_PROCESS_ACKS;
1368 case FETCH_PROCESS_ACKS:
1369 /* Process ACKs/NAKs */
1370 switch (process_acks(&negotiator, &reader, &common)) {
1372 state = FETCH_GET_PACK;
1378 state = FETCH_SEND_REQUEST;
1382 case FETCH_GET_PACK:
1383 /* Check for shallow-info section */
1384 if (process_section_header(&reader, "shallow-info", 1))
1385 receive_shallow_info(args, &reader);
1387 if (process_section_header(&reader, "wanted-refs", 1))
1388 receive_wanted_refs(&reader, sought, nr_sought);
1391 process_section_header(&reader, "packfile", 0);
1392 if (get_pack(args, fd, pack_lockfile))
1393 die(_("git fetch-pack: fetch failed."));
1402 negotiator.release(&negotiator);
1403 oidset_clear(&common);
1407 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1409 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1412 if (git_config_pathname(&path, var, value))
1414 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1415 fsck_msg_types.len ? ',' : '=', path);
1420 if (skip_prefix(var, "fetch.fsck.", &var)) {
1421 if (is_valid_msg_type(var, value))
1422 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1423 fsck_msg_types.len ? ',' : '=', var, value);
1425 warning("Skipping unknown msg id '%s'", var);
1429 return git_default_config(var, value, cb);
1432 static void fetch_pack_config(void)
1434 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1435 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1436 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1437 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1438 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1439 git_config_get_string("fetch.negotiationalgorithm",
1440 &negotiation_algorithm);
1442 git_config(fetch_pack_config_cb, NULL);
1445 static void fetch_pack_setup(void)
1447 static int did_setup;
1450 fetch_pack_config();
1451 if (0 <= transfer_unpack_limit)
1452 unpack_limit = transfer_unpack_limit;
1453 else if (0 <= fetch_unpack_limit)
1454 unpack_limit = fetch_unpack_limit;
1458 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1460 struct string_list names = STRING_LIST_INIT_NODUP;
1463 for (src = dst = 0; src < nr; src++) {
1464 struct string_list_item *item;
1465 item = string_list_insert(&names, ref[src]->name);
1467 continue; /* already have it */
1468 item->util = ref[src];
1470 ref[dst] = ref[src];
1473 for (src = dst; src < nr; src++)
1475 string_list_clear(&names, 0);
1479 static void update_shallow(struct fetch_pack_args *args,
1480 struct ref **sought, int nr_sought,
1481 struct shallow_info *si)
1483 struct oid_array ref = OID_ARRAY_INIT;
1487 if (args->deepen && alternate_shallow_file) {
1488 if (*alternate_shallow_file == '\0') { /* --unshallow */
1489 unlink_or_warn(git_path_shallow(the_repository));
1490 rollback_lock_file(&shallow_lock);
1492 commit_lock_file(&shallow_lock);
1496 if (!si->shallow || !si->shallow->nr)
1499 if (args->cloning) {
1501 * remote is shallow, but this is a clone, there are
1502 * no objects in repo to worry about. Accept any
1503 * shallow points that exist in the pack (iow in repo
1504 * after get_pack() and reprepare_packed_git())
1506 struct oid_array extra = OID_ARRAY_INIT;
1507 struct object_id *oid = si->shallow->oid;
1508 for (i = 0; i < si->shallow->nr; i++)
1509 if (has_object_file(&oid[i]))
1510 oid_array_append(&extra, &oid[i]);
1512 setup_alternate_shallow(&shallow_lock,
1513 &alternate_shallow_file,
1515 commit_lock_file(&shallow_lock);
1517 oid_array_clear(&extra);
1521 if (!si->nr_ours && !si->nr_theirs)
1524 remove_nonexistent_theirs_shallow(si);
1525 if (!si->nr_ours && !si->nr_theirs)
1527 for (i = 0; i < nr_sought; i++)
1528 oid_array_append(&ref, &sought[i]->old_oid);
1531 if (args->update_shallow) {
1533 * remote is also shallow, .git/shallow may be updated
1534 * so all refs can be accepted. Make sure we only add
1535 * shallow roots that are actually reachable from new
1538 struct oid_array extra = OID_ARRAY_INIT;
1539 struct object_id *oid = si->shallow->oid;
1540 assign_shallow_commits_to_refs(si, NULL, NULL);
1541 if (!si->nr_ours && !si->nr_theirs) {
1542 oid_array_clear(&ref);
1545 for (i = 0; i < si->nr_ours; i++)
1546 oid_array_append(&extra, &oid[si->ours[i]]);
1547 for (i = 0; i < si->nr_theirs; i++)
1548 oid_array_append(&extra, &oid[si->theirs[i]]);
1549 setup_alternate_shallow(&shallow_lock,
1550 &alternate_shallow_file,
1552 commit_lock_file(&shallow_lock);
1553 oid_array_clear(&extra);
1554 oid_array_clear(&ref);
1559 * remote is also shallow, check what ref is safe to update
1560 * without updating .git/shallow
1562 status = xcalloc(nr_sought, sizeof(*status));
1563 assign_shallow_commits_to_refs(si, NULL, status);
1564 if (si->nr_ours || si->nr_theirs) {
1565 for (i = 0; i < nr_sought; i++)
1567 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1570 oid_array_clear(&ref);
1573 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1575 struct ref **rm = cb_data;
1576 struct ref *ref = *rm;
1579 return -1; /* end of the list */
1581 oidcpy(oid, &ref->old_oid);
1585 struct ref *fetch_pack(struct fetch_pack_args *args,
1586 int fd[], struct child_process *conn,
1587 const struct ref *ref,
1589 struct ref **sought, int nr_sought,
1590 struct oid_array *shallow,
1591 char **pack_lockfile,
1592 enum protocol_version version)
1594 struct ref *ref_cpy;
1595 struct shallow_info si;
1599 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1602 packet_flush(fd[1]);
1603 die(_("no matching remote head"));
1605 prepare_shallow_info(&si, shallow);
1606 if (version == protocol_v2)
1607 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1610 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1611 &si, pack_lockfile);
1612 reprepare_packed_git(the_repository);
1614 if (!args->cloning && args->deepen) {
1615 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1616 struct ref *iterator = ref_cpy;
1617 opt.shallow_file = alternate_shallow_file;
1619 opt.is_deepening_fetch = 1;
1620 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1621 error(_("remote did not send all necessary objects"));
1624 rollback_lock_file(&shallow_lock);
1627 args->connectivity_checked = 1;
1630 update_shallow(args, sought, nr_sought, &si);
1632 clear_shallow_info(&si);
1636 int report_unmatched_refs(struct ref **sought, int nr_sought)
1640 for (i = 0; i < nr_sought; i++) {
1643 switch (sought[i]->match_status) {
1646 case REF_NOT_MATCHED:
1647 error(_("no such remote ref %s"), sought[i]->name);
1649 case REF_UNADVERTISED_NOT_ALLOWED:
1650 error(_("Server does not allow request for unadvertised object %s"),