10 #include "fetch-pack.h"
12 #include "run-command.h"
14 #include "transport.h"
16 #include "prio-queue.h"
17 #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);
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 struct object_id *oid)
124 struct object *o = deref_tag(parse_object(oid), 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);
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), 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 struct object_id *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;
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, struct object_id *result_oid)
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_oid_hex(arg, result_oid)) {
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);
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], struct object_id *result_oid,
325 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
326 const struct object_id *oid;
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 struct object_id *remote = &refs->old_oid;
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->hash)) != NULL) &&
359 (o->flags & COMPLETE)) {
363 remote_hex = oid_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 timestamp_t max_age = approxidate(args->deepen_since);
399 packet_buf_write(&req_buf, "deepen-since %"PRItime, 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 struct object_id oid;
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_oid_hex(arg, &oid))
420 die(_("invalid shallow line: %s"), line);
421 register_shallow(&oid);
424 if (skip_prefix(line, "unshallow ", &arg)) {
425 if (get_oid_hex(arg, &oid))
426 die(_("invalid unshallow line: %s"), line);
427 if (!lookup_object(oid.hash))
428 die(_("object not found: %s"), line);
429 /* make sure that it is parsed as shallow */
430 if (!parse_object(&oid))
431 die(_("error in object: %s"), line);
432 if (unregister_shallow(&oid))
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 ((oid = get_rev())) {
452 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
453 print_verbose(args, "have %s", oid_to_hex(oid));
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_oid);
475 print_verbose(args, _("got %s %d %s"), "ack",
476 ack, oid_to_hex(result_oid));
486 struct commit *commit =
487 lookup_commit(result_oid);
489 die(_("invalid commit %s"), oid_to_hex(result_oid));
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 = oid_to_hex(result_oid);
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_oid);
544 print_verbose(args, _("got %s (%d) %s"), "ack",
545 ack, oid_to_hex(result_oid));
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 struct object_id *oid)
561 struct object *o = parse_object(oid);
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);
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);
586 static void mark_recent_complete_commits(struct fetch_pack_args *args,
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 add_refs_to_oidset(struct oidset *oids, struct ref *refs)
598 for (; refs; refs = refs->next)
599 oidset_insert(oids, &refs->old_oid);
602 static int tip_oids_contain(struct oidset *tip_oids,
603 struct ref *unmatched, struct ref *newlist,
604 const struct object_id *id)
607 * Note that this only looks at the ref lists the first time it's
608 * called. This works out in filter_refs() because even though it may
609 * add to "newlist" between calls, the additions will always be for
610 * oids that are already in the set.
612 if (!tip_oids->map.tablesize) {
613 add_refs_to_oidset(tip_oids, unmatched);
614 add_refs_to_oidset(tip_oids, newlist);
616 return oidset_contains(tip_oids, id);
619 static void filter_refs(struct fetch_pack_args *args,
621 struct ref **sought, int nr_sought)
623 struct ref *newlist = NULL;
624 struct ref **newtail = &newlist;
625 struct ref *unmatched = NULL;
626 struct ref *ref, *next;
627 struct oidset tip_oids = OIDSET_INIT;
631 for (ref = *refs; ref; ref = next) {
635 if (starts_with(ref->name, "refs/") &&
636 check_refname_format(ref->name, 0))
639 while (i < nr_sought) {
640 int cmp = strcmp(ref->name, sought[i]->name);
642 break; /* definitely do not have it */
644 keep = 1; /* definitely have it */
645 sought[i]->match_status = REF_MATCHED;
651 if (!keep && args->fetch_all &&
652 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
658 newtail = &ref->next;
660 ref->next = unmatched;
665 /* Append unmatched requests to the list */
666 for (i = 0; i < nr_sought; i++) {
667 struct object_id oid;
671 if (ref->match_status != REF_NOT_MATCHED)
673 if (parse_oid_hex(ref->name, &oid, &p) ||
675 oidcmp(&oid, &ref->old_oid))
678 if ((allow_unadvertised_object_request &
679 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
680 tip_oids_contain(&tip_oids, unmatched, newlist,
682 ref->match_status = REF_MATCHED;
683 *newtail = copy_ref(ref);
684 newtail = &(*newtail)->next;
686 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
690 oidset_clear(&tip_oids);
691 for (ref = unmatched; ref; ref = next) {
699 static void mark_alternate_complete(struct object *obj)
701 mark_complete(&obj->oid);
704 static int everything_local(struct fetch_pack_args *args,
706 struct ref **sought, int nr_sought)
710 timestamp_t cutoff = 0;
712 save_commit_buffer = 0;
714 for (ref = *refs; ref; ref = ref->next) {
717 if (!has_object_file(&ref->old_oid))
720 o = parse_object(&ref->old_oid);
724 /* We already have it -- which may mean that we were
725 * in sync with the other side at some time after
726 * that (it is OK if we guess wrong here).
728 if (o->type == OBJ_COMMIT) {
729 struct commit *commit = (struct commit *)o;
730 if (!cutoff || cutoff < commit->date)
731 cutoff = commit->date;
736 for_each_ref(mark_complete_oid, NULL);
737 for_each_cached_alternate(mark_alternate_complete);
738 commit_list_sort_by_date(&complete);
740 mark_recent_complete_commits(args, cutoff);
744 * Mark all complete remote refs as common refs.
745 * Don't mark them common yet; the server has to be told so first.
747 for (ref = *refs; ref; ref = ref->next) {
748 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
751 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
754 if (!(o->flags & SEEN)) {
755 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
757 mark_common((struct commit *)o, 1, 1);
761 filter_refs(args, refs, sought, nr_sought);
763 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
764 const struct object_id *remote = &ref->old_oid;
767 o = lookup_object(remote->hash);
768 if (!o || !(o->flags & COMPLETE)) {
770 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
774 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
780 static int sideband_demux(int in, int out, void *data)
785 ret = recv_sideband("fetch-pack", xd[0], out);
790 static int get_pack(struct fetch_pack_args *args,
791 int xd[2], char **pack_lockfile)
794 int do_keep = args->keep_pack;
795 const char *cmd_name;
796 struct pack_header header;
798 struct child_process cmd = CHILD_PROCESS_INIT;
801 memset(&demux, 0, sizeof(demux));
803 /* xd[] is talking with upload-pack; subprocess reads from
804 * xd[0], spits out band#2 to stderr, and feeds us band#1
805 * through demux->out.
807 demux.proc = sideband_demux;
810 demux.isolate_sigpipe = 1;
811 if (start_async(&demux))
812 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
817 if (!args->keep_pack && unpack_limit) {
819 if (read_pack_header(demux.out, &header))
820 die(_("protocol error: bad pack header"));
822 if (ntohl(header.hdr_entries) < unpack_limit)
828 if (alternate_shallow_file) {
829 argv_array_push(&cmd.args, "--shallow-file");
830 argv_array_push(&cmd.args, alternate_shallow_file);
836 cmd_name = "index-pack";
837 argv_array_push(&cmd.args, cmd_name);
838 argv_array_push(&cmd.args, "--stdin");
839 if (!args->quiet && !args->no_progress)
840 argv_array_push(&cmd.args, "-v");
841 if (args->use_thin_pack)
842 argv_array_push(&cmd.args, "--fix-thin");
843 if (args->lock_pack || unpack_limit) {
844 char hostname[HOST_NAME_MAX + 1];
845 if (xgethostname(hostname, sizeof(hostname)))
846 xsnprintf(hostname, sizeof(hostname), "localhost");
847 argv_array_pushf(&cmd.args,
848 "--keep=fetch-pack %"PRIuMAX " on %s",
849 (uintmax_t)getpid(), hostname);
851 if (args->check_self_contained_and_connected)
852 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
855 cmd_name = "unpack-objects";
856 argv_array_push(&cmd.args, cmd_name);
857 if (args->quiet || args->no_progress)
858 argv_array_push(&cmd.args, "-q");
859 args->check_self_contained_and_connected = 0;
863 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
864 ntohl(header.hdr_version),
865 ntohl(header.hdr_entries));
866 if (fetch_fsck_objects >= 0
868 : transfer_fsck_objects >= 0
869 ? transfer_fsck_objects
871 argv_array_push(&cmd.args, "--strict");
875 if (start_command(&cmd))
876 die(_("fetch-pack: unable to fork off %s"), cmd_name);
877 if (do_keep && pack_lockfile) {
878 *pack_lockfile = index_pack_lockfile(cmd.out);
883 /* Closed by start_command() */
886 ret = finish_command(&cmd);
887 if (!ret || (args->check_self_contained_and_connected && ret == 1))
888 args->self_contained_and_connected =
889 args->check_self_contained_and_connected &&
892 die(_("%s failed"), cmd_name);
893 if (use_sideband && finish_async(&demux))
894 die(_("error in sideband demultiplexer"));
898 static int cmp_ref_by_name(const void *a_, const void *b_)
900 const struct ref *a = *((const struct ref **)a_);
901 const struct ref *b = *((const struct ref **)b_);
902 return strcmp(a->name, b->name);
905 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
907 const struct ref *orig_ref,
908 struct ref **sought, int nr_sought,
909 struct shallow_info *si,
910 char **pack_lockfile)
912 struct ref *ref = copy_ref_list(orig_ref);
913 struct object_id oid;
914 const char *agent_feature;
917 sort_ref_list(&ref, ref_compare_name);
918 QSORT(sought, nr_sought, cmp_ref_by_name);
920 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
921 die(_("Server does not support shallow clients"));
922 if (args->depth > 0 || args->deepen_since || args->deepen_not)
924 if (server_supports("multi_ack_detailed")) {
925 print_verbose(args, _("Server supports multi_ack_detailed"));
927 if (server_supports("no-done")) {
928 print_verbose(args, _("Server supports no-done"));
929 if (args->stateless_rpc)
933 else if (server_supports("multi_ack")) {
934 print_verbose(args, _("Server supports multi_ack"));
937 if (server_supports("side-band-64k")) {
938 print_verbose(args, _("Server supports side-band-64k"));
941 else if (server_supports("side-band")) {
942 print_verbose(args, _("Server supports side-band"));
945 if (server_supports("allow-tip-sha1-in-want")) {
946 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
947 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
949 if (server_supports("allow-reachable-sha1-in-want")) {
950 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
951 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
953 if (!server_supports("thin-pack"))
954 args->use_thin_pack = 0;
955 if (!server_supports("no-progress"))
956 args->no_progress = 0;
957 if (!server_supports("include-tag"))
958 args->include_tag = 0;
959 if (server_supports("ofs-delta"))
960 print_verbose(args, _("Server supports ofs-delta"));
962 prefer_ofs_delta = 0;
964 if ((agent_feature = server_feature_value("agent", &agent_len))) {
967 print_verbose(args, _("Server version is %.*s"),
968 agent_len, agent_feature);
970 if (server_supports("deepen-since"))
972 else if (args->deepen_since)
973 die(_("Server does not support --shallow-since"));
974 if (server_supports("deepen-not"))
976 else if (args->deepen_not)
977 die(_("Server does not support --shallow-exclude"));
978 if (!server_supports("deepen-relative") && args->deepen_relative)
979 die(_("Server does not support --deepen"));
981 if (everything_local(args, &ref, sought, nr_sought)) {
985 if (find_common(args, fd, &oid, ref) < 0)
986 if (!args->keep_pack)
987 /* When cloning, it is not unusual to have
990 warning(_("no common commits"));
992 if (args->stateless_rpc)
995 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
997 else if (si->nr_ours || si->nr_theirs)
998 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1000 alternate_shallow_file = NULL;
1001 if (get_pack(args, fd, pack_lockfile))
1002 die(_("git fetch-pack: fetch failed."));
1008 static void fetch_pack_config(void)
1010 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1011 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1012 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1013 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1014 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1016 git_config(git_default_config, NULL);
1019 static void fetch_pack_setup(void)
1021 static int did_setup;
1024 fetch_pack_config();
1025 if (0 <= transfer_unpack_limit)
1026 unpack_limit = transfer_unpack_limit;
1027 else if (0 <= fetch_unpack_limit)
1028 unpack_limit = fetch_unpack_limit;
1032 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1034 struct string_list names = STRING_LIST_INIT_NODUP;
1037 for (src = dst = 0; src < nr; src++) {
1038 struct string_list_item *item;
1039 item = string_list_insert(&names, ref[src]->name);
1041 continue; /* already have it */
1042 item->util = ref[src];
1044 ref[dst] = ref[src];
1047 for (src = dst; src < nr; src++)
1049 string_list_clear(&names, 0);
1053 static void update_shallow(struct fetch_pack_args *args,
1054 struct ref **sought, int nr_sought,
1055 struct shallow_info *si)
1057 struct oid_array ref = OID_ARRAY_INIT;
1061 if (args->deepen && alternate_shallow_file) {
1062 if (*alternate_shallow_file == '\0') { /* --unshallow */
1063 unlink_or_warn(git_path_shallow());
1064 rollback_lock_file(&shallow_lock);
1066 commit_lock_file(&shallow_lock);
1070 if (!si->shallow || !si->shallow->nr)
1073 if (args->cloning) {
1075 * remote is shallow, but this is a clone, there are
1076 * no objects in repo to worry about. Accept any
1077 * shallow points that exist in the pack (iow in repo
1078 * after get_pack() and reprepare_packed_git())
1080 struct oid_array extra = OID_ARRAY_INIT;
1081 struct object_id *oid = si->shallow->oid;
1082 for (i = 0; i < si->shallow->nr; i++)
1083 if (has_object_file(&oid[i]))
1084 oid_array_append(&extra, &oid[i]);
1086 setup_alternate_shallow(&shallow_lock,
1087 &alternate_shallow_file,
1089 commit_lock_file(&shallow_lock);
1091 oid_array_clear(&extra);
1095 if (!si->nr_ours && !si->nr_theirs)
1098 remove_nonexistent_theirs_shallow(si);
1099 if (!si->nr_ours && !si->nr_theirs)
1101 for (i = 0; i < nr_sought; i++)
1102 oid_array_append(&ref, &sought[i]->old_oid);
1105 if (args->update_shallow) {
1107 * remote is also shallow, .git/shallow may be updated
1108 * so all refs can be accepted. Make sure we only add
1109 * shallow roots that are actually reachable from new
1112 struct oid_array extra = OID_ARRAY_INIT;
1113 struct object_id *oid = si->shallow->oid;
1114 assign_shallow_commits_to_refs(si, NULL, NULL);
1115 if (!si->nr_ours && !si->nr_theirs) {
1116 oid_array_clear(&ref);
1119 for (i = 0; i < si->nr_ours; i++)
1120 oid_array_append(&extra, &oid[si->ours[i]]);
1121 for (i = 0; i < si->nr_theirs; i++)
1122 oid_array_append(&extra, &oid[si->theirs[i]]);
1123 setup_alternate_shallow(&shallow_lock,
1124 &alternate_shallow_file,
1126 commit_lock_file(&shallow_lock);
1127 oid_array_clear(&extra);
1128 oid_array_clear(&ref);
1133 * remote is also shallow, check what ref is safe to update
1134 * without updating .git/shallow
1136 status = xcalloc(nr_sought, sizeof(*status));
1137 assign_shallow_commits_to_refs(si, NULL, status);
1138 if (si->nr_ours || si->nr_theirs) {
1139 for (i = 0; i < nr_sought; i++)
1141 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1144 oid_array_clear(&ref);
1147 struct ref *fetch_pack(struct fetch_pack_args *args,
1148 int fd[], struct child_process *conn,
1149 const struct ref *ref,
1151 struct ref **sought, int nr_sought,
1152 struct oid_array *shallow,
1153 char **pack_lockfile)
1155 struct ref *ref_cpy;
1156 struct shallow_info si;
1160 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1163 packet_flush(fd[1]);
1164 die(_("no matching remote head"));
1166 prepare_shallow_info(&si, shallow);
1167 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1168 &si, pack_lockfile);
1169 reprepare_packed_git();
1170 update_shallow(args, sought, nr_sought, &si);
1171 clear_shallow_info(&si);
1175 int report_unmatched_refs(struct ref **sought, int nr_sought)
1179 for (i = 0; i < nr_sought; i++) {
1182 switch (sought[i]->match_status) {
1185 case REF_NOT_MATCHED:
1186 error(_("no such remote ref %s"), sought[i]->name);
1188 case REF_UNADVERTISED_NOT_ALLOWED:
1189 error(_("Server does not allow request for unadvertised object %s"),