11 #include "fetch-pack.h"
13 #include "run-command.h"
15 #include "transport.h"
17 #include "prio-queue.h"
18 #include "sha1-array.h"
22 static int transfer_unpack_limit = -1;
23 static int fetch_unpack_limit = -1;
24 static int unpack_limit = 100;
25 static int prefer_ofs_delta = 1;
27 static int deepen_since_ok;
28 static int deepen_not_ok;
29 static int fetch_fsck_objects = -1;
30 static int transfer_fsck_objects = -1;
31 static int agent_supported;
32 static int server_supports_filtering;
33 static struct lock_file shallow_lock;
34 static const char *alternate_shallow_file;
36 /* Remember to update object flag allocation in object.h */
37 #define COMPLETE (1U << 0)
38 #define COMMON (1U << 1)
39 #define COMMON_REF (1U << 2)
40 #define SEEN (1U << 3)
41 #define POPPED (1U << 4)
42 #define ALTERNATE (1U << 5)
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
50 #define MAX_IN_VAIN 256
52 static struct prio_queue rev_list = { compare_commits_by_commit_date };
53 static int non_common_revs, 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 char *refname,
81 const struct object_id *oid,
84 struct alternate_object_cache *cache = vcache;
85 struct object *obj = parse_object(oid);
87 if (!obj || (obj->flags & ALTERNATE))
90 obj->flags |= ALTERNATE;
91 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
92 cache->items[cache->nr++] = obj;
95 static void for_each_cached_alternate(void (*cb)(struct object *))
97 static int initialized;
98 static struct alternate_object_cache cache;
102 for_each_alternate_ref(cache_one_alternate, &cache);
106 for (i = 0; i < cache.nr; i++)
110 static void rev_list_push(struct commit *commit, int mark)
112 if (!(commit->object.flags & mark)) {
113 commit->object.flags |= mark;
115 if (parse_commit(commit))
118 prio_queue_put(&rev_list, commit);
120 if (!(commit->object.flags & COMMON))
125 static int rev_list_insert_ref(const char *refname, const struct object_id *oid)
127 struct object *o = deref_tag(parse_object(oid), refname, 0);
129 if (o && o->type == OBJ_COMMIT)
130 rev_list_push((struct commit *)o, SEEN);
135 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
136 int flag, void *cb_data)
138 return rev_list_insert_ref(refname, oid);
141 static int clear_marks(const char *refname, const struct object_id *oid,
142 int flag, void *cb_data)
144 struct object *o = deref_tag(parse_object(oid), refname, 0);
146 if (o && o->type == OBJ_COMMIT)
147 clear_commit_marks((struct commit *)o,
148 COMMON | COMMON_REF | SEEN | POPPED);
153 This function marks a rev and its ancestors as common.
154 In some cases, it is desirable to mark only the ancestors (for example
155 when only the server does not yet know that they are common).
158 static void mark_common(struct commit *commit,
159 int ancestors_only, int dont_parse)
161 if (commit != NULL && !(commit->object.flags & COMMON)) {
162 struct object *o = (struct object *)commit;
167 if (!(o->flags & SEEN))
168 rev_list_push(commit, SEEN);
170 struct commit_list *parents;
172 if (!ancestors_only && !(o->flags & POPPED))
174 if (!o->parsed && !dont_parse)
175 if (parse_commit(commit))
178 for (parents = commit->parents;
180 parents = parents->next)
181 mark_common(parents->item, 0, dont_parse);
187 Get the next rev to send, ignoring the common.
190 static const struct object_id *get_rev(void)
192 struct commit *commit = NULL;
194 while (commit == NULL) {
196 struct commit_list *parents;
198 if (rev_list.nr == 0 || non_common_revs == 0)
201 commit = prio_queue_get(&rev_list);
202 parse_commit(commit);
203 parents = commit->parents;
205 commit->object.flags |= POPPED;
206 if (!(commit->object.flags & COMMON))
209 if (commit->object.flags & COMMON) {
210 /* do not send "have", and ignore ancestors */
212 mark = COMMON | SEEN;
213 } else if (commit->object.flags & COMMON_REF)
214 /* send "have", and ignore ancestors */
215 mark = COMMON | SEEN;
217 /* send "have", also for its ancestors */
221 if (!(parents->item->object.flags & SEEN))
222 rev_list_push(parents->item, mark);
224 mark_common(parents->item, 1, 0);
225 parents = parents->next;
229 return &commit->object.oid;
240 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
242 if (args->stateless_rpc && args->deepen) {
243 /* If we sent a depth we will get back "duplicate"
244 * shallow and unshallow commands every time there
245 * is a block of have lines exchanged.
248 while ((line = packet_read_line(fd, NULL))) {
249 if (starts_with(line, "shallow "))
251 if (starts_with(line, "unshallow "))
253 die(_("git fetch-pack: expected shallow list"));
258 static enum ack_type get_ack(int fd, struct object_id *result_oid)
261 char *line = packet_read_line(fd, &len);
265 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
266 if (!strcmp(line, "NAK"))
268 if (skip_prefix(line, "ACK ", &arg)) {
269 if (!get_oid_hex(arg, result_oid)) {
274 if (strstr(arg, "continue"))
276 if (strstr(arg, "common"))
278 if (strstr(arg, "ready"))
283 if (skip_prefix(line, "ERR ", &arg))
284 die(_("remote error: %s"), arg);
285 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
288 static void send_request(struct fetch_pack_args *args,
289 int fd, struct strbuf *buf)
291 if (args->stateless_rpc) {
292 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
295 write_or_die(fd, buf->buf, buf->len);
298 static void insert_one_alternate_object(struct object *obj)
300 rev_list_insert_ref(NULL, &obj->oid);
303 #define INITIAL_FLUSH 16
304 #define PIPESAFE_FLUSH 32
305 #define LARGE_FLUSH 16384
307 static int next_flush(struct fetch_pack_args *args, int count)
309 if (args->stateless_rpc) {
310 if (count < LARGE_FLUSH)
313 count = count * 11 / 10;
315 if (count < PIPESAFE_FLUSH)
318 count += PIPESAFE_FLUSH;
323 static int find_common(struct fetch_pack_args *args,
324 int fd[2], struct object_id *result_oid,
328 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
329 const struct object_id *oid;
330 unsigned in_vain = 0;
331 int got_continue = 0;
333 struct strbuf req_buf = STRBUF_INIT;
334 size_t state_len = 0;
336 if (args->stateless_rpc && multi_ack == 1)
337 die(_("--stateless-rpc requires multi_ack_detailed"));
339 for_each_ref(clear_marks, NULL);
342 for_each_ref(rev_list_insert_ref_oid, NULL);
343 for_each_cached_alternate(insert_one_alternate_object);
346 for ( ; refs ; refs = refs->next) {
347 struct object_id *remote = &refs->old_oid;
348 const char *remote_hex;
352 * If that object is complete (i.e. it is an ancestor of a
353 * local ref), we tell them we have it but do not have to
354 * tell them about its ancestors, which they already know
357 * We use lookup_object here because we are only
358 * interested in the case we *know* the object is
359 * reachable and we have already scanned it.
361 if (((o = lookup_object(remote->hash)) != NULL) &&
362 (o->flags & COMPLETE)) {
366 remote_hex = oid_to_hex(remote);
368 struct strbuf c = STRBUF_INIT;
369 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
370 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
371 if (no_done) strbuf_addstr(&c, " no-done");
372 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
373 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
374 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
375 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
376 if (args->no_progress) strbuf_addstr(&c, " no-progress");
377 if (args->include_tag) strbuf_addstr(&c, " include-tag");
378 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
379 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
380 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
381 if (agent_supported) strbuf_addf(&c, " agent=%s",
382 git_user_agent_sanitized());
383 if (args->filter_options.choice)
384 strbuf_addstr(&c, " filter");
385 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
388 packet_buf_write(&req_buf, "want %s\n", remote_hex);
393 strbuf_release(&req_buf);
398 if (is_repository_shallow())
399 write_shallow_commits(&req_buf, 1, NULL);
401 packet_buf_write(&req_buf, "deepen %d", args->depth);
402 if (args->deepen_since) {
403 timestamp_t max_age = approxidate(args->deepen_since);
404 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
406 if (args->deepen_not) {
408 for (i = 0; i < args->deepen_not->nr; i++) {
409 struct string_list_item *s = args->deepen_not->items + i;
410 packet_buf_write(&req_buf, "deepen-not %s", s->string);
413 if (server_supports_filtering && args->filter_options.choice)
414 packet_buf_write(&req_buf, "filter %s",
415 args->filter_options.filter_spec);
416 packet_buf_flush(&req_buf);
417 state_len = req_buf.len;
422 struct object_id oid;
424 send_request(args, fd[1], &req_buf);
425 while ((line = packet_read_line(fd[0], NULL))) {
426 if (skip_prefix(line, "shallow ", &arg)) {
427 if (get_oid_hex(arg, &oid))
428 die(_("invalid shallow line: %s"), line);
429 register_shallow(&oid);
432 if (skip_prefix(line, "unshallow ", &arg)) {
433 if (get_oid_hex(arg, &oid))
434 die(_("invalid unshallow line: %s"), line);
435 if (!lookup_object(oid.hash))
436 die(_("object not found: %s"), line);
437 /* make sure that it is parsed as shallow */
438 if (!parse_object(&oid))
439 die(_("error in object: %s"), line);
440 if (unregister_shallow(&oid))
441 die(_("no shallow found: %s"), line);
444 die(_("expected shallow/unshallow, got %s"), line);
446 } else if (!args->stateless_rpc)
447 send_request(args, fd[1], &req_buf);
449 if (!args->stateless_rpc) {
450 /* If we aren't using the stateless-rpc interface
451 * we don't need to retain the headers.
453 strbuf_setlen(&req_buf, 0);
459 if (args->no_dependents)
461 while ((oid = get_rev())) {
462 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
463 print_verbose(args, "have %s", oid_to_hex(oid));
465 if (flush_at <= ++count) {
468 packet_buf_flush(&req_buf);
469 send_request(args, fd[1], &req_buf);
470 strbuf_setlen(&req_buf, state_len);
472 flush_at = next_flush(args, count);
475 * We keep one window "ahead" of the other side, and
476 * will wait for an ACK only on the next one
478 if (!args->stateless_rpc && count == INITIAL_FLUSH)
481 consume_shallow_list(args, fd[0]);
483 ack = get_ack(fd[0], result_oid);
485 print_verbose(args, _("got %s %d %s"), "ack",
486 ack, oid_to_hex(result_oid));
496 struct commit *commit =
497 lookup_commit(result_oid);
499 die(_("invalid commit %s"), oid_to_hex(result_oid));
500 if (args->stateless_rpc
502 && !(commit->object.flags & COMMON)) {
503 /* We need to replay the have for this object
504 * on the next RPC request so the peer knows
505 * it is in common with us.
507 const char *hex = oid_to_hex(result_oid);
508 packet_buf_write(&req_buf, "have %s\n", hex);
509 state_len = req_buf.len;
511 * Reset in_vain because an ack
512 * for this commit has not been
516 } else if (!args->stateless_rpc
517 || ack != ACK_common)
519 mark_common(commit, 0, 1);
522 if (ack == ACK_ready) {
523 clear_prio_queue(&rev_list);
531 if (got_continue && MAX_IN_VAIN < in_vain) {
532 print_verbose(args, _("giving up"));
538 if (!got_ready || !no_done) {
539 packet_buf_write(&req_buf, "done\n");
540 send_request(args, fd[1], &req_buf);
542 print_verbose(args, _("done"));
547 strbuf_release(&req_buf);
549 if (!got_ready || !no_done)
550 consume_shallow_list(args, fd[0]);
551 while (flushes || multi_ack) {
552 int ack = get_ack(fd[0], result_oid);
554 print_verbose(args, _("got %s (%d) %s"), "ack",
555 ack, oid_to_hex(result_oid));
563 /* it is no error to fetch into a completely empty repo */
564 return count ? retval : 0;
567 static struct commit_list *complete;
569 static int mark_complete(const struct object_id *oid)
571 struct object *o = parse_object(oid);
573 while (o && o->type == OBJ_TAG) {
574 struct tag *t = (struct tag *) o;
576 break; /* broken repository */
577 o->flags |= COMPLETE;
578 o = parse_object(&t->tagged->oid);
580 if (o && o->type == OBJ_COMMIT) {
581 struct commit *commit = (struct commit *)o;
582 if (!(commit->object.flags & COMPLETE)) {
583 commit->object.flags |= COMPLETE;
584 commit_list_insert(commit, &complete);
590 static int mark_complete_oid(const char *refname, const struct object_id *oid,
591 int flag, void *cb_data)
593 return mark_complete(oid);
596 static void mark_recent_complete_commits(struct fetch_pack_args *args,
599 while (complete && cutoff <= complete->item->date) {
600 print_verbose(args, _("Marking %s as complete"),
601 oid_to_hex(&complete->item->object.oid));
602 pop_most_recent_commit(&complete, COMPLETE);
606 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
608 for (; refs; refs = refs->next)
609 oidset_insert(oids, &refs->old_oid);
612 static int tip_oids_contain(struct oidset *tip_oids,
613 struct ref *unmatched, struct ref *newlist,
614 const struct object_id *id)
617 * Note that this only looks at the ref lists the first time it's
618 * called. This works out in filter_refs() because even though it may
619 * add to "newlist" between calls, the additions will always be for
620 * oids that are already in the set.
622 if (!tip_oids->map.map.tablesize) {
623 add_refs_to_oidset(tip_oids, unmatched);
624 add_refs_to_oidset(tip_oids, newlist);
626 return oidset_contains(tip_oids, id);
629 static void filter_refs(struct fetch_pack_args *args,
631 struct ref **sought, int nr_sought)
633 struct ref *newlist = NULL;
634 struct ref **newtail = &newlist;
635 struct ref *unmatched = NULL;
636 struct ref *ref, *next;
637 struct oidset tip_oids = OIDSET_INIT;
641 for (ref = *refs; ref; ref = next) {
645 if (starts_with(ref->name, "refs/") &&
646 check_refname_format(ref->name, 0))
649 while (i < nr_sought) {
650 int cmp = strcmp(ref->name, sought[i]->name);
652 break; /* definitely do not have it */
654 keep = 1; /* definitely have it */
655 sought[i]->match_status = REF_MATCHED;
661 if (!keep && args->fetch_all &&
662 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
668 newtail = &ref->next;
670 ref->next = unmatched;
675 /* Append unmatched requests to the list */
676 for (i = 0; i < nr_sought; i++) {
677 struct object_id oid;
681 if (ref->match_status != REF_NOT_MATCHED)
683 if (parse_oid_hex(ref->name, &oid, &p) ||
685 oidcmp(&oid, &ref->old_oid))
688 if ((allow_unadvertised_object_request &
689 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
690 tip_oids_contain(&tip_oids, unmatched, newlist,
692 ref->match_status = REF_MATCHED;
693 *newtail = copy_ref(ref);
694 newtail = &(*newtail)->next;
696 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
700 oidset_clear(&tip_oids);
701 for (ref = unmatched; ref; ref = next) {
709 static void mark_alternate_complete(struct object *obj)
711 mark_complete(&obj->oid);
714 static int everything_local(struct fetch_pack_args *args,
716 struct ref **sought, int nr_sought)
720 int old_save_commit_buffer = save_commit_buffer;
721 timestamp_t cutoff = 0;
723 save_commit_buffer = 0;
725 for (ref = *refs; ref; ref = ref->next) {
728 if (!has_object_file_with_flags(&ref->old_oid,
732 o = parse_object(&ref->old_oid);
736 /* We already have it -- which may mean that we were
737 * in sync with the other side at some time after
738 * that (it is OK if we guess wrong here).
740 if (o->type == OBJ_COMMIT) {
741 struct commit *commit = (struct commit *)o;
742 if (!cutoff || cutoff < commit->date)
743 cutoff = commit->date;
747 if (!args->no_dependents) {
749 for_each_ref(mark_complete_oid, NULL);
750 for_each_cached_alternate(mark_alternate_complete);
751 commit_list_sort_by_date(&complete);
753 mark_recent_complete_commits(args, cutoff);
757 * Mark all complete remote refs as common refs.
758 * Don't mark them common yet; the server has to be told so first.
760 for (ref = *refs; ref; ref = ref->next) {
761 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
764 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
767 if (!(o->flags & SEEN)) {
768 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
770 mark_common((struct commit *)o, 1, 1);
775 filter_refs(args, refs, sought, nr_sought);
777 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
778 const struct object_id *remote = &ref->old_oid;
781 o = lookup_object(remote->hash);
782 if (!o || !(o->flags & COMPLETE)) {
784 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
788 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
792 save_commit_buffer = old_save_commit_buffer;
797 static int sideband_demux(int in, int out, void *data)
802 ret = recv_sideband("fetch-pack", xd[0], out);
807 static int get_pack(struct fetch_pack_args *args,
808 int xd[2], char **pack_lockfile)
811 int do_keep = args->keep_pack;
812 const char *cmd_name;
813 struct pack_header header;
815 struct child_process cmd = CHILD_PROCESS_INIT;
818 memset(&demux, 0, sizeof(demux));
820 /* xd[] is talking with upload-pack; subprocess reads from
821 * xd[0], spits out band#2 to stderr, and feeds us band#1
822 * through demux->out.
824 demux.proc = sideband_demux;
827 demux.isolate_sigpipe = 1;
828 if (start_async(&demux))
829 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
834 if (!args->keep_pack && unpack_limit) {
836 if (read_pack_header(demux.out, &header))
837 die(_("protocol error: bad pack header"));
839 if (ntohl(header.hdr_entries) < unpack_limit)
845 if (alternate_shallow_file) {
846 argv_array_push(&cmd.args, "--shallow-file");
847 argv_array_push(&cmd.args, alternate_shallow_file);
850 if (do_keep || args->from_promisor) {
853 cmd_name = "index-pack";
854 argv_array_push(&cmd.args, cmd_name);
855 argv_array_push(&cmd.args, "--stdin");
856 if (!args->quiet && !args->no_progress)
857 argv_array_push(&cmd.args, "-v");
858 if (args->use_thin_pack)
859 argv_array_push(&cmd.args, "--fix-thin");
860 if (do_keep && (args->lock_pack || unpack_limit)) {
861 char hostname[HOST_NAME_MAX + 1];
862 if (xgethostname(hostname, sizeof(hostname)))
863 xsnprintf(hostname, sizeof(hostname), "localhost");
864 argv_array_pushf(&cmd.args,
865 "--keep=fetch-pack %"PRIuMAX " on %s",
866 (uintmax_t)getpid(), hostname);
868 if (args->check_self_contained_and_connected)
869 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
870 if (args->from_promisor)
871 argv_array_push(&cmd.args, "--promisor");
874 cmd_name = "unpack-objects";
875 argv_array_push(&cmd.args, cmd_name);
876 if (args->quiet || args->no_progress)
877 argv_array_push(&cmd.args, "-q");
878 args->check_self_contained_and_connected = 0;
882 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
883 ntohl(header.hdr_version),
884 ntohl(header.hdr_entries));
885 if (fetch_fsck_objects >= 0
887 : transfer_fsck_objects >= 0
888 ? transfer_fsck_objects
890 argv_array_push(&cmd.args, "--strict");
894 if (start_command(&cmd))
895 die(_("fetch-pack: unable to fork off %s"), cmd_name);
896 if (do_keep && pack_lockfile) {
897 *pack_lockfile = index_pack_lockfile(cmd.out);
902 /* Closed by start_command() */
905 ret = finish_command(&cmd);
906 if (!ret || (args->check_self_contained_and_connected && ret == 1))
907 args->self_contained_and_connected =
908 args->check_self_contained_and_connected &&
911 die(_("%s failed"), cmd_name);
912 if (use_sideband && finish_async(&demux))
913 die(_("error in sideband demultiplexer"));
917 static int cmp_ref_by_name(const void *a_, const void *b_)
919 const struct ref *a = *((const struct ref **)a_);
920 const struct ref *b = *((const struct ref **)b_);
921 return strcmp(a->name, b->name);
924 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
926 const struct ref *orig_ref,
927 struct ref **sought, int nr_sought,
928 struct shallow_info *si,
929 char **pack_lockfile)
931 struct ref *ref = copy_ref_list(orig_ref);
932 struct object_id oid;
933 const char *agent_feature;
936 sort_ref_list(&ref, ref_compare_name);
937 QSORT(sought, nr_sought, cmp_ref_by_name);
939 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
940 die(_("Server does not support shallow clients"));
941 if (args->depth > 0 || args->deepen_since || args->deepen_not)
943 if (server_supports("multi_ack_detailed")) {
944 print_verbose(args, _("Server supports multi_ack_detailed"));
946 if (server_supports("no-done")) {
947 print_verbose(args, _("Server supports no-done"));
948 if (args->stateless_rpc)
952 else if (server_supports("multi_ack")) {
953 print_verbose(args, _("Server supports multi_ack"));
956 if (server_supports("side-band-64k")) {
957 print_verbose(args, _("Server supports side-band-64k"));
960 else if (server_supports("side-band")) {
961 print_verbose(args, _("Server supports side-band"));
964 if (server_supports("allow-tip-sha1-in-want")) {
965 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
966 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
968 if (server_supports("allow-reachable-sha1-in-want")) {
969 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
970 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
972 if (!server_supports("thin-pack"))
973 args->use_thin_pack = 0;
974 if (!server_supports("no-progress"))
975 args->no_progress = 0;
976 if (!server_supports("include-tag"))
977 args->include_tag = 0;
978 if (server_supports("ofs-delta"))
979 print_verbose(args, _("Server supports ofs-delta"));
981 prefer_ofs_delta = 0;
983 if (server_supports("filter")) {
984 server_supports_filtering = 1;
985 print_verbose(args, _("Server supports filter"));
986 } else if (args->filter_options.choice) {
987 warning("filtering not recognized by server, ignoring");
990 if ((agent_feature = server_feature_value("agent", &agent_len))) {
993 print_verbose(args, _("Server version is %.*s"),
994 agent_len, agent_feature);
996 if (server_supports("deepen-since"))
998 else if (args->deepen_since)
999 die(_("Server does not support --shallow-since"));
1000 if (server_supports("deepen-not"))
1002 else if (args->deepen_not)
1003 die(_("Server does not support --shallow-exclude"));
1004 if (!server_supports("deepen-relative") && args->deepen_relative)
1005 die(_("Server does not support --deepen"));
1007 if (everything_local(args, &ref, sought, nr_sought)) {
1008 packet_flush(fd[1]);
1011 if (find_common(args, fd, &oid, ref) < 0)
1012 if (!args->keep_pack)
1013 /* When cloning, it is not unusual to have
1016 warning(_("no common commits"));
1018 if (args->stateless_rpc)
1019 packet_flush(fd[1]);
1021 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1023 else if (si->nr_ours || si->nr_theirs)
1024 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1026 alternate_shallow_file = NULL;
1027 if (get_pack(args, fd, pack_lockfile))
1028 die(_("git fetch-pack: fetch failed."));
1034 static void add_shallow_requests(struct strbuf *req_buf,
1035 const struct fetch_pack_args *args)
1037 if (is_repository_shallow())
1038 write_shallow_commits(req_buf, 1, NULL);
1039 if (args->depth > 0)
1040 packet_buf_write(req_buf, "deepen %d", args->depth);
1041 if (args->deepen_since) {
1042 timestamp_t max_age = approxidate(args->deepen_since);
1043 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1045 if (args->deepen_not) {
1047 for (i = 0; i < args->deepen_not->nr; i++) {
1048 struct string_list_item *s = args->deepen_not->items + i;
1049 packet_buf_write(req_buf, "deepen-not %s", s->string);
1054 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1056 for ( ; wants ; wants = wants->next) {
1057 const struct object_id *remote = &wants->old_oid;
1058 const char *remote_hex;
1062 * If that object is complete (i.e. it is an ancestor of a
1063 * local ref), we tell them we have it but do not have to
1064 * tell them about its ancestors, which they already know
1067 * We use lookup_object here because we are only
1068 * interested in the case we *know* the object is
1069 * reachable and we have already scanned it.
1071 if (((o = lookup_object(remote->hash)) != NULL) &&
1072 (o->flags & COMPLETE)) {
1076 remote_hex = oid_to_hex(remote);
1077 packet_buf_write(req_buf, "want %s\n", remote_hex);
1081 static void add_common(struct strbuf *req_buf, struct oidset *common)
1083 struct oidset_iter iter;
1084 const struct object_id *oid;
1085 oidset_iter_init(common, &iter);
1087 while ((oid = oidset_iter_next(&iter))) {
1088 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1092 static int add_haves(struct strbuf *req_buf, int *in_vain)
1095 int haves_added = 0;
1096 const struct object_id *oid;
1098 while ((oid = get_rev())) {
1099 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1100 if (++haves_added >= INITIAL_FLUSH)
1104 *in_vain += haves_added;
1105 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1107 packet_buf_write(req_buf, "done\n");
1114 static int send_fetch_request(int fd_out, const struct fetch_pack_args *args,
1115 const struct ref *wants, struct oidset *common,
1119 struct strbuf req_buf = STRBUF_INIT;
1121 if (server_supports_v2("fetch", 1))
1122 packet_buf_write(&req_buf, "command=fetch");
1123 if (server_supports_v2("agent", 0))
1124 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1126 packet_buf_delim(&req_buf);
1127 if (args->use_thin_pack)
1128 packet_buf_write(&req_buf, "thin-pack");
1129 if (args->no_progress)
1130 packet_buf_write(&req_buf, "no-progress");
1131 if (args->include_tag)
1132 packet_buf_write(&req_buf, "include-tag");
1133 if (prefer_ofs_delta)
1134 packet_buf_write(&req_buf, "ofs-delta");
1136 /* Add shallow-info and deepen request */
1137 if (server_supports_feature("fetch", "shallow", 1))
1138 add_shallow_requests(&req_buf, args);
1141 add_wants(wants, &req_buf);
1143 /* Add all of the common commits we've found in previous rounds */
1144 add_common(&req_buf, common);
1146 /* Add initial haves */
1147 ret = add_haves(&req_buf, in_vain);
1150 packet_buf_flush(&req_buf);
1151 write_or_die(fd_out, req_buf.buf, req_buf.len);
1153 strbuf_release(&req_buf);
1158 * Processes a section header in a server's response and checks if it matches
1159 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1160 * not consumed); if 0, the line will be consumed and the function will die if
1161 * the section header doesn't match what was expected.
1163 static int process_section_header(struct packet_reader *reader,
1164 const char *section, int peek)
1168 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1169 die("error reading section header '%s'", section);
1171 ret = !strcmp(reader->line, section);
1175 die("expected '%s', received '%s'",
1176 section, reader->line);
1177 packet_reader_read(reader);
1183 static int process_acks(struct packet_reader *reader, struct oidset *common)
1186 int received_ready = 0;
1187 int received_ack = 0;
1189 process_section_header(reader, "acknowledgments", 0);
1190 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1193 if (!strcmp(reader->line, "NAK"))
1196 if (skip_prefix(reader->line, "ACK ", &arg)) {
1197 struct object_id oid;
1198 if (!get_oid_hex(arg, &oid)) {
1199 struct commit *commit;
1200 oidset_insert(common, &oid);
1201 commit = lookup_commit(&oid);
1202 mark_common(commit, 0, 1);
1207 if (!strcmp(reader->line, "ready")) {
1208 clear_prio_queue(&rev_list);
1213 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
1216 if (reader->status != PACKET_READ_FLUSH &&
1217 reader->status != PACKET_READ_DELIM)
1218 die("Error during processing acks: %d", reader->status);
1220 /* return 0 if no common, 1 if there are common, or 2 if ready */
1221 return received_ready ? 2 : (received_ack ? 1 : 0);
1224 static void receive_shallow_info(struct fetch_pack_args *args,
1225 struct packet_reader *reader)
1227 process_section_header(reader, "shallow-info", 0);
1228 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1230 struct object_id oid;
1232 if (skip_prefix(reader->line, "shallow ", &arg)) {
1233 if (get_oid_hex(arg, &oid))
1234 die(_("invalid shallow line: %s"), reader->line);
1235 register_shallow(&oid);
1238 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1239 if (get_oid_hex(arg, &oid))
1240 die(_("invalid unshallow line: %s"), reader->line);
1241 if (!lookup_object(oid.hash))
1242 die(_("object not found: %s"), reader->line);
1243 /* make sure that it is parsed as shallow */
1244 if (!parse_object(&oid))
1245 die(_("error in object: %s"), reader->line);
1246 if (unregister_shallow(&oid))
1247 die(_("no shallow found: %s"), reader->line);
1250 die(_("expected shallow/unshallow, got %s"), reader->line);
1253 if (reader->status != PACKET_READ_FLUSH &&
1254 reader->status != PACKET_READ_DELIM)
1255 die("error processing shallow info: %d", reader->status);
1257 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1262 FETCH_CHECK_LOCAL = 0,
1269 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1271 const struct ref *orig_ref,
1272 struct ref **sought, int nr_sought,
1273 char **pack_lockfile)
1275 struct ref *ref = copy_ref_list(orig_ref);
1276 enum fetch_state state = FETCH_CHECK_LOCAL;
1277 struct oidset common = OIDSET_INIT;
1278 struct packet_reader reader;
1280 packet_reader_init(&reader, fd[0], NULL, 0,
1281 PACKET_READ_CHOMP_NEWLINE);
1283 while (state != FETCH_DONE) {
1285 case FETCH_CHECK_LOCAL:
1286 sort_ref_list(&ref, ref_compare_name);
1287 QSORT(sought, nr_sought, cmp_ref_by_name);
1289 /* v2 supports these by default */
1290 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1292 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1295 /* Filter 'ref' by 'sought' and those that aren't local */
1296 if (everything_local(args, &ref, sought, nr_sought))
1299 state = FETCH_SEND_REQUEST;
1301 case FETCH_SEND_REQUEST:
1302 if (send_fetch_request(fd[1], args, ref, &common, &in_vain))
1303 state = FETCH_GET_PACK;
1305 state = FETCH_PROCESS_ACKS;
1307 case FETCH_PROCESS_ACKS:
1308 /* Process ACKs/NAKs */
1309 switch (process_acks(&reader, &common)) {
1311 state = FETCH_GET_PACK;
1317 state = FETCH_SEND_REQUEST;
1321 case FETCH_GET_PACK:
1322 /* Check for shallow-info section */
1323 if (process_section_header(&reader, "shallow-info", 1))
1324 receive_shallow_info(args, &reader);
1327 process_section_header(&reader, "packfile", 0);
1328 if (get_pack(args, fd, pack_lockfile))
1329 die(_("git fetch-pack: fetch failed."));
1338 oidset_clear(&common);
1342 static void fetch_pack_config(void)
1344 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1345 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1346 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1347 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1348 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1350 git_config(git_default_config, NULL);
1353 static void fetch_pack_setup(void)
1355 static int did_setup;
1358 fetch_pack_config();
1359 if (0 <= transfer_unpack_limit)
1360 unpack_limit = transfer_unpack_limit;
1361 else if (0 <= fetch_unpack_limit)
1362 unpack_limit = fetch_unpack_limit;
1366 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1368 struct string_list names = STRING_LIST_INIT_NODUP;
1371 for (src = dst = 0; src < nr; src++) {
1372 struct string_list_item *item;
1373 item = string_list_insert(&names, ref[src]->name);
1375 continue; /* already have it */
1376 item->util = ref[src];
1378 ref[dst] = ref[src];
1381 for (src = dst; src < nr; src++)
1383 string_list_clear(&names, 0);
1387 static void update_shallow(struct fetch_pack_args *args,
1388 struct ref **sought, int nr_sought,
1389 struct shallow_info *si)
1391 struct oid_array ref = OID_ARRAY_INIT;
1395 if (args->deepen && alternate_shallow_file) {
1396 if (*alternate_shallow_file == '\0') { /* --unshallow */
1397 unlink_or_warn(git_path_shallow());
1398 rollback_lock_file(&shallow_lock);
1400 commit_lock_file(&shallow_lock);
1404 if (!si->shallow || !si->shallow->nr)
1407 if (args->cloning) {
1409 * remote is shallow, but this is a clone, there are
1410 * no objects in repo to worry about. Accept any
1411 * shallow points that exist in the pack (iow in repo
1412 * after get_pack() and reprepare_packed_git())
1414 struct oid_array extra = OID_ARRAY_INIT;
1415 struct object_id *oid = si->shallow->oid;
1416 for (i = 0; i < si->shallow->nr; i++)
1417 if (has_object_file(&oid[i]))
1418 oid_array_append(&extra, &oid[i]);
1420 setup_alternate_shallow(&shallow_lock,
1421 &alternate_shallow_file,
1423 commit_lock_file(&shallow_lock);
1425 oid_array_clear(&extra);
1429 if (!si->nr_ours && !si->nr_theirs)
1432 remove_nonexistent_theirs_shallow(si);
1433 if (!si->nr_ours && !si->nr_theirs)
1435 for (i = 0; i < nr_sought; i++)
1436 oid_array_append(&ref, &sought[i]->old_oid);
1439 if (args->update_shallow) {
1441 * remote is also shallow, .git/shallow may be updated
1442 * so all refs can be accepted. Make sure we only add
1443 * shallow roots that are actually reachable from new
1446 struct oid_array extra = OID_ARRAY_INIT;
1447 struct object_id *oid = si->shallow->oid;
1448 assign_shallow_commits_to_refs(si, NULL, NULL);
1449 if (!si->nr_ours && !si->nr_theirs) {
1450 oid_array_clear(&ref);
1453 for (i = 0; i < si->nr_ours; i++)
1454 oid_array_append(&extra, &oid[si->ours[i]]);
1455 for (i = 0; i < si->nr_theirs; i++)
1456 oid_array_append(&extra, &oid[si->theirs[i]]);
1457 setup_alternate_shallow(&shallow_lock,
1458 &alternate_shallow_file,
1460 commit_lock_file(&shallow_lock);
1461 oid_array_clear(&extra);
1462 oid_array_clear(&ref);
1467 * remote is also shallow, check what ref is safe to update
1468 * without updating .git/shallow
1470 status = xcalloc(nr_sought, sizeof(*status));
1471 assign_shallow_commits_to_refs(si, NULL, status);
1472 if (si->nr_ours || si->nr_theirs) {
1473 for (i = 0; i < nr_sought; i++)
1475 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1478 oid_array_clear(&ref);
1481 struct ref *fetch_pack(struct fetch_pack_args *args,
1482 int fd[], struct child_process *conn,
1483 const struct ref *ref,
1485 struct ref **sought, int nr_sought,
1486 struct oid_array *shallow,
1487 char **pack_lockfile,
1488 enum protocol_version version)
1490 struct ref *ref_cpy;
1491 struct shallow_info si;
1495 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1498 packet_flush(fd[1]);
1499 die(_("no matching remote head"));
1501 prepare_shallow_info(&si, shallow);
1502 if (version == protocol_v2)
1503 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1506 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1507 &si, pack_lockfile);
1508 reprepare_packed_git();
1509 update_shallow(args, sought, nr_sought, &si);
1510 clear_shallow_info(&si);
1514 int report_unmatched_refs(struct ref **sought, int nr_sought)
1518 for (i = 0; i < nr_sought; i++) {
1521 switch (sought[i]->match_status) {
1524 case REF_NOT_MATCHED:
1525 error(_("no such remote ref %s"), sought[i]->name);
1527 case REF_UNADVERTISED_NOT_ALLOWED:
1528 error(_("Server does not allow request for unadvertised object %s"),