2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "prio-queue.h"
19 #include "sha1-array.h"
22 #include "object-store.h"
24 static int transfer_unpack_limit = -1;
25 static int fetch_unpack_limit = -1;
26 static int unpack_limit = 100;
27 static int prefer_ofs_delta = 1;
29 static int deepen_since_ok;
30 static int deepen_not_ok;
31 static int fetch_fsck_objects = -1;
32 static int transfer_fsck_objects = -1;
33 static int agent_supported;
34 static int server_supports_filtering;
35 static struct lock_file shallow_lock;
36 static const char *alternate_shallow_file;
38 /* Remember to update object flag allocation in object.h */
39 #define COMPLETE (1U << 0)
40 #define COMMON (1U << 1)
41 #define COMMON_REF (1U << 2)
42 #define SEEN (1U << 3)
43 #define POPPED (1U << 4)
44 #define ALTERNATE (1U << 5)
49 * After sending this many "have"s if we do not get any new ACK , we
50 * give up traversing our history.
52 #define MAX_IN_VAIN 256
54 static struct prio_queue rev_list = { compare_commits_by_commit_date };
55 static int non_common_revs, multi_ack, use_sideband;
56 /* Allow specifying sha1 if it is a ref tip. */
57 #define ALLOW_TIP_SHA1 01
58 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
59 #define ALLOW_REACHABLE_SHA1 02
60 static unsigned int allow_unadvertised_object_request;
62 __attribute__((format (printf, 2, 3)))
63 static inline void print_verbose(const struct fetch_pack_args *args,
71 va_start(params, fmt);
72 vfprintf(stderr, fmt, params);
77 struct alternate_object_cache {
78 struct object **items;
82 static void cache_one_alternate(const char *refname,
83 const struct object_id *oid,
86 struct alternate_object_cache *cache = vcache;
87 struct object *obj = parse_object(oid);
89 if (!obj || (obj->flags & ALTERNATE))
92 obj->flags |= ALTERNATE;
93 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
94 cache->items[cache->nr++] = obj;
97 static void for_each_cached_alternate(void (*cb)(struct object *))
99 static int initialized;
100 static struct alternate_object_cache cache;
104 for_each_alternate_ref(cache_one_alternate, &cache);
108 for (i = 0; i < cache.nr; i++)
112 static void rev_list_push(struct commit *commit, int mark)
114 if (!(commit->object.flags & mark)) {
115 commit->object.flags |= mark;
117 if (parse_commit(commit))
120 prio_queue_put(&rev_list, commit);
122 if (!(commit->object.flags & COMMON))
127 static int rev_list_insert_ref(const char *refname, const struct object_id *oid)
129 struct object *o = deref_tag(parse_object(oid), refname, 0);
131 if (o && o->type == OBJ_COMMIT)
132 rev_list_push((struct commit *)o, SEEN);
137 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
138 int flag, void *cb_data)
140 return rev_list_insert_ref(refname, oid);
143 static int clear_marks(const char *refname, const struct object_id *oid,
144 int flag, void *cb_data)
146 struct object *o = deref_tag(parse_object(oid), refname, 0);
148 if (o && o->type == OBJ_COMMIT)
149 clear_commit_marks((struct commit *)o,
150 COMMON | COMMON_REF | SEEN | POPPED);
155 This function marks a rev and its ancestors as common.
156 In some cases, it is desirable to mark only the ancestors (for example
157 when only the server does not yet know that they are common).
160 static void mark_common(struct commit *commit,
161 int ancestors_only, int dont_parse)
163 if (commit != NULL && !(commit->object.flags & COMMON)) {
164 struct object *o = (struct object *)commit;
169 if (!(o->flags & SEEN))
170 rev_list_push(commit, SEEN);
172 struct commit_list *parents;
174 if (!ancestors_only && !(o->flags & POPPED))
176 if (!o->parsed && !dont_parse)
177 if (parse_commit(commit))
180 for (parents = commit->parents;
182 parents = parents->next)
183 mark_common(parents->item, 0, dont_parse);
189 Get the next rev to send, ignoring the common.
192 static const struct object_id *get_rev(void)
194 struct commit *commit = NULL;
196 while (commit == NULL) {
198 struct commit_list *parents;
200 if (rev_list.nr == 0 || non_common_revs == 0)
203 commit = prio_queue_get(&rev_list);
204 parse_commit(commit);
205 parents = commit->parents;
207 commit->object.flags |= POPPED;
208 if (!(commit->object.flags & COMMON))
211 if (commit->object.flags & COMMON) {
212 /* do not send "have", and ignore ancestors */
214 mark = COMMON | SEEN;
215 } else if (commit->object.flags & COMMON_REF)
216 /* send "have", and ignore ancestors */
217 mark = COMMON | SEEN;
219 /* send "have", also for its ancestors */
223 if (!(parents->item->object.flags & SEEN))
224 rev_list_push(parents->item, mark);
226 mark_common(parents->item, 1, 0);
227 parents = parents->next;
231 return &commit->object.oid;
242 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
244 if (args->stateless_rpc && args->deepen) {
245 /* If we sent a depth we will get back "duplicate"
246 * shallow and unshallow commands every time there
247 * is a block of have lines exchanged.
250 while ((line = packet_read_line(fd, NULL))) {
251 if (starts_with(line, "shallow "))
253 if (starts_with(line, "unshallow "))
255 die(_("git fetch-pack: expected shallow list"));
260 static enum ack_type get_ack(int fd, struct object_id *result_oid)
263 char *line = packet_read_line(fd, &len);
267 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
268 if (!strcmp(line, "NAK"))
270 if (skip_prefix(line, "ACK ", &arg)) {
271 if (!get_oid_hex(arg, result_oid)) {
276 if (strstr(arg, "continue"))
278 if (strstr(arg, "common"))
280 if (strstr(arg, "ready"))
285 if (skip_prefix(line, "ERR ", &arg))
286 die(_("remote error: %s"), arg);
287 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
290 static void send_request(struct fetch_pack_args *args,
291 int fd, struct strbuf *buf)
293 if (args->stateless_rpc) {
294 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
297 write_or_die(fd, buf->buf, buf->len);
300 static void insert_one_alternate_object(struct object *obj)
302 rev_list_insert_ref(NULL, &obj->oid);
305 #define INITIAL_FLUSH 16
306 #define PIPESAFE_FLUSH 32
307 #define LARGE_FLUSH 16384
309 static int next_flush(int stateless_rpc, int count)
312 if (count < LARGE_FLUSH)
315 count = count * 11 / 10;
317 if (count < PIPESAFE_FLUSH)
320 count += PIPESAFE_FLUSH;
325 static int find_common(struct fetch_pack_args *args,
326 int fd[2], struct object_id *result_oid,
330 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
331 const struct object_id *oid;
332 unsigned in_vain = 0;
333 int got_continue = 0;
335 struct strbuf req_buf = STRBUF_INIT;
336 size_t state_len = 0;
338 if (args->stateless_rpc && multi_ack == 1)
339 die(_("--stateless-rpc requires multi_ack_detailed"));
341 for_each_ref(clear_marks, NULL);
344 for_each_ref(rev_list_insert_ref_oid, NULL);
345 for_each_cached_alternate(insert_one_alternate_object);
348 for ( ; refs ; refs = refs->next) {
349 struct object_id *remote = &refs->old_oid;
350 const char *remote_hex;
354 * If that object is complete (i.e. it is an ancestor of a
355 * local ref), we tell them we have it but do not have to
356 * tell them about its ancestors, which they already know
359 * We use lookup_object here because we are only
360 * interested in the case we *know* the object is
361 * reachable and we have already scanned it.
363 if (((o = lookup_object(remote->hash)) != NULL) &&
364 (o->flags & COMPLETE)) {
368 remote_hex = oid_to_hex(remote);
370 struct strbuf c = STRBUF_INIT;
371 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
372 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
373 if (no_done) strbuf_addstr(&c, " no-done");
374 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
375 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
376 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
377 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
378 if (args->no_progress) strbuf_addstr(&c, " no-progress");
379 if (args->include_tag) strbuf_addstr(&c, " include-tag");
380 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
381 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
382 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
383 if (agent_supported) strbuf_addf(&c, " agent=%s",
384 git_user_agent_sanitized());
385 if (args->filter_options.choice)
386 strbuf_addstr(&c, " filter");
387 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
390 packet_buf_write(&req_buf, "want %s\n", remote_hex);
395 strbuf_release(&req_buf);
400 if (is_repository_shallow(the_repository))
401 write_shallow_commits(&req_buf, 1, NULL);
403 packet_buf_write(&req_buf, "deepen %d", args->depth);
404 if (args->deepen_since) {
405 timestamp_t max_age = approxidate(args->deepen_since);
406 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
408 if (args->deepen_not) {
410 for (i = 0; i < args->deepen_not->nr; i++) {
411 struct string_list_item *s = args->deepen_not->items + i;
412 packet_buf_write(&req_buf, "deepen-not %s", s->string);
415 if (server_supports_filtering && args->filter_options.choice)
416 packet_buf_write(&req_buf, "filter %s",
417 args->filter_options.filter_spec);
418 packet_buf_flush(&req_buf);
419 state_len = req_buf.len;
424 struct object_id oid;
426 send_request(args, fd[1], &req_buf);
427 while ((line = packet_read_line(fd[0], NULL))) {
428 if (skip_prefix(line, "shallow ", &arg)) {
429 if (get_oid_hex(arg, &oid))
430 die(_("invalid shallow line: %s"), line);
431 register_shallow(the_repository, &oid);
434 if (skip_prefix(line, "unshallow ", &arg)) {
435 if (get_oid_hex(arg, &oid))
436 die(_("invalid unshallow line: %s"), line);
437 if (!lookup_object(oid.hash))
438 die(_("object not found: %s"), line);
439 /* make sure that it is parsed as shallow */
440 if (!parse_object(&oid))
441 die(_("error in object: %s"), line);
442 if (unregister_shallow(&oid))
443 die(_("no shallow found: %s"), line);
446 die(_("expected shallow/unshallow, got %s"), line);
448 } else if (!args->stateless_rpc)
449 send_request(args, fd[1], &req_buf);
451 if (!args->stateless_rpc) {
452 /* If we aren't using the stateless-rpc interface
453 * we don't need to retain the headers.
455 strbuf_setlen(&req_buf, 0);
461 if (args->no_dependents)
463 while ((oid = get_rev())) {
464 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
465 print_verbose(args, "have %s", oid_to_hex(oid));
467 if (flush_at <= ++count) {
470 packet_buf_flush(&req_buf);
471 send_request(args, fd[1], &req_buf);
472 strbuf_setlen(&req_buf, state_len);
474 flush_at = next_flush(args->stateless_rpc, count);
477 * We keep one window "ahead" of the other side, and
478 * will wait for an ACK only on the next one
480 if (!args->stateless_rpc && count == INITIAL_FLUSH)
483 consume_shallow_list(args, fd[0]);
485 ack = get_ack(fd[0], result_oid);
487 print_verbose(args, _("got %s %d %s"), "ack",
488 ack, oid_to_hex(result_oid));
498 struct commit *commit =
499 lookup_commit(result_oid);
501 die(_("invalid commit %s"), oid_to_hex(result_oid));
502 if (args->stateless_rpc
504 && !(commit->object.flags & COMMON)) {
505 /* We need to replay the have for this object
506 * on the next RPC request so the peer knows
507 * it is in common with us.
509 const char *hex = oid_to_hex(result_oid);
510 packet_buf_write(&req_buf, "have %s\n", hex);
511 state_len = req_buf.len;
513 * Reset in_vain because an ack
514 * for this commit has not been
518 } else if (!args->stateless_rpc
519 || ack != ACK_common)
521 mark_common(commit, 0, 1);
524 if (ack == ACK_ready) {
525 clear_prio_queue(&rev_list);
533 if (got_continue && MAX_IN_VAIN < in_vain) {
534 print_verbose(args, _("giving up"));
540 if (!got_ready || !no_done) {
541 packet_buf_write(&req_buf, "done\n");
542 send_request(args, fd[1], &req_buf);
544 print_verbose(args, _("done"));
549 strbuf_release(&req_buf);
551 if (!got_ready || !no_done)
552 consume_shallow_list(args, fd[0]);
553 while (flushes || multi_ack) {
554 int ack = get_ack(fd[0], result_oid);
556 print_verbose(args, _("got %s (%d) %s"), "ack",
557 ack, oid_to_hex(result_oid));
565 /* it is no error to fetch into a completely empty repo */
566 return count ? retval : 0;
569 static struct commit_list *complete;
571 static int mark_complete(const struct object_id *oid)
573 struct object *o = parse_object(oid);
575 while (o && o->type == OBJ_TAG) {
576 struct tag *t = (struct tag *) o;
578 break; /* broken repository */
579 o->flags |= COMPLETE;
580 o = parse_object(&t->tagged->oid);
582 if (o && o->type == OBJ_COMMIT) {
583 struct commit *commit = (struct commit *)o;
584 if (!(commit->object.flags & COMPLETE)) {
585 commit->object.flags |= COMPLETE;
586 commit_list_insert(commit, &complete);
592 static int mark_complete_oid(const char *refname, const struct object_id *oid,
593 int flag, void *cb_data)
595 return mark_complete(oid);
598 static void mark_recent_complete_commits(struct fetch_pack_args *args,
601 while (complete && cutoff <= complete->item->date) {
602 print_verbose(args, _("Marking %s as complete"),
603 oid_to_hex(&complete->item->object.oid));
604 pop_most_recent_commit(&complete, COMPLETE);
608 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
610 for (; refs; refs = refs->next)
611 oidset_insert(oids, &refs->old_oid);
614 static int tip_oids_contain(struct oidset *tip_oids,
615 struct ref *unmatched, struct ref *newlist,
616 const struct object_id *id)
619 * Note that this only looks at the ref lists the first time it's
620 * called. This works out in filter_refs() because even though it may
621 * add to "newlist" between calls, the additions will always be for
622 * oids that are already in the set.
624 if (!tip_oids->map.map.tablesize) {
625 add_refs_to_oidset(tip_oids, unmatched);
626 add_refs_to_oidset(tip_oids, newlist);
628 return oidset_contains(tip_oids, id);
631 static void filter_refs(struct fetch_pack_args *args,
633 struct ref **sought, int nr_sought)
635 struct ref *newlist = NULL;
636 struct ref **newtail = &newlist;
637 struct ref *unmatched = NULL;
638 struct ref *ref, *next;
639 struct oidset tip_oids = OIDSET_INIT;
643 for (ref = *refs; ref; ref = next) {
647 if (starts_with(ref->name, "refs/") &&
648 check_refname_format(ref->name, 0))
651 while (i < nr_sought) {
652 int cmp = strcmp(ref->name, sought[i]->name);
654 break; /* definitely do not have it */
656 keep = 1; /* definitely have it */
657 sought[i]->match_status = REF_MATCHED;
662 if (!keep && args->fetch_all &&
663 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
670 newtail = &ref->next;
672 ref->next = unmatched;
677 /* Append unmatched requests to the list */
678 for (i = 0; i < nr_sought; i++) {
679 struct object_id oid;
683 if (ref->match_status != REF_NOT_MATCHED)
685 if (parse_oid_hex(ref->name, &oid, &p) ||
687 oidcmp(&oid, &ref->old_oid))
690 if ((allow_unadvertised_object_request &
691 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
692 tip_oids_contain(&tip_oids, unmatched, newlist,
694 ref->match_status = REF_MATCHED;
695 *newtail = copy_ref(ref);
696 newtail = &(*newtail)->next;
698 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
702 oidset_clear(&tip_oids);
703 for (ref = unmatched; ref; ref = next) {
711 static void mark_alternate_complete(struct object *obj)
713 mark_complete(&obj->oid);
716 struct loose_object_iter {
717 struct oidset *loose_object_set;
722 * If the number of refs is not larger than the number of loose objects,
723 * this function stops inserting.
725 static int add_loose_objects_to_set(const struct object_id *oid,
729 struct loose_object_iter *iter = data;
730 oidset_insert(iter->loose_object_set, oid);
731 if (iter->refs == NULL)
734 iter->refs = iter->refs->next;
738 static int everything_local(struct fetch_pack_args *args,
740 struct ref **sought, int nr_sought)
744 int old_save_commit_buffer = save_commit_buffer;
745 timestamp_t cutoff = 0;
746 struct oidset loose_oid_set = OIDSET_INIT;
748 struct loose_object_iter iter = {&loose_oid_set, *refs};
750 /* Enumerate all loose objects or know refs are not so many. */
751 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
754 save_commit_buffer = 0;
756 for (ref = *refs; ref; ref = ref->next) {
758 unsigned int flags = OBJECT_INFO_QUICK;
761 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
763 * I know this does not exist in the loose form,
764 * so check if it exists in a non-loose form.
766 flags |= OBJECT_INFO_IGNORE_LOOSE;
769 if (!has_object_file_with_flags(&ref->old_oid, flags))
771 o = parse_object(&ref->old_oid);
775 /* We already have it -- which may mean that we were
776 * in sync with the other side at some time after
777 * that (it is OK if we guess wrong here).
779 if (o->type == OBJ_COMMIT) {
780 struct commit *commit = (struct commit *)o;
781 if (!cutoff || cutoff < commit->date)
782 cutoff = commit->date;
786 oidset_clear(&loose_oid_set);
788 if (!args->no_dependents) {
790 for_each_ref(mark_complete_oid, NULL);
791 for_each_cached_alternate(mark_alternate_complete);
792 commit_list_sort_by_date(&complete);
794 mark_recent_complete_commits(args, cutoff);
798 * Mark all complete remote refs as common refs.
799 * Don't mark them common yet; the server has to be told so first.
801 for (ref = *refs; ref; ref = ref->next) {
802 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
805 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
808 if (!(o->flags & SEEN)) {
809 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
811 mark_common((struct commit *)o, 1, 1);
816 filter_refs(args, refs, sought, nr_sought);
818 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
819 const struct object_id *remote = &ref->old_oid;
822 o = lookup_object(remote->hash);
823 if (!o || !(o->flags & COMPLETE)) {
825 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
829 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
833 save_commit_buffer = old_save_commit_buffer;
838 static int sideband_demux(int in, int out, void *data)
843 ret = recv_sideband("fetch-pack", xd[0], out);
848 static int get_pack(struct fetch_pack_args *args,
849 int xd[2], char **pack_lockfile)
852 int do_keep = args->keep_pack;
853 const char *cmd_name;
854 struct pack_header header;
856 struct child_process cmd = CHILD_PROCESS_INIT;
859 memset(&demux, 0, sizeof(demux));
861 /* xd[] is talking with upload-pack; subprocess reads from
862 * xd[0], spits out band#2 to stderr, and feeds us band#1
863 * through demux->out.
865 demux.proc = sideband_demux;
868 demux.isolate_sigpipe = 1;
869 if (start_async(&demux))
870 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
875 if (!args->keep_pack && unpack_limit) {
877 if (read_pack_header(demux.out, &header))
878 die(_("protocol error: bad pack header"));
880 if (ntohl(header.hdr_entries) < unpack_limit)
886 if (alternate_shallow_file) {
887 argv_array_push(&cmd.args, "--shallow-file");
888 argv_array_push(&cmd.args, alternate_shallow_file);
891 if (do_keep || args->from_promisor) {
894 cmd_name = "index-pack";
895 argv_array_push(&cmd.args, cmd_name);
896 argv_array_push(&cmd.args, "--stdin");
897 if (!args->quiet && !args->no_progress)
898 argv_array_push(&cmd.args, "-v");
899 if (args->use_thin_pack)
900 argv_array_push(&cmd.args, "--fix-thin");
901 if (do_keep && (args->lock_pack || unpack_limit)) {
902 char hostname[HOST_NAME_MAX + 1];
903 if (xgethostname(hostname, sizeof(hostname)))
904 xsnprintf(hostname, sizeof(hostname), "localhost");
905 argv_array_pushf(&cmd.args,
906 "--keep=fetch-pack %"PRIuMAX " on %s",
907 (uintmax_t)getpid(), hostname);
909 if (args->check_self_contained_and_connected)
910 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
911 if (args->from_promisor)
912 argv_array_push(&cmd.args, "--promisor");
915 cmd_name = "unpack-objects";
916 argv_array_push(&cmd.args, cmd_name);
917 if (args->quiet || args->no_progress)
918 argv_array_push(&cmd.args, "-q");
919 args->check_self_contained_and_connected = 0;
923 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
924 ntohl(header.hdr_version),
925 ntohl(header.hdr_entries));
926 if (fetch_fsck_objects >= 0
928 : transfer_fsck_objects >= 0
929 ? transfer_fsck_objects
931 if (args->from_promisor)
933 * We cannot use --strict in index-pack because it
934 * checks both broken objects and links, but we only
935 * want to check for broken objects.
937 argv_array_push(&cmd.args, "--fsck-objects");
939 argv_array_push(&cmd.args, "--strict");
944 if (start_command(&cmd))
945 die(_("fetch-pack: unable to fork off %s"), cmd_name);
946 if (do_keep && pack_lockfile) {
947 *pack_lockfile = index_pack_lockfile(cmd.out);
952 /* Closed by start_command() */
955 ret = finish_command(&cmd);
956 if (!ret || (args->check_self_contained_and_connected && ret == 1))
957 args->self_contained_and_connected =
958 args->check_self_contained_and_connected &&
961 die(_("%s failed"), cmd_name);
962 if (use_sideband && finish_async(&demux))
963 die(_("error in sideband demultiplexer"));
967 static int cmp_ref_by_name(const void *a_, const void *b_)
969 const struct ref *a = *((const struct ref **)a_);
970 const struct ref *b = *((const struct ref **)b_);
971 return strcmp(a->name, b->name);
974 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
976 const struct ref *orig_ref,
977 struct ref **sought, int nr_sought,
978 struct shallow_info *si,
979 char **pack_lockfile)
981 struct ref *ref = copy_ref_list(orig_ref);
982 struct object_id oid;
983 const char *agent_feature;
986 sort_ref_list(&ref, ref_compare_name);
987 QSORT(sought, nr_sought, cmp_ref_by_name);
989 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
990 die(_("Server does not support shallow clients"));
991 if (args->depth > 0 || args->deepen_since || args->deepen_not)
993 if (server_supports("multi_ack_detailed")) {
994 print_verbose(args, _("Server supports multi_ack_detailed"));
996 if (server_supports("no-done")) {
997 print_verbose(args, _("Server supports no-done"));
998 if (args->stateless_rpc)
1002 else if (server_supports("multi_ack")) {
1003 print_verbose(args, _("Server supports multi_ack"));
1006 if (server_supports("side-band-64k")) {
1007 print_verbose(args, _("Server supports side-band-64k"));
1010 else if (server_supports("side-band")) {
1011 print_verbose(args, _("Server supports side-band"));
1014 if (server_supports("allow-tip-sha1-in-want")) {
1015 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
1016 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1018 if (server_supports("allow-reachable-sha1-in-want")) {
1019 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
1020 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1022 if (!server_supports("thin-pack"))
1023 args->use_thin_pack = 0;
1024 if (!server_supports("no-progress"))
1025 args->no_progress = 0;
1026 if (!server_supports("include-tag"))
1027 args->include_tag = 0;
1028 if (server_supports("ofs-delta"))
1029 print_verbose(args, _("Server supports ofs-delta"));
1031 prefer_ofs_delta = 0;
1033 if (server_supports("filter")) {
1034 server_supports_filtering = 1;
1035 print_verbose(args, _("Server supports filter"));
1036 } else if (args->filter_options.choice) {
1037 warning("filtering not recognized by server, ignoring");
1040 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1041 agent_supported = 1;
1043 print_verbose(args, _("Server version is %.*s"),
1044 agent_len, agent_feature);
1046 if (server_supports("deepen-since"))
1047 deepen_since_ok = 1;
1048 else if (args->deepen_since)
1049 die(_("Server does not support --shallow-since"));
1050 if (server_supports("deepen-not"))
1052 else if (args->deepen_not)
1053 die(_("Server does not support --shallow-exclude"));
1054 if (!server_supports("deepen-relative") && args->deepen_relative)
1055 die(_("Server does not support --deepen"));
1057 if (everything_local(args, &ref, sought, nr_sought)) {
1058 packet_flush(fd[1]);
1061 if (find_common(args, fd, &oid, ref) < 0)
1062 if (!args->keep_pack)
1063 /* When cloning, it is not unusual to have
1066 warning(_("no common commits"));
1068 if (args->stateless_rpc)
1069 packet_flush(fd[1]);
1071 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1073 else if (si->nr_ours || si->nr_theirs)
1074 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1076 alternate_shallow_file = NULL;
1077 if (get_pack(args, fd, pack_lockfile))
1078 die(_("git fetch-pack: fetch failed."));
1084 static void add_shallow_requests(struct strbuf *req_buf,
1085 const struct fetch_pack_args *args)
1087 if (is_repository_shallow(the_repository))
1088 write_shallow_commits(req_buf, 1, NULL);
1089 if (args->depth > 0)
1090 packet_buf_write(req_buf, "deepen %d", args->depth);
1091 if (args->deepen_since) {
1092 timestamp_t max_age = approxidate(args->deepen_since);
1093 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1095 if (args->deepen_not) {
1097 for (i = 0; i < args->deepen_not->nr; i++) {
1098 struct string_list_item *s = args->deepen_not->items + i;
1099 packet_buf_write(req_buf, "deepen-not %s", s->string);
1104 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1106 for ( ; wants ; wants = wants->next) {
1107 const struct object_id *remote = &wants->old_oid;
1108 const char *remote_hex;
1112 * If that object is complete (i.e. it is an ancestor of a
1113 * local ref), we tell them we have it but do not have to
1114 * tell them about its ancestors, which they already know
1117 * We use lookup_object here because we are only
1118 * interested in the case we *know* the object is
1119 * reachable and we have already scanned it.
1121 if (((o = lookup_object(remote->hash)) != NULL) &&
1122 (o->flags & COMPLETE)) {
1126 remote_hex = oid_to_hex(remote);
1127 packet_buf_write(req_buf, "want %s\n", remote_hex);
1131 static void add_common(struct strbuf *req_buf, struct oidset *common)
1133 struct oidset_iter iter;
1134 const struct object_id *oid;
1135 oidset_iter_init(common, &iter);
1137 while ((oid = oidset_iter_next(&iter))) {
1138 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1142 static int add_haves(struct strbuf *req_buf, int *haves_to_send, int *in_vain)
1145 int haves_added = 0;
1146 const struct object_id *oid;
1148 while ((oid = get_rev())) {
1149 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1150 if (++haves_added >= *haves_to_send)
1154 *in_vain += haves_added;
1155 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1157 packet_buf_write(req_buf, "done\n");
1161 /* Increase haves to send on next round */
1162 *haves_to_send = next_flush(1, *haves_to_send);
1167 static int send_fetch_request(int fd_out, const struct fetch_pack_args *args,
1168 const struct ref *wants, struct oidset *common,
1169 int *haves_to_send, int *in_vain)
1172 struct strbuf req_buf = STRBUF_INIT;
1174 if (server_supports_v2("fetch", 1))
1175 packet_buf_write(&req_buf, "command=fetch");
1176 if (server_supports_v2("agent", 0))
1177 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1178 if (args->server_options && args->server_options->nr &&
1179 server_supports_v2("server-option", 1)) {
1181 for (i = 0; i < args->server_options->nr; i++)
1182 packet_write_fmt(fd_out, "server-option=%s",
1183 args->server_options->items[i].string);
1186 packet_buf_delim(&req_buf);
1187 if (args->use_thin_pack)
1188 packet_buf_write(&req_buf, "thin-pack");
1189 if (args->no_progress)
1190 packet_buf_write(&req_buf, "no-progress");
1191 if (args->include_tag)
1192 packet_buf_write(&req_buf, "include-tag");
1193 if (prefer_ofs_delta)
1194 packet_buf_write(&req_buf, "ofs-delta");
1196 /* Add shallow-info and deepen request */
1197 if (server_supports_feature("fetch", "shallow", 0))
1198 add_shallow_requests(&req_buf, args);
1199 else if (is_repository_shallow(the_repository) || args->deepen)
1200 die(_("Server does not support shallow requests"));
1203 if (server_supports_feature("fetch", "filter", 0) &&
1204 args->filter_options.choice) {
1205 print_verbose(args, _("Server supports filter"));
1206 packet_buf_write(&req_buf, "filter %s",
1207 args->filter_options.filter_spec);
1208 } else if (args->filter_options.choice) {
1209 warning("filtering not recognized by server, ignoring");
1213 add_wants(wants, &req_buf);
1215 if (args->no_dependents) {
1216 packet_buf_write(&req_buf, "done");
1219 /* Add all of the common commits we've found in previous rounds */
1220 add_common(&req_buf, common);
1222 /* Add initial haves */
1223 ret = add_haves(&req_buf, haves_to_send, in_vain);
1227 packet_buf_flush(&req_buf);
1228 write_or_die(fd_out, req_buf.buf, req_buf.len);
1230 strbuf_release(&req_buf);
1235 * Processes a section header in a server's response and checks if it matches
1236 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1237 * not consumed); if 0, the line will be consumed and the function will die if
1238 * the section header doesn't match what was expected.
1240 static int process_section_header(struct packet_reader *reader,
1241 const char *section, int peek)
1245 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1246 die("error reading section header '%s'", section);
1248 ret = !strcmp(reader->line, section);
1252 die("expected '%s', received '%s'",
1253 section, reader->line);
1254 packet_reader_read(reader);
1260 static int process_acks(struct packet_reader *reader, struct oidset *common)
1263 int received_ready = 0;
1264 int received_ack = 0;
1266 process_section_header(reader, "acknowledgments", 0);
1267 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1270 if (!strcmp(reader->line, "NAK"))
1273 if (skip_prefix(reader->line, "ACK ", &arg)) {
1274 struct object_id oid;
1275 if (!get_oid_hex(arg, &oid)) {
1276 struct commit *commit;
1277 oidset_insert(common, &oid);
1278 commit = lookup_commit(&oid);
1279 mark_common(commit, 0, 1);
1284 if (!strcmp(reader->line, "ready")) {
1285 clear_prio_queue(&rev_list);
1290 die("unexpected acknowledgment line: '%s'", reader->line);
1293 if (reader->status != PACKET_READ_FLUSH &&
1294 reader->status != PACKET_READ_DELIM)
1295 die("error processing acks: %d", reader->status);
1297 /* return 0 if no common, 1 if there are common, or 2 if ready */
1298 return received_ready ? 2 : (received_ack ? 1 : 0);
1301 static void receive_shallow_info(struct fetch_pack_args *args,
1302 struct packet_reader *reader)
1304 process_section_header(reader, "shallow-info", 0);
1305 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1307 struct object_id oid;
1309 if (skip_prefix(reader->line, "shallow ", &arg)) {
1310 if (get_oid_hex(arg, &oid))
1311 die(_("invalid shallow line: %s"), reader->line);
1312 register_shallow(the_repository, &oid);
1315 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1316 if (get_oid_hex(arg, &oid))
1317 die(_("invalid unshallow line: %s"), reader->line);
1318 if (!lookup_object(oid.hash))
1319 die(_("object not found: %s"), reader->line);
1320 /* make sure that it is parsed as shallow */
1321 if (!parse_object(&oid))
1322 die(_("error in object: %s"), reader->line);
1323 if (unregister_shallow(&oid))
1324 die(_("no shallow found: %s"), reader->line);
1327 die(_("expected shallow/unshallow, got %s"), reader->line);
1330 if (reader->status != PACKET_READ_FLUSH &&
1331 reader->status != PACKET_READ_DELIM)
1332 die("error processing shallow info: %d", reader->status);
1334 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1339 FETCH_CHECK_LOCAL = 0,
1346 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1348 const struct ref *orig_ref,
1349 struct ref **sought, int nr_sought,
1350 char **pack_lockfile)
1352 struct ref *ref = copy_ref_list(orig_ref);
1353 enum fetch_state state = FETCH_CHECK_LOCAL;
1354 struct oidset common = OIDSET_INIT;
1355 struct packet_reader reader;
1357 int haves_to_send = INITIAL_FLUSH;
1358 packet_reader_init(&reader, fd[0], NULL, 0,
1359 PACKET_READ_CHOMP_NEWLINE);
1361 while (state != FETCH_DONE) {
1363 case FETCH_CHECK_LOCAL:
1364 sort_ref_list(&ref, ref_compare_name);
1365 QSORT(sought, nr_sought, cmp_ref_by_name);
1367 /* v2 supports these by default */
1368 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1370 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1374 for_each_ref(clear_marks, NULL);
1377 for_each_ref(rev_list_insert_ref_oid, NULL);
1378 for_each_cached_alternate(insert_one_alternate_object);
1380 /* Filter 'ref' by 'sought' and those that aren't local */
1381 if (everything_local(args, &ref, sought, nr_sought))
1384 state = FETCH_SEND_REQUEST;
1386 case FETCH_SEND_REQUEST:
1387 if (send_fetch_request(fd[1], args, ref, &common,
1388 &haves_to_send, &in_vain))
1389 state = FETCH_GET_PACK;
1391 state = FETCH_PROCESS_ACKS;
1393 case FETCH_PROCESS_ACKS:
1394 /* Process ACKs/NAKs */
1395 switch (process_acks(&reader, &common)) {
1397 state = FETCH_GET_PACK;
1403 state = FETCH_SEND_REQUEST;
1407 case FETCH_GET_PACK:
1408 /* Check for shallow-info section */
1409 if (process_section_header(&reader, "shallow-info", 1))
1410 receive_shallow_info(args, &reader);
1413 process_section_header(&reader, "packfile", 0);
1414 if (get_pack(args, fd, pack_lockfile))
1415 die(_("git fetch-pack: fetch failed."));
1424 oidset_clear(&common);
1428 static void fetch_pack_config(void)
1430 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1431 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1432 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1433 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1434 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1436 git_config(git_default_config, NULL);
1439 static void fetch_pack_setup(void)
1441 static int did_setup;
1444 fetch_pack_config();
1445 if (0 <= transfer_unpack_limit)
1446 unpack_limit = transfer_unpack_limit;
1447 else if (0 <= fetch_unpack_limit)
1448 unpack_limit = fetch_unpack_limit;
1452 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1454 struct string_list names = STRING_LIST_INIT_NODUP;
1457 for (src = dst = 0; src < nr; src++) {
1458 struct string_list_item *item;
1459 item = string_list_insert(&names, ref[src]->name);
1461 continue; /* already have it */
1462 item->util = ref[src];
1464 ref[dst] = ref[src];
1467 for (src = dst; src < nr; src++)
1469 string_list_clear(&names, 0);
1473 static void update_shallow(struct fetch_pack_args *args,
1474 struct ref **sought, int nr_sought,
1475 struct shallow_info *si)
1477 struct oid_array ref = OID_ARRAY_INIT;
1481 if (args->deepen && alternate_shallow_file) {
1482 if (*alternate_shallow_file == '\0') { /* --unshallow */
1483 unlink_or_warn(git_path_shallow(the_repository));
1484 rollback_lock_file(&shallow_lock);
1486 commit_lock_file(&shallow_lock);
1490 if (!si->shallow || !si->shallow->nr)
1493 if (args->cloning) {
1495 * remote is shallow, but this is a clone, there are
1496 * no objects in repo to worry about. Accept any
1497 * shallow points that exist in the pack (iow in repo
1498 * after get_pack() and reprepare_packed_git())
1500 struct oid_array extra = OID_ARRAY_INIT;
1501 struct object_id *oid = si->shallow->oid;
1502 for (i = 0; i < si->shallow->nr; i++)
1503 if (has_object_file(&oid[i]))
1504 oid_array_append(&extra, &oid[i]);
1506 setup_alternate_shallow(&shallow_lock,
1507 &alternate_shallow_file,
1509 commit_lock_file(&shallow_lock);
1511 oid_array_clear(&extra);
1515 if (!si->nr_ours && !si->nr_theirs)
1518 remove_nonexistent_theirs_shallow(si);
1519 if (!si->nr_ours && !si->nr_theirs)
1521 for (i = 0; i < nr_sought; i++)
1522 oid_array_append(&ref, &sought[i]->old_oid);
1525 if (args->update_shallow) {
1527 * remote is also shallow, .git/shallow may be updated
1528 * so all refs can be accepted. Make sure we only add
1529 * shallow roots that are actually reachable from new
1532 struct oid_array extra = OID_ARRAY_INIT;
1533 struct object_id *oid = si->shallow->oid;
1534 assign_shallow_commits_to_refs(si, NULL, NULL);
1535 if (!si->nr_ours && !si->nr_theirs) {
1536 oid_array_clear(&ref);
1539 for (i = 0; i < si->nr_ours; i++)
1540 oid_array_append(&extra, &oid[si->ours[i]]);
1541 for (i = 0; i < si->nr_theirs; i++)
1542 oid_array_append(&extra, &oid[si->theirs[i]]);
1543 setup_alternate_shallow(&shallow_lock,
1544 &alternate_shallow_file,
1546 commit_lock_file(&shallow_lock);
1547 oid_array_clear(&extra);
1548 oid_array_clear(&ref);
1553 * remote is also shallow, check what ref is safe to update
1554 * without updating .git/shallow
1556 status = xcalloc(nr_sought, sizeof(*status));
1557 assign_shallow_commits_to_refs(si, NULL, status);
1558 if (si->nr_ours || si->nr_theirs) {
1559 for (i = 0; i < nr_sought; i++)
1561 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1564 oid_array_clear(&ref);
1567 struct ref *fetch_pack(struct fetch_pack_args *args,
1568 int fd[], struct child_process *conn,
1569 const struct ref *ref,
1571 struct ref **sought, int nr_sought,
1572 struct oid_array *shallow,
1573 char **pack_lockfile,
1574 enum protocol_version version)
1576 struct ref *ref_cpy;
1577 struct shallow_info si;
1581 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1584 packet_flush(fd[1]);
1585 die(_("no matching remote head"));
1587 prepare_shallow_info(&si, shallow);
1588 if (version == protocol_v2)
1589 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1592 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1593 &si, pack_lockfile);
1594 reprepare_packed_git(the_repository);
1595 update_shallow(args, sought, nr_sought, &si);
1596 clear_shallow_info(&si);
1600 int report_unmatched_refs(struct ref **sought, int nr_sought)
1604 for (i = 0; i < nr_sought; i++) {
1607 switch (sought[i]->match_status) {
1610 case REF_NOT_MATCHED:
1611 error(_("no such remote ref %s"), sought[i]->name);
1613 case REF_UNADVERTISED_NOT_ALLOWED:
1614 error(_("Server does not allow request for unadvertised object %s"),