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 struct shallow_lock shallow_lock;
39 static const char *alternate_shallow_file;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
41 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
43 /* Remember to update object flag allocation in object.h */
44 #define COMPLETE (1U << 0)
45 #define ALTERNATE (1U << 1)
48 * After sending this many "have"s if we do not get any new ACK , we
49 * give up traversing our history.
51 #define MAX_IN_VAIN 256
53 static int multi_ack, use_sideband;
54 /* Allow specifying sha1 if it is a ref tip. */
55 #define ALLOW_TIP_SHA1 01
56 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57 #define ALLOW_REACHABLE_SHA1 02
58 static unsigned int allow_unadvertised_object_request;
60 __attribute__((format (printf, 2, 3)))
61 static inline void print_verbose(const struct fetch_pack_args *args,
69 va_start(params, fmt);
70 vfprintf(stderr, fmt, params);
75 struct alternate_object_cache {
76 struct object **items;
80 static void cache_one_alternate(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 struct commit *deref_without_lazy_fetch(const struct object_id *oid,
112 int mark_tags_complete)
114 enum object_type type;
115 struct object_info info = { .typep = &type };
118 if (oid_object_info_extended(the_repository, oid, &info,
119 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
121 if (type == OBJ_TAG) {
122 struct tag *tag = (struct tag *)
123 parse_object(the_repository, oid);
127 if (mark_tags_complete)
128 tag->object.flags |= COMPLETE;
129 oid = &tag->tagged->oid;
134 if (type == OBJ_COMMIT)
135 return (struct commit *) parse_object(the_repository, oid);
139 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
140 const struct object_id *oid)
142 struct commit *c = deref_without_lazy_fetch(oid, 0);
145 negotiator->add_tip(negotiator, c);
149 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
150 int flag, void *cb_data)
152 return rev_list_insert_ref(cb_data, oid);
163 static void consume_shallow_list(struct fetch_pack_args *args,
164 struct packet_reader *reader)
166 if (args->stateless_rpc && args->deepen) {
167 /* If we sent a depth we will get back "duplicate"
168 * shallow and unshallow commands every time there
169 * is a block of have lines exchanged.
171 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
172 if (starts_with(reader->line, "shallow "))
174 if (starts_with(reader->line, "unshallow "))
176 die(_("git fetch-pack: expected shallow list"));
178 if (reader->status != PACKET_READ_FLUSH)
179 die(_("git fetch-pack: expected a flush packet after shallow list"));
183 static enum ack_type get_ack(struct packet_reader *reader,
184 struct object_id *result_oid)
189 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
190 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
191 len = reader->pktlen;
193 if (!strcmp(reader->line, "NAK"))
195 if (skip_prefix(reader->line, "ACK ", &arg)) {
197 if (!parse_oid_hex(arg, result_oid, &p)) {
198 len -= p - reader->line;
201 if (strstr(p, "continue"))
203 if (strstr(p, "common"))
205 if (strstr(p, "ready"))
210 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
213 static void send_request(struct fetch_pack_args *args,
214 int fd, struct strbuf *buf)
216 if (args->stateless_rpc) {
217 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
220 if (write_in_full(fd, buf->buf, buf->len) < 0)
221 die_errno(_("unable to write to remote"));
225 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
228 rev_list_insert_ref(negotiator, &obj->oid);
231 #define INITIAL_FLUSH 16
232 #define PIPESAFE_FLUSH 32
233 #define LARGE_FLUSH 16384
235 static int next_flush(int stateless_rpc, int count)
238 if (count < LARGE_FLUSH)
241 count = count * 11 / 10;
243 if (count < PIPESAFE_FLUSH)
246 count += PIPESAFE_FLUSH;
251 static void mark_tips(struct fetch_negotiator *negotiator,
252 const struct oid_array *negotiation_tips)
256 if (!negotiation_tips) {
257 for_each_rawref(rev_list_insert_ref_oid, negotiator);
261 for (i = 0; i < negotiation_tips->nr; i++)
262 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
266 static int find_common(struct fetch_negotiator *negotiator,
267 struct fetch_pack_args *args,
268 int fd[2], struct object_id *result_oid,
272 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
273 const struct object_id *oid;
274 unsigned in_vain = 0;
275 int got_continue = 0;
277 struct strbuf req_buf = STRBUF_INIT;
278 size_t state_len = 0;
279 struct packet_reader reader;
281 if (args->stateless_rpc && multi_ack == 1)
282 die(_("--stateless-rpc requires multi_ack_detailed"));
284 packet_reader_init(&reader, fd[0], NULL, 0,
285 PACKET_READ_CHOMP_NEWLINE |
286 PACKET_READ_DIE_ON_ERR_PACKET);
288 mark_tips(negotiator, args->negotiation_tips);
289 for_each_cached_alternate(negotiator, insert_one_alternate_object);
292 for ( ; refs ; refs = refs->next) {
293 struct object_id *remote = &refs->old_oid;
294 const char *remote_hex;
298 * If that object is complete (i.e. it is an ancestor of a
299 * local ref), we tell them we have it but do not have to
300 * tell them about its ancestors, which they already know
303 * We use lookup_object here because we are only
304 * interested in the case we *know* the object is
305 * reachable and we have already scanned it.
307 if (((o = lookup_object(the_repository, remote)) != NULL) &&
308 (o->flags & COMPLETE)) {
312 remote_hex = oid_to_hex(remote);
314 struct strbuf c = STRBUF_INIT;
315 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
316 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
317 if (no_done) strbuf_addstr(&c, " no-done");
318 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
319 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
320 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
321 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
322 if (args->no_progress) strbuf_addstr(&c, " no-progress");
323 if (args->include_tag) strbuf_addstr(&c, " include-tag");
324 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
325 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
326 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
327 if (agent_supported) strbuf_addf(&c, " agent=%s",
328 git_user_agent_sanitized());
329 if (args->filter_options.choice)
330 strbuf_addstr(&c, " filter");
331 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
334 packet_buf_write(&req_buf, "want %s\n", remote_hex);
339 strbuf_release(&req_buf);
344 if (is_repository_shallow(the_repository))
345 write_shallow_commits(&req_buf, 1, NULL);
347 packet_buf_write(&req_buf, "deepen %d", args->depth);
348 if (args->deepen_since) {
349 timestamp_t max_age = approxidate(args->deepen_since);
350 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
352 if (args->deepen_not) {
354 for (i = 0; i < args->deepen_not->nr; i++) {
355 struct string_list_item *s = args->deepen_not->items + i;
356 packet_buf_write(&req_buf, "deepen-not %s", s->string);
359 if (server_supports_filtering && args->filter_options.choice) {
361 expand_list_objects_filter_spec(&args->filter_options);
362 packet_buf_write(&req_buf, "filter %s", spec);
364 packet_buf_flush(&req_buf);
365 state_len = req_buf.len;
369 struct object_id oid;
371 send_request(args, fd[1], &req_buf);
372 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
373 if (skip_prefix(reader.line, "shallow ", &arg)) {
374 if (get_oid_hex(arg, &oid))
375 die(_("invalid shallow line: %s"), reader.line);
376 register_shallow(the_repository, &oid);
379 if (skip_prefix(reader.line, "unshallow ", &arg)) {
380 if (get_oid_hex(arg, &oid))
381 die(_("invalid unshallow line: %s"), reader.line);
382 if (!lookup_object(the_repository, &oid))
383 die(_("object not found: %s"), reader.line);
384 /* make sure that it is parsed as shallow */
385 if (!parse_object(the_repository, &oid))
386 die(_("error in object: %s"), reader.line);
387 if (unregister_shallow(&oid))
388 die(_("no shallow found: %s"), reader.line);
391 die(_("expected shallow/unshallow, got %s"), reader.line);
393 } else if (!args->stateless_rpc)
394 send_request(args, fd[1], &req_buf);
396 if (!args->stateless_rpc) {
397 /* If we aren't using the stateless-rpc interface
398 * we don't need to retain the headers.
400 strbuf_setlen(&req_buf, 0);
404 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
407 while ((oid = negotiator->next(negotiator))) {
408 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
409 print_verbose(args, "have %s", oid_to_hex(oid));
411 if (flush_at <= ++count) {
414 packet_buf_flush(&req_buf);
415 send_request(args, fd[1], &req_buf);
416 strbuf_setlen(&req_buf, state_len);
418 flush_at = next_flush(args->stateless_rpc, count);
421 * We keep one window "ahead" of the other side, and
422 * will wait for an ACK only on the next one
424 if (!args->stateless_rpc && count == INITIAL_FLUSH)
427 consume_shallow_list(args, &reader);
429 ack = get_ack(&reader, result_oid);
431 print_verbose(args, _("got %s %d %s"), "ack",
432 ack, oid_to_hex(result_oid));
442 struct commit *commit =
443 lookup_commit(the_repository,
448 die(_("invalid commit %s"), oid_to_hex(result_oid));
449 was_common = negotiator->ack(negotiator, commit);
450 if (args->stateless_rpc
453 /* We need to replay the have for this object
454 * on the next RPC request so the peer knows
455 * it is in common with us.
457 const char *hex = oid_to_hex(result_oid);
458 packet_buf_write(&req_buf, "have %s\n", hex);
459 state_len = req_buf.len;
461 * Reset in_vain because an ack
462 * for this commit has not been
466 } else if (!args->stateless_rpc
467 || ack != ACK_common)
471 if (ack == ACK_ready)
478 if (got_continue && MAX_IN_VAIN < in_vain) {
479 print_verbose(args, _("giving up"));
487 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
488 if (!got_ready || !no_done) {
489 packet_buf_write(&req_buf, "done\n");
490 send_request(args, fd[1], &req_buf);
492 print_verbose(args, _("done"));
497 strbuf_release(&req_buf);
499 if (!got_ready || !no_done)
500 consume_shallow_list(args, &reader);
501 while (flushes || multi_ack) {
502 int ack = get_ack(&reader, result_oid);
504 print_verbose(args, _("got %s (%d) %s"), "ack",
505 ack, oid_to_hex(result_oid));
513 /* it is no error to fetch into a completely empty repo */
514 return count ? retval : 0;
517 static struct commit_list *complete;
519 static int mark_complete(const struct object_id *oid)
521 struct commit *commit = deref_without_lazy_fetch(oid, 1);
523 if (commit && !(commit->object.flags & COMPLETE)) {
524 commit->object.flags |= COMPLETE;
525 commit_list_insert(commit, &complete);
530 static int mark_complete_oid(const char *refname, const struct object_id *oid,
531 int flag, void *cb_data)
533 return mark_complete(oid);
536 static void mark_recent_complete_commits(struct fetch_pack_args *args,
539 while (complete && cutoff <= complete->item->date) {
540 print_verbose(args, _("Marking %s as complete"),
541 oid_to_hex(&complete->item->object.oid));
542 pop_most_recent_commit(&complete, COMPLETE);
546 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
548 for (; refs; refs = refs->next)
549 oidset_insert(oids, &refs->old_oid);
552 static int is_unmatched_ref(const struct ref *ref)
554 struct object_id oid;
556 return ref->match_status == REF_NOT_MATCHED &&
557 !parse_oid_hex(ref->name, &oid, &p) &&
559 oideq(&oid, &ref->old_oid);
562 static void filter_refs(struct fetch_pack_args *args,
564 struct ref **sought, int nr_sought)
566 struct ref *newlist = NULL;
567 struct ref **newtail = &newlist;
568 struct ref *unmatched = NULL;
569 struct ref *ref, *next;
570 struct oidset tip_oids = OIDSET_INIT;
572 int strict = !(allow_unadvertised_object_request &
573 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
576 for (ref = *refs; ref; ref = next) {
580 if (starts_with(ref->name, "refs/") &&
581 check_refname_format(ref->name, 0)) {
583 * trash or a peeled value; do not even add it to
589 while (i < nr_sought) {
590 int cmp = strcmp(ref->name, sought[i]->name);
592 break; /* definitely do not have it */
594 keep = 1; /* definitely have it */
595 sought[i]->match_status = REF_MATCHED;
600 if (!keep && args->fetch_all &&
601 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
608 newtail = &ref->next;
610 ref->next = unmatched;
616 for (i = 0; i < nr_sought; i++) {
618 if (!is_unmatched_ref(ref))
621 add_refs_to_oidset(&tip_oids, unmatched);
622 add_refs_to_oidset(&tip_oids, newlist);
627 /* Append unmatched requests to the list */
628 for (i = 0; i < nr_sought; i++) {
630 if (!is_unmatched_ref(ref))
633 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
634 ref->match_status = REF_MATCHED;
635 *newtail = copy_ref(ref);
636 newtail = &(*newtail)->next;
638 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
642 oidset_clear(&tip_oids);
643 free_refs(unmatched);
648 static void mark_alternate_complete(struct fetch_negotiator *unused,
651 mark_complete(&obj->oid);
654 struct loose_object_iter {
655 struct oidset *loose_object_set;
660 * Mark recent commits available locally and reachable from a local ref as
663 * The cutoff time for recency is determined by this heuristic: it is the
664 * earliest commit time of the objects in refs that are commits and that we know
665 * the commit time of.
667 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
668 struct fetch_pack_args *args,
672 int old_save_commit_buffer = save_commit_buffer;
673 timestamp_t cutoff = 0;
675 save_commit_buffer = 0;
677 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
678 for (ref = *refs; ref; ref = ref->next) {
681 if (!has_object_file_with_flags(&ref->old_oid,
683 OBJECT_INFO_SKIP_FETCH_OBJECT))
685 o = parse_object(the_repository, &ref->old_oid);
690 * We already have it -- which may mean that we were
691 * in sync with the other side at some time after
692 * that (it is OK if we guess wrong here).
694 if (o->type == OBJ_COMMIT) {
695 struct commit *commit = (struct commit *)o;
696 if (!cutoff || cutoff < commit->date)
697 cutoff = commit->date;
700 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
703 * This block marks all local refs as COMPLETE, and then recursively marks all
704 * parents of those refs as COMPLETE.
706 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
708 for_each_rawref(mark_complete_oid, NULL);
709 for_each_cached_alternate(NULL, mark_alternate_complete);
710 commit_list_sort_by_date(&complete);
712 mark_recent_complete_commits(args, cutoff);
714 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
717 * Mark all complete remote refs as common refs.
718 * Don't mark them common yet; the server has to be told so first.
720 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
721 for (ref = *refs; ref; ref = ref->next) {
722 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
724 if (!c || !(c->object.flags & COMPLETE))
727 negotiator->known_common(negotiator, c);
729 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
731 save_commit_buffer = old_save_commit_buffer;
735 * Returns 1 if every object pointed to by the given remote refs is available
736 * locally and reachable from a local ref, and 0 otherwise.
738 static int everything_local(struct fetch_pack_args *args,
744 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
745 const struct object_id *remote = &ref->old_oid;
748 o = lookup_object(the_repository, remote);
749 if (!o || !(o->flags & COMPLETE)) {
751 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
755 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
762 static int sideband_demux(int in, int out, void *data)
767 ret = recv_sideband("fetch-pack", xd[0], out);
772 static void write_promisor_file(const char *keep_name,
773 struct ref **sought, int nr_sought)
775 struct strbuf promisor_name = STRBUF_INIT;
780 strbuf_addstr(&promisor_name, keep_name);
781 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
782 if (!suffix_stripped)
783 BUG("name of pack lockfile should end with .keep (was '%s')",
785 strbuf_addstr(&promisor_name, ".promisor");
787 output = xfopen(promisor_name.buf, "w");
788 for (i = 0; i < nr_sought; i++)
789 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
793 strbuf_release(&promisor_name);
796 static int get_pack(struct fetch_pack_args *args,
797 int xd[2], struct string_list *pack_lockfiles,
799 struct ref **sought, int nr_sought)
802 int do_keep = args->keep_pack;
803 const char *cmd_name;
804 struct pack_header header;
806 struct child_process cmd = CHILD_PROCESS_INIT;
809 memset(&demux, 0, sizeof(demux));
811 /* xd[] is talking with upload-pack; subprocess reads from
812 * xd[0], spits out band#2 to stderr, and feeds us band#1
813 * through demux->out.
815 demux.proc = sideband_demux;
818 demux.isolate_sigpipe = 1;
819 if (start_async(&demux))
820 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
825 if (!args->keep_pack && unpack_limit) {
827 if (read_pack_header(demux.out, &header))
828 die(_("protocol error: bad pack header"));
830 if (ntohl(header.hdr_entries) < unpack_limit)
836 if (alternate_shallow_file) {
837 strvec_push(&cmd.args, "--shallow-file");
838 strvec_push(&cmd.args, alternate_shallow_file);
841 if (do_keep || args->from_promisor) {
844 cmd_name = "index-pack";
845 strvec_push(&cmd.args, cmd_name);
846 strvec_push(&cmd.args, "--stdin");
847 if (!args->quiet && !args->no_progress)
848 strvec_push(&cmd.args, "-v");
849 if (args->use_thin_pack)
850 strvec_push(&cmd.args, "--fix-thin");
851 if (do_keep && (args->lock_pack || unpack_limit)) {
852 char hostname[HOST_NAME_MAX + 1];
853 if (xgethostname(hostname, sizeof(hostname)))
854 xsnprintf(hostname, sizeof(hostname), "localhost");
855 strvec_pushf(&cmd.args,
856 "--keep=fetch-pack %"PRIuMAX " on %s",
857 (uintmax_t)getpid(), hostname);
859 if (only_packfile && args->check_self_contained_and_connected)
860 strvec_push(&cmd.args, "--check-self-contained-and-connected");
863 * We cannot perform any connectivity checks because
864 * not all packs have been downloaded; let the caller
865 * have this responsibility.
867 args->check_self_contained_and_connected = 0;
869 * If we're obtaining the filename of a lockfile, we'll use
870 * that filename to write a .promisor file with more
871 * information below. If not, we need index-pack to do it for
874 if (!(do_keep && pack_lockfiles) && args->from_promisor)
875 strvec_push(&cmd.args, "--promisor");
878 cmd_name = "unpack-objects";
879 strvec_push(&cmd.args, cmd_name);
880 if (args->quiet || args->no_progress)
881 strvec_push(&cmd.args, "-q");
882 args->check_self_contained_and_connected = 0;
886 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
887 ntohl(header.hdr_version),
888 ntohl(header.hdr_entries));
889 if (fetch_fsck_objects >= 0
891 : transfer_fsck_objects >= 0
892 ? transfer_fsck_objects
894 if (args->from_promisor)
896 * We cannot use --strict in index-pack because it
897 * checks both broken objects and links, but we only
898 * want to check for broken objects.
900 strvec_push(&cmd.args, "--fsck-objects");
902 strvec_pushf(&cmd.args, "--strict%s",
908 if (start_command(&cmd))
909 die(_("fetch-pack: unable to fork off %s"), cmd_name);
910 if (do_keep && pack_lockfiles) {
911 string_list_append_nodup(pack_lockfiles,
912 index_pack_lockfile(cmd.out));
917 /* Closed by start_command() */
920 ret = finish_command(&cmd);
921 if (!ret || (args->check_self_contained_and_connected && ret == 1))
922 args->self_contained_and_connected =
923 args->check_self_contained_and_connected &&
926 die(_("%s failed"), cmd_name);
927 if (use_sideband && finish_async(&demux))
928 die(_("error in sideband demultiplexer"));
931 * Now that index-pack has succeeded, write the promisor file using the
932 * obtained .keep filename if necessary
934 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
935 write_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
940 static int cmp_ref_by_name(const void *a_, const void *b_)
942 const struct ref *a = *((const struct ref **)a_);
943 const struct ref *b = *((const struct ref **)b_);
944 return strcmp(a->name, b->name);
947 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
949 const struct ref *orig_ref,
950 struct ref **sought, int nr_sought,
951 struct shallow_info *si,
952 struct string_list *pack_lockfiles)
954 struct repository *r = the_repository;
955 struct ref *ref = copy_ref_list(orig_ref);
956 struct object_id oid;
957 const char *agent_feature;
959 struct fetch_negotiator negotiator_alloc;
960 struct fetch_negotiator *negotiator;
962 negotiator = &negotiator_alloc;
963 fetch_negotiator_init(r, negotiator);
965 sort_ref_list(&ref, ref_compare_name);
966 QSORT(sought, nr_sought, cmp_ref_by_name);
968 if ((agent_feature = server_feature_value("agent", &agent_len))) {
971 print_verbose(args, _("Server version is %.*s"),
972 agent_len, agent_feature);
975 if (server_supports("shallow"))
976 print_verbose(args, _("Server supports %s"), "shallow");
977 else if (args->depth > 0 || is_repository_shallow(r))
978 die(_("Server does not support shallow clients"));
979 if (args->depth > 0 || args->deepen_since || args->deepen_not)
981 if (server_supports("multi_ack_detailed")) {
982 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
984 if (server_supports("no-done")) {
985 print_verbose(args, _("Server supports %s"), "no-done");
986 if (args->stateless_rpc)
990 else if (server_supports("multi_ack")) {
991 print_verbose(args, _("Server supports %s"), "multi_ack");
994 if (server_supports("side-band-64k")) {
995 print_verbose(args, _("Server supports %s"), "side-band-64k");
998 else if (server_supports("side-band")) {
999 print_verbose(args, _("Server supports %s"), "side-band");
1002 if (server_supports("allow-tip-sha1-in-want")) {
1003 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
1004 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1006 if (server_supports("allow-reachable-sha1-in-want")) {
1007 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
1008 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1010 if (server_supports("thin-pack"))
1011 print_verbose(args, _("Server supports %s"), "thin-pack");
1013 args->use_thin_pack = 0;
1014 if (server_supports("no-progress"))
1015 print_verbose(args, _("Server supports %s"), "no-progress");
1017 args->no_progress = 0;
1018 if (server_supports("include-tag"))
1019 print_verbose(args, _("Server supports %s"), "include-tag");
1021 args->include_tag = 0;
1022 if (server_supports("ofs-delta"))
1023 print_verbose(args, _("Server supports %s"), "ofs-delta");
1025 prefer_ofs_delta = 0;
1027 if (server_supports("filter")) {
1028 server_supports_filtering = 1;
1029 print_verbose(args, _("Server supports %s"), "filter");
1030 } else if (args->filter_options.choice) {
1031 warning("filtering not recognized by server, ignoring");
1034 if (server_supports("deepen-since")) {
1035 print_verbose(args, _("Server supports %s"), "deepen-since");
1036 deepen_since_ok = 1;
1037 } else if (args->deepen_since)
1038 die(_("Server does not support --shallow-since"));
1039 if (server_supports("deepen-not")) {
1040 print_verbose(args, _("Server supports %s"), "deepen-not");
1042 } else if (args->deepen_not)
1043 die(_("Server does not support --shallow-exclude"));
1044 if (server_supports("deepen-relative"))
1045 print_verbose(args, _("Server supports %s"), "deepen-relative");
1046 else if (args->deepen_relative)
1047 die(_("Server does not support --deepen"));
1048 if (!server_supports_hash(the_hash_algo->name, NULL))
1049 die(_("Server does not support this repository's object format"));
1051 mark_complete_and_common_ref(negotiator, args, &ref);
1052 filter_refs(args, &ref, sought, nr_sought);
1053 if (everything_local(args, &ref)) {
1054 packet_flush(fd[1]);
1057 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1058 if (!args->keep_pack)
1059 /* When cloning, it is not unusual to have
1062 warning(_("no common commits"));
1064 if (args->stateless_rpc)
1065 packet_flush(fd[1]);
1067 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1069 else if (si->nr_ours || si->nr_theirs)
1070 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1072 alternate_shallow_file = NULL;
1073 if (get_pack(args, fd, pack_lockfiles, 1, sought, nr_sought))
1074 die(_("git fetch-pack: fetch failed."));
1078 negotiator->release(negotiator);
1082 static void add_shallow_requests(struct strbuf *req_buf,
1083 const struct fetch_pack_args *args)
1085 if (is_repository_shallow(the_repository))
1086 write_shallow_commits(req_buf, 1, NULL);
1087 if (args->depth > 0)
1088 packet_buf_write(req_buf, "deepen %d", args->depth);
1089 if (args->deepen_since) {
1090 timestamp_t max_age = approxidate(args->deepen_since);
1091 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1093 if (args->deepen_not) {
1095 for (i = 0; i < args->deepen_not->nr; i++) {
1096 struct string_list_item *s = args->deepen_not->items + i;
1097 packet_buf_write(req_buf, "deepen-not %s", s->string);
1100 if (args->deepen_relative)
1101 packet_buf_write(req_buf, "deepen-relative\n");
1104 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1106 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1108 for ( ; wants ; wants = wants->next) {
1109 const struct object_id *remote = &wants->old_oid;
1113 * If that object is complete (i.e. it is an ancestor of a
1114 * local ref), we tell them we have it but do not have to
1115 * tell them about its ancestors, which they already know
1118 * We use lookup_object here because we are only
1119 * interested in the case we *know* the object is
1120 * reachable and we have already scanned it.
1122 if (((o = lookup_object(the_repository, remote)) != NULL) &&
1123 (o->flags & COMPLETE)) {
1127 if (!use_ref_in_want || wants->exact_oid)
1128 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1130 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1134 static void add_common(struct strbuf *req_buf, struct oidset *common)
1136 struct oidset_iter iter;
1137 const struct object_id *oid;
1138 oidset_iter_init(common, &iter);
1140 while ((oid = oidset_iter_next(&iter))) {
1141 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1145 static int add_haves(struct fetch_negotiator *negotiator,
1147 struct strbuf *req_buf,
1148 int *haves_to_send, int *in_vain)
1151 int haves_added = 0;
1152 const struct object_id *oid;
1154 while ((oid = negotiator->next(negotiator))) {
1155 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1156 if (++haves_added >= *haves_to_send)
1160 *in_vain += haves_added;
1161 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1163 packet_buf_write(req_buf, "done\n");
1167 /* Increase haves to send on next round */
1168 *haves_to_send = next_flush(1, *haves_to_send);
1173 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1174 struct fetch_pack_args *args,
1175 const struct ref *wants, struct oidset *common,
1176 int *haves_to_send, int *in_vain,
1177 int sideband_all, int seen_ack)
1180 const char *hash_name;
1181 struct strbuf req_buf = STRBUF_INIT;
1183 if (server_supports_v2("fetch", 1))
1184 packet_buf_write(&req_buf, "command=fetch");
1185 if (server_supports_v2("agent", 0))
1186 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1187 if (args->server_options && args->server_options->nr &&
1188 server_supports_v2("server-option", 1)) {
1190 for (i = 0; i < args->server_options->nr; i++)
1191 packet_buf_write(&req_buf, "server-option=%s",
1192 args->server_options->items[i].string);
1195 if (server_feature_v2("object-format", &hash_name)) {
1196 int hash_algo = hash_algo_by_name(hash_name);
1197 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1198 die(_("mismatched algorithms: client %s; server %s"),
1199 the_hash_algo->name, hash_name);
1200 packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
1201 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1202 die(_("the server does not support algorithm '%s'"),
1203 the_hash_algo->name);
1206 packet_buf_delim(&req_buf);
1207 if (args->use_thin_pack)
1208 packet_buf_write(&req_buf, "thin-pack");
1209 if (args->no_progress)
1210 packet_buf_write(&req_buf, "no-progress");
1211 if (args->include_tag)
1212 packet_buf_write(&req_buf, "include-tag");
1213 if (prefer_ofs_delta)
1214 packet_buf_write(&req_buf, "ofs-delta");
1216 packet_buf_write(&req_buf, "sideband-all");
1218 /* Add shallow-info and deepen request */
1219 if (server_supports_feature("fetch", "shallow", 0))
1220 add_shallow_requests(&req_buf, args);
1221 else if (is_repository_shallow(the_repository) || args->deepen)
1222 die(_("Server does not support shallow requests"));
1225 if (server_supports_feature("fetch", "filter", 0) &&
1226 args->filter_options.choice) {
1228 expand_list_objects_filter_spec(&args->filter_options);
1229 print_verbose(args, _("Server supports filter"));
1230 packet_buf_write(&req_buf, "filter %s", spec);
1231 } else if (args->filter_options.choice) {
1232 warning("filtering not recognized by server, ignoring");
1235 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1237 struct strbuf to_send = STRBUF_INIT;
1239 for (i = 0; i < uri_protocols.nr; i++) {
1240 const char *s = uri_protocols.items[i].string;
1242 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1244 strbuf_addch(&to_send, ',');
1245 strbuf_addstr(&to_send, s);
1249 packet_buf_write(&req_buf, "packfile-uris %s",
1251 strbuf_release(&to_send);
1256 add_wants(wants, &req_buf);
1258 /* Add all of the common commits we've found in previous rounds */
1259 add_common(&req_buf, common);
1261 /* Add initial haves */
1262 ret = add_haves(negotiator, seen_ack, &req_buf,
1263 haves_to_send, in_vain);
1266 packet_buf_flush(&req_buf);
1267 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1268 die_errno(_("unable to write request to remote"));
1270 strbuf_release(&req_buf);
1275 * Processes a section header in a server's response and checks if it matches
1276 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1277 * not consumed); if 0, the line will be consumed and the function will die if
1278 * the section header doesn't match what was expected.
1280 static int process_section_header(struct packet_reader *reader,
1281 const char *section, int peek)
1285 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1286 die(_("error reading section header '%s'"), section);
1288 ret = !strcmp(reader->line, section);
1292 die(_("expected '%s', received '%s'"),
1293 section, reader->line);
1294 packet_reader_read(reader);
1302 * No commit was found to be possessed by both the client and the
1303 * server, and "ready" was not received.
1308 * At least one commit was found to be possessed by both the client and
1309 * the server, and "ready" was not received.
1314 * "ready" was received, indicating that the server is ready to send
1315 * the packfile without any further negotiation.
1320 static enum common_found process_acks(struct fetch_negotiator *negotiator,
1321 struct packet_reader *reader,
1322 struct oidset *common)
1325 int received_ready = 0;
1326 int received_ack = 0;
1328 process_section_header(reader, "acknowledgments", 0);
1329 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1332 if (!strcmp(reader->line, "NAK"))
1335 if (skip_prefix(reader->line, "ACK ", &arg)) {
1336 struct object_id oid;
1338 if (!get_oid_hex(arg, &oid)) {
1339 struct commit *commit;
1340 oidset_insert(common, &oid);
1341 commit = lookup_commit(the_repository, &oid);
1343 negotiator->ack(negotiator, commit);
1348 if (!strcmp(reader->line, "ready")) {
1353 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1356 if (reader->status != PACKET_READ_FLUSH &&
1357 reader->status != PACKET_READ_DELIM)
1358 die(_("error processing acks: %d"), reader->status);
1361 * If an "acknowledgments" section is sent, a packfile is sent if and
1362 * only if "ready" was sent in this section. The other sections
1363 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1364 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1367 if (received_ready && reader->status != PACKET_READ_DELIM)
1368 die(_("expected packfile to be sent after 'ready'"));
1369 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1370 die(_("expected no other sections to be sent after no 'ready'"));
1372 return received_ready ? READY :
1373 (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
1376 static void receive_shallow_info(struct fetch_pack_args *args,
1377 struct packet_reader *reader,
1378 struct oid_array *shallows,
1379 struct shallow_info *si)
1381 int unshallow_received = 0;
1383 process_section_header(reader, "shallow-info", 0);
1384 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1386 struct object_id oid;
1388 if (skip_prefix(reader->line, "shallow ", &arg)) {
1389 if (get_oid_hex(arg, &oid))
1390 die(_("invalid shallow line: %s"), reader->line);
1391 oid_array_append(shallows, &oid);
1394 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1395 if (get_oid_hex(arg, &oid))
1396 die(_("invalid unshallow line: %s"), reader->line);
1397 if (!lookup_object(the_repository, &oid))
1398 die(_("object not found: %s"), reader->line);
1399 /* make sure that it is parsed as shallow */
1400 if (!parse_object(the_repository, &oid))
1401 die(_("error in object: %s"), reader->line);
1402 if (unregister_shallow(&oid))
1403 die(_("no shallow found: %s"), reader->line);
1404 unshallow_received = 1;
1407 die(_("expected shallow/unshallow, got %s"), reader->line);
1410 if (reader->status != PACKET_READ_FLUSH &&
1411 reader->status != PACKET_READ_DELIM)
1412 die(_("error processing shallow info: %d"), reader->status);
1414 if (args->deepen || unshallow_received) {
1416 * Treat these as shallow lines caused by our depth settings.
1417 * In v0, these lines cannot cause refs to be rejected; do the
1422 for (i = 0; i < shallows->nr; i++)
1423 register_shallow(the_repository, &shallows->oid[i]);
1424 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1427 } else if (shallows->nr) {
1429 * Treat these as shallow lines caused by the remote being
1430 * shallow. In v0, remote refs that reach these objects are
1431 * rejected (unless --update-shallow is set); do the same.
1433 prepare_shallow_info(si, shallows);
1434 if (si->nr_ours || si->nr_theirs)
1435 alternate_shallow_file =
1436 setup_temporary_shallow(si->shallow);
1438 alternate_shallow_file = NULL;
1440 alternate_shallow_file = NULL;
1444 static int cmp_name_ref(const void *name, const void *ref)
1446 return strcmp(name, (*(struct ref **)ref)->name);
1449 static void receive_wanted_refs(struct packet_reader *reader,
1450 struct ref **sought, int nr_sought)
1452 process_section_header(reader, "wanted-refs", 0);
1453 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1454 struct object_id oid;
1458 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1459 die(_("expected wanted-ref, got '%s'"), reader->line);
1461 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1464 die(_("unexpected wanted-ref: '%s'"), reader->line);
1465 oidcpy(&(*found)->old_oid, &oid);
1468 if (reader->status != PACKET_READ_DELIM)
1469 die(_("error processing wanted refs: %d"), reader->status);
1472 static void receive_packfile_uris(struct packet_reader *reader,
1473 struct string_list *uris)
1475 process_section_header(reader, "packfile-uris", 0);
1476 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1477 if (reader->pktlen < the_hash_algo->hexsz ||
1478 reader->line[the_hash_algo->hexsz] != ' ')
1479 die("expected '<hash> <uri>', got: %s\n", reader->line);
1481 string_list_append(uris, reader->line);
1483 if (reader->status != PACKET_READ_DELIM)
1484 die("expected DELIM");
1488 FETCH_CHECK_LOCAL = 0,
1495 static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1496 struct packet_reader *reader)
1498 check_stateless_delimiter(args->stateless_rpc, reader,
1499 _("git fetch-pack: expected response end packet"));
1502 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1504 const struct ref *orig_ref,
1505 struct ref **sought, int nr_sought,
1506 struct oid_array *shallows,
1507 struct shallow_info *si,
1508 struct string_list *pack_lockfiles)
1510 struct repository *r = the_repository;
1511 struct ref *ref = copy_ref_list(orig_ref);
1512 enum fetch_state state = FETCH_CHECK_LOCAL;
1513 struct oidset common = OIDSET_INIT;
1514 struct packet_reader reader;
1515 int in_vain = 0, negotiation_started = 0;
1516 int haves_to_send = INITIAL_FLUSH;
1517 struct fetch_negotiator negotiator_alloc;
1518 struct fetch_negotiator *negotiator;
1520 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1523 negotiator = &negotiator_alloc;
1524 fetch_negotiator_init(r, negotiator);
1526 packet_reader_init(&reader, fd[0], NULL, 0,
1527 PACKET_READ_CHOMP_NEWLINE |
1528 PACKET_READ_DIE_ON_ERR_PACKET);
1529 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1530 server_supports_feature("fetch", "sideband-all", 0)) {
1531 reader.use_sideband = 1;
1532 reader.me = "fetch-pack";
1535 while (state != FETCH_DONE) {
1537 case FETCH_CHECK_LOCAL:
1538 sort_ref_list(&ref, ref_compare_name);
1539 QSORT(sought, nr_sought, cmp_ref_by_name);
1541 /* v2 supports these by default */
1542 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1544 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1547 /* Filter 'ref' by 'sought' and those that aren't local */
1548 mark_complete_and_common_ref(negotiator, args, &ref);
1549 filter_refs(args, &ref, sought, nr_sought);
1550 if (everything_local(args, &ref))
1553 state = FETCH_SEND_REQUEST;
1555 mark_tips(negotiator, args->negotiation_tips);
1556 for_each_cached_alternate(negotiator,
1557 insert_one_alternate_object);
1559 case FETCH_SEND_REQUEST:
1560 if (!negotiation_started) {
1561 negotiation_started = 1;
1562 trace2_region_enter("fetch-pack",
1566 if (send_fetch_request(negotiator, fd[1], args, ref,
1568 &haves_to_send, &in_vain,
1569 reader.use_sideband,
1571 state = FETCH_GET_PACK;
1573 state = FETCH_PROCESS_ACKS;
1575 case FETCH_PROCESS_ACKS:
1576 /* Process ACKs/NAKs */
1577 switch (process_acks(negotiator, &reader, &common)) {
1580 * Don't check for response delimiter; get_pack() will
1581 * read the rest of this response.
1583 state = FETCH_GET_PACK;
1589 case NO_COMMON_FOUND:
1590 do_check_stateless_delimiter(args, &reader);
1591 state = FETCH_SEND_REQUEST;
1595 case FETCH_GET_PACK:
1596 trace2_region_leave("fetch-pack",
1599 /* Check for shallow-info section */
1600 if (process_section_header(&reader, "shallow-info", 1))
1601 receive_shallow_info(args, &reader, shallows, si);
1603 if (process_section_header(&reader, "wanted-refs", 1))
1604 receive_wanted_refs(&reader, sought, nr_sought);
1606 /* get the pack(s) */
1607 if (process_section_header(&reader, "packfile-uris", 1))
1608 receive_packfile_uris(&reader, &packfile_uris);
1609 process_section_header(&reader, "packfile", 0);
1610 if (get_pack(args, fd, pack_lockfiles,
1611 !packfile_uris.nr, sought, nr_sought))
1612 die(_("git fetch-pack: fetch failed."));
1613 do_check_stateless_delimiter(args, &reader);
1622 for (i = 0; i < packfile_uris.nr; i++) {
1623 struct child_process cmd = CHILD_PROCESS_INIT;
1624 char packname[GIT_MAX_HEXSZ + 1];
1625 const char *uri = packfile_uris.items[i].string +
1626 the_hash_algo->hexsz + 1;
1628 strvec_push(&cmd.args, "http-fetch");
1629 strvec_pushf(&cmd.args, "--packfile=%.*s",
1630 (int) the_hash_algo->hexsz,
1631 packfile_uris.items[i].string);
1632 strvec_push(&cmd.args, uri);
1636 if (start_command(&cmd))
1637 die("fetch-pack: unable to spawn http-fetch");
1639 if (read_in_full(cmd.out, packname, 5) < 0 ||
1640 memcmp(packname, "keep\t", 5))
1641 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1643 if (read_in_full(cmd.out, packname,
1644 the_hash_algo->hexsz + 1) < 0 ||
1645 packname[the_hash_algo->hexsz] != '\n')
1646 die("fetch-pack: expected hash then LF at end of http-fetch output");
1648 packname[the_hash_algo->hexsz] = '\0';
1652 if (finish_command(&cmd))
1653 die("fetch-pack: unable to finish http-fetch");
1655 if (memcmp(packfile_uris.items[i].string, packname,
1656 the_hash_algo->hexsz))
1657 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1658 uri, (int) the_hash_algo->hexsz,
1659 packfile_uris.items[i].string);
1661 string_list_append_nodup(pack_lockfiles,
1662 xstrfmt("%s/pack/pack-%s.keep",
1663 get_object_directory(),
1666 string_list_clear(&packfile_uris, 0);
1669 negotiator->release(negotiator);
1671 oidset_clear(&common);
1675 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1677 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1680 if (git_config_pathname(&path, var, value))
1682 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1683 fsck_msg_types.len ? ',' : '=', path);
1688 if (skip_prefix(var, "fetch.fsck.", &var)) {
1689 if (is_valid_msg_type(var, value))
1690 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1691 fsck_msg_types.len ? ',' : '=', var, value);
1693 warning("Skipping unknown msg id '%s'", var);
1697 return git_default_config(var, value, cb);
1700 static void fetch_pack_config(void)
1702 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1703 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1704 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1705 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1706 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1707 if (!uri_protocols.nr) {
1710 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1711 string_list_split(&uri_protocols, str, ',', -1);
1716 git_config(fetch_pack_config_cb, NULL);
1719 static void fetch_pack_setup(void)
1721 static int did_setup;
1724 fetch_pack_config();
1725 if (0 <= transfer_unpack_limit)
1726 unpack_limit = transfer_unpack_limit;
1727 else if (0 <= fetch_unpack_limit)
1728 unpack_limit = fetch_unpack_limit;
1732 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1734 struct string_list names = STRING_LIST_INIT_NODUP;
1737 for (src = dst = 0; src < nr; src++) {
1738 struct string_list_item *item;
1739 item = string_list_insert(&names, ref[src]->name);
1741 continue; /* already have it */
1742 item->util = ref[src];
1744 ref[dst] = ref[src];
1747 for (src = dst; src < nr; src++)
1749 string_list_clear(&names, 0);
1753 static void update_shallow(struct fetch_pack_args *args,
1754 struct ref **sought, int nr_sought,
1755 struct shallow_info *si)
1757 struct oid_array ref = OID_ARRAY_INIT;
1761 if (args->deepen && alternate_shallow_file) {
1762 if (*alternate_shallow_file == '\0') { /* --unshallow */
1763 unlink_or_warn(git_path_shallow(the_repository));
1764 rollback_shallow_file(the_repository, &shallow_lock);
1766 commit_shallow_file(the_repository, &shallow_lock);
1767 alternate_shallow_file = NULL;
1771 if (!si->shallow || !si->shallow->nr)
1774 if (args->cloning) {
1776 * remote is shallow, but this is a clone, there are
1777 * no objects in repo to worry about. Accept any
1778 * shallow points that exist in the pack (iow in repo
1779 * after get_pack() and reprepare_packed_git())
1781 struct oid_array extra = OID_ARRAY_INIT;
1782 struct object_id *oid = si->shallow->oid;
1783 for (i = 0; i < si->shallow->nr; i++)
1784 if (has_object_file(&oid[i]))
1785 oid_array_append(&extra, &oid[i]);
1787 setup_alternate_shallow(&shallow_lock,
1788 &alternate_shallow_file,
1790 commit_shallow_file(the_repository, &shallow_lock);
1791 alternate_shallow_file = NULL;
1793 oid_array_clear(&extra);
1797 if (!si->nr_ours && !si->nr_theirs)
1800 remove_nonexistent_theirs_shallow(si);
1801 if (!si->nr_ours && !si->nr_theirs)
1803 for (i = 0; i < nr_sought; i++)
1804 oid_array_append(&ref, &sought[i]->old_oid);
1807 if (args->update_shallow) {
1809 * remote is also shallow, .git/shallow may be updated
1810 * so all refs can be accepted. Make sure we only add
1811 * shallow roots that are actually reachable from new
1814 struct oid_array extra = OID_ARRAY_INIT;
1815 struct object_id *oid = si->shallow->oid;
1816 assign_shallow_commits_to_refs(si, NULL, NULL);
1817 if (!si->nr_ours && !si->nr_theirs) {
1818 oid_array_clear(&ref);
1821 for (i = 0; i < si->nr_ours; i++)
1822 oid_array_append(&extra, &oid[si->ours[i]]);
1823 for (i = 0; i < si->nr_theirs; i++)
1824 oid_array_append(&extra, &oid[si->theirs[i]]);
1825 setup_alternate_shallow(&shallow_lock,
1826 &alternate_shallow_file,
1828 commit_shallow_file(the_repository, &shallow_lock);
1829 oid_array_clear(&extra);
1830 oid_array_clear(&ref);
1831 alternate_shallow_file = NULL;
1836 * remote is also shallow, check what ref is safe to update
1837 * without updating .git/shallow
1839 status = xcalloc(nr_sought, sizeof(*status));
1840 assign_shallow_commits_to_refs(si, NULL, status);
1841 if (si->nr_ours || si->nr_theirs) {
1842 for (i = 0; i < nr_sought; i++)
1844 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1847 oid_array_clear(&ref);
1850 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1852 struct ref **rm = cb_data;
1853 struct ref *ref = *rm;
1856 return -1; /* end of the list */
1858 oidcpy(oid, &ref->old_oid);
1862 struct ref *fetch_pack(struct fetch_pack_args *args,
1864 const struct ref *ref,
1865 struct ref **sought, int nr_sought,
1866 struct oid_array *shallow,
1867 struct string_list *pack_lockfiles,
1868 enum protocol_version version)
1870 struct ref *ref_cpy;
1871 struct shallow_info si;
1872 struct oid_array shallows_scratch = OID_ARRAY_INIT;
1876 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1878 if (version != protocol_v2 && !ref) {
1879 packet_flush(fd[1]);
1880 die(_("no matching remote head"));
1882 if (version == protocol_v2) {
1884 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1885 memset(&si, 0, sizeof(si));
1886 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1887 &shallows_scratch, &si,
1890 prepare_shallow_info(&si, shallow);
1891 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1892 &si, pack_lockfiles);
1894 reprepare_packed_git(the_repository);
1896 if (!args->cloning && args->deepen) {
1897 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1898 struct ref *iterator = ref_cpy;
1899 opt.shallow_file = alternate_shallow_file;
1901 opt.is_deepening_fetch = 1;
1902 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1903 error(_("remote did not send all necessary objects"));
1906 rollback_shallow_file(the_repository, &shallow_lock);
1909 args->connectivity_checked = 1;
1912 update_shallow(args, sought, nr_sought, &si);
1914 clear_shallow_info(&si);
1915 oid_array_clear(&shallows_scratch);
1919 int report_unmatched_refs(struct ref **sought, int nr_sought)
1923 for (i = 0; i < nr_sought; i++) {
1926 switch (sought[i]->match_status) {
1929 case REF_NOT_MATCHED:
1930 error(_("no such remote ref %s"), sought[i]->name);
1932 case REF_UNADVERTISED_NOT_ALLOWED:
1933 error(_("Server does not allow request for unadvertised object %s"),