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 if (!args->no_dependents) {
289 mark_tips(negotiator, args->negotiation_tips);
290 for_each_cached_alternate(negotiator, insert_one_alternate_object);
294 for ( ; refs ; refs = refs->next) {
295 struct object_id *remote = &refs->old_oid;
296 const char *remote_hex;
300 * If that object is complete (i.e. it is an ancestor of a
301 * local ref), we tell them we have it but do not have to
302 * tell them about its ancestors, which they already know
305 * We use lookup_object here because we are only
306 * interested in the case we *know* the object is
307 * reachable and we have already scanned it.
309 * Do this only if args->no_dependents is false (if it is true,
310 * we cannot trust the object flags).
312 if (!args->no_dependents &&
313 ((o = lookup_object(the_repository, remote)) != NULL) &&
314 (o->flags & COMPLETE)) {
318 remote_hex = oid_to_hex(remote);
320 struct strbuf c = STRBUF_INIT;
321 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
322 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
323 if (no_done) strbuf_addstr(&c, " no-done");
324 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
325 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
326 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
327 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
328 if (args->no_progress) strbuf_addstr(&c, " no-progress");
329 if (args->include_tag) strbuf_addstr(&c, " include-tag");
330 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
331 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
332 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
333 if (agent_supported) strbuf_addf(&c, " agent=%s",
334 git_user_agent_sanitized());
335 if (args->filter_options.choice)
336 strbuf_addstr(&c, " filter");
337 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
340 packet_buf_write(&req_buf, "want %s\n", remote_hex);
345 strbuf_release(&req_buf);
350 if (is_repository_shallow(the_repository))
351 write_shallow_commits(&req_buf, 1, NULL);
353 packet_buf_write(&req_buf, "deepen %d", args->depth);
354 if (args->deepen_since) {
355 timestamp_t max_age = approxidate(args->deepen_since);
356 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
358 if (args->deepen_not) {
360 for (i = 0; i < args->deepen_not->nr; i++) {
361 struct string_list_item *s = args->deepen_not->items + i;
362 packet_buf_write(&req_buf, "deepen-not %s", s->string);
365 if (server_supports_filtering && args->filter_options.choice) {
367 expand_list_objects_filter_spec(&args->filter_options);
368 packet_buf_write(&req_buf, "filter %s", spec);
370 packet_buf_flush(&req_buf);
371 state_len = req_buf.len;
375 struct object_id oid;
377 send_request(args, fd[1], &req_buf);
378 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
379 if (skip_prefix(reader.line, "shallow ", &arg)) {
380 if (get_oid_hex(arg, &oid))
381 die(_("invalid shallow line: %s"), reader.line);
382 register_shallow(the_repository, &oid);
385 if (skip_prefix(reader.line, "unshallow ", &arg)) {
386 if (get_oid_hex(arg, &oid))
387 die(_("invalid unshallow line: %s"), reader.line);
388 if (!lookup_object(the_repository, &oid))
389 die(_("object not found: %s"), reader.line);
390 /* make sure that it is parsed as shallow */
391 if (!parse_object(the_repository, &oid))
392 die(_("error in object: %s"), reader.line);
393 if (unregister_shallow(&oid))
394 die(_("no shallow found: %s"), reader.line);
397 die(_("expected shallow/unshallow, got %s"), reader.line);
399 } else if (!args->stateless_rpc)
400 send_request(args, fd[1], &req_buf);
402 if (!args->stateless_rpc) {
403 /* If we aren't using the stateless-rpc interface
404 * we don't need to retain the headers.
406 strbuf_setlen(&req_buf, 0);
410 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
413 if (args->no_dependents)
415 while ((oid = negotiator->next(negotiator))) {
416 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
417 print_verbose(args, "have %s", oid_to_hex(oid));
419 if (flush_at <= ++count) {
422 packet_buf_flush(&req_buf);
423 send_request(args, fd[1], &req_buf);
424 strbuf_setlen(&req_buf, state_len);
426 flush_at = next_flush(args->stateless_rpc, count);
429 * We keep one window "ahead" of the other side, and
430 * will wait for an ACK only on the next one
432 if (!args->stateless_rpc && count == INITIAL_FLUSH)
435 consume_shallow_list(args, &reader);
437 ack = get_ack(&reader, result_oid);
439 print_verbose(args, _("got %s %d %s"), "ack",
440 ack, oid_to_hex(result_oid));
450 struct commit *commit =
451 lookup_commit(the_repository,
456 die(_("invalid commit %s"), oid_to_hex(result_oid));
457 was_common = negotiator->ack(negotiator, commit);
458 if (args->stateless_rpc
461 /* We need to replay the have for this object
462 * on the next RPC request so the peer knows
463 * it is in common with us.
465 const char *hex = oid_to_hex(result_oid);
466 packet_buf_write(&req_buf, "have %s\n", hex);
467 state_len = req_buf.len;
469 * Reset in_vain because an ack
470 * for this commit has not been
474 } else if (!args->stateless_rpc
475 || ack != ACK_common)
479 if (ack == ACK_ready)
486 if (got_continue && MAX_IN_VAIN < in_vain) {
487 print_verbose(args, _("giving up"));
495 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
496 if (!got_ready || !no_done) {
497 packet_buf_write(&req_buf, "done\n");
498 send_request(args, fd[1], &req_buf);
500 print_verbose(args, _("done"));
505 strbuf_release(&req_buf);
507 if (!got_ready || !no_done)
508 consume_shallow_list(args, &reader);
509 while (flushes || multi_ack) {
510 int ack = get_ack(&reader, result_oid);
512 print_verbose(args, _("got %s (%d) %s"), "ack",
513 ack, oid_to_hex(result_oid));
521 /* it is no error to fetch into a completely empty repo */
522 return count ? retval : 0;
525 static struct commit_list *complete;
527 static int mark_complete(const struct object_id *oid)
529 struct commit *commit = deref_without_lazy_fetch(oid, 1);
531 if (commit && !(commit->object.flags & COMPLETE)) {
532 commit->object.flags |= COMPLETE;
533 commit_list_insert(commit, &complete);
538 static int mark_complete_oid(const char *refname, const struct object_id *oid,
539 int flag, void *cb_data)
541 return mark_complete(oid);
544 static void mark_recent_complete_commits(struct fetch_pack_args *args,
547 while (complete && cutoff <= complete->item->date) {
548 print_verbose(args, _("Marking %s as complete"),
549 oid_to_hex(&complete->item->object.oid));
550 pop_most_recent_commit(&complete, COMPLETE);
554 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
556 for (; refs; refs = refs->next)
557 oidset_insert(oids, &refs->old_oid);
560 static int is_unmatched_ref(const struct ref *ref)
562 struct object_id oid;
564 return ref->match_status == REF_NOT_MATCHED &&
565 !parse_oid_hex(ref->name, &oid, &p) &&
567 oideq(&oid, &ref->old_oid);
570 static void filter_refs(struct fetch_pack_args *args,
572 struct ref **sought, int nr_sought)
574 struct ref *newlist = NULL;
575 struct ref **newtail = &newlist;
576 struct ref *unmatched = NULL;
577 struct ref *ref, *next;
578 struct oidset tip_oids = OIDSET_INIT;
580 int strict = !(allow_unadvertised_object_request &
581 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
584 for (ref = *refs; ref; ref = next) {
588 if (starts_with(ref->name, "refs/") &&
589 check_refname_format(ref->name, 0)) {
591 * trash or a peeled value; do not even add it to
597 while (i < nr_sought) {
598 int cmp = strcmp(ref->name, sought[i]->name);
600 break; /* definitely do not have it */
602 keep = 1; /* definitely have it */
603 sought[i]->match_status = REF_MATCHED;
608 if (!keep && args->fetch_all &&
609 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
616 newtail = &ref->next;
618 ref->next = unmatched;
624 for (i = 0; i < nr_sought; i++) {
626 if (!is_unmatched_ref(ref))
629 add_refs_to_oidset(&tip_oids, unmatched);
630 add_refs_to_oidset(&tip_oids, newlist);
635 /* Append unmatched requests to the list */
636 for (i = 0; i < nr_sought; i++) {
638 if (!is_unmatched_ref(ref))
641 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
642 ref->match_status = REF_MATCHED;
643 *newtail = copy_ref(ref);
644 newtail = &(*newtail)->next;
646 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
650 oidset_clear(&tip_oids);
651 free_refs(unmatched);
656 static void mark_alternate_complete(struct fetch_negotiator *unused,
659 mark_complete(&obj->oid);
662 struct loose_object_iter {
663 struct oidset *loose_object_set;
668 * Mark recent commits available locally and reachable from a local ref as
669 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
670 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
671 * thus do not need COMMON_REF marks).
673 * The cutoff time for recency is determined by this heuristic: it is the
674 * earliest commit time of the objects in refs that are commits and that we know
675 * the commit time of.
677 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
678 struct fetch_pack_args *args,
682 int old_save_commit_buffer = save_commit_buffer;
683 timestamp_t cutoff = 0;
685 save_commit_buffer = 0;
687 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
688 for (ref = *refs; ref; ref = ref->next) {
691 if (!has_object_file_with_flags(&ref->old_oid,
693 OBJECT_INFO_SKIP_FETCH_OBJECT))
695 o = parse_object(the_repository, &ref->old_oid);
700 * We already have it -- which may mean that we were
701 * in sync with the other side at some time after
702 * that (it is OK if we guess wrong here).
704 if (o->type == OBJ_COMMIT) {
705 struct commit *commit = (struct commit *)o;
706 if (!cutoff || cutoff < commit->date)
707 cutoff = commit->date;
710 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
713 * This block marks all local refs as COMPLETE, and then recursively marks all
714 * parents of those refs as COMPLETE.
716 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
718 for_each_rawref(mark_complete_oid, NULL);
719 for_each_cached_alternate(NULL, mark_alternate_complete);
720 commit_list_sort_by_date(&complete);
722 mark_recent_complete_commits(args, cutoff);
724 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
727 * Mark all complete remote refs as common refs.
728 * Don't mark them common yet; the server has to be told so first.
730 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
731 for (ref = *refs; ref; ref = ref->next) {
732 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
734 if (!c || !(c->object.flags & COMPLETE))
737 negotiator->known_common(negotiator, c);
739 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
741 save_commit_buffer = old_save_commit_buffer;
745 * Returns 1 if every object pointed to by the given remote refs is available
746 * locally and reachable from a local ref, and 0 otherwise.
748 static int everything_local(struct fetch_pack_args *args,
754 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
755 const struct object_id *remote = &ref->old_oid;
758 o = lookup_object(the_repository, remote);
759 if (!o || !(o->flags & COMPLETE)) {
761 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
765 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
772 static int sideband_demux(int in, int out, void *data)
777 ret = recv_sideband("fetch-pack", xd[0], out);
782 static void write_promisor_file(const char *keep_name,
783 struct ref **sought, int nr_sought)
785 struct strbuf promisor_name = STRBUF_INIT;
790 strbuf_addstr(&promisor_name, keep_name);
791 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
792 if (!suffix_stripped)
793 BUG("name of pack lockfile should end with .keep (was '%s')",
795 strbuf_addstr(&promisor_name, ".promisor");
797 output = xfopen(promisor_name.buf, "w");
798 for (i = 0; i < nr_sought; i++)
799 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
803 strbuf_release(&promisor_name);
806 static int get_pack(struct fetch_pack_args *args,
807 int xd[2], struct string_list *pack_lockfiles,
809 struct ref **sought, int nr_sought)
812 int do_keep = args->keep_pack;
813 const char *cmd_name;
814 struct pack_header header;
816 struct child_process cmd = CHILD_PROCESS_INIT;
819 memset(&demux, 0, sizeof(demux));
821 /* xd[] is talking with upload-pack; subprocess reads from
822 * xd[0], spits out band#2 to stderr, and feeds us band#1
823 * through demux->out.
825 demux.proc = sideband_demux;
828 demux.isolate_sigpipe = 1;
829 if (start_async(&demux))
830 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
835 if (!args->keep_pack && unpack_limit) {
837 if (read_pack_header(demux.out, &header))
838 die(_("protocol error: bad pack header"));
840 if (ntohl(header.hdr_entries) < unpack_limit)
846 if (alternate_shallow_file) {
847 strvec_push(&cmd.args, "--shallow-file");
848 strvec_push(&cmd.args, alternate_shallow_file);
851 if (do_keep || args->from_promisor) {
854 cmd_name = "index-pack";
855 strvec_push(&cmd.args, cmd_name);
856 strvec_push(&cmd.args, "--stdin");
857 if (!args->quiet && !args->no_progress)
858 strvec_push(&cmd.args, "-v");
859 if (args->use_thin_pack)
860 strvec_push(&cmd.args, "--fix-thin");
861 if (do_keep && (args->lock_pack || unpack_limit)) {
862 char hostname[HOST_NAME_MAX + 1];
863 if (xgethostname(hostname, sizeof(hostname)))
864 xsnprintf(hostname, sizeof(hostname), "localhost");
865 strvec_pushf(&cmd.args,
866 "--keep=fetch-pack %"PRIuMAX " on %s",
867 (uintmax_t)getpid(), hostname);
869 if (only_packfile && args->check_self_contained_and_connected)
870 strvec_push(&cmd.args, "--check-self-contained-and-connected");
873 * We cannot perform any connectivity checks because
874 * not all packs have been downloaded; let the caller
875 * have this responsibility.
877 args->check_self_contained_and_connected = 0;
879 * If we're obtaining the filename of a lockfile, we'll use
880 * that filename to write a .promisor file with more
881 * information below. If not, we need index-pack to do it for
884 if (!(do_keep && pack_lockfiles) && args->from_promisor)
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)
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 string_list_append_nodup(pack_lockfiles,
922 index_pack_lockfile(cmd.out));
927 /* Closed by start_command() */
930 ret = finish_command(&cmd);
931 if (!ret || (args->check_self_contained_and_connected && ret == 1))
932 args->self_contained_and_connected =
933 args->check_self_contained_and_connected &&
936 die(_("%s failed"), cmd_name);
937 if (use_sideband && finish_async(&demux))
938 die(_("error in sideband demultiplexer"));
941 * Now that index-pack has succeeded, write the promisor file using the
942 * obtained .keep filename if necessary
944 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
945 write_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
950 static int cmp_ref_by_name(const void *a_, const void *b_)
952 const struct ref *a = *((const struct ref **)a_);
953 const struct ref *b = *((const struct ref **)b_);
954 return strcmp(a->name, b->name);
957 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
959 const struct ref *orig_ref,
960 struct ref **sought, int nr_sought,
961 struct shallow_info *si,
962 struct string_list *pack_lockfiles)
964 struct repository *r = the_repository;
965 struct ref *ref = copy_ref_list(orig_ref);
966 struct object_id oid;
967 const char *agent_feature;
969 struct fetch_negotiator negotiator_alloc;
970 struct fetch_negotiator *negotiator;
972 if (args->no_dependents) {
975 negotiator = &negotiator_alloc;
976 fetch_negotiator_init(r, negotiator);
979 sort_ref_list(&ref, ref_compare_name);
980 QSORT(sought, nr_sought, cmp_ref_by_name);
982 if ((agent_feature = server_feature_value("agent", &agent_len))) {
985 print_verbose(args, _("Server version is %.*s"),
986 agent_len, agent_feature);
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 if (!args->no_dependents) {
1066 mark_complete_and_common_ref(negotiator, args, &ref);
1067 filter_refs(args, &ref, sought, nr_sought);
1068 if (everything_local(args, &ref)) {
1069 packet_flush(fd[1]);
1073 filter_refs(args, &ref, sought, nr_sought);
1075 if (find_common(negotiator, args, fd, &oid, ref) < 0)
1076 if (!args->keep_pack)
1077 /* When cloning, it is not unusual to have
1080 warning(_("no common commits"));
1082 if (args->stateless_rpc)
1083 packet_flush(fd[1]);
1085 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1087 else if (si->nr_ours || si->nr_theirs)
1088 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1090 alternate_shallow_file = NULL;
1091 if (get_pack(args, fd, pack_lockfiles, 1, sought, nr_sought))
1092 die(_("git fetch-pack: fetch failed."));
1096 negotiator->release(negotiator);
1100 static void add_shallow_requests(struct strbuf *req_buf,
1101 const struct fetch_pack_args *args)
1103 if (is_repository_shallow(the_repository))
1104 write_shallow_commits(req_buf, 1, NULL);
1105 if (args->depth > 0)
1106 packet_buf_write(req_buf, "deepen %d", args->depth);
1107 if (args->deepen_since) {
1108 timestamp_t max_age = approxidate(args->deepen_since);
1109 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1111 if (args->deepen_not) {
1113 for (i = 0; i < args->deepen_not->nr; i++) {
1114 struct string_list_item *s = args->deepen_not->items + i;
1115 packet_buf_write(req_buf, "deepen-not %s", s->string);
1118 if (args->deepen_relative)
1119 packet_buf_write(req_buf, "deepen-relative\n");
1122 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1124 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1126 for ( ; wants ; wants = wants->next) {
1127 const struct object_id *remote = &wants->old_oid;
1131 * If that object is complete (i.e. it is an ancestor of a
1132 * local ref), we tell them we have it but do not have to
1133 * tell them about its ancestors, which they already know
1136 * We use lookup_object here because we are only
1137 * interested in the case we *know* the object is
1138 * reachable and we have already scanned it.
1140 * Do this only if args->no_dependents is false (if it is true,
1141 * we cannot trust the object flags).
1143 if (!no_dependents &&
1144 ((o = lookup_object(the_repository, remote)) != NULL) &&
1145 (o->flags & COMPLETE)) {
1149 if (!use_ref_in_want || wants->exact_oid)
1150 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1152 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1156 static void add_common(struct strbuf *req_buf, struct oidset *common)
1158 struct oidset_iter iter;
1159 const struct object_id *oid;
1160 oidset_iter_init(common, &iter);
1162 while ((oid = oidset_iter_next(&iter))) {
1163 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1167 static int add_haves(struct fetch_negotiator *negotiator,
1169 struct strbuf *req_buf,
1170 int *haves_to_send, int *in_vain)
1173 int haves_added = 0;
1174 const struct object_id *oid;
1176 while ((oid = negotiator->next(negotiator))) {
1177 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1178 if (++haves_added >= *haves_to_send)
1182 *in_vain += haves_added;
1183 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1185 packet_buf_write(req_buf, "done\n");
1189 /* Increase haves to send on next round */
1190 *haves_to_send = next_flush(1, *haves_to_send);
1195 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1196 struct fetch_pack_args *args,
1197 const struct ref *wants, struct oidset *common,
1198 int *haves_to_send, int *in_vain,
1199 int sideband_all, int seen_ack)
1202 const char *hash_name;
1203 struct strbuf req_buf = STRBUF_INIT;
1205 if (server_supports_v2("fetch", 1))
1206 packet_buf_write(&req_buf, "command=fetch");
1207 if (server_supports_v2("agent", 0))
1208 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1209 if (args->server_options && args->server_options->nr &&
1210 server_supports_v2("server-option", 1)) {
1212 for (i = 0; i < args->server_options->nr; i++)
1213 packet_buf_write(&req_buf, "server-option=%s",
1214 args->server_options->items[i].string);
1217 if (server_feature_v2("object-format", &hash_name)) {
1218 int hash_algo = hash_algo_by_name(hash_name);
1219 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1220 die(_("mismatched algorithms: client %s; server %s"),
1221 the_hash_algo->name, hash_name);
1222 packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
1223 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1224 die(_("the server does not support algorithm '%s'"),
1225 the_hash_algo->name);
1228 packet_buf_delim(&req_buf);
1229 if (args->use_thin_pack)
1230 packet_buf_write(&req_buf, "thin-pack");
1231 if (args->no_progress)
1232 packet_buf_write(&req_buf, "no-progress");
1233 if (args->include_tag)
1234 packet_buf_write(&req_buf, "include-tag");
1235 if (prefer_ofs_delta)
1236 packet_buf_write(&req_buf, "ofs-delta");
1238 packet_buf_write(&req_buf, "sideband-all");
1240 /* Add shallow-info and deepen request */
1241 if (server_supports_feature("fetch", "shallow", 0))
1242 add_shallow_requests(&req_buf, args);
1243 else if (is_repository_shallow(the_repository) || args->deepen)
1244 die(_("Server does not support shallow requests"));
1247 if (server_supports_feature("fetch", "filter", 0) &&
1248 args->filter_options.choice) {
1250 expand_list_objects_filter_spec(&args->filter_options);
1251 print_verbose(args, _("Server supports filter"));
1252 packet_buf_write(&req_buf, "filter %s", spec);
1253 } else if (args->filter_options.choice) {
1254 warning("filtering not recognized by server, ignoring");
1257 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1259 struct strbuf to_send = STRBUF_INIT;
1261 for (i = 0; i < uri_protocols.nr; i++) {
1262 const char *s = uri_protocols.items[i].string;
1264 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1266 strbuf_addch(&to_send, ',');
1267 strbuf_addstr(&to_send, s);
1271 packet_buf_write(&req_buf, "packfile-uris %s",
1273 strbuf_release(&to_send);
1278 add_wants(args->no_dependents, wants, &req_buf);
1280 if (args->no_dependents) {
1281 packet_buf_write(&req_buf, "done");
1284 /* Add all of the common commits we've found in previous rounds */
1285 add_common(&req_buf, common);
1287 /* Add initial haves */
1288 ret = add_haves(negotiator, seen_ack, &req_buf,
1289 haves_to_send, in_vain);
1293 packet_buf_flush(&req_buf);
1294 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1295 die_errno(_("unable to write request to remote"));
1297 strbuf_release(&req_buf);
1302 * Processes a section header in a server's response and checks if it matches
1303 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1304 * not consumed); if 0, the line will be consumed and the function will die if
1305 * the section header doesn't match what was expected.
1307 static int process_section_header(struct packet_reader *reader,
1308 const char *section, int peek)
1312 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1313 die(_("error reading section header '%s'"), section);
1315 ret = !strcmp(reader->line, section);
1319 die(_("expected '%s', received '%s'"),
1320 section, reader->line);
1321 packet_reader_read(reader);
1329 * No commit was found to be possessed by both the client and the
1330 * server, and "ready" was not received.
1335 * At least one commit was found to be possessed by both the client and
1336 * the server, and "ready" was not received.
1341 * "ready" was received, indicating that the server is ready to send
1342 * the packfile without any further negotiation.
1347 static enum common_found process_acks(struct fetch_negotiator *negotiator,
1348 struct packet_reader *reader,
1349 struct oidset *common)
1352 int received_ready = 0;
1353 int received_ack = 0;
1355 process_section_header(reader, "acknowledgments", 0);
1356 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1359 if (!strcmp(reader->line, "NAK"))
1362 if (skip_prefix(reader->line, "ACK ", &arg)) {
1363 struct object_id oid;
1365 if (!get_oid_hex(arg, &oid)) {
1366 struct commit *commit;
1367 oidset_insert(common, &oid);
1368 commit = lookup_commit(the_repository, &oid);
1370 negotiator->ack(negotiator, commit);
1375 if (!strcmp(reader->line, "ready")) {
1380 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1383 if (reader->status != PACKET_READ_FLUSH &&
1384 reader->status != PACKET_READ_DELIM)
1385 die(_("error processing acks: %d"), reader->status);
1388 * If an "acknowledgments" section is sent, a packfile is sent if and
1389 * only if "ready" was sent in this section. The other sections
1390 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1391 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1394 if (received_ready && reader->status != PACKET_READ_DELIM)
1395 die(_("expected packfile to be sent after 'ready'"));
1396 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1397 die(_("expected no other sections to be sent after no 'ready'"));
1399 return received_ready ? READY :
1400 (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
1403 static void receive_shallow_info(struct fetch_pack_args *args,
1404 struct packet_reader *reader,
1405 struct oid_array *shallows,
1406 struct shallow_info *si)
1408 int unshallow_received = 0;
1410 process_section_header(reader, "shallow-info", 0);
1411 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1413 struct object_id oid;
1415 if (skip_prefix(reader->line, "shallow ", &arg)) {
1416 if (get_oid_hex(arg, &oid))
1417 die(_("invalid shallow line: %s"), reader->line);
1418 oid_array_append(shallows, &oid);
1421 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1422 if (get_oid_hex(arg, &oid))
1423 die(_("invalid unshallow line: %s"), reader->line);
1424 if (!lookup_object(the_repository, &oid))
1425 die(_("object not found: %s"), reader->line);
1426 /* make sure that it is parsed as shallow */
1427 if (!parse_object(the_repository, &oid))
1428 die(_("error in object: %s"), reader->line);
1429 if (unregister_shallow(&oid))
1430 die(_("no shallow found: %s"), reader->line);
1431 unshallow_received = 1;
1434 die(_("expected shallow/unshallow, got %s"), reader->line);
1437 if (reader->status != PACKET_READ_FLUSH &&
1438 reader->status != PACKET_READ_DELIM)
1439 die(_("error processing shallow info: %d"), reader->status);
1441 if (args->deepen || unshallow_received) {
1443 * Treat these as shallow lines caused by our depth settings.
1444 * In v0, these lines cannot cause refs to be rejected; do the
1449 for (i = 0; i < shallows->nr; i++)
1450 register_shallow(the_repository, &shallows->oid[i]);
1451 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1454 } else if (shallows->nr) {
1456 * Treat these as shallow lines caused by the remote being
1457 * shallow. In v0, remote refs that reach these objects are
1458 * rejected (unless --update-shallow is set); do the same.
1460 prepare_shallow_info(si, shallows);
1461 if (si->nr_ours || si->nr_theirs)
1462 alternate_shallow_file =
1463 setup_temporary_shallow(si->shallow);
1465 alternate_shallow_file = NULL;
1467 alternate_shallow_file = NULL;
1471 static int cmp_name_ref(const void *name, const void *ref)
1473 return strcmp(name, (*(struct ref **)ref)->name);
1476 static void receive_wanted_refs(struct packet_reader *reader,
1477 struct ref **sought, int nr_sought)
1479 process_section_header(reader, "wanted-refs", 0);
1480 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1481 struct object_id oid;
1485 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1486 die(_("expected wanted-ref, got '%s'"), reader->line);
1488 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1491 die(_("unexpected wanted-ref: '%s'"), reader->line);
1492 oidcpy(&(*found)->old_oid, &oid);
1495 if (reader->status != PACKET_READ_DELIM)
1496 die(_("error processing wanted refs: %d"), reader->status);
1499 static void receive_packfile_uris(struct packet_reader *reader,
1500 struct string_list *uris)
1502 process_section_header(reader, "packfile-uris", 0);
1503 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1504 if (reader->pktlen < the_hash_algo->hexsz ||
1505 reader->line[the_hash_algo->hexsz] != ' ')
1506 die("expected '<hash> <uri>', got: %s\n", reader->line);
1508 string_list_append(uris, reader->line);
1510 if (reader->status != PACKET_READ_DELIM)
1511 die("expected DELIM");
1515 FETCH_CHECK_LOCAL = 0,
1522 static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1523 struct packet_reader *reader)
1525 check_stateless_delimiter(args->stateless_rpc, reader,
1526 _("git fetch-pack: expected response end packet"));
1529 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1531 const struct ref *orig_ref,
1532 struct ref **sought, int nr_sought,
1533 struct oid_array *shallows,
1534 struct shallow_info *si,
1535 struct string_list *pack_lockfiles)
1537 struct repository *r = the_repository;
1538 struct ref *ref = copy_ref_list(orig_ref);
1539 enum fetch_state state = FETCH_CHECK_LOCAL;
1540 struct oidset common = OIDSET_INIT;
1541 struct packet_reader reader;
1542 int in_vain = 0, negotiation_started = 0;
1543 int haves_to_send = INITIAL_FLUSH;
1544 struct fetch_negotiator negotiator_alloc;
1545 struct fetch_negotiator *negotiator;
1547 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1550 if (args->no_dependents) {
1553 negotiator = &negotiator_alloc;
1554 fetch_negotiator_init(r, negotiator);
1557 packet_reader_init(&reader, fd[0], NULL, 0,
1558 PACKET_READ_CHOMP_NEWLINE |
1559 PACKET_READ_DIE_ON_ERR_PACKET);
1560 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1561 server_supports_feature("fetch", "sideband-all", 0)) {
1562 reader.use_sideband = 1;
1563 reader.me = "fetch-pack";
1566 while (state != FETCH_DONE) {
1568 case FETCH_CHECK_LOCAL:
1569 sort_ref_list(&ref, ref_compare_name);
1570 QSORT(sought, nr_sought, cmp_ref_by_name);
1572 /* v2 supports these by default */
1573 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1575 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1578 /* Filter 'ref' by 'sought' and those that aren't local */
1579 if (!args->no_dependents) {
1580 mark_complete_and_common_ref(negotiator, args, &ref);
1581 filter_refs(args, &ref, sought, nr_sought);
1582 if (everything_local(args, &ref))
1585 state = FETCH_SEND_REQUEST;
1587 mark_tips(negotiator, args->negotiation_tips);
1588 for_each_cached_alternate(negotiator,
1589 insert_one_alternate_object);
1591 filter_refs(args, &ref, sought, nr_sought);
1592 state = FETCH_SEND_REQUEST;
1595 case FETCH_SEND_REQUEST:
1596 if (!negotiation_started) {
1597 negotiation_started = 1;
1598 trace2_region_enter("fetch-pack",
1602 if (send_fetch_request(negotiator, fd[1], args, ref,
1604 &haves_to_send, &in_vain,
1605 reader.use_sideband,
1607 state = FETCH_GET_PACK;
1609 state = FETCH_PROCESS_ACKS;
1611 case FETCH_PROCESS_ACKS:
1612 /* Process ACKs/NAKs */
1613 switch (process_acks(negotiator, &reader, &common)) {
1616 * Don't check for response delimiter; get_pack() will
1617 * read the rest of this response.
1619 state = FETCH_GET_PACK;
1625 case NO_COMMON_FOUND:
1626 do_check_stateless_delimiter(args, &reader);
1627 state = FETCH_SEND_REQUEST;
1631 case FETCH_GET_PACK:
1632 trace2_region_leave("fetch-pack",
1635 /* Check for shallow-info section */
1636 if (process_section_header(&reader, "shallow-info", 1))
1637 receive_shallow_info(args, &reader, shallows, si);
1639 if (process_section_header(&reader, "wanted-refs", 1))
1640 receive_wanted_refs(&reader, sought, nr_sought);
1642 /* get the pack(s) */
1643 if (process_section_header(&reader, "packfile-uris", 1))
1644 receive_packfile_uris(&reader, &packfile_uris);
1645 process_section_header(&reader, "packfile", 0);
1646 if (get_pack(args, fd, pack_lockfiles,
1647 !packfile_uris.nr, sought, nr_sought))
1648 die(_("git fetch-pack: fetch failed."));
1649 do_check_stateless_delimiter(args, &reader);
1658 for (i = 0; i < packfile_uris.nr; i++) {
1659 struct child_process cmd = CHILD_PROCESS_INIT;
1660 char packname[GIT_MAX_HEXSZ + 1];
1661 const char *uri = packfile_uris.items[i].string +
1662 the_hash_algo->hexsz + 1;
1664 strvec_push(&cmd.args, "http-fetch");
1665 strvec_pushf(&cmd.args, "--packfile=%.*s",
1666 (int) the_hash_algo->hexsz,
1667 packfile_uris.items[i].string);
1668 strvec_push(&cmd.args, uri);
1672 if (start_command(&cmd))
1673 die("fetch-pack: unable to spawn http-fetch");
1675 if (read_in_full(cmd.out, packname, 5) < 0 ||
1676 memcmp(packname, "keep\t", 5))
1677 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1679 if (read_in_full(cmd.out, packname,
1680 the_hash_algo->hexsz + 1) < 0 ||
1681 packname[the_hash_algo->hexsz] != '\n')
1682 die("fetch-pack: expected hash then LF at end of http-fetch output");
1684 packname[the_hash_algo->hexsz] = '\0';
1688 if (finish_command(&cmd))
1689 die("fetch-pack: unable to finish http-fetch");
1691 if (memcmp(packfile_uris.items[i].string, packname,
1692 the_hash_algo->hexsz))
1693 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1694 uri, (int) the_hash_algo->hexsz,
1695 packfile_uris.items[i].string);
1697 string_list_append_nodup(pack_lockfiles,
1698 xstrfmt("%s/pack/pack-%s.keep",
1699 get_object_directory(),
1702 string_list_clear(&packfile_uris, 0);
1705 negotiator->release(negotiator);
1707 oidset_clear(&common);
1711 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1713 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1716 if (git_config_pathname(&path, var, value))
1718 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1719 fsck_msg_types.len ? ',' : '=', path);
1724 if (skip_prefix(var, "fetch.fsck.", &var)) {
1725 if (is_valid_msg_type(var, value))
1726 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1727 fsck_msg_types.len ? ',' : '=', var, value);
1729 warning("Skipping unknown msg id '%s'", var);
1733 return git_default_config(var, value, cb);
1736 static void fetch_pack_config(void)
1738 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1739 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1740 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1741 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1742 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1743 if (!uri_protocols.nr) {
1746 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1747 string_list_split(&uri_protocols, str, ',', -1);
1752 git_config(fetch_pack_config_cb, NULL);
1755 static void fetch_pack_setup(void)
1757 static int did_setup;
1760 fetch_pack_config();
1761 if (0 <= transfer_unpack_limit)
1762 unpack_limit = transfer_unpack_limit;
1763 else if (0 <= fetch_unpack_limit)
1764 unpack_limit = fetch_unpack_limit;
1768 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1770 struct string_list names = STRING_LIST_INIT_NODUP;
1773 for (src = dst = 0; src < nr; src++) {
1774 struct string_list_item *item;
1775 item = string_list_insert(&names, ref[src]->name);
1777 continue; /* already have it */
1778 item->util = ref[src];
1780 ref[dst] = ref[src];
1783 for (src = dst; src < nr; src++)
1785 string_list_clear(&names, 0);
1789 static void update_shallow(struct fetch_pack_args *args,
1790 struct ref **sought, int nr_sought,
1791 struct shallow_info *si)
1793 struct oid_array ref = OID_ARRAY_INIT;
1797 if (args->deepen && alternate_shallow_file) {
1798 if (*alternate_shallow_file == '\0') { /* --unshallow */
1799 unlink_or_warn(git_path_shallow(the_repository));
1800 rollback_shallow_file(the_repository, &shallow_lock);
1802 commit_shallow_file(the_repository, &shallow_lock);
1803 alternate_shallow_file = NULL;
1807 if (!si->shallow || !si->shallow->nr)
1810 if (args->cloning) {
1812 * remote is shallow, but this is a clone, there are
1813 * no objects in repo to worry about. Accept any
1814 * shallow points that exist in the pack (iow in repo
1815 * after get_pack() and reprepare_packed_git())
1817 struct oid_array extra = OID_ARRAY_INIT;
1818 struct object_id *oid = si->shallow->oid;
1819 for (i = 0; i < si->shallow->nr; i++)
1820 if (has_object_file(&oid[i]))
1821 oid_array_append(&extra, &oid[i]);
1823 setup_alternate_shallow(&shallow_lock,
1824 &alternate_shallow_file,
1826 commit_shallow_file(the_repository, &shallow_lock);
1827 alternate_shallow_file = NULL;
1829 oid_array_clear(&extra);
1833 if (!si->nr_ours && !si->nr_theirs)
1836 remove_nonexistent_theirs_shallow(si);
1837 if (!si->nr_ours && !si->nr_theirs)
1839 for (i = 0; i < nr_sought; i++)
1840 oid_array_append(&ref, &sought[i]->old_oid);
1843 if (args->update_shallow) {
1845 * remote is also shallow, .git/shallow may be updated
1846 * so all refs can be accepted. Make sure we only add
1847 * shallow roots that are actually reachable from new
1850 struct oid_array extra = OID_ARRAY_INIT;
1851 struct object_id *oid = si->shallow->oid;
1852 assign_shallow_commits_to_refs(si, NULL, NULL);
1853 if (!si->nr_ours && !si->nr_theirs) {
1854 oid_array_clear(&ref);
1857 for (i = 0; i < si->nr_ours; i++)
1858 oid_array_append(&extra, &oid[si->ours[i]]);
1859 for (i = 0; i < si->nr_theirs; i++)
1860 oid_array_append(&extra, &oid[si->theirs[i]]);
1861 setup_alternate_shallow(&shallow_lock,
1862 &alternate_shallow_file,
1864 commit_shallow_file(the_repository, &shallow_lock);
1865 oid_array_clear(&extra);
1866 oid_array_clear(&ref);
1867 alternate_shallow_file = NULL;
1872 * remote is also shallow, check what ref is safe to update
1873 * without updating .git/shallow
1875 status = xcalloc(nr_sought, sizeof(*status));
1876 assign_shallow_commits_to_refs(si, NULL, status);
1877 if (si->nr_ours || si->nr_theirs) {
1878 for (i = 0; i < nr_sought; i++)
1880 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1883 oid_array_clear(&ref);
1886 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1888 struct ref **rm = cb_data;
1889 struct ref *ref = *rm;
1892 return -1; /* end of the list */
1894 oidcpy(oid, &ref->old_oid);
1898 struct ref *fetch_pack(struct fetch_pack_args *args,
1900 const struct ref *ref,
1901 struct ref **sought, int nr_sought,
1902 struct oid_array *shallow,
1903 struct string_list *pack_lockfiles,
1904 enum protocol_version version)
1906 struct ref *ref_cpy;
1907 struct shallow_info si;
1908 struct oid_array shallows_scratch = OID_ARRAY_INIT;
1912 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1914 if (args->no_dependents && !args->filter_options.choice) {
1916 * The protocol does not support requesting that only the
1917 * wanted objects be sent, so approximate this by setting a
1918 * "blob:none" filter if no filter is already set. This works
1919 * for all object types: note that wanted blobs will still be
1920 * sent because they are directly specified as a "want".
1922 * NEEDSWORK: Add an option in the protocol to request that
1923 * only the wanted objects be sent, and implement it.
1925 parse_list_objects_filter(&args->filter_options, "blob:none");
1928 if (version != protocol_v2 && !ref) {
1929 packet_flush(fd[1]);
1930 die(_("no matching remote head"));
1932 if (version == protocol_v2) {
1934 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1935 memset(&si, 0, sizeof(si));
1936 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1937 &shallows_scratch, &si,
1940 prepare_shallow_info(&si, shallow);
1941 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1942 &si, pack_lockfiles);
1944 reprepare_packed_git(the_repository);
1946 if (!args->cloning && args->deepen) {
1947 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1948 struct ref *iterator = ref_cpy;
1949 opt.shallow_file = alternate_shallow_file;
1951 opt.is_deepening_fetch = 1;
1952 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1953 error(_("remote did not send all necessary objects"));
1956 rollback_shallow_file(the_repository, &shallow_lock);
1959 args->connectivity_checked = 1;
1962 update_shallow(args, sought, nr_sought, &si);
1964 clear_shallow_info(&si);
1965 oid_array_clear(&shallows_scratch);
1969 int report_unmatched_refs(struct ref **sought, int nr_sought)
1973 for (i = 0; i < nr_sought; i++) {
1976 switch (sought[i]->match_status) {
1979 case REF_NOT_MATCHED:
1980 error(_("no such remote ref %s"), sought[i]->name);
1982 case REF_UNADVERTISED_NOT_ALLOWED:
1983 error(_("Server does not allow request for unadvertised object %s"),