2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "sha1-array.h"
21 #include "fetch-negotiator.h"
23 static int transfer_unpack_limit = -1;
24 static int fetch_unpack_limit = -1;
25 static int unpack_limit = 100;
26 static int prefer_ofs_delta = 1;
28 static int deepen_since_ok;
29 static int deepen_not_ok;
30 static int fetch_fsck_objects = -1;
31 static int transfer_fsck_objects = -1;
32 static int agent_supported;
33 static int server_supports_filtering;
34 static struct lock_file shallow_lock;
35 static const char *alternate_shallow_file;
37 /* Remember to update object flag allocation in object.h */
38 #define COMPLETE (1U << 0)
39 #define ALTERNATE (1U << 1)
42 * After sending this many "have"s if we do not get any new ACK , we
43 * give up traversing our history.
45 #define MAX_IN_VAIN 256
47 static int multi_ack, use_sideband;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 static unsigned int allow_unadvertised_object_request;
54 __attribute__((format (printf, 2, 3)))
55 static inline void print_verbose(const struct fetch_pack_args *args,
63 va_start(params, fmt);
64 vfprintf(stderr, fmt, params);
69 struct alternate_object_cache {
70 struct object **items;
74 static void cache_one_alternate(const char *refname,
75 const struct object_id *oid,
78 struct alternate_object_cache *cache = vcache;
79 struct object *obj = parse_object(oid);
81 if (!obj || (obj->flags & ALTERNATE))
84 obj->flags |= ALTERNATE;
85 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
86 cache->items[cache->nr++] = obj;
89 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
90 void (*cb)(struct fetch_negotiator *,
93 static int initialized;
94 static struct alternate_object_cache cache;
98 for_each_alternate_ref(cache_one_alternate, &cache);
102 for (i = 0; i < cache.nr; i++)
103 cb(negotiator, cache.items[i]);
106 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
108 const struct object_id *oid)
110 struct object *o = deref_tag(parse_object(oid), refname, 0);
112 if (o && o->type == OBJ_COMMIT)
113 negotiator->add_tip(negotiator, (struct commit *)o);
118 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
119 int flag, void *cb_data)
121 return rev_list_insert_ref(cb_data, refname, oid);
132 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
134 if (args->stateless_rpc && args->deepen) {
135 /* If we sent a depth we will get back "duplicate"
136 * shallow and unshallow commands every time there
137 * is a block of have lines exchanged.
140 while ((line = packet_read_line(fd, NULL))) {
141 if (starts_with(line, "shallow "))
143 if (starts_with(line, "unshallow "))
145 die(_("git fetch-pack: expected shallow list"));
150 static enum ack_type get_ack(int fd, struct object_id *result_oid)
153 char *line = packet_read_line(fd, &len);
157 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
158 if (!strcmp(line, "NAK"))
160 if (skip_prefix(line, "ACK ", &arg)) {
161 if (!get_oid_hex(arg, result_oid)) {
166 if (strstr(arg, "continue"))
168 if (strstr(arg, "common"))
170 if (strstr(arg, "ready"))
175 if (skip_prefix(line, "ERR ", &arg))
176 die(_("remote error: %s"), arg);
177 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
180 static void send_request(struct fetch_pack_args *args,
181 int fd, struct strbuf *buf)
183 if (args->stateless_rpc) {
184 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
187 write_or_die(fd, buf->buf, buf->len);
190 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
193 rev_list_insert_ref(negotiator, NULL, &obj->oid);
196 #define INITIAL_FLUSH 16
197 #define PIPESAFE_FLUSH 32
198 #define LARGE_FLUSH 16384
200 static int next_flush(int stateless_rpc, int count)
203 if (count < LARGE_FLUSH)
206 count = count * 11 / 10;
208 if (count < PIPESAFE_FLUSH)
211 count += PIPESAFE_FLUSH;
216 static int find_common(struct fetch_negotiator *negotiator,
217 struct fetch_pack_args *args,
218 int fd[2], struct object_id *result_oid,
222 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
223 const struct object_id *oid;
224 unsigned in_vain = 0;
225 int got_continue = 0;
227 struct strbuf req_buf = STRBUF_INIT;
228 size_t state_len = 0;
230 if (args->stateless_rpc && multi_ack == 1)
231 die(_("--stateless-rpc requires multi_ack_detailed"));
233 for_each_ref(rev_list_insert_ref_oid, negotiator);
234 for_each_cached_alternate(negotiator, insert_one_alternate_object);
237 for ( ; refs ; refs = refs->next) {
238 struct object_id *remote = &refs->old_oid;
239 const char *remote_hex;
243 * If that object is complete (i.e. it is an ancestor of a
244 * local ref), we tell them we have it but do not have to
245 * tell them about its ancestors, which they already know
248 * We use lookup_object here because we are only
249 * interested in the case we *know* the object is
250 * reachable and we have already scanned it.
252 if (((o = lookup_object(remote->hash)) != NULL) &&
253 (o->flags & COMPLETE)) {
257 remote_hex = oid_to_hex(remote);
259 struct strbuf c = STRBUF_INIT;
260 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
261 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
262 if (no_done) strbuf_addstr(&c, " no-done");
263 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
264 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
265 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
266 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
267 if (args->no_progress) strbuf_addstr(&c, " no-progress");
268 if (args->include_tag) strbuf_addstr(&c, " include-tag");
269 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
270 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
271 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
272 if (agent_supported) strbuf_addf(&c, " agent=%s",
273 git_user_agent_sanitized());
274 if (args->filter_options.choice)
275 strbuf_addstr(&c, " filter");
276 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
279 packet_buf_write(&req_buf, "want %s\n", remote_hex);
284 strbuf_release(&req_buf);
289 if (is_repository_shallow())
290 write_shallow_commits(&req_buf, 1, NULL);
292 packet_buf_write(&req_buf, "deepen %d", args->depth);
293 if (args->deepen_since) {
294 timestamp_t max_age = approxidate(args->deepen_since);
295 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
297 if (args->deepen_not) {
299 for (i = 0; i < args->deepen_not->nr; i++) {
300 struct string_list_item *s = args->deepen_not->items + i;
301 packet_buf_write(&req_buf, "deepen-not %s", s->string);
304 if (server_supports_filtering && args->filter_options.choice)
305 packet_buf_write(&req_buf, "filter %s",
306 args->filter_options.filter_spec);
307 packet_buf_flush(&req_buf);
308 state_len = req_buf.len;
313 struct object_id oid;
315 send_request(args, fd[1], &req_buf);
316 while ((line = packet_read_line(fd[0], NULL))) {
317 if (skip_prefix(line, "shallow ", &arg)) {
318 if (get_oid_hex(arg, &oid))
319 die(_("invalid shallow line: %s"), line);
320 register_shallow(&oid);
323 if (skip_prefix(line, "unshallow ", &arg)) {
324 if (get_oid_hex(arg, &oid))
325 die(_("invalid unshallow line: %s"), line);
326 if (!lookup_object(oid.hash))
327 die(_("object not found: %s"), line);
328 /* make sure that it is parsed as shallow */
329 if (!parse_object(&oid))
330 die(_("error in object: %s"), line);
331 if (unregister_shallow(&oid))
332 die(_("no shallow found: %s"), line);
335 die(_("expected shallow/unshallow, got %s"), line);
337 } else if (!args->stateless_rpc)
338 send_request(args, fd[1], &req_buf);
340 if (!args->stateless_rpc) {
341 /* If we aren't using the stateless-rpc interface
342 * we don't need to retain the headers.
344 strbuf_setlen(&req_buf, 0);
350 if (args->no_dependents)
352 while ((oid = negotiator->next(negotiator))) {
353 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
354 print_verbose(args, "have %s", oid_to_hex(oid));
356 if (flush_at <= ++count) {
359 packet_buf_flush(&req_buf);
360 send_request(args, fd[1], &req_buf);
361 strbuf_setlen(&req_buf, state_len);
363 flush_at = next_flush(args->stateless_rpc, count);
366 * We keep one window "ahead" of the other side, and
367 * will wait for an ACK only on the next one
369 if (!args->stateless_rpc && count == INITIAL_FLUSH)
372 consume_shallow_list(args, fd[0]);
374 ack = get_ack(fd[0], result_oid);
376 print_verbose(args, _("got %s %d %s"), "ack",
377 ack, oid_to_hex(result_oid));
387 struct commit *commit =
388 lookup_commit(result_oid);
391 die(_("invalid commit %s"), oid_to_hex(result_oid));
392 was_common = negotiator->ack(negotiator, commit);
393 if (args->stateless_rpc
396 /* We need to replay the have for this object
397 * on the next RPC request so the peer knows
398 * it is in common with us.
400 const char *hex = oid_to_hex(result_oid);
401 packet_buf_write(&req_buf, "have %s\n", hex);
402 state_len = req_buf.len;
404 * Reset in_vain because an ack
405 * for this commit has not been
409 } else if (!args->stateless_rpc
410 || ack != ACK_common)
414 if (ack == ACK_ready)
421 if (got_continue && MAX_IN_VAIN < in_vain) {
422 print_verbose(args, _("giving up"));
430 if (!got_ready || !no_done) {
431 packet_buf_write(&req_buf, "done\n");
432 send_request(args, fd[1], &req_buf);
434 print_verbose(args, _("done"));
439 strbuf_release(&req_buf);
441 if (!got_ready || !no_done)
442 consume_shallow_list(args, fd[0]);
443 while (flushes || multi_ack) {
444 int ack = get_ack(fd[0], result_oid);
446 print_verbose(args, _("got %s (%d) %s"), "ack",
447 ack, oid_to_hex(result_oid));
455 /* it is no error to fetch into a completely empty repo */
456 return count ? retval : 0;
459 static struct commit_list *complete;
461 static int mark_complete(const struct object_id *oid)
463 struct object *o = parse_object(oid);
465 while (o && o->type == OBJ_TAG) {
466 struct tag *t = (struct tag *) o;
468 break; /* broken repository */
469 o->flags |= COMPLETE;
470 o = parse_object(&t->tagged->oid);
472 if (o && o->type == OBJ_COMMIT) {
473 struct commit *commit = (struct commit *)o;
474 if (!(commit->object.flags & COMPLETE)) {
475 commit->object.flags |= COMPLETE;
476 commit_list_insert(commit, &complete);
482 static int mark_complete_oid(const char *refname, const struct object_id *oid,
483 int flag, void *cb_data)
485 return mark_complete(oid);
488 static void mark_recent_complete_commits(struct fetch_pack_args *args,
491 while (complete && cutoff <= complete->item->date) {
492 print_verbose(args, _("Marking %s as complete"),
493 oid_to_hex(&complete->item->object.oid));
494 pop_most_recent_commit(&complete, COMPLETE);
498 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
500 for (; refs; refs = refs->next)
501 oidset_insert(oids, &refs->old_oid);
504 static int tip_oids_contain(struct oidset *tip_oids,
505 struct ref *unmatched, struct ref *newlist,
506 const struct object_id *id)
509 * Note that this only looks at the ref lists the first time it's
510 * called. This works out in filter_refs() because even though it may
511 * add to "newlist" between calls, the additions will always be for
512 * oids that are already in the set.
514 if (!tip_oids->map.map.tablesize) {
515 add_refs_to_oidset(tip_oids, unmatched);
516 add_refs_to_oidset(tip_oids, newlist);
518 return oidset_contains(tip_oids, id);
521 static void filter_refs(struct fetch_pack_args *args,
523 struct ref **sought, int nr_sought)
525 struct ref *newlist = NULL;
526 struct ref **newtail = &newlist;
527 struct ref *unmatched = NULL;
528 struct ref *ref, *next;
529 struct oidset tip_oids = OIDSET_INIT;
533 for (ref = *refs; ref; ref = next) {
537 if (starts_with(ref->name, "refs/") &&
538 check_refname_format(ref->name, 0))
541 while (i < nr_sought) {
542 int cmp = strcmp(ref->name, sought[i]->name);
544 break; /* definitely do not have it */
546 keep = 1; /* definitely have it */
547 sought[i]->match_status = REF_MATCHED;
553 if (!keep && args->fetch_all &&
554 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
560 newtail = &ref->next;
562 ref->next = unmatched;
567 /* Append unmatched requests to the list */
568 for (i = 0; i < nr_sought; i++) {
569 struct object_id oid;
573 if (ref->match_status != REF_NOT_MATCHED)
575 if (parse_oid_hex(ref->name, &oid, &p) ||
577 oidcmp(&oid, &ref->old_oid))
580 if ((allow_unadvertised_object_request &
581 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
582 tip_oids_contain(&tip_oids, unmatched, newlist,
584 ref->match_status = REF_MATCHED;
585 *newtail = copy_ref(ref);
586 newtail = &(*newtail)->next;
588 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
592 oidset_clear(&tip_oids);
593 for (ref = unmatched; ref; ref = next) {
601 static void mark_alternate_complete(struct fetch_negotiator *unused,
604 mark_complete(&obj->oid);
607 struct loose_object_iter {
608 struct oidset *loose_object_set;
613 * If the number of refs is not larger than the number of loose objects,
614 * this function stops inserting.
616 static int add_loose_objects_to_set(const struct object_id *oid,
620 struct loose_object_iter *iter = data;
621 oidset_insert(iter->loose_object_set, oid);
622 if (iter->refs == NULL)
625 iter->refs = iter->refs->next;
630 * Mark recent commits available locally and reachable from a local ref as
631 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
632 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
633 * thus do not need COMMON_REF marks).
635 * The cutoff time for recency is determined by this heuristic: it is the
636 * earliest commit time of the objects in refs that are commits and that we know
637 * the commit time of.
639 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
640 struct fetch_pack_args *args,
644 int old_save_commit_buffer = save_commit_buffer;
645 timestamp_t cutoff = 0;
646 struct oidset loose_oid_set = OIDSET_INIT;
648 struct loose_object_iter iter = {&loose_oid_set, *refs};
650 /* Enumerate all loose objects or know refs are not so many. */
651 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
654 save_commit_buffer = 0;
656 for (ref = *refs; ref; ref = ref->next) {
658 unsigned int flags = OBJECT_INFO_QUICK;
661 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
663 * I know this does not exist in the loose form,
664 * so check if it exists in a non-loose form.
666 flags |= OBJECT_INFO_IGNORE_LOOSE;
669 if (!has_object_file_with_flags(&ref->old_oid, flags))
671 o = parse_object(&ref->old_oid);
675 /* We already have it -- which may mean that we were
676 * in sync with the other side at some time after
677 * that (it is OK if we guess wrong here).
679 if (o->type == OBJ_COMMIT) {
680 struct commit *commit = (struct commit *)o;
681 if (!cutoff || cutoff < commit->date)
682 cutoff = commit->date;
686 oidset_clear(&loose_oid_set);
688 if (!args->no_dependents) {
690 for_each_ref(mark_complete_oid, NULL);
691 for_each_cached_alternate(NULL, mark_alternate_complete);
692 commit_list_sort_by_date(&complete);
694 mark_recent_complete_commits(args, cutoff);
698 * Mark all complete remote refs as common refs.
699 * Don't mark them common yet; the server has to be told so first.
701 for (ref = *refs; ref; ref = ref->next) {
702 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
705 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
708 negotiator->known_common(negotiator,
713 save_commit_buffer = old_save_commit_buffer;
717 * Returns 1 if every object pointed to by the given remote refs is available
718 * locally and reachable from a local ref, and 0 otherwise.
720 static int everything_local(struct fetch_pack_args *args,
726 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
727 const struct object_id *remote = &ref->old_oid;
730 o = lookup_object(remote->hash);
731 if (!o || !(o->flags & COMPLETE)) {
733 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
737 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
744 static int sideband_demux(int in, int out, void *data)
749 ret = recv_sideband("fetch-pack", xd[0], out);
754 static int get_pack(struct fetch_pack_args *args,
755 int xd[2], char **pack_lockfile)
758 int do_keep = args->keep_pack;
759 const char *cmd_name;
760 struct pack_header header;
762 struct child_process cmd = CHILD_PROCESS_INIT;
765 memset(&demux, 0, sizeof(demux));
767 /* xd[] is talking with upload-pack; subprocess reads from
768 * xd[0], spits out band#2 to stderr, and feeds us band#1
769 * through demux->out.
771 demux.proc = sideband_demux;
774 demux.isolate_sigpipe = 1;
775 if (start_async(&demux))
776 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
781 if (!args->keep_pack && unpack_limit) {
783 if (read_pack_header(demux.out, &header))
784 die(_("protocol error: bad pack header"));
786 if (ntohl(header.hdr_entries) < unpack_limit)
792 if (alternate_shallow_file) {
793 argv_array_push(&cmd.args, "--shallow-file");
794 argv_array_push(&cmd.args, alternate_shallow_file);
797 if (do_keep || args->from_promisor) {
800 cmd_name = "index-pack";
801 argv_array_push(&cmd.args, cmd_name);
802 argv_array_push(&cmd.args, "--stdin");
803 if (!args->quiet && !args->no_progress)
804 argv_array_push(&cmd.args, "-v");
805 if (args->use_thin_pack)
806 argv_array_push(&cmd.args, "--fix-thin");
807 if (do_keep && (args->lock_pack || unpack_limit)) {
808 char hostname[HOST_NAME_MAX + 1];
809 if (xgethostname(hostname, sizeof(hostname)))
810 xsnprintf(hostname, sizeof(hostname), "localhost");
811 argv_array_pushf(&cmd.args,
812 "--keep=fetch-pack %"PRIuMAX " on %s",
813 (uintmax_t)getpid(), hostname);
815 if (args->check_self_contained_and_connected)
816 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
817 if (args->from_promisor)
818 argv_array_push(&cmd.args, "--promisor");
821 cmd_name = "unpack-objects";
822 argv_array_push(&cmd.args, cmd_name);
823 if (args->quiet || args->no_progress)
824 argv_array_push(&cmd.args, "-q");
825 args->check_self_contained_and_connected = 0;
829 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
830 ntohl(header.hdr_version),
831 ntohl(header.hdr_entries));
832 if (fetch_fsck_objects >= 0
834 : transfer_fsck_objects >= 0
835 ? transfer_fsck_objects
837 if (args->from_promisor)
839 * We cannot use --strict in index-pack because it
840 * checks both broken objects and links, but we only
841 * want to check for broken objects.
843 argv_array_push(&cmd.args, "--fsck-objects");
845 argv_array_push(&cmd.args, "--strict");
850 if (start_command(&cmd))
851 die(_("fetch-pack: unable to fork off %s"), cmd_name);
852 if (do_keep && pack_lockfile) {
853 *pack_lockfile = index_pack_lockfile(cmd.out);
858 /* Closed by start_command() */
861 ret = finish_command(&cmd);
862 if (!ret || (args->check_self_contained_and_connected && ret == 1))
863 args->self_contained_and_connected =
864 args->check_self_contained_and_connected &&
867 die(_("%s failed"), cmd_name);
868 if (use_sideband && finish_async(&demux))
869 die(_("error in sideband demultiplexer"));
873 static int cmp_ref_by_name(const void *a_, const void *b_)
875 const struct ref *a = *((const struct ref **)a_);
876 const struct ref *b = *((const struct ref **)b_);
877 return strcmp(a->name, b->name);
880 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
882 const struct ref *orig_ref,
883 struct ref **sought, int nr_sought,
884 struct shallow_info *si,
885 char **pack_lockfile)
887 struct ref *ref = copy_ref_list(orig_ref);
888 struct object_id oid;
889 const char *agent_feature;
891 struct fetch_negotiator negotiator;
892 fetch_negotiator_init(&negotiator);
894 sort_ref_list(&ref, ref_compare_name);
895 QSORT(sought, nr_sought, cmp_ref_by_name);
897 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
898 die(_("Server does not support shallow clients"));
899 if (args->depth > 0 || args->deepen_since || args->deepen_not)
901 if (server_supports("multi_ack_detailed")) {
902 print_verbose(args, _("Server supports multi_ack_detailed"));
904 if (server_supports("no-done")) {
905 print_verbose(args, _("Server supports no-done"));
906 if (args->stateless_rpc)
910 else if (server_supports("multi_ack")) {
911 print_verbose(args, _("Server supports multi_ack"));
914 if (server_supports("side-band-64k")) {
915 print_verbose(args, _("Server supports side-band-64k"));
918 else if (server_supports("side-band")) {
919 print_verbose(args, _("Server supports side-band"));
922 if (server_supports("allow-tip-sha1-in-want")) {
923 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
924 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
926 if (server_supports("allow-reachable-sha1-in-want")) {
927 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
928 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
930 if (!server_supports("thin-pack"))
931 args->use_thin_pack = 0;
932 if (!server_supports("no-progress"))
933 args->no_progress = 0;
934 if (!server_supports("include-tag"))
935 args->include_tag = 0;
936 if (server_supports("ofs-delta"))
937 print_verbose(args, _("Server supports ofs-delta"));
939 prefer_ofs_delta = 0;
941 if (server_supports("filter")) {
942 server_supports_filtering = 1;
943 print_verbose(args, _("Server supports filter"));
944 } else if (args->filter_options.choice) {
945 warning("filtering not recognized by server, ignoring");
948 if ((agent_feature = server_feature_value("agent", &agent_len))) {
951 print_verbose(args, _("Server version is %.*s"),
952 agent_len, agent_feature);
954 if (server_supports("deepen-since"))
956 else if (args->deepen_since)
957 die(_("Server does not support --shallow-since"));
958 if (server_supports("deepen-not"))
960 else if (args->deepen_not)
961 die(_("Server does not support --shallow-exclude"));
962 if (!server_supports("deepen-relative") && args->deepen_relative)
963 die(_("Server does not support --deepen"));
965 mark_complete_and_common_ref(&negotiator, args, &ref);
966 filter_refs(args, &ref, sought, nr_sought);
967 if (everything_local(args, &ref)) {
971 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
972 if (!args->keep_pack)
973 /* When cloning, it is not unusual to have
976 warning(_("no common commits"));
978 if (args->stateless_rpc)
981 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
983 else if (si->nr_ours || si->nr_theirs)
984 alternate_shallow_file = setup_temporary_shallow(si->shallow);
986 alternate_shallow_file = NULL;
987 if (get_pack(args, fd, pack_lockfile))
988 die(_("git fetch-pack: fetch failed."));
991 negotiator.release(&negotiator);
995 static void add_shallow_requests(struct strbuf *req_buf,
996 const struct fetch_pack_args *args)
998 if (is_repository_shallow())
999 write_shallow_commits(req_buf, 1, NULL);
1000 if (args->depth > 0)
1001 packet_buf_write(req_buf, "deepen %d", args->depth);
1002 if (args->deepen_since) {
1003 timestamp_t max_age = approxidate(args->deepen_since);
1004 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1006 if (args->deepen_not) {
1008 for (i = 0; i < args->deepen_not->nr; i++) {
1009 struct string_list_item *s = args->deepen_not->items + i;
1010 packet_buf_write(req_buf, "deepen-not %s", s->string);
1015 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1017 for ( ; wants ; wants = wants->next) {
1018 const struct object_id *remote = &wants->old_oid;
1019 const char *remote_hex;
1023 * If that object is complete (i.e. it is an ancestor of a
1024 * local ref), we tell them we have it but do not have to
1025 * tell them about its ancestors, which they already know
1028 * We use lookup_object here because we are only
1029 * interested in the case we *know* the object is
1030 * reachable and we have already scanned it.
1032 if (((o = lookup_object(remote->hash)) != NULL) &&
1033 (o->flags & COMPLETE)) {
1037 remote_hex = oid_to_hex(remote);
1038 packet_buf_write(req_buf, "want %s\n", remote_hex);
1042 static void add_common(struct strbuf *req_buf, struct oidset *common)
1044 struct oidset_iter iter;
1045 const struct object_id *oid;
1046 oidset_iter_init(common, &iter);
1048 while ((oid = oidset_iter_next(&iter))) {
1049 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1053 static int add_haves(struct fetch_negotiator *negotiator,
1054 struct strbuf *req_buf,
1055 int *haves_to_send, int *in_vain)
1058 int haves_added = 0;
1059 const struct object_id *oid;
1061 while ((oid = negotiator->next(negotiator))) {
1062 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1063 if (++haves_added >= *haves_to_send)
1067 *in_vain += haves_added;
1068 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1070 packet_buf_write(req_buf, "done\n");
1074 /* Increase haves to send on next round */
1075 *haves_to_send = next_flush(1, *haves_to_send);
1080 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1081 const struct fetch_pack_args *args,
1082 const struct ref *wants, struct oidset *common,
1083 int *haves_to_send, int *in_vain)
1086 struct strbuf req_buf = STRBUF_INIT;
1088 if (server_supports_v2("fetch", 1))
1089 packet_buf_write(&req_buf, "command=fetch");
1090 if (server_supports_v2("agent", 0))
1091 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1092 if (args->server_options && args->server_options->nr &&
1093 server_supports_v2("server-option", 1)) {
1095 for (i = 0; i < args->server_options->nr; i++)
1096 packet_write_fmt(fd_out, "server-option=%s",
1097 args->server_options->items[i].string);
1100 packet_buf_delim(&req_buf);
1101 if (args->use_thin_pack)
1102 packet_buf_write(&req_buf, "thin-pack");
1103 if (args->no_progress)
1104 packet_buf_write(&req_buf, "no-progress");
1105 if (args->include_tag)
1106 packet_buf_write(&req_buf, "include-tag");
1107 if (prefer_ofs_delta)
1108 packet_buf_write(&req_buf, "ofs-delta");
1110 /* Add shallow-info and deepen request */
1111 if (server_supports_feature("fetch", "shallow", 0))
1112 add_shallow_requests(&req_buf, args);
1113 else if (is_repository_shallow() || args->deepen)
1114 die(_("Server does not support shallow requests"));
1117 if (server_supports_feature("fetch", "filter", 0) &&
1118 args->filter_options.choice) {
1119 print_verbose(args, _("Server supports filter"));
1120 packet_buf_write(&req_buf, "filter %s",
1121 args->filter_options.filter_spec);
1122 } else if (args->filter_options.choice) {
1123 warning("filtering not recognized by server, ignoring");
1127 add_wants(wants, &req_buf);
1129 if (args->no_dependents) {
1130 packet_buf_write(&req_buf, "done");
1133 /* Add all of the common commits we've found in previous rounds */
1134 add_common(&req_buf, common);
1136 /* Add initial haves */
1137 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1141 packet_buf_flush(&req_buf);
1142 write_or_die(fd_out, req_buf.buf, req_buf.len);
1144 strbuf_release(&req_buf);
1149 * Processes a section header in a server's response and checks if it matches
1150 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1151 * not consumed); if 0, the line will be consumed and the function will die if
1152 * the section header doesn't match what was expected.
1154 static int process_section_header(struct packet_reader *reader,
1155 const char *section, int peek)
1159 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1160 die("error reading section header '%s'", section);
1162 ret = !strcmp(reader->line, section);
1166 die("expected '%s', received '%s'",
1167 section, reader->line);
1168 packet_reader_read(reader);
1174 static int process_acks(struct fetch_negotiator *negotiator,
1175 struct packet_reader *reader,
1176 struct oidset *common)
1179 int received_ready = 0;
1180 int received_ack = 0;
1182 process_section_header(reader, "acknowledgments", 0);
1183 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1186 if (!strcmp(reader->line, "NAK"))
1189 if (skip_prefix(reader->line, "ACK ", &arg)) {
1190 struct object_id oid;
1191 if (!get_oid_hex(arg, &oid)) {
1192 struct commit *commit;
1193 oidset_insert(common, &oid);
1194 commit = lookup_commit(&oid);
1195 negotiator->ack(negotiator, commit);
1200 if (!strcmp(reader->line, "ready")) {
1205 die("unexpected acknowledgment line: '%s'", reader->line);
1208 if (reader->status != PACKET_READ_FLUSH &&
1209 reader->status != PACKET_READ_DELIM)
1210 die("error processing acks: %d", reader->status);
1212 /* return 0 if no common, 1 if there are common, or 2 if ready */
1213 return received_ready ? 2 : (received_ack ? 1 : 0);
1216 static void receive_shallow_info(struct fetch_pack_args *args,
1217 struct packet_reader *reader)
1219 process_section_header(reader, "shallow-info", 0);
1220 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1222 struct object_id oid;
1224 if (skip_prefix(reader->line, "shallow ", &arg)) {
1225 if (get_oid_hex(arg, &oid))
1226 die(_("invalid shallow line: %s"), reader->line);
1227 register_shallow(&oid);
1230 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1231 if (get_oid_hex(arg, &oid))
1232 die(_("invalid unshallow line: %s"), reader->line);
1233 if (!lookup_object(oid.hash))
1234 die(_("object not found: %s"), reader->line);
1235 /* make sure that it is parsed as shallow */
1236 if (!parse_object(&oid))
1237 die(_("error in object: %s"), reader->line);
1238 if (unregister_shallow(&oid))
1239 die(_("no shallow found: %s"), reader->line);
1242 die(_("expected shallow/unshallow, got %s"), reader->line);
1245 if (reader->status != PACKET_READ_FLUSH &&
1246 reader->status != PACKET_READ_DELIM)
1247 die("error processing shallow info: %d", reader->status);
1249 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1254 FETCH_CHECK_LOCAL = 0,
1261 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1263 const struct ref *orig_ref,
1264 struct ref **sought, int nr_sought,
1265 char **pack_lockfile)
1267 struct ref *ref = copy_ref_list(orig_ref);
1268 enum fetch_state state = FETCH_CHECK_LOCAL;
1269 struct oidset common = OIDSET_INIT;
1270 struct packet_reader reader;
1272 int haves_to_send = INITIAL_FLUSH;
1273 struct fetch_negotiator negotiator;
1274 fetch_negotiator_init(&negotiator);
1275 packet_reader_init(&reader, fd[0], NULL, 0,
1276 PACKET_READ_CHOMP_NEWLINE);
1278 while (state != FETCH_DONE) {
1280 case FETCH_CHECK_LOCAL:
1281 sort_ref_list(&ref, ref_compare_name);
1282 QSORT(sought, nr_sought, cmp_ref_by_name);
1284 /* v2 supports these by default */
1285 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1287 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1290 /* Filter 'ref' by 'sought' and those that aren't local */
1291 mark_complete_and_common_ref(&negotiator, args, &ref);
1292 filter_refs(args, &ref, sought, nr_sought);
1293 if (everything_local(args, &ref))
1296 state = FETCH_SEND_REQUEST;
1298 for_each_ref(rev_list_insert_ref_oid, &negotiator);
1299 for_each_cached_alternate(&negotiator,
1300 insert_one_alternate_object);
1302 case FETCH_SEND_REQUEST:
1303 if (send_fetch_request(&negotiator, fd[1], args, ref,
1305 &haves_to_send, &in_vain))
1306 state = FETCH_GET_PACK;
1308 state = FETCH_PROCESS_ACKS;
1310 case FETCH_PROCESS_ACKS:
1311 /* Process ACKs/NAKs */
1312 switch (process_acks(&negotiator, &reader, &common)) {
1314 state = FETCH_GET_PACK;
1320 state = FETCH_SEND_REQUEST;
1324 case FETCH_GET_PACK:
1325 /* Check for shallow-info section */
1326 if (process_section_header(&reader, "shallow-info", 1))
1327 receive_shallow_info(args, &reader);
1330 process_section_header(&reader, "packfile", 0);
1331 if (get_pack(args, fd, pack_lockfile))
1332 die(_("git fetch-pack: fetch failed."));
1341 negotiator.release(&negotiator);
1342 oidset_clear(&common);
1346 static void fetch_pack_config(void)
1348 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1349 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1350 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1351 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1352 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1354 git_config(git_default_config, NULL);
1357 static void fetch_pack_setup(void)
1359 static int did_setup;
1362 fetch_pack_config();
1363 if (0 <= transfer_unpack_limit)
1364 unpack_limit = transfer_unpack_limit;
1365 else if (0 <= fetch_unpack_limit)
1366 unpack_limit = fetch_unpack_limit;
1370 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1372 struct string_list names = STRING_LIST_INIT_NODUP;
1375 for (src = dst = 0; src < nr; src++) {
1376 struct string_list_item *item;
1377 item = string_list_insert(&names, ref[src]->name);
1379 continue; /* already have it */
1380 item->util = ref[src];
1382 ref[dst] = ref[src];
1385 for (src = dst; src < nr; src++)
1387 string_list_clear(&names, 0);
1391 static void update_shallow(struct fetch_pack_args *args,
1392 struct ref **sought, int nr_sought,
1393 struct shallow_info *si)
1395 struct oid_array ref = OID_ARRAY_INIT;
1399 if (args->deepen && alternate_shallow_file) {
1400 if (*alternate_shallow_file == '\0') { /* --unshallow */
1401 unlink_or_warn(git_path_shallow());
1402 rollback_lock_file(&shallow_lock);
1404 commit_lock_file(&shallow_lock);
1408 if (!si->shallow || !si->shallow->nr)
1411 if (args->cloning) {
1413 * remote is shallow, but this is a clone, there are
1414 * no objects in repo to worry about. Accept any
1415 * shallow points that exist in the pack (iow in repo
1416 * after get_pack() and reprepare_packed_git())
1418 struct oid_array extra = OID_ARRAY_INIT;
1419 struct object_id *oid = si->shallow->oid;
1420 for (i = 0; i < si->shallow->nr; i++)
1421 if (has_object_file(&oid[i]))
1422 oid_array_append(&extra, &oid[i]);
1424 setup_alternate_shallow(&shallow_lock,
1425 &alternate_shallow_file,
1427 commit_lock_file(&shallow_lock);
1429 oid_array_clear(&extra);
1433 if (!si->nr_ours && !si->nr_theirs)
1436 remove_nonexistent_theirs_shallow(si);
1437 if (!si->nr_ours && !si->nr_theirs)
1439 for (i = 0; i < nr_sought; i++)
1440 oid_array_append(&ref, &sought[i]->old_oid);
1443 if (args->update_shallow) {
1445 * remote is also shallow, .git/shallow may be updated
1446 * so all refs can be accepted. Make sure we only add
1447 * shallow roots that are actually reachable from new
1450 struct oid_array extra = OID_ARRAY_INIT;
1451 struct object_id *oid = si->shallow->oid;
1452 assign_shallow_commits_to_refs(si, NULL, NULL);
1453 if (!si->nr_ours && !si->nr_theirs) {
1454 oid_array_clear(&ref);
1457 for (i = 0; i < si->nr_ours; i++)
1458 oid_array_append(&extra, &oid[si->ours[i]]);
1459 for (i = 0; i < si->nr_theirs; i++)
1460 oid_array_append(&extra, &oid[si->theirs[i]]);
1461 setup_alternate_shallow(&shallow_lock,
1462 &alternate_shallow_file,
1464 commit_lock_file(&shallow_lock);
1465 oid_array_clear(&extra);
1466 oid_array_clear(&ref);
1471 * remote is also shallow, check what ref is safe to update
1472 * without updating .git/shallow
1474 status = xcalloc(nr_sought, sizeof(*status));
1475 assign_shallow_commits_to_refs(si, NULL, status);
1476 if (si->nr_ours || si->nr_theirs) {
1477 for (i = 0; i < nr_sought; i++)
1479 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1482 oid_array_clear(&ref);
1485 struct ref *fetch_pack(struct fetch_pack_args *args,
1486 int fd[], struct child_process *conn,
1487 const struct ref *ref,
1489 struct ref **sought, int nr_sought,
1490 struct oid_array *shallow,
1491 char **pack_lockfile,
1492 enum protocol_version version)
1494 struct ref *ref_cpy;
1495 struct shallow_info si;
1499 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1502 packet_flush(fd[1]);
1503 die(_("no matching remote head"));
1505 prepare_shallow_info(&si, shallow);
1506 if (version == protocol_v2)
1507 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1510 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1511 &si, pack_lockfile);
1512 reprepare_packed_git(the_repository);
1513 update_shallow(args, sought, nr_sought, &si);
1514 clear_shallow_info(&si);
1518 int report_unmatched_refs(struct ref **sought, int nr_sought)
1522 for (i = 0; i < nr_sought; i++) {
1525 switch (sought[i]->match_status) {
1528 case REF_NOT_MATCHED:
1529 error(_("no such remote ref %s"), sought[i]->name);
1531 case REF_UNADVERTISED_NOT_ALLOWED:
1532 error(_("Server does not allow request for unadvertised object %s"),