2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "oid-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
27 static int transfer_unpack_limit = -1;
28 static int fetch_unpack_limit = -1;
29 static int unpack_limit = 100;
30 static int prefer_ofs_delta = 1;
32 static int deepen_since_ok;
33 static int deepen_not_ok;
34 static int fetch_fsck_objects = -1;
35 static int transfer_fsck_objects = -1;
36 static int agent_supported;
37 static int server_supports_filtering;
38 static int advertise_sid;
39 static struct shallow_lock shallow_lock;
40 static const char *alternate_shallow_file;
41 static struct strbuf fsck_msg_types = STRBUF_INIT;
42 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
44 /* Remember to update object flag allocation in object.h */
45 #define COMPLETE (1U << 0)
46 #define ALTERNATE (1U << 1)
49 * After sending this many "have"s if we do not get any new ACK , we
50 * give up traversing our history.
52 #define MAX_IN_VAIN 256
54 static int multi_ack, use_sideband;
55 /* Allow specifying sha1 if it is a ref tip. */
56 #define ALLOW_TIP_SHA1 01
57 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
58 #define ALLOW_REACHABLE_SHA1 02
59 static unsigned int allow_unadvertised_object_request;
61 __attribute__((format (printf, 2, 3)))
62 static inline void print_verbose(const struct fetch_pack_args *args,
70 va_start(params, fmt);
71 vfprintf(stderr, fmt, params);
76 struct alternate_object_cache {
77 struct object **items;
81 static void cache_one_alternate(const struct object_id *oid,
84 struct alternate_object_cache *cache = vcache;
85 struct object *obj = parse_object(the_repository, oid);
87 if (!obj || (obj->flags & ALTERNATE))
90 obj->flags |= ALTERNATE;
91 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
92 cache->items[cache->nr++] = obj;
95 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
96 void (*cb)(struct fetch_negotiator *,
99 static int initialized;
100 static struct alternate_object_cache cache;
104 for_each_alternate_ref(cache_one_alternate, &cache);
108 for (i = 0; i < cache.nr; i++)
109 cb(negotiator, cache.items[i]);
112 static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
113 int mark_tags_complete)
115 enum object_type type;
116 struct object_info info = { .typep = &type };
119 if (oid_object_info_extended(the_repository, oid, &info,
120 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
122 if (type == OBJ_TAG) {
123 struct tag *tag = (struct tag *)
124 parse_object(the_repository, oid);
128 if (mark_tags_complete)
129 tag->object.flags |= COMPLETE;
130 oid = &tag->tagged->oid;
135 if (type == OBJ_COMMIT)
136 return (struct commit *) parse_object(the_repository, oid);
140 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
141 const struct object_id *oid)
143 struct commit *c = deref_without_lazy_fetch(oid, 0);
146 negotiator->add_tip(negotiator, c);
150 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
151 int flag, void *cb_data)
153 return rev_list_insert_ref(cb_data, oid);
164 static void consume_shallow_list(struct fetch_pack_args *args,
165 struct packet_reader *reader)
167 if (args->stateless_rpc && args->deepen) {
168 /* If we sent a depth we will get back "duplicate"
169 * shallow and unshallow commands every time there
170 * is a block of have lines exchanged.
172 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
173 if (starts_with(reader->line, "shallow "))
175 if (starts_with(reader->line, "unshallow "))
177 die(_("git fetch-pack: expected shallow list"));
179 if (reader->status != PACKET_READ_FLUSH)
180 die(_("git fetch-pack: expected a flush packet after shallow list"));
184 static enum ack_type get_ack(struct packet_reader *reader,
185 struct object_id *result_oid)
190 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
191 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
192 len = reader->pktlen;
194 if (!strcmp(reader->line, "NAK"))
196 if (skip_prefix(reader->line, "ACK ", &arg)) {
198 if (!parse_oid_hex(arg, result_oid, &p)) {
199 len -= p - reader->line;
202 if (strstr(p, "continue"))
204 if (strstr(p, "common"))
206 if (strstr(p, "ready"))
211 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
214 static void send_request(struct fetch_pack_args *args,
215 int fd, struct strbuf *buf)
217 if (args->stateless_rpc) {
218 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
221 if (write_in_full(fd, buf->buf, buf->len) < 0)
222 die_errno(_("unable to write to remote"));
226 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
229 rev_list_insert_ref(negotiator, &obj->oid);
232 #define INITIAL_FLUSH 16
233 #define PIPESAFE_FLUSH 32
234 #define LARGE_FLUSH 16384
236 static int next_flush(int stateless_rpc, int count)
239 if (count < LARGE_FLUSH)
242 count = count * 11 / 10;
244 if (count < PIPESAFE_FLUSH)
247 count += PIPESAFE_FLUSH;
252 static void mark_tips(struct fetch_negotiator *negotiator,
253 const struct oid_array *negotiation_tips)
257 if (!negotiation_tips) {
258 for_each_rawref(rev_list_insert_ref_oid, negotiator);
262 for (i = 0; i < negotiation_tips->nr; i++)
263 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
267 static int find_common(struct fetch_negotiator *negotiator,
268 struct fetch_pack_args *args,
269 int fd[2], struct object_id *result_oid,
273 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
274 const struct object_id *oid;
275 unsigned in_vain = 0;
276 int got_continue = 0;
278 struct strbuf req_buf = STRBUF_INIT;
279 size_t state_len = 0;
280 struct packet_reader reader;
282 if (args->stateless_rpc && multi_ack == 1)
283 die(_("--stateless-rpc requires multi_ack_detailed"));
285 packet_reader_init(&reader, fd[0], NULL, 0,
286 PACKET_READ_CHOMP_NEWLINE |
287 PACKET_READ_DIE_ON_ERR_PACKET);
289 mark_tips(negotiator, args->negotiation_tips);
290 for_each_cached_alternate(negotiator, insert_one_alternate_object);
293 for ( ; refs ; refs = refs->next) {
294 struct object_id *remote = &refs->old_oid;
295 const char *remote_hex;
299 * If that object is complete (i.e. it is an ancestor of a
300 * local ref), we tell them we have it but do not have to
301 * tell them about its ancestors, which they already know
304 * We use lookup_object here because we are only
305 * interested in the case we *know* the object is
306 * reachable and we have already scanned it.
308 if (((o = lookup_object(the_repository, remote)) != NULL) &&
309 (o->flags & COMPLETE)) {
313 remote_hex = oid_to_hex(remote);
315 struct strbuf c = STRBUF_INIT;
316 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
317 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
318 if (no_done) strbuf_addstr(&c, " no-done");
319 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
320 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
321 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
322 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
323 if (args->no_progress) strbuf_addstr(&c, " no-progress");
324 if (args->include_tag) strbuf_addstr(&c, " include-tag");
325 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
326 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
327 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
328 if (agent_supported) strbuf_addf(&c, " agent=%s",
329 git_user_agent_sanitized());
331 strbuf_addf(&c, " session-id=%s", trace2_session_id());
332 if (args->filter_options.choice)
333 strbuf_addstr(&c, " filter");
334 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
337 packet_buf_write(&req_buf, "want %s\n", remote_hex);
342 strbuf_release(&req_buf);
347 if (is_repository_shallow(the_repository))
348 write_shallow_commits(&req_buf, 1, NULL);
350 packet_buf_write(&req_buf, "deepen %d", args->depth);
351 if (args->deepen_since) {
352 timestamp_t max_age = approxidate(args->deepen_since);
353 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
355 if (args->deepen_not) {
357 for (i = 0; i < args->deepen_not->nr; i++) {
358 struct string_list_item *s = args->deepen_not->items + i;
359 packet_buf_write(&req_buf, "deepen-not %s", s->string);
362 if (server_supports_filtering && args->filter_options.choice) {
364 expand_list_objects_filter_spec(&args->filter_options);
365 packet_buf_write(&req_buf, "filter %s", spec);
367 packet_buf_flush(&req_buf);
368 state_len = req_buf.len;
372 struct object_id oid;
374 send_request(args, fd[1], &req_buf);
375 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
376 if (skip_prefix(reader.line, "shallow ", &arg)) {
377 if (get_oid_hex(arg, &oid))
378 die(_("invalid shallow line: %s"), reader.line);
379 register_shallow(the_repository, &oid);
382 if (skip_prefix(reader.line, "unshallow ", &arg)) {
383 if (get_oid_hex(arg, &oid))
384 die(_("invalid unshallow line: %s"), reader.line);
385 if (!lookup_object(the_repository, &oid))
386 die(_("object not found: %s"), reader.line);
387 /* make sure that it is parsed as shallow */
388 if (!parse_object(the_repository, &oid))
389 die(_("error in object: %s"), reader.line);
390 if (unregister_shallow(&oid))
391 die(_("no shallow found: %s"), reader.line);
394 die(_("expected shallow/unshallow, got %s"), reader.line);
396 } else if (!args->stateless_rpc)
397 send_request(args, fd[1], &req_buf);
399 if (!args->stateless_rpc) {
400 /* If we aren't using the stateless-rpc interface
401 * we don't need to retain the headers.
403 strbuf_setlen(&req_buf, 0);
407 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
410 while ((oid = negotiator->next(negotiator))) {
411 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
412 print_verbose(args, "have %s", oid_to_hex(oid));
414 if (flush_at <= ++count) {
417 packet_buf_flush(&req_buf);
418 send_request(args, fd[1], &req_buf);
419 strbuf_setlen(&req_buf, state_len);
421 flush_at = next_flush(args->stateless_rpc, count);
424 * We keep one window "ahead" of the other side, and
425 * will wait for an ACK only on the next one
427 if (!args->stateless_rpc && count == INITIAL_FLUSH)
430 consume_shallow_list(args, &reader);
432 ack = get_ack(&reader, result_oid);
434 print_verbose(args, _("got %s %d %s"), "ack",
435 ack, oid_to_hex(result_oid));
445 struct commit *commit =
446 lookup_commit(the_repository,
451 die(_("invalid commit %s"), oid_to_hex(result_oid));
452 was_common = negotiator->ack(negotiator, commit);
453 if (args->stateless_rpc
456 /* We need to replay the have for this object
457 * on the next RPC request so the peer knows
458 * it is in common with us.
460 const char *hex = oid_to_hex(result_oid);
461 packet_buf_write(&req_buf, "have %s\n", hex);
462 state_len = req_buf.len;
464 * Reset in_vain because an ack
465 * for this commit has not been
469 } else if (!args->stateless_rpc
470 || ack != ACK_common)
474 if (ack == ACK_ready)
481 if (got_continue && MAX_IN_VAIN < in_vain) {
482 print_verbose(args, _("giving up"));
490 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
491 if (!got_ready || !no_done) {
492 packet_buf_write(&req_buf, "done\n");
493 send_request(args, fd[1], &req_buf);
495 print_verbose(args, _("done"));
500 strbuf_release(&req_buf);
502 if (!got_ready || !no_done)
503 consume_shallow_list(args, &reader);
504 while (flushes || multi_ack) {
505 int ack = get_ack(&reader, result_oid);
507 print_verbose(args, _("got %s (%d) %s"), "ack",
508 ack, oid_to_hex(result_oid));
516 /* it is no error to fetch into a completely empty repo */
517 return count ? retval : 0;
520 static struct commit_list *complete;
522 static int mark_complete(const struct object_id *oid)
524 struct commit *commit = deref_without_lazy_fetch(oid, 1);
526 if (commit && !(commit->object.flags & COMPLETE)) {
527 commit->object.flags |= COMPLETE;
528 commit_list_insert(commit, &complete);
533 static int mark_complete_oid(const char *refname, const struct object_id *oid,
534 int flag, void *cb_data)
536 return mark_complete(oid);
539 static void mark_recent_complete_commits(struct fetch_pack_args *args,
542 while (complete && cutoff <= complete->item->date) {
543 print_verbose(args, _("Marking %s as complete"),
544 oid_to_hex(&complete->item->object.oid));
545 pop_most_recent_commit(&complete, COMPLETE);
549 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
551 for (; refs; refs = refs->next)
552 oidset_insert(oids, &refs->old_oid);
555 static int is_unmatched_ref(const struct ref *ref)
557 struct object_id oid;
559 return ref->match_status == REF_NOT_MATCHED &&
560 !parse_oid_hex(ref->name, &oid, &p) &&
562 oideq(&oid, &ref->old_oid);
565 static void filter_refs(struct fetch_pack_args *args,
567 struct ref **sought, int nr_sought)
569 struct ref *newlist = NULL;
570 struct ref **newtail = &newlist;
571 struct ref *unmatched = NULL;
572 struct ref *ref, *next;
573 struct oidset tip_oids = OIDSET_INIT;
575 int strict = !(allow_unadvertised_object_request &
576 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
579 for (ref = *refs; ref; ref = next) {
583 if (starts_with(ref->name, "refs/") &&
584 check_refname_format(ref->name, 0)) {
586 * trash or a peeled value; do not even add it to
592 while (i < nr_sought) {
593 int cmp = strcmp(ref->name, sought[i]->name);
595 break; /* definitely do not have it */
597 keep = 1; /* definitely have it */
598 sought[i]->match_status = REF_MATCHED;
603 if (!keep && args->fetch_all &&
604 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
611 newtail = &ref->next;
613 ref->next = unmatched;
619 for (i = 0; i < nr_sought; i++) {
621 if (!is_unmatched_ref(ref))
624 add_refs_to_oidset(&tip_oids, unmatched);
625 add_refs_to_oidset(&tip_oids, newlist);
630 /* Append unmatched requests to the list */
631 for (i = 0; i < nr_sought; i++) {
633 if (!is_unmatched_ref(ref))
636 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
637 ref->match_status = REF_MATCHED;
638 *newtail = copy_ref(ref);
639 newtail = &(*newtail)->next;
641 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
645 oidset_clear(&tip_oids);
646 free_refs(unmatched);
651 static void mark_alternate_complete(struct fetch_negotiator *unused,
654 mark_complete(&obj->oid);
657 struct loose_object_iter {
658 struct oidset *loose_object_set;
663 * Mark recent commits available locally and reachable from a local ref as
666 * The cutoff time for recency is determined by this heuristic: it is the
667 * earliest commit time of the objects in refs that are commits and that we know
668 * the commit time of.
670 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
671 struct fetch_pack_args *args,
675 int old_save_commit_buffer = save_commit_buffer;
676 timestamp_t cutoff = 0;
678 save_commit_buffer = 0;
680 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
681 for (ref = *refs; ref; ref = ref->next) {
684 if (!has_object_file_with_flags(&ref->old_oid,
686 OBJECT_INFO_SKIP_FETCH_OBJECT))
688 o = parse_object(the_repository, &ref->old_oid);
693 * We already have it -- which may mean that we were
694 * in sync with the other side at some time after
695 * that (it is OK if we guess wrong here).
697 if (o->type == OBJ_COMMIT) {
698 struct commit *commit = (struct commit *)o;
699 if (!cutoff || cutoff < commit->date)
700 cutoff = commit->date;
703 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
706 * This block marks all local refs as COMPLETE, and then recursively marks all
707 * parents of those refs as COMPLETE.
709 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
711 for_each_rawref(mark_complete_oid, NULL);
712 for_each_cached_alternate(NULL, mark_alternate_complete);
713 commit_list_sort_by_date(&complete);
715 mark_recent_complete_commits(args, cutoff);
717 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
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 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
724 for (ref = *refs; ref; ref = ref->next) {
725 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
727 if (!c || !(c->object.flags & COMPLETE))
730 negotiator->known_common(negotiator, c);
732 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
734 save_commit_buffer = old_save_commit_buffer;
738 * Returns 1 if every object pointed to by the given remote refs is available
739 * locally and reachable from a local ref, and 0 otherwise.
741 static int everything_local(struct fetch_pack_args *args,
747 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
748 const struct object_id *remote = &ref->old_oid;
751 o = lookup_object(the_repository, remote);
752 if (!o || !(o->flags & COMPLETE)) {
754 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
758 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
765 static int sideband_demux(int in, int out, void *data)
770 ret = recv_sideband("fetch-pack", xd[0], out);
775 static void write_promisor_file(const char *keep_name,
776 struct ref **sought, int nr_sought)
778 struct strbuf promisor_name = STRBUF_INIT;
783 strbuf_addstr(&promisor_name, keep_name);
784 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
785 if (!suffix_stripped)
786 BUG("name of pack lockfile should end with .keep (was '%s')",
788 strbuf_addstr(&promisor_name, ".promisor");
790 output = xfopen(promisor_name.buf, "w");
791 for (i = 0; i < nr_sought; i++)
792 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
796 strbuf_release(&promisor_name);
800 * Pass 1 as "only_packfile" if the pack received is the only pack in this
801 * fetch request (that is, if there were no packfile URIs provided).
803 static int get_pack(struct fetch_pack_args *args,
804 int xd[2], struct string_list *pack_lockfiles,
806 struct ref **sought, int nr_sought)
809 int do_keep = args->keep_pack;
810 const char *cmd_name;
811 struct pack_header header;
813 struct child_process cmd = CHILD_PROCESS_INIT;
816 memset(&demux, 0, sizeof(demux));
818 /* xd[] is talking with upload-pack; subprocess reads from
819 * xd[0], spits out band#2 to stderr, and feeds us band#1
820 * through demux->out.
822 demux.proc = sideband_demux;
825 demux.isolate_sigpipe = 1;
826 if (start_async(&demux))
827 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
832 if (!args->keep_pack && unpack_limit) {
834 if (read_pack_header(demux.out, &header))
835 die(_("protocol error: bad pack header"));
837 if (ntohl(header.hdr_entries) < unpack_limit)
843 if (alternate_shallow_file) {
844 strvec_push(&cmd.args, "--shallow-file");
845 strvec_push(&cmd.args, alternate_shallow_file);
848 if (do_keep || args->from_promisor) {
851 cmd_name = "index-pack";
852 strvec_push(&cmd.args, cmd_name);
853 strvec_push(&cmd.args, "--stdin");
854 if (!args->quiet && !args->no_progress)
855 strvec_push(&cmd.args, "-v");
856 if (args->use_thin_pack)
857 strvec_push(&cmd.args, "--fix-thin");
858 if (do_keep && (args->lock_pack || unpack_limit)) {
859 char hostname[HOST_NAME_MAX + 1];
860 if (xgethostname(hostname, sizeof(hostname)))
861 xsnprintf(hostname, sizeof(hostname), "localhost");
862 strvec_pushf(&cmd.args,
863 "--keep=fetch-pack %"PRIuMAX " on %s",
864 (uintmax_t)getpid(), hostname);
866 if (only_packfile && args->check_self_contained_and_connected)
867 strvec_push(&cmd.args, "--check-self-contained-and-connected");
870 * We cannot perform any connectivity checks because
871 * not all packs have been downloaded; let the caller
872 * have this responsibility.
874 args->check_self_contained_and_connected = 0;
876 if (args->from_promisor)
878 * write_promisor_file() may be called afterwards but
879 * we still need index-pack to know that this is a
880 * promisor pack. For example, if transfer.fsckobjects
881 * is true, index-pack needs to know that .gitmodules
882 * is a promisor object (so that it won't complain if
885 strvec_push(&cmd.args, "--promisor");
888 cmd_name = "unpack-objects";
889 strvec_push(&cmd.args, cmd_name);
890 if (args->quiet || args->no_progress)
891 strvec_push(&cmd.args, "-q");
892 args->check_self_contained_and_connected = 0;
896 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
897 ntohl(header.hdr_version),
898 ntohl(header.hdr_entries));
899 if (fetch_fsck_objects >= 0
901 : transfer_fsck_objects >= 0
902 ? transfer_fsck_objects
904 if (args->from_promisor || !only_packfile)
906 * We cannot use --strict in index-pack because it
907 * checks both broken objects and links, but we only
908 * want to check for broken objects.
910 strvec_push(&cmd.args, "--fsck-objects");
912 strvec_pushf(&cmd.args, "--strict%s",
918 if (start_command(&cmd))
919 die(_("fetch-pack: unable to fork off %s"), cmd_name);
920 if (do_keep && pack_lockfiles) {
921 char *pack_lockfile = index_pack_lockfile(cmd.out);
923 string_list_append_nodup(pack_lockfiles, pack_lockfile);
928 /* Closed by start_command() */
931 ret = finish_command(&cmd);
932 if (!ret || (args->check_self_contained_and_connected && ret == 1))
933 args->self_contained_and_connected =
934 args->check_self_contained_and_connected &&
937 die(_("%s failed"), cmd_name);
938 if (use_sideband && finish_async(&demux))
939 die(_("error in sideband demultiplexer"));
942 * Now that index-pack has succeeded, write the promisor file using the
943 * obtained .keep filename if necessary
945 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
946 write_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
951 static int cmp_ref_by_name(const void *a_, const void *b_)
953 const struct ref *a = *((const struct ref **)a_);
954 const struct ref *b = *((const struct ref **)b_);
955 return strcmp(a->name, b->name);
958 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
960 const struct ref *orig_ref,
961 struct ref **sought, int nr_sought,
962 struct shallow_info *si,
963 struct string_list *pack_lockfiles)
965 struct repository *r = the_repository;
966 struct ref *ref = copy_ref_list(orig_ref);
967 struct object_id oid;
968 const char *agent_feature;
970 struct fetch_negotiator negotiator_alloc;
971 struct fetch_negotiator *negotiator;
973 negotiator = &negotiator_alloc;
974 fetch_negotiator_init(r, negotiator);
976 sort_ref_list(&ref, ref_compare_name);
977 QSORT(sought, nr_sought, cmp_ref_by_name);
979 if ((agent_feature = server_feature_value("agent", &agent_len))) {
982 print_verbose(args, _("Server version is %.*s"),
983 agent_len, agent_feature);
986 if (!server_supports("session-id"))
989 if (server_supports("shallow"))
990 print_verbose(args, _("Server supports %s"), "shallow");
991 else if (args->depth > 0 || is_repository_shallow(r))
992 die(_("Server does not support shallow clients"));
993 if (args->depth > 0 || args->deepen_since || args->deepen_not)
995 if (server_supports("multi_ack_detailed")) {
996 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
998 if (server_supports("no-done")) {
999 print_verbose(args, _("Server supports %s"), "no-done");
1000 if (args->stateless_rpc)
1004 else if (server_supports("multi_ack")) {
1005 print_verbose(args, _("Server supports %s"), "multi_ack");
1008 if (server_supports("side-band-64k")) {
1009 print_verbose(args, _("Server supports %s"), "side-band-64k");
1012 else if (server_supports("side-band")) {
1013 print_verbose(args, _("Server supports %s"), "side-band");
1016 if (server_supports("allow-tip-sha1-in-want")) {
1017 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1018 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1020 if (server_supports("allow-reachable-sha1-in-want")) {
1021 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1022 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1024 if (server_supports("thin-pack"))
1025 print_verbose(args, _("Server supports %s"), "thin-pack");
1027 args->use_thin_pack = 0;
1028 if (server_supports("no-progress"))
1029 print_verbose(args, _("Server supports %s"), "no-progress");
1031 args->no_progress = 0;
1032 if (server_supports("include-tag"))
1033 print_verbose(args, _("Server supports %s"), "include-tag");
1035 args->include_tag = 0;
1036 if (server_supports("ofs-delta"))
1037 print_verbose(args, _("Server supports %s"), "ofs-delta");
1039 prefer_ofs_delta = 0;
1041 if (server_supports("filter")) {
1042 server_supports_filtering = 1;
1043 print_verbose(args, _("Server supports %s"), "filter");
1044 } else if (args->filter_options.choice) {
1045 warning("filtering not recognized by server, ignoring");
1048 if (server_supports("deepen-since")) {
1049 print_verbose(args, _("Server supports %s"), "deepen-since");
1050 deepen_since_ok = 1;
1051 } else if (args->deepen_since)
1052 die(_("Server does not support --shallow-since"));
1053 if (server_supports("deepen-not")) {
1054 print_verbose(args, _("Server supports %s"), "deepen-not");
1056 } else if (args->deepen_not)
1057 die(_("Server does not support --shallow-exclude"));
1058 if (server_supports("deepen-relative"))
1059 print_verbose(args, _("Server supports %s"), "deepen-relative");
1060 else if (args->deepen_relative)
1061 die(_("Server does not support --deepen"));
1062 if (!server_supports_hash(the_hash_algo->name, NULL))
1063 die(_("Server does not support this repository's object format"));
1065 mark_complete_and_common_ref(negotiator, args, &ref);
1066 filter_refs(args, &ref, sought, nr_sought);
1067 if (everything_local(args, &ref)) {
1068 packet_flush(fd[1]);
1071 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1072 if (!args->keep_pack)
1073 /* When cloning, it is not unusual to have
1076 warning(_("no common commits"));
1078 if (args->stateless_rpc)
1079 packet_flush(fd[1]);
1081 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1083 else if (si->nr_ours || si->nr_theirs)
1084 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1086 alternate_shallow_file = NULL;
1087 if (get_pack(args, fd, pack_lockfiles, 1, sought, nr_sought))
1088 die(_("git fetch-pack: fetch failed."));
1092 negotiator->release(negotiator);
1096 static void add_shallow_requests(struct strbuf *req_buf,
1097 const struct fetch_pack_args *args)
1099 if (is_repository_shallow(the_repository))
1100 write_shallow_commits(req_buf, 1, NULL);
1101 if (args->depth > 0)
1102 packet_buf_write(req_buf, "deepen %d", args->depth);
1103 if (args->deepen_since) {
1104 timestamp_t max_age = approxidate(args->deepen_since);
1105 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1107 if (args->deepen_not) {
1109 for (i = 0; i < args->deepen_not->nr; i++) {
1110 struct string_list_item *s = args->deepen_not->items + i;
1111 packet_buf_write(req_buf, "deepen-not %s", s->string);
1114 if (args->deepen_relative)
1115 packet_buf_write(req_buf, "deepen-relative\n");
1118 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1120 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1122 for ( ; wants ; wants = wants->next) {
1123 const struct object_id *remote = &wants->old_oid;
1127 * If that object is complete (i.e. it is an ancestor of a
1128 * local ref), we tell them we have it but do not have to
1129 * tell them about its ancestors, which they already know
1132 * We use lookup_object here because we are only
1133 * interested in the case we *know* the object is
1134 * reachable and we have already scanned it.
1136 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1137 (o->flags & COMPLETE)) {
1141 if (!use_ref_in_want || wants->exact_oid)
1142 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1144 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1148 static void add_common(struct strbuf *req_buf, struct oidset *common)
1150 struct oidset_iter iter;
1151 const struct object_id *oid;
1152 oidset_iter_init(common, &iter);
1154 while ((oid = oidset_iter_next(&iter))) {
1155 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1159 static int add_haves(struct fetch_negotiator *negotiator,
1161 struct strbuf *req_buf,
1162 int *haves_to_send, int *in_vain)
1165 int haves_added = 0;
1166 const struct object_id *oid;
1168 while ((oid = negotiator->next(negotiator))) {
1169 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1170 if (++haves_added >= *haves_to_send)
1174 *in_vain += haves_added;
1175 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1177 packet_buf_write(req_buf, "done\n");
1181 /* Increase haves to send on next round */
1182 *haves_to_send = next_flush(1, *haves_to_send);
1187 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1188 struct fetch_pack_args *args,
1189 const struct ref *wants, struct oidset *common,
1190 int *haves_to_send, int *in_vain,
1191 int sideband_all, int seen_ack)
1194 const char *hash_name;
1195 struct strbuf req_buf = STRBUF_INIT;
1197 if (server_supports_v2("fetch", 1))
1198 packet_buf_write(&req_buf, "command=fetch");
1199 if (server_supports_v2("agent", 0))
1200 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1201 if (advertise_sid && server_supports_v2("session-id", 0))
1202 packet_buf_write(&req_buf, "session-id=%s", trace2_session_id());
1203 if (args->server_options && args->server_options->nr &&
1204 server_supports_v2("server-option", 1)) {
1206 for (i = 0; i < args->server_options->nr; i++)
1207 packet_buf_write(&req_buf, "server-option=%s",
1208 args->server_options->items[i].string);
1211 if (server_feature_v2("object-format", &hash_name)) {
1212 int hash_algo = hash_algo_by_name(hash_name);
1213 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1214 die(_("mismatched algorithms: client %s; server %s"),
1215 the_hash_algo->name, hash_name);
1216 packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
1217 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1218 die(_("the server does not support algorithm '%s'"),
1219 the_hash_algo->name);
1222 packet_buf_delim(&req_buf);
1223 if (args->use_thin_pack)
1224 packet_buf_write(&req_buf, "thin-pack");
1225 if (args->no_progress)
1226 packet_buf_write(&req_buf, "no-progress");
1227 if (args->include_tag)
1228 packet_buf_write(&req_buf, "include-tag");
1229 if (prefer_ofs_delta)
1230 packet_buf_write(&req_buf, "ofs-delta");
1232 packet_buf_write(&req_buf, "sideband-all");
1234 /* Add shallow-info and deepen request */
1235 if (server_supports_feature("fetch", "shallow", 0))
1236 add_shallow_requests(&req_buf, args);
1237 else if (is_repository_shallow(the_repository) || args->deepen)
1238 die(_("Server does not support shallow requests"));
1241 if (server_supports_feature("fetch", "filter", 0) &&
1242 args->filter_options.choice) {
1244 expand_list_objects_filter_spec(&args->filter_options);
1245 print_verbose(args, _("Server supports filter"));
1246 packet_buf_write(&req_buf, "filter %s", spec);
1247 } else if (args->filter_options.choice) {
1248 warning("filtering not recognized by server, ignoring");
1251 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1253 struct strbuf to_send = STRBUF_INIT;
1255 for (i = 0; i < uri_protocols.nr; i++) {
1256 const char *s = uri_protocols.items[i].string;
1258 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1260 strbuf_addch(&to_send, ',');
1261 strbuf_addstr(&to_send, s);
1265 packet_buf_write(&req_buf, "packfile-uris %s",
1267 strbuf_release(&to_send);
1272 add_wants(wants, &req_buf);
1274 /* Add all of the common commits we've found in previous rounds */
1275 add_common(&req_buf, common);
1277 /* Add initial haves */
1278 ret = add_haves(negotiator, seen_ack, &req_buf,
1279 haves_to_send, in_vain);
1282 packet_buf_flush(&req_buf);
1283 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1284 die_errno(_("unable to write request to remote"));
1286 strbuf_release(&req_buf);
1291 * Processes a section header in a server's response and checks if it matches
1292 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1293 * not consumed); if 0, the line will be consumed and the function will die if
1294 * the section header doesn't match what was expected.
1296 static int process_section_header(struct packet_reader *reader,
1297 const char *section, int peek)
1301 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1302 die(_("error reading section header '%s'"), section);
1304 ret = !strcmp(reader->line, section);
1308 die(_("expected '%s', received '%s'"),
1309 section, reader->line);
1310 packet_reader_read(reader);
1318 * No commit was found to be possessed by both the client and the
1319 * server, and "ready" was not received.
1324 * At least one commit was found to be possessed by both the client and
1325 * the server, and "ready" was not received.
1330 * "ready" was received, indicating that the server is ready to send
1331 * the packfile without any further negotiation.
1336 static enum common_found process_acks(struct fetch_negotiator *negotiator,
1337 struct packet_reader *reader,
1338 struct oidset *common)
1341 int received_ready = 0;
1342 int received_ack = 0;
1344 process_section_header(reader, "acknowledgments", 0);
1345 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1348 if (!strcmp(reader->line, "NAK"))
1351 if (skip_prefix(reader->line, "ACK ", &arg)) {
1352 struct object_id oid;
1354 if (!get_oid_hex(arg, &oid)) {
1355 struct commit *commit;
1356 oidset_insert(common, &oid);
1357 commit = lookup_commit(the_repository, &oid);
1359 negotiator->ack(negotiator, commit);
1364 if (!strcmp(reader->line, "ready")) {
1369 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1372 if (reader->status != PACKET_READ_FLUSH &&
1373 reader->status != PACKET_READ_DELIM)
1374 die(_("error processing acks: %d"), reader->status);
1377 * If an "acknowledgments" section is sent, a packfile is sent if and
1378 * only if "ready" was sent in this section. The other sections
1379 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1380 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1383 if (received_ready && reader->status != PACKET_READ_DELIM)
1384 die(_("expected packfile to be sent after 'ready'"));
1385 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1386 die(_("expected no other sections to be sent after no 'ready'"));
1388 return received_ready ? READY :
1389 (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
1392 static void receive_shallow_info(struct fetch_pack_args *args,
1393 struct packet_reader *reader,
1394 struct oid_array *shallows,
1395 struct shallow_info *si)
1397 int unshallow_received = 0;
1399 process_section_header(reader, "shallow-info", 0);
1400 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1402 struct object_id oid;
1404 if (skip_prefix(reader->line, "shallow ", &arg)) {
1405 if (get_oid_hex(arg, &oid))
1406 die(_("invalid shallow line: %s"), reader->line);
1407 oid_array_append(shallows, &oid);
1410 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1411 if (get_oid_hex(arg, &oid))
1412 die(_("invalid unshallow line: %s"), reader->line);
1413 if (!lookup_object(the_repository, &oid))
1414 die(_("object not found: %s"), reader->line);
1415 /* make sure that it is parsed as shallow */
1416 if (!parse_object(the_repository, &oid))
1417 die(_("error in object: %s"), reader->line);
1418 if (unregister_shallow(&oid))
1419 die(_("no shallow found: %s"), reader->line);
1420 unshallow_received = 1;
1423 die(_("expected shallow/unshallow, got %s"), reader->line);
1426 if (reader->status != PACKET_READ_FLUSH &&
1427 reader->status != PACKET_READ_DELIM)
1428 die(_("error processing shallow info: %d"), reader->status);
1430 if (args->deepen || unshallow_received) {
1432 * Treat these as shallow lines caused by our depth settings.
1433 * In v0, these lines cannot cause refs to be rejected; do the
1438 for (i = 0; i < shallows->nr; i++)
1439 register_shallow(the_repository, &shallows->oid[i]);
1440 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1443 } else if (shallows->nr) {
1445 * Treat these as shallow lines caused by the remote being
1446 * shallow. In v0, remote refs that reach these objects are
1447 * rejected (unless --update-shallow is set); do the same.
1449 prepare_shallow_info(si, shallows);
1450 if (si->nr_ours || si->nr_theirs)
1451 alternate_shallow_file =
1452 setup_temporary_shallow(si->shallow);
1454 alternate_shallow_file = NULL;
1456 alternate_shallow_file = NULL;
1460 static int cmp_name_ref(const void *name, const void *ref)
1462 return strcmp(name, (*(struct ref **)ref)->name);
1465 static void receive_wanted_refs(struct packet_reader *reader,
1466 struct ref **sought, int nr_sought)
1468 process_section_header(reader, "wanted-refs", 0);
1469 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1470 struct object_id oid;
1474 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1475 die(_("expected wanted-ref, got '%s'"), reader->line);
1477 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1480 die(_("unexpected wanted-ref: '%s'"), reader->line);
1481 oidcpy(&(*found)->old_oid, &oid);
1484 if (reader->status != PACKET_READ_DELIM)
1485 die(_("error processing wanted refs: %d"), reader->status);
1488 static void receive_packfile_uris(struct packet_reader *reader,
1489 struct string_list *uris)
1491 process_section_header(reader, "packfile-uris", 0);
1492 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1493 if (reader->pktlen < the_hash_algo->hexsz ||
1494 reader->line[the_hash_algo->hexsz] != ' ')
1495 die("expected '<hash> <uri>', got: %s\n", reader->line);
1497 string_list_append(uris, reader->line);
1499 if (reader->status != PACKET_READ_DELIM)
1500 die("expected DELIM");
1504 FETCH_CHECK_LOCAL = 0,
1511 static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1512 struct packet_reader *reader)
1514 check_stateless_delimiter(args->stateless_rpc, reader,
1515 _("git fetch-pack: expected response end packet"));
1518 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1520 const struct ref *orig_ref,
1521 struct ref **sought, int nr_sought,
1522 struct oid_array *shallows,
1523 struct shallow_info *si,
1524 struct string_list *pack_lockfiles)
1526 struct repository *r = the_repository;
1527 struct ref *ref = copy_ref_list(orig_ref);
1528 enum fetch_state state = FETCH_CHECK_LOCAL;
1529 struct oidset common = OIDSET_INIT;
1530 struct packet_reader reader;
1531 int in_vain = 0, negotiation_started = 0;
1532 int haves_to_send = INITIAL_FLUSH;
1533 struct fetch_negotiator negotiator_alloc;
1534 struct fetch_negotiator *negotiator;
1536 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1539 negotiator = &negotiator_alloc;
1540 fetch_negotiator_init(r, negotiator);
1542 packet_reader_init(&reader, fd[0], NULL, 0,
1543 PACKET_READ_CHOMP_NEWLINE |
1544 PACKET_READ_DIE_ON_ERR_PACKET);
1545 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1546 server_supports_feature("fetch", "sideband-all", 0)) {
1547 reader.use_sideband = 1;
1548 reader.me = "fetch-pack";
1551 while (state != FETCH_DONE) {
1553 case FETCH_CHECK_LOCAL:
1554 sort_ref_list(&ref, ref_compare_name);
1555 QSORT(sought, nr_sought, cmp_ref_by_name);
1557 /* v2 supports these by default */
1558 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1560 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1563 /* Filter 'ref' by 'sought' and those that aren't local */
1564 mark_complete_and_common_ref(negotiator, args, &ref);
1565 filter_refs(args, &ref, sought, nr_sought);
1566 if (everything_local(args, &ref))
1569 state = FETCH_SEND_REQUEST;
1571 mark_tips(negotiator, args->negotiation_tips);
1572 for_each_cached_alternate(negotiator,
1573 insert_one_alternate_object);
1575 case FETCH_SEND_REQUEST:
1576 if (!negotiation_started) {
1577 negotiation_started = 1;
1578 trace2_region_enter("fetch-pack",
1582 if (send_fetch_request(negotiator, fd[1], args, ref,
1584 &haves_to_send, &in_vain,
1585 reader.use_sideband,
1587 state = FETCH_GET_PACK;
1589 state = FETCH_PROCESS_ACKS;
1591 case FETCH_PROCESS_ACKS:
1592 /* Process ACKs/NAKs */
1593 switch (process_acks(negotiator, &reader, &common)) {
1596 * Don't check for response delimiter; get_pack() will
1597 * read the rest of this response.
1599 state = FETCH_GET_PACK;
1605 case NO_COMMON_FOUND:
1606 do_check_stateless_delimiter(args, &reader);
1607 state = FETCH_SEND_REQUEST;
1611 case FETCH_GET_PACK:
1612 trace2_region_leave("fetch-pack",
1615 /* Check for shallow-info section */
1616 if (process_section_header(&reader, "shallow-info", 1))
1617 receive_shallow_info(args, &reader, shallows, si);
1619 if (process_section_header(&reader, "wanted-refs", 1))
1620 receive_wanted_refs(&reader, sought, nr_sought);
1622 /* get the pack(s) */
1623 if (process_section_header(&reader, "packfile-uris", 1))
1624 receive_packfile_uris(&reader, &packfile_uris);
1625 process_section_header(&reader, "packfile", 0);
1626 if (get_pack(args, fd, pack_lockfiles,
1627 !packfile_uris.nr, sought, nr_sought))
1628 die(_("git fetch-pack: fetch failed."));
1629 do_check_stateless_delimiter(args, &reader);
1638 for (i = 0; i < packfile_uris.nr; i++) {
1639 struct child_process cmd = CHILD_PROCESS_INIT;
1640 char packname[GIT_MAX_HEXSZ + 1];
1641 const char *uri = packfile_uris.items[i].string +
1642 the_hash_algo->hexsz + 1;
1644 strvec_push(&cmd.args, "http-fetch");
1645 strvec_pushf(&cmd.args, "--packfile=%.*s",
1646 (int) the_hash_algo->hexsz,
1647 packfile_uris.items[i].string);
1648 strvec_push(&cmd.args, uri);
1652 if (start_command(&cmd))
1653 die("fetch-pack: unable to spawn http-fetch");
1655 if (read_in_full(cmd.out, packname, 5) < 0 ||
1656 memcmp(packname, "keep\t", 5))
1657 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1659 if (read_in_full(cmd.out, packname,
1660 the_hash_algo->hexsz + 1) < 0 ||
1661 packname[the_hash_algo->hexsz] != '\n')
1662 die("fetch-pack: expected hash then LF at end of http-fetch output");
1664 packname[the_hash_algo->hexsz] = '\0';
1668 if (finish_command(&cmd))
1669 die("fetch-pack: unable to finish http-fetch");
1671 if (memcmp(packfile_uris.items[i].string, packname,
1672 the_hash_algo->hexsz))
1673 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1674 uri, (int) the_hash_algo->hexsz,
1675 packfile_uris.items[i].string);
1677 string_list_append_nodup(pack_lockfiles,
1678 xstrfmt("%s/pack/pack-%s.keep",
1679 get_object_directory(),
1682 string_list_clear(&packfile_uris, 0);
1685 negotiator->release(negotiator);
1687 oidset_clear(&common);
1691 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1693 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1696 if (git_config_pathname(&path, var, value))
1698 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1699 fsck_msg_types.len ? ',' : '=', path);
1704 if (skip_prefix(var, "fetch.fsck.", &var)) {
1705 if (is_valid_msg_type(var, value))
1706 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1707 fsck_msg_types.len ? ',' : '=', var, value);
1709 warning("Skipping unknown msg id '%s'", var);
1713 return git_default_config(var, value, cb);
1716 static void fetch_pack_config(void)
1718 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1719 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1720 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1721 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1722 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1723 git_config_get_bool("transfer.advertisesid", &advertise_sid);
1724 if (!uri_protocols.nr) {
1727 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1728 string_list_split(&uri_protocols, str, ',', -1);
1733 git_config(fetch_pack_config_cb, NULL);
1736 static void fetch_pack_setup(void)
1738 static int did_setup;
1741 fetch_pack_config();
1742 if (0 <= transfer_unpack_limit)
1743 unpack_limit = transfer_unpack_limit;
1744 else if (0 <= fetch_unpack_limit)
1745 unpack_limit = fetch_unpack_limit;
1749 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1751 struct string_list names = STRING_LIST_INIT_NODUP;
1754 for (src = dst = 0; src < nr; src++) {
1755 struct string_list_item *item;
1756 item = string_list_insert(&names, ref[src]->name);
1758 continue; /* already have it */
1759 item->util = ref[src];
1761 ref[dst] = ref[src];
1764 for (src = dst; src < nr; src++)
1766 string_list_clear(&names, 0);
1770 static void update_shallow(struct fetch_pack_args *args,
1771 struct ref **sought, int nr_sought,
1772 struct shallow_info *si)
1774 struct oid_array ref = OID_ARRAY_INIT;
1778 if (args->deepen && alternate_shallow_file) {
1779 if (*alternate_shallow_file == '\0') { /* --unshallow */
1780 unlink_or_warn(git_path_shallow(the_repository));
1781 rollback_shallow_file(the_repository, &shallow_lock);
1783 commit_shallow_file(the_repository, &shallow_lock);
1784 alternate_shallow_file = NULL;
1788 if (!si->shallow || !si->shallow->nr)
1791 if (args->cloning) {
1793 * remote is shallow, but this is a clone, there are
1794 * no objects in repo to worry about. Accept any
1795 * shallow points that exist in the pack (iow in repo
1796 * after get_pack() and reprepare_packed_git())
1798 struct oid_array extra = OID_ARRAY_INIT;
1799 struct object_id *oid = si->shallow->oid;
1800 for (i = 0; i < si->shallow->nr; i++)
1801 if (has_object_file(&oid[i]))
1802 oid_array_append(&extra, &oid[i]);
1804 setup_alternate_shallow(&shallow_lock,
1805 &alternate_shallow_file,
1807 commit_shallow_file(the_repository, &shallow_lock);
1808 alternate_shallow_file = NULL;
1810 oid_array_clear(&extra);
1814 if (!si->nr_ours && !si->nr_theirs)
1817 remove_nonexistent_theirs_shallow(si);
1818 if (!si->nr_ours && !si->nr_theirs)
1820 for (i = 0; i < nr_sought; i++)
1821 oid_array_append(&ref, &sought[i]->old_oid);
1824 if (args->update_shallow) {
1826 * remote is also shallow, .git/shallow may be updated
1827 * so all refs can be accepted. Make sure we only add
1828 * shallow roots that are actually reachable from new
1831 struct oid_array extra = OID_ARRAY_INIT;
1832 struct object_id *oid = si->shallow->oid;
1833 assign_shallow_commits_to_refs(si, NULL, NULL);
1834 if (!si->nr_ours && !si->nr_theirs) {
1835 oid_array_clear(&ref);
1838 for (i = 0; i < si->nr_ours; i++)
1839 oid_array_append(&extra, &oid[si->ours[i]]);
1840 for (i = 0; i < si->nr_theirs; i++)
1841 oid_array_append(&extra, &oid[si->theirs[i]]);
1842 setup_alternate_shallow(&shallow_lock,
1843 &alternate_shallow_file,
1845 commit_shallow_file(the_repository, &shallow_lock);
1846 oid_array_clear(&extra);
1847 oid_array_clear(&ref);
1848 alternate_shallow_file = NULL;
1853 * remote is also shallow, check what ref is safe to update
1854 * without updating .git/shallow
1856 status = xcalloc(nr_sought, sizeof(*status));
1857 assign_shallow_commits_to_refs(si, NULL, status);
1858 if (si->nr_ours || si->nr_theirs) {
1859 for (i = 0; i < nr_sought; i++)
1861 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1864 oid_array_clear(&ref);
1867 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1869 struct ref **rm = cb_data;
1870 struct ref *ref = *rm;
1873 return -1; /* end of the list */
1875 oidcpy(oid, &ref->old_oid);
1879 struct ref *fetch_pack(struct fetch_pack_args *args,
1881 const struct ref *ref,
1882 struct ref **sought, int nr_sought,
1883 struct oid_array *shallow,
1884 struct string_list *pack_lockfiles,
1885 enum protocol_version version)
1887 struct ref *ref_cpy;
1888 struct shallow_info si;
1889 struct oid_array shallows_scratch = OID_ARRAY_INIT;
1893 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1895 if (version != protocol_v2 && !ref) {
1896 packet_flush(fd[1]);
1897 die(_("no matching remote head"));
1899 if (version == protocol_v2) {
1901 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1902 memset(&si, 0, sizeof(si));
1903 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1904 &shallows_scratch, &si,
1907 prepare_shallow_info(&si, shallow);
1908 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1909 &si, pack_lockfiles);
1911 reprepare_packed_git(the_repository);
1913 if (!args->cloning && args->deepen) {
1914 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1915 struct ref *iterator = ref_cpy;
1916 opt.shallow_file = alternate_shallow_file;
1918 opt.is_deepening_fetch = 1;
1919 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1920 error(_("remote did not send all necessary objects"));
1923 rollback_shallow_file(the_repository, &shallow_lock);
1926 args->connectivity_checked = 1;
1929 update_shallow(args, sought, nr_sought, &si);
1931 clear_shallow_info(&si);
1932 oid_array_clear(&shallows_scratch);
1936 int report_unmatched_refs(struct ref **sought, int nr_sought)
1940 for (i = 0; i < nr_sought; i++) {
1943 switch (sought[i]->match_status) {
1946 case REF_NOT_MATCHED:
1947 error(_("no such remote ref %s"), sought[i]->name);
1949 case REF_UNADVERTISED_NOT_ALLOWED:
1950 error(_("Server does not allow request for unadvertised object %s"),