11 #include "fetch-pack.h"
13 #include "run-command.h"
15 #include "transport.h"
17 #include "prio-queue.h"
18 #include "sha1-array.h"
20 static int transfer_unpack_limit = -1;
21 static int fetch_unpack_limit = -1;
22 static int unpack_limit = 100;
23 static int prefer_ofs_delta = 1;
25 static int deepen_since_ok;
26 static int deepen_not_ok;
27 static int fetch_fsck_objects = -1;
28 static int transfer_fsck_objects = -1;
29 static int agent_supported;
30 static struct lock_file shallow_lock;
31 static const char *alternate_shallow_file;
33 /* Remember to update object flag allocation in object.h */
34 #define COMPLETE (1U << 0)
35 #define COMMON (1U << 1)
36 #define COMMON_REF (1U << 2)
37 #define SEEN (1U << 3)
38 #define POPPED (1U << 4)
39 #define ALTERNATE (1U << 5)
44 * After sending this many "have"s if we do not get any new ACK , we
45 * give up traversing our history.
47 #define MAX_IN_VAIN 256
49 static struct prio_queue rev_list = { compare_commits_by_commit_date };
50 static int non_common_revs, multi_ack, use_sideband;
51 /* Allow specifying sha1 if it is a ref tip. */
52 #define ALLOW_TIP_SHA1 01
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 #define ALLOW_REACHABLE_SHA1 02
55 static unsigned int allow_unadvertised_object_request;
57 __attribute__((format (printf, 2, 3)))
58 static inline void print_verbose(const struct fetch_pack_args *args,
66 va_start(params, fmt);
67 vfprintf(stderr, fmt, params);
72 struct alternate_object_cache {
73 struct object **items;
77 static void cache_one_alternate(const char *refname,
78 const struct object_id *oid,
81 struct alternate_object_cache *cache = vcache;
82 struct object *obj = parse_object(oid->hash);
84 if (!obj || (obj->flags & ALTERNATE))
87 obj->flags |= ALTERNATE;
88 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
89 cache->items[cache->nr++] = obj;
92 static void for_each_cached_alternate(void (*cb)(struct object *))
94 static int initialized;
95 static struct alternate_object_cache cache;
99 for_each_alternate_ref(cache_one_alternate, &cache);
103 for (i = 0; i < cache.nr; i++)
107 static void rev_list_push(struct commit *commit, int mark)
109 if (!(commit->object.flags & mark)) {
110 commit->object.flags |= mark;
112 if (parse_commit(commit))
115 prio_queue_put(&rev_list, commit);
117 if (!(commit->object.flags & COMMON))
122 static int rev_list_insert_ref(const char *refname, const unsigned char *sha1)
124 struct object *o = deref_tag(parse_object(sha1), refname, 0);
126 if (o && o->type == OBJ_COMMIT)
127 rev_list_push((struct commit *)o, SEEN);
132 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
133 int flag, void *cb_data)
135 return rev_list_insert_ref(refname, oid->hash);
138 static int clear_marks(const char *refname, const struct object_id *oid,
139 int flag, void *cb_data)
141 struct object *o = deref_tag(parse_object(oid->hash), refname, 0);
143 if (o && o->type == OBJ_COMMIT)
144 clear_commit_marks((struct commit *)o,
145 COMMON | COMMON_REF | SEEN | POPPED);
150 This function marks a rev and its ancestors as common.
151 In some cases, it is desirable to mark only the ancestors (for example
152 when only the server does not yet know that they are common).
155 static void mark_common(struct commit *commit,
156 int ancestors_only, int dont_parse)
158 if (commit != NULL && !(commit->object.flags & COMMON)) {
159 struct object *o = (struct object *)commit;
164 if (!(o->flags & SEEN))
165 rev_list_push(commit, SEEN);
167 struct commit_list *parents;
169 if (!ancestors_only && !(o->flags & POPPED))
171 if (!o->parsed && !dont_parse)
172 if (parse_commit(commit))
175 for (parents = commit->parents;
177 parents = parents->next)
178 mark_common(parents->item, 0, dont_parse);
184 Get the next rev to send, ignoring the common.
187 static const unsigned char *get_rev(void)
189 struct commit *commit = NULL;
191 while (commit == NULL) {
193 struct commit_list *parents;
195 if (rev_list.nr == 0 || non_common_revs == 0)
198 commit = prio_queue_get(&rev_list);
199 parse_commit(commit);
200 parents = commit->parents;
202 commit->object.flags |= POPPED;
203 if (!(commit->object.flags & COMMON))
206 if (commit->object.flags & COMMON) {
207 /* do not send "have", and ignore ancestors */
209 mark = COMMON | SEEN;
210 } else if (commit->object.flags & COMMON_REF)
211 /* send "have", and ignore ancestors */
212 mark = COMMON | SEEN;
214 /* send "have", also for its ancestors */
218 if (!(parents->item->object.flags & SEEN))
219 rev_list_push(parents->item, mark);
221 mark_common(parents->item, 1, 0);
222 parents = parents->next;
226 return commit->object.oid.hash;
237 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
239 if (args->stateless_rpc && args->deepen) {
240 /* If we sent a depth we will get back "duplicate"
241 * shallow and unshallow commands every time there
242 * is a block of have lines exchanged.
245 while ((line = packet_read_line(fd, NULL))) {
246 if (starts_with(line, "shallow "))
248 if (starts_with(line, "unshallow "))
250 die(_("git fetch-pack: expected shallow list"));
255 static enum ack_type get_ack(int fd, unsigned char *result_sha1)
258 char *line = packet_read_line(fd, &len);
262 die(_("git fetch-pack: expected ACK/NAK, got EOF"));
263 if (!strcmp(line, "NAK"))
265 if (skip_prefix(line, "ACK ", &arg)) {
266 if (!get_sha1_hex(arg, result_sha1)) {
271 if (strstr(arg, "continue"))
273 if (strstr(arg, "common"))
275 if (strstr(arg, "ready"))
280 if (skip_prefix(line, "ERR ", &arg))
281 die(_("remote error: %s"), arg);
282 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
285 static void send_request(struct fetch_pack_args *args,
286 int fd, struct strbuf *buf)
288 if (args->stateless_rpc) {
289 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
292 write_or_die(fd, buf->buf, buf->len);
295 static void insert_one_alternate_object(struct object *obj)
297 rev_list_insert_ref(NULL, obj->oid.hash);
300 #define INITIAL_FLUSH 16
301 #define PIPESAFE_FLUSH 32
302 #define LARGE_FLUSH 16384
304 static int next_flush(struct fetch_pack_args *args, int count)
306 if (args->stateless_rpc) {
307 if (count < LARGE_FLUSH)
310 count = count * 11 / 10;
312 if (count < PIPESAFE_FLUSH)
315 count += PIPESAFE_FLUSH;
320 static int find_common(struct fetch_pack_args *args,
321 int fd[2], unsigned char *result_sha1,
325 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
326 const unsigned char *sha1;
327 unsigned in_vain = 0;
328 int got_continue = 0;
330 struct strbuf req_buf = STRBUF_INIT;
331 size_t state_len = 0;
333 if (args->stateless_rpc && multi_ack == 1)
334 die(_("--stateless-rpc requires multi_ack_detailed"));
336 for_each_ref(clear_marks, NULL);
339 for_each_ref(rev_list_insert_ref_oid, NULL);
340 for_each_cached_alternate(insert_one_alternate_object);
343 for ( ; refs ; refs = refs->next) {
344 unsigned char *remote = refs->old_oid.hash;
345 const char *remote_hex;
349 * If that object is complete (i.e. it is an ancestor of a
350 * local ref), we tell them we have it but do not have to
351 * tell them about its ancestors, which they already know
354 * We use lookup_object here because we are only
355 * interested in the case we *know* the object is
356 * reachable and we have already scanned it.
358 if (((o = lookup_object(remote)) != NULL) &&
359 (o->flags & COMPLETE)) {
363 remote_hex = sha1_to_hex(remote);
365 struct strbuf c = STRBUF_INIT;
366 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
367 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
368 if (no_done) strbuf_addstr(&c, " no-done");
369 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
370 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
371 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
372 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
373 if (args->no_progress) strbuf_addstr(&c, " no-progress");
374 if (args->include_tag) strbuf_addstr(&c, " include-tag");
375 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
376 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
377 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
378 if (agent_supported) strbuf_addf(&c, " agent=%s",
379 git_user_agent_sanitized());
380 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
383 packet_buf_write(&req_buf, "want %s\n", remote_hex);
388 strbuf_release(&req_buf);
393 if (is_repository_shallow())
394 write_shallow_commits(&req_buf, 1, NULL);
396 packet_buf_write(&req_buf, "deepen %d", args->depth);
397 if (args->deepen_since) {
398 unsigned long max_age = approxidate(args->deepen_since);
399 packet_buf_write(&req_buf, "deepen-since %lu", max_age);
401 if (args->deepen_not) {
403 for (i = 0; i < args->deepen_not->nr; i++) {
404 struct string_list_item *s = args->deepen_not->items + i;
405 packet_buf_write(&req_buf, "deepen-not %s", s->string);
408 packet_buf_flush(&req_buf);
409 state_len = req_buf.len;
414 unsigned char sha1[20];
416 send_request(args, fd[1], &req_buf);
417 while ((line = packet_read_line(fd[0], NULL))) {
418 if (skip_prefix(line, "shallow ", &arg)) {
419 if (get_sha1_hex(arg, sha1))
420 die(_("invalid shallow line: %s"), line);
421 register_shallow(sha1);
424 if (skip_prefix(line, "unshallow ", &arg)) {
425 if (get_sha1_hex(arg, sha1))
426 die(_("invalid unshallow line: %s"), line);
427 if (!lookup_object(sha1))
428 die(_("object not found: %s"), line);
429 /* make sure that it is parsed as shallow */
430 if (!parse_object(sha1))
431 die(_("error in object: %s"), line);
432 if (unregister_shallow(sha1))
433 die(_("no shallow found: %s"), line);
436 die(_("expected shallow/unshallow, got %s"), line);
438 } else if (!args->stateless_rpc)
439 send_request(args, fd[1], &req_buf);
441 if (!args->stateless_rpc) {
442 /* If we aren't using the stateless-rpc interface
443 * we don't need to retain the headers.
445 strbuf_setlen(&req_buf, 0);
451 while ((sha1 = get_rev())) {
452 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
453 print_verbose(args, "have %s", sha1_to_hex(sha1));
455 if (flush_at <= ++count) {
458 packet_buf_flush(&req_buf);
459 send_request(args, fd[1], &req_buf);
460 strbuf_setlen(&req_buf, state_len);
462 flush_at = next_flush(args, count);
465 * We keep one window "ahead" of the other side, and
466 * will wait for an ACK only on the next one
468 if (!args->stateless_rpc && count == INITIAL_FLUSH)
471 consume_shallow_list(args, fd[0]);
473 ack = get_ack(fd[0], result_sha1);
475 print_verbose(args, _("got %s %d %s"), "ack",
476 ack, sha1_to_hex(result_sha1));
486 struct commit *commit =
487 lookup_commit(result_sha1);
489 die(_("invalid commit %s"), sha1_to_hex(result_sha1));
490 if (args->stateless_rpc
492 && !(commit->object.flags & COMMON)) {
493 /* We need to replay the have for this object
494 * on the next RPC request so the peer knows
495 * it is in common with us.
497 const char *hex = sha1_to_hex(result_sha1);
498 packet_buf_write(&req_buf, "have %s\n", hex);
499 state_len = req_buf.len;
501 * Reset in_vain because an ack
502 * for this commit has not been
506 } else if (!args->stateless_rpc
507 || ack != ACK_common)
509 mark_common(commit, 0, 1);
512 if (ack == ACK_ready) {
513 clear_prio_queue(&rev_list);
521 if (got_continue && MAX_IN_VAIN < in_vain) {
522 print_verbose(args, _("giving up"));
528 if (!got_ready || !no_done) {
529 packet_buf_write(&req_buf, "done\n");
530 send_request(args, fd[1], &req_buf);
532 print_verbose(args, _("done"));
537 strbuf_release(&req_buf);
539 if (!got_ready || !no_done)
540 consume_shallow_list(args, fd[0]);
541 while (flushes || multi_ack) {
542 int ack = get_ack(fd[0], result_sha1);
544 print_verbose(args, _("got %s (%d) %s"), "ack",
545 ack, sha1_to_hex(result_sha1));
553 /* it is no error to fetch into a completely empty repo */
554 return count ? retval : 0;
557 static struct commit_list *complete;
559 static int mark_complete(const unsigned char *sha1)
561 struct object *o = parse_object(sha1);
563 while (o && o->type == OBJ_TAG) {
564 struct tag *t = (struct tag *) o;
566 break; /* broken repository */
567 o->flags |= COMPLETE;
568 o = parse_object(t->tagged->oid.hash);
570 if (o && o->type == OBJ_COMMIT) {
571 struct commit *commit = (struct commit *)o;
572 if (!(commit->object.flags & COMPLETE)) {
573 commit->object.flags |= COMPLETE;
574 commit_list_insert(commit, &complete);
580 static int mark_complete_oid(const char *refname, const struct object_id *oid,
581 int flag, void *cb_data)
583 return mark_complete(oid->hash);
586 static void mark_recent_complete_commits(struct fetch_pack_args *args,
587 unsigned long cutoff)
589 while (complete && cutoff <= complete->item->date) {
590 print_verbose(args, _("Marking %s as complete"),
591 oid_to_hex(&complete->item->object.oid));
592 pop_most_recent_commit(&complete, COMPLETE);
596 static void filter_refs(struct fetch_pack_args *args,
598 struct ref **sought, int nr_sought)
600 struct ref *newlist = NULL;
601 struct ref **newtail = &newlist;
602 struct ref *ref, *next;
606 for (ref = *refs; ref; ref = next) {
610 if (starts_with(ref->name, "refs/") &&
611 check_refname_format(ref->name, 0))
614 while (i < nr_sought) {
615 int cmp = strcmp(ref->name, sought[i]->name);
617 break; /* definitely do not have it */
619 keep = 1; /* definitely have it */
620 sought[i]->match_status = REF_MATCHED;
626 if (!keep && args->fetch_all &&
627 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
633 newtail = &ref->next;
639 /* Append unmatched requests to the list */
640 for (i = 0; i < nr_sought; i++) {
641 unsigned char sha1[20];
644 if (ref->match_status != REF_NOT_MATCHED)
646 if (get_sha1_hex(ref->name, sha1) ||
647 ref->name[40] != '\0' ||
648 hashcmp(sha1, ref->old_oid.hash))
651 if ((allow_unadvertised_object_request &
652 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1))) {
653 ref->match_status = REF_MATCHED;
654 *newtail = copy_ref(ref);
655 newtail = &(*newtail)->next;
657 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
663 static void mark_alternate_complete(struct object *obj)
665 mark_complete(obj->oid.hash);
668 static int everything_local(struct fetch_pack_args *args,
670 struct ref **sought, int nr_sought)
674 unsigned long cutoff = 0;
676 save_commit_buffer = 0;
678 for (ref = *refs; ref; ref = ref->next) {
681 if (!has_object_file(&ref->old_oid))
684 o = parse_object(ref->old_oid.hash);
688 /* We already have it -- which may mean that we were
689 * in sync with the other side at some time after
690 * that (it is OK if we guess wrong here).
692 if (o->type == OBJ_COMMIT) {
693 struct commit *commit = (struct commit *)o;
694 if (!cutoff || cutoff < commit->date)
695 cutoff = commit->date;
700 for_each_ref(mark_complete_oid, NULL);
701 for_each_cached_alternate(mark_alternate_complete);
702 commit_list_sort_by_date(&complete);
704 mark_recent_complete_commits(args, cutoff);
708 * Mark all complete remote refs as common refs.
709 * Don't mark them common yet; the server has to be told so first.
711 for (ref = *refs; ref; ref = ref->next) {
712 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
715 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
718 if (!(o->flags & SEEN)) {
719 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
721 mark_common((struct commit *)o, 1, 1);
725 filter_refs(args, refs, sought, nr_sought);
727 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
728 const unsigned char *remote = ref->old_oid.hash;
731 o = lookup_object(remote);
732 if (!o || !(o->flags & COMPLETE)) {
734 print_verbose(args, "want %s (%s)", sha1_to_hex(remote),
738 print_verbose(args, _("already have %s (%s)"), sha1_to_hex(remote),
744 static int sideband_demux(int in, int out, void *data)
749 ret = recv_sideband("fetch-pack", xd[0], out);
754 static int get_pack(struct fetch_pack_args *args,
755 int xd[2], char **pack_lockfile)
758 int do_keep = args->keep_pack;
759 const char *cmd_name;
760 struct pack_header header;
762 struct child_process cmd = CHILD_PROCESS_INIT;
765 memset(&demux, 0, sizeof(demux));
767 /* xd[] is talking with upload-pack; subprocess reads from
768 * xd[0], spits out band#2 to stderr, and feeds us band#1
769 * through demux->out.
771 demux.proc = sideband_demux;
774 demux.isolate_sigpipe = 1;
775 if (start_async(&demux))
776 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
781 if (!args->keep_pack && unpack_limit) {
783 if (read_pack_header(demux.out, &header))
784 die(_("protocol error: bad pack header"));
786 if (ntohl(header.hdr_entries) < unpack_limit)
792 if (alternate_shallow_file) {
793 argv_array_push(&cmd.args, "--shallow-file");
794 argv_array_push(&cmd.args, alternate_shallow_file);
800 cmd_name = "index-pack";
801 argv_array_push(&cmd.args, cmd_name);
802 argv_array_push(&cmd.args, "--stdin");
803 if (!args->quiet && !args->no_progress)
804 argv_array_push(&cmd.args, "-v");
805 if (args->use_thin_pack)
806 argv_array_push(&cmd.args, "--fix-thin");
807 if (args->lock_pack || unpack_limit) {
808 char hostname[HOST_NAME_MAX + 1];
809 if (xgethostname(hostname, sizeof(hostname)))
810 xsnprintf(hostname, sizeof(hostname), "localhost");
811 argv_array_pushf(&cmd.args,
812 "--keep=fetch-pack %"PRIuMAX " on %s",
813 (uintmax_t)getpid(), hostname);
815 if (args->check_self_contained_and_connected)
816 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
819 cmd_name = "unpack-objects";
820 argv_array_push(&cmd.args, cmd_name);
821 if (args->quiet || args->no_progress)
822 argv_array_push(&cmd.args, "-q");
823 args->check_self_contained_and_connected = 0;
827 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
828 ntohl(header.hdr_version),
829 ntohl(header.hdr_entries));
830 if (fetch_fsck_objects >= 0
832 : transfer_fsck_objects >= 0
833 ? transfer_fsck_objects
835 argv_array_push(&cmd.args, "--strict");
839 if (start_command(&cmd))
840 die(_("fetch-pack: unable to fork off %s"), cmd_name);
841 if (do_keep && pack_lockfile) {
842 *pack_lockfile = index_pack_lockfile(cmd.out);
847 /* Closed by start_command() */
850 ret = finish_command(&cmd);
851 if (!ret || (args->check_self_contained_and_connected && ret == 1))
852 args->self_contained_and_connected =
853 args->check_self_contained_and_connected &&
856 die(_("%s failed"), cmd_name);
857 if (use_sideband && finish_async(&demux))
858 die(_("error in sideband demultiplexer"));
862 static int cmp_ref_by_name(const void *a_, const void *b_)
864 const struct ref *a = *((const struct ref **)a_);
865 const struct ref *b = *((const struct ref **)b_);
866 return strcmp(a->name, b->name);
869 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
871 const struct ref *orig_ref,
872 struct ref **sought, int nr_sought,
873 struct shallow_info *si,
874 char **pack_lockfile)
876 struct ref *ref = copy_ref_list(orig_ref);
877 unsigned char sha1[20];
878 const char *agent_feature;
881 sort_ref_list(&ref, ref_compare_name);
882 QSORT(sought, nr_sought, cmp_ref_by_name);
884 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
885 die(_("Server does not support shallow clients"));
886 if (args->depth > 0 || args->deepen_since || args->deepen_not)
888 if (server_supports("multi_ack_detailed")) {
889 print_verbose(args, _("Server supports multi_ack_detailed"));
891 if (server_supports("no-done")) {
892 print_verbose(args, _("Server supports no-done"));
893 if (args->stateless_rpc)
897 else if (server_supports("multi_ack")) {
898 print_verbose(args, _("Server supports multi_ack"));
901 if (server_supports("side-band-64k")) {
902 print_verbose(args, _("Server supports side-band-64k"));
905 else if (server_supports("side-band")) {
906 print_verbose(args, _("Server supports side-band"));
909 if (server_supports("allow-tip-sha1-in-want")) {
910 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
911 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
913 if (server_supports("allow-reachable-sha1-in-want")) {
914 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
915 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
917 if (!server_supports("thin-pack"))
918 args->use_thin_pack = 0;
919 if (!server_supports("no-progress"))
920 args->no_progress = 0;
921 if (!server_supports("include-tag"))
922 args->include_tag = 0;
923 if (server_supports("ofs-delta"))
924 print_verbose(args, _("Server supports ofs-delta"));
926 prefer_ofs_delta = 0;
928 if ((agent_feature = server_feature_value("agent", &agent_len))) {
931 print_verbose(args, _("Server version is %.*s"),
932 agent_len, agent_feature);
934 if (server_supports("deepen-since"))
936 else if (args->deepen_since)
937 die(_("Server does not support --shallow-since"));
938 if (server_supports("deepen-not"))
940 else if (args->deepen_not)
941 die(_("Server does not support --shallow-exclude"));
942 if (!server_supports("deepen-relative") && args->deepen_relative)
943 die(_("Server does not support --deepen"));
945 if (everything_local(args, &ref, sought, nr_sought)) {
949 if (find_common(args, fd, sha1, ref) < 0)
950 if (!args->keep_pack)
951 /* When cloning, it is not unusual to have
954 warning(_("no common commits"));
956 if (args->stateless_rpc)
959 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
961 else if (si->nr_ours || si->nr_theirs)
962 alternate_shallow_file = setup_temporary_shallow(si->shallow);
964 alternate_shallow_file = NULL;
965 if (get_pack(args, fd, pack_lockfile))
966 die(_("git fetch-pack: fetch failed."));
972 static void fetch_pack_config(void)
974 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
975 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
976 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
977 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
978 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
980 git_config(git_default_config, NULL);
983 static void fetch_pack_setup(void)
985 static int did_setup;
989 if (0 <= transfer_unpack_limit)
990 unpack_limit = transfer_unpack_limit;
991 else if (0 <= fetch_unpack_limit)
992 unpack_limit = fetch_unpack_limit;
996 static int remove_duplicates_in_refs(struct ref **ref, int nr)
998 struct string_list names = STRING_LIST_INIT_NODUP;
1001 for (src = dst = 0; src < nr; src++) {
1002 struct string_list_item *item;
1003 item = string_list_insert(&names, ref[src]->name);
1005 continue; /* already have it */
1006 item->util = ref[src];
1008 ref[dst] = ref[src];
1011 for (src = dst; src < nr; src++)
1013 string_list_clear(&names, 0);
1017 static void update_shallow(struct fetch_pack_args *args,
1018 struct ref **sought, int nr_sought,
1019 struct shallow_info *si)
1021 struct oid_array ref = OID_ARRAY_INIT;
1025 if (args->deepen && alternate_shallow_file) {
1026 if (*alternate_shallow_file == '\0') { /* --unshallow */
1027 unlink_or_warn(git_path_shallow());
1028 rollback_lock_file(&shallow_lock);
1030 commit_lock_file(&shallow_lock);
1034 if (!si->shallow || !si->shallow->nr)
1037 if (args->cloning) {
1039 * remote is shallow, but this is a clone, there are
1040 * no objects in repo to worry about. Accept any
1041 * shallow points that exist in the pack (iow in repo
1042 * after get_pack() and reprepare_packed_git())
1044 struct oid_array extra = OID_ARRAY_INIT;
1045 struct object_id *oid = si->shallow->oid;
1046 for (i = 0; i < si->shallow->nr; i++)
1047 if (has_object_file(&oid[i]))
1048 oid_array_append(&extra, &oid[i]);
1050 setup_alternate_shallow(&shallow_lock,
1051 &alternate_shallow_file,
1053 commit_lock_file(&shallow_lock);
1055 oid_array_clear(&extra);
1059 if (!si->nr_ours && !si->nr_theirs)
1062 remove_nonexistent_theirs_shallow(si);
1063 if (!si->nr_ours && !si->nr_theirs)
1065 for (i = 0; i < nr_sought; i++)
1066 oid_array_append(&ref, &sought[i]->old_oid);
1069 if (args->update_shallow) {
1071 * remote is also shallow, .git/shallow may be updated
1072 * so all refs can be accepted. Make sure we only add
1073 * shallow roots that are actually reachable from new
1076 struct oid_array extra = OID_ARRAY_INIT;
1077 struct object_id *oid = si->shallow->oid;
1078 assign_shallow_commits_to_refs(si, NULL, NULL);
1079 if (!si->nr_ours && !si->nr_theirs) {
1080 oid_array_clear(&ref);
1083 for (i = 0; i < si->nr_ours; i++)
1084 oid_array_append(&extra, &oid[si->ours[i]]);
1085 for (i = 0; i < si->nr_theirs; i++)
1086 oid_array_append(&extra, &oid[si->theirs[i]]);
1087 setup_alternate_shallow(&shallow_lock,
1088 &alternate_shallow_file,
1090 commit_lock_file(&shallow_lock);
1091 oid_array_clear(&extra);
1092 oid_array_clear(&ref);
1097 * remote is also shallow, check what ref is safe to update
1098 * without updating .git/shallow
1100 status = xcalloc(nr_sought, sizeof(*status));
1101 assign_shallow_commits_to_refs(si, NULL, status);
1102 if (si->nr_ours || si->nr_theirs) {
1103 for (i = 0; i < nr_sought; i++)
1105 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1108 oid_array_clear(&ref);
1111 struct ref *fetch_pack(struct fetch_pack_args *args,
1112 int fd[], struct child_process *conn,
1113 const struct ref *ref,
1115 struct ref **sought, int nr_sought,
1116 struct oid_array *shallow,
1117 char **pack_lockfile)
1119 struct ref *ref_cpy;
1120 struct shallow_info si;
1124 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1127 packet_flush(fd[1]);
1128 die(_("no matching remote head"));
1130 prepare_shallow_info(&si, shallow);
1131 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1132 &si, pack_lockfile);
1133 reprepare_packed_git();
1134 update_shallow(args, sought, nr_sought, &si);
1135 clear_shallow_info(&si);
1139 int report_unmatched_refs(struct ref **sought, int nr_sought)
1143 for (i = 0; i < nr_sought; i++) {
1146 switch (sought[i]->match_status) {
1149 case REF_NOT_MATCHED:
1150 error(_("no such remote ref %s"), sought[i]->name);
1152 case REF_UNADVERTISED_NOT_ALLOWED:
1153 error(_("Server does not allow request for unadvertised object %s"),