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;
36 static char *negotiation_algorithm;
38 /* Remember to update object flag allocation in object.h */
39 #define COMPLETE (1U << 0)
40 #define ALTERNATE (1U << 1)
43 * After sending this many "have"s if we do not get any new ACK , we
44 * give up traversing our history.
46 #define MAX_IN_VAIN 256
48 static int multi_ack, use_sideband;
49 /* Allow specifying sha1 if it is a ref tip. */
50 #define ALLOW_TIP_SHA1 01
51 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52 #define ALLOW_REACHABLE_SHA1 02
53 static unsigned int allow_unadvertised_object_request;
55 __attribute__((format (printf, 2, 3)))
56 static inline void print_verbose(const struct fetch_pack_args *args,
64 va_start(params, fmt);
65 vfprintf(stderr, fmt, params);
70 struct alternate_object_cache {
71 struct object **items;
75 static void cache_one_alternate(const char *refname,
76 const struct object_id *oid,
79 struct alternate_object_cache *cache = vcache;
80 struct object *obj = parse_object(oid);
82 if (!obj || (obj->flags & ALTERNATE))
85 obj->flags |= ALTERNATE;
86 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
87 cache->items[cache->nr++] = obj;
90 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
91 void (*cb)(struct fetch_negotiator *,
94 static int initialized;
95 static struct alternate_object_cache cache;
99 for_each_alternate_ref(cache_one_alternate, &cache);
103 for (i = 0; i < cache.nr; i++)
104 cb(negotiator, cache.items[i]);
107 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
109 const struct object_id *oid)
111 struct object *o = deref_tag(parse_object(oid), refname, 0);
113 if (o && o->type == OBJ_COMMIT)
114 negotiator->add_tip(negotiator, (struct commit *)o);
119 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
120 int flag, void *cb_data)
122 return rev_list_insert_ref(cb_data, refname, oid);
133 static void consume_shallow_list(struct fetch_pack_args *args, int fd)
135 if (args->stateless_rpc && args->deepen) {
136 /* If we sent a depth we will get back "duplicate"
137 * shallow and unshallow commands every time there
138 * is a block of have lines exchanged.
141 while ((line = packet_read_line(fd, NULL))) {
142 if (starts_with(line, "shallow "))
144 if (starts_with(line, "unshallow "))
146 die(_("git fetch-pack: expected shallow list"));
151 static enum ack_type get_ack(int fd, struct object_id *result_oid)
154 char *line = packet_read_line(fd, &len);
158 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
159 if (!strcmp(line, "NAK"))
161 if (skip_prefix(line, "ACK ", &arg)) {
162 if (!get_oid_hex(arg, result_oid)) {
167 if (strstr(arg, "continue"))
169 if (strstr(arg, "common"))
171 if (strstr(arg, "ready"))
176 if (skip_prefix(line, "ERR ", &arg))
177 die(_("remote error: %s"), arg);
178 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
181 static void send_request(struct fetch_pack_args *args,
182 int fd, struct strbuf *buf)
184 if (args->stateless_rpc) {
185 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
188 write_or_die(fd, buf->buf, buf->len);
191 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
194 rev_list_insert_ref(negotiator, NULL, &obj->oid);
197 #define INITIAL_FLUSH 16
198 #define PIPESAFE_FLUSH 32
199 #define LARGE_FLUSH 16384
201 static int next_flush(int stateless_rpc, int count)
204 if (count < LARGE_FLUSH)
207 count = count * 11 / 10;
209 if (count < PIPESAFE_FLUSH)
212 count += PIPESAFE_FLUSH;
217 static int find_common(struct fetch_negotiator *negotiator,
218 struct fetch_pack_args *args,
219 int fd[2], struct object_id *result_oid,
223 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
224 const struct object_id *oid;
225 unsigned in_vain = 0;
226 int got_continue = 0;
228 struct strbuf req_buf = STRBUF_INIT;
229 size_t state_len = 0;
231 if (args->stateless_rpc && multi_ack == 1)
232 die(_("--stateless-rpc requires multi_ack_detailed"));
234 for_each_ref(rev_list_insert_ref_oid, negotiator);
235 for_each_cached_alternate(negotiator, insert_one_alternate_object);
238 for ( ; refs ; refs = refs->next) {
239 struct object_id *remote = &refs->old_oid;
240 const char *remote_hex;
244 * If that object is complete (i.e. it is an ancestor of a
245 * local ref), we tell them we have it but do not have to
246 * tell them about its ancestors, which they already know
249 * We use lookup_object here because we are only
250 * interested in the case we *know* the object is
251 * reachable and we have already scanned it.
253 if (((o = lookup_object(remote->hash)) != NULL) &&
254 (o->flags & COMPLETE)) {
258 remote_hex = oid_to_hex(remote);
260 struct strbuf c = STRBUF_INIT;
261 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
262 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
263 if (no_done) strbuf_addstr(&c, " no-done");
264 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
265 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
266 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
267 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
268 if (args->no_progress) strbuf_addstr(&c, " no-progress");
269 if (args->include_tag) strbuf_addstr(&c, " include-tag");
270 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
271 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
272 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
273 if (agent_supported) strbuf_addf(&c, " agent=%s",
274 git_user_agent_sanitized());
275 if (args->filter_options.choice)
276 strbuf_addstr(&c, " filter");
277 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
280 packet_buf_write(&req_buf, "want %s\n", remote_hex);
285 strbuf_release(&req_buf);
290 if (is_repository_shallow())
291 write_shallow_commits(&req_buf, 1, NULL);
293 packet_buf_write(&req_buf, "deepen %d", args->depth);
294 if (args->deepen_since) {
295 timestamp_t max_age = approxidate(args->deepen_since);
296 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
298 if (args->deepen_not) {
300 for (i = 0; i < args->deepen_not->nr; i++) {
301 struct string_list_item *s = args->deepen_not->items + i;
302 packet_buf_write(&req_buf, "deepen-not %s", s->string);
305 if (server_supports_filtering && args->filter_options.choice)
306 packet_buf_write(&req_buf, "filter %s",
307 args->filter_options.filter_spec);
308 packet_buf_flush(&req_buf);
309 state_len = req_buf.len;
314 struct object_id oid;
316 send_request(args, fd[1], &req_buf);
317 while ((line = packet_read_line(fd[0], NULL))) {
318 if (skip_prefix(line, "shallow ", &arg)) {
319 if (get_oid_hex(arg, &oid))
320 die(_("invalid shallow line: %s"), line);
321 register_shallow(&oid);
324 if (skip_prefix(line, "unshallow ", &arg)) {
325 if (get_oid_hex(arg, &oid))
326 die(_("invalid unshallow line: %s"), line);
327 if (!lookup_object(oid.hash))
328 die(_("object not found: %s"), line);
329 /* make sure that it is parsed as shallow */
330 if (!parse_object(&oid))
331 die(_("error in object: %s"), line);
332 if (unregister_shallow(&oid))
333 die(_("no shallow found: %s"), line);
336 die(_("expected shallow/unshallow, got %s"), line);
338 } else if (!args->stateless_rpc)
339 send_request(args, fd[1], &req_buf);
341 if (!args->stateless_rpc) {
342 /* If we aren't using the stateless-rpc interface
343 * we don't need to retain the headers.
345 strbuf_setlen(&req_buf, 0);
351 if (args->no_dependents)
353 while ((oid = negotiator->next(negotiator))) {
354 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
355 print_verbose(args, "have %s", oid_to_hex(oid));
357 if (flush_at <= ++count) {
360 packet_buf_flush(&req_buf);
361 send_request(args, fd[1], &req_buf);
362 strbuf_setlen(&req_buf, state_len);
364 flush_at = next_flush(args->stateless_rpc, count);
367 * We keep one window "ahead" of the other side, and
368 * will wait for an ACK only on the next one
370 if (!args->stateless_rpc && count == INITIAL_FLUSH)
373 consume_shallow_list(args, fd[0]);
375 ack = get_ack(fd[0], result_oid);
377 print_verbose(args, _("got %s %d %s"), "ack",
378 ack, oid_to_hex(result_oid));
388 struct commit *commit =
389 lookup_commit(result_oid);
392 die(_("invalid commit %s"), oid_to_hex(result_oid));
393 was_common = negotiator->ack(negotiator, commit);
394 if (args->stateless_rpc
397 /* We need to replay the have for this object
398 * on the next RPC request so the peer knows
399 * it is in common with us.
401 const char *hex = oid_to_hex(result_oid);
402 packet_buf_write(&req_buf, "have %s\n", hex);
403 state_len = req_buf.len;
405 * Reset in_vain because an ack
406 * for this commit has not been
410 } else if (!args->stateless_rpc
411 || ack != ACK_common)
415 if (ack == ACK_ready)
422 if (got_continue && MAX_IN_VAIN < in_vain) {
423 print_verbose(args, _("giving up"));
431 if (!got_ready || !no_done) {
432 packet_buf_write(&req_buf, "done\n");
433 send_request(args, fd[1], &req_buf);
435 print_verbose(args, _("done"));
440 strbuf_release(&req_buf);
442 if (!got_ready || !no_done)
443 consume_shallow_list(args, fd[0]);
444 while (flushes || multi_ack) {
445 int ack = get_ack(fd[0], result_oid);
447 print_verbose(args, _("got %s (%d) %s"), "ack",
448 ack, oid_to_hex(result_oid));
456 /* it is no error to fetch into a completely empty repo */
457 return count ? retval : 0;
460 static struct commit_list *complete;
462 static int mark_complete(const struct object_id *oid)
464 struct object *o = parse_object(oid);
466 while (o && o->type == OBJ_TAG) {
467 struct tag *t = (struct tag *) o;
469 break; /* broken repository */
470 o->flags |= COMPLETE;
471 o = parse_object(&t->tagged->oid);
473 if (o && o->type == OBJ_COMMIT) {
474 struct commit *commit = (struct commit *)o;
475 if (!(commit->object.flags & COMPLETE)) {
476 commit->object.flags |= COMPLETE;
477 commit_list_insert(commit, &complete);
483 static int mark_complete_oid(const char *refname, const struct object_id *oid,
484 int flag, void *cb_data)
486 return mark_complete(oid);
489 static void mark_recent_complete_commits(struct fetch_pack_args *args,
492 while (complete && cutoff <= complete->item->date) {
493 print_verbose(args, _("Marking %s as complete"),
494 oid_to_hex(&complete->item->object.oid));
495 pop_most_recent_commit(&complete, COMPLETE);
499 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
501 for (; refs; refs = refs->next)
502 oidset_insert(oids, &refs->old_oid);
505 static int tip_oids_contain(struct oidset *tip_oids,
506 struct ref *unmatched, struct ref *newlist,
507 const struct object_id *id)
510 * Note that this only looks at the ref lists the first time it's
511 * called. This works out in filter_refs() because even though it may
512 * add to "newlist" between calls, the additions will always be for
513 * oids that are already in the set.
515 if (!tip_oids->map.map.tablesize) {
516 add_refs_to_oidset(tip_oids, unmatched);
517 add_refs_to_oidset(tip_oids, newlist);
519 return oidset_contains(tip_oids, id);
522 static void filter_refs(struct fetch_pack_args *args,
524 struct ref **sought, int nr_sought)
526 struct ref *newlist = NULL;
527 struct ref **newtail = &newlist;
528 struct ref *unmatched = NULL;
529 struct ref *ref, *next;
530 struct oidset tip_oids = OIDSET_INIT;
534 for (ref = *refs; ref; ref = next) {
538 if (starts_with(ref->name, "refs/") &&
539 check_refname_format(ref->name, 0))
542 while (i < nr_sought) {
543 int cmp = strcmp(ref->name, sought[i]->name);
545 break; /* definitely do not have it */
547 keep = 1; /* definitely have it */
548 sought[i]->match_status = REF_MATCHED;
554 if (!keep && args->fetch_all &&
555 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
561 newtail = &ref->next;
563 ref->next = unmatched;
568 /* Append unmatched requests to the list */
569 for (i = 0; i < nr_sought; i++) {
570 struct object_id oid;
574 if (ref->match_status != REF_NOT_MATCHED)
576 if (parse_oid_hex(ref->name, &oid, &p) ||
578 oidcmp(&oid, &ref->old_oid))
581 if ((allow_unadvertised_object_request &
582 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
583 tip_oids_contain(&tip_oids, unmatched, newlist,
585 ref->match_status = REF_MATCHED;
586 *newtail = copy_ref(ref);
587 newtail = &(*newtail)->next;
589 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
593 oidset_clear(&tip_oids);
594 for (ref = unmatched; ref; ref = next) {
602 static void mark_alternate_complete(struct fetch_negotiator *unused,
605 mark_complete(&obj->oid);
608 struct loose_object_iter {
609 struct oidset *loose_object_set;
614 * If the number of refs is not larger than the number of loose objects,
615 * this function stops inserting.
617 static int add_loose_objects_to_set(const struct object_id *oid,
621 struct loose_object_iter *iter = data;
622 oidset_insert(iter->loose_object_set, oid);
623 if (iter->refs == NULL)
626 iter->refs = iter->refs->next;
631 * Mark recent commits available locally and reachable from a local ref as
632 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
633 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
634 * thus do not need COMMON_REF marks).
636 * The cutoff time for recency is determined by this heuristic: it is the
637 * earliest commit time of the objects in refs that are commits and that we know
638 * the commit time of.
640 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
641 struct fetch_pack_args *args,
645 int old_save_commit_buffer = save_commit_buffer;
646 timestamp_t cutoff = 0;
647 struct oidset loose_oid_set = OIDSET_INIT;
649 struct loose_object_iter iter = {&loose_oid_set, *refs};
651 /* Enumerate all loose objects or know refs are not so many. */
652 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
655 save_commit_buffer = 0;
657 for (ref = *refs; ref; ref = ref->next) {
659 unsigned int flags = OBJECT_INFO_QUICK;
662 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
664 * I know this does not exist in the loose form,
665 * so check if it exists in a non-loose form.
667 flags |= OBJECT_INFO_IGNORE_LOOSE;
670 if (!has_object_file_with_flags(&ref->old_oid, flags))
672 o = parse_object(&ref->old_oid);
676 /* We already have it -- which may mean that we were
677 * in sync with the other side at some time after
678 * that (it is OK if we guess wrong here).
680 if (o->type == OBJ_COMMIT) {
681 struct commit *commit = (struct commit *)o;
682 if (!cutoff || cutoff < commit->date)
683 cutoff = commit->date;
687 oidset_clear(&loose_oid_set);
689 if (!args->no_dependents) {
691 for_each_ref(mark_complete_oid, NULL);
692 for_each_cached_alternate(NULL, mark_alternate_complete);
693 commit_list_sort_by_date(&complete);
695 mark_recent_complete_commits(args, cutoff);
699 * Mark all complete remote refs as common refs.
700 * Don't mark them common yet; the server has to be told so first.
702 for (ref = *refs; ref; ref = ref->next) {
703 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
706 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
709 negotiator->known_common(negotiator,
714 save_commit_buffer = old_save_commit_buffer;
718 * Returns 1 if every object pointed to by the given remote refs is available
719 * locally and reachable from a local ref, and 0 otherwise.
721 static int everything_local(struct fetch_pack_args *args,
727 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
728 const struct object_id *remote = &ref->old_oid;
731 o = lookup_object(remote->hash);
732 if (!o || !(o->flags & COMPLETE)) {
734 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
738 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
745 static int sideband_demux(int in, int out, void *data)
750 ret = recv_sideband("fetch-pack", xd[0], out);
755 static int get_pack(struct fetch_pack_args *args,
756 int xd[2], char **pack_lockfile)
759 int do_keep = args->keep_pack;
760 const char *cmd_name;
761 struct pack_header header;
763 struct child_process cmd = CHILD_PROCESS_INIT;
766 memset(&demux, 0, sizeof(demux));
768 /* xd[] is talking with upload-pack; subprocess reads from
769 * xd[0], spits out band#2 to stderr, and feeds us band#1
770 * through demux->out.
772 demux.proc = sideband_demux;
775 demux.isolate_sigpipe = 1;
776 if (start_async(&demux))
777 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
782 if (!args->keep_pack && unpack_limit) {
784 if (read_pack_header(demux.out, &header))
785 die(_("protocol error: bad pack header"));
787 if (ntohl(header.hdr_entries) < unpack_limit)
793 if (alternate_shallow_file) {
794 argv_array_push(&cmd.args, "--shallow-file");
795 argv_array_push(&cmd.args, alternate_shallow_file);
798 if (do_keep || args->from_promisor) {
801 cmd_name = "index-pack";
802 argv_array_push(&cmd.args, cmd_name);
803 argv_array_push(&cmd.args, "--stdin");
804 if (!args->quiet && !args->no_progress)
805 argv_array_push(&cmd.args, "-v");
806 if (args->use_thin_pack)
807 argv_array_push(&cmd.args, "--fix-thin");
808 if (do_keep && (args->lock_pack || unpack_limit)) {
809 char hostname[HOST_NAME_MAX + 1];
810 if (xgethostname(hostname, sizeof(hostname)))
811 xsnprintf(hostname, sizeof(hostname), "localhost");
812 argv_array_pushf(&cmd.args,
813 "--keep=fetch-pack %"PRIuMAX " on %s",
814 (uintmax_t)getpid(), hostname);
816 if (args->check_self_contained_and_connected)
817 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
818 if (args->from_promisor)
819 argv_array_push(&cmd.args, "--promisor");
822 cmd_name = "unpack-objects";
823 argv_array_push(&cmd.args, cmd_name);
824 if (args->quiet || args->no_progress)
825 argv_array_push(&cmd.args, "-q");
826 args->check_self_contained_and_connected = 0;
830 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
831 ntohl(header.hdr_version),
832 ntohl(header.hdr_entries));
833 if (fetch_fsck_objects >= 0
835 : transfer_fsck_objects >= 0
836 ? transfer_fsck_objects
838 if (args->from_promisor)
840 * We cannot use --strict in index-pack because it
841 * checks both broken objects and links, but we only
842 * want to check for broken objects.
844 argv_array_push(&cmd.args, "--fsck-objects");
846 argv_array_push(&cmd.args, "--strict");
851 if (start_command(&cmd))
852 die(_("fetch-pack: unable to fork off %s"), cmd_name);
853 if (do_keep && pack_lockfile) {
854 *pack_lockfile = index_pack_lockfile(cmd.out);
859 /* Closed by start_command() */
862 ret = finish_command(&cmd);
863 if (!ret || (args->check_self_contained_and_connected && ret == 1))
864 args->self_contained_and_connected =
865 args->check_self_contained_and_connected &&
868 die(_("%s failed"), cmd_name);
869 if (use_sideband && finish_async(&demux))
870 die(_("error in sideband demultiplexer"));
874 static int cmp_ref_by_name(const void *a_, const void *b_)
876 const struct ref *a = *((const struct ref **)a_);
877 const struct ref *b = *((const struct ref **)b_);
878 return strcmp(a->name, b->name);
881 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
883 const struct ref *orig_ref,
884 struct ref **sought, int nr_sought,
885 struct shallow_info *si,
886 char **pack_lockfile)
888 struct ref *ref = copy_ref_list(orig_ref);
889 struct object_id oid;
890 const char *agent_feature;
892 struct fetch_negotiator negotiator;
893 fetch_negotiator_init(&negotiator, negotiation_algorithm);
895 sort_ref_list(&ref, ref_compare_name);
896 QSORT(sought, nr_sought, cmp_ref_by_name);
898 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
899 die(_("Server does not support shallow clients"));
900 if (args->depth > 0 || args->deepen_since || args->deepen_not)
902 if (server_supports("multi_ack_detailed")) {
903 print_verbose(args, _("Server supports multi_ack_detailed"));
905 if (server_supports("no-done")) {
906 print_verbose(args, _("Server supports no-done"));
907 if (args->stateless_rpc)
911 else if (server_supports("multi_ack")) {
912 print_verbose(args, _("Server supports multi_ack"));
915 if (server_supports("side-band-64k")) {
916 print_verbose(args, _("Server supports side-band-64k"));
919 else if (server_supports("side-band")) {
920 print_verbose(args, _("Server supports side-band"));
923 if (server_supports("allow-tip-sha1-in-want")) {
924 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
925 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
927 if (server_supports("allow-reachable-sha1-in-want")) {
928 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
929 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
931 if (!server_supports("thin-pack"))
932 args->use_thin_pack = 0;
933 if (!server_supports("no-progress"))
934 args->no_progress = 0;
935 if (!server_supports("include-tag"))
936 args->include_tag = 0;
937 if (server_supports("ofs-delta"))
938 print_verbose(args, _("Server supports ofs-delta"));
940 prefer_ofs_delta = 0;
942 if (server_supports("filter")) {
943 server_supports_filtering = 1;
944 print_verbose(args, _("Server supports filter"));
945 } else if (args->filter_options.choice) {
946 warning("filtering not recognized by server, ignoring");
949 if ((agent_feature = server_feature_value("agent", &agent_len))) {
952 print_verbose(args, _("Server version is %.*s"),
953 agent_len, agent_feature);
955 if (server_supports("deepen-since"))
957 else if (args->deepen_since)
958 die(_("Server does not support --shallow-since"));
959 if (server_supports("deepen-not"))
961 else if (args->deepen_not)
962 die(_("Server does not support --shallow-exclude"));
963 if (!server_supports("deepen-relative") && args->deepen_relative)
964 die(_("Server does not support --deepen"));
966 mark_complete_and_common_ref(&negotiator, args, &ref);
967 filter_refs(args, &ref, sought, nr_sought);
968 if (everything_local(args, &ref)) {
972 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
973 if (!args->keep_pack)
974 /* When cloning, it is not unusual to have
977 warning(_("no common commits"));
979 if (args->stateless_rpc)
982 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
984 else if (si->nr_ours || si->nr_theirs)
985 alternate_shallow_file = setup_temporary_shallow(si->shallow);
987 alternate_shallow_file = NULL;
988 if (get_pack(args, fd, pack_lockfile))
989 die(_("git fetch-pack: fetch failed."));
992 negotiator.release(&negotiator);
996 static void add_shallow_requests(struct strbuf *req_buf,
997 const struct fetch_pack_args *args)
999 if (is_repository_shallow())
1000 write_shallow_commits(req_buf, 1, NULL);
1001 if (args->depth > 0)
1002 packet_buf_write(req_buf, "deepen %d", args->depth);
1003 if (args->deepen_since) {
1004 timestamp_t max_age = approxidate(args->deepen_since);
1005 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1007 if (args->deepen_not) {
1009 for (i = 0; i < args->deepen_not->nr; i++) {
1010 struct string_list_item *s = args->deepen_not->items + i;
1011 packet_buf_write(req_buf, "deepen-not %s", s->string);
1016 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1018 for ( ; wants ; wants = wants->next) {
1019 const struct object_id *remote = &wants->old_oid;
1020 const char *remote_hex;
1024 * If that object is complete (i.e. it is an ancestor of a
1025 * local ref), we tell them we have it but do not have to
1026 * tell them about its ancestors, which they already know
1029 * We use lookup_object here because we are only
1030 * interested in the case we *know* the object is
1031 * reachable and we have already scanned it.
1033 if (((o = lookup_object(remote->hash)) != NULL) &&
1034 (o->flags & COMPLETE)) {
1038 remote_hex = oid_to_hex(remote);
1039 packet_buf_write(req_buf, "want %s\n", remote_hex);
1043 static void add_common(struct strbuf *req_buf, struct oidset *common)
1045 struct oidset_iter iter;
1046 const struct object_id *oid;
1047 oidset_iter_init(common, &iter);
1049 while ((oid = oidset_iter_next(&iter))) {
1050 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1054 static int add_haves(struct fetch_negotiator *negotiator,
1055 struct strbuf *req_buf,
1056 int *haves_to_send, int *in_vain)
1059 int haves_added = 0;
1060 const struct object_id *oid;
1062 while ((oid = negotiator->next(negotiator))) {
1063 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1064 if (++haves_added >= *haves_to_send)
1068 *in_vain += haves_added;
1069 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1071 packet_buf_write(req_buf, "done\n");
1075 /* Increase haves to send on next round */
1076 *haves_to_send = next_flush(1, *haves_to_send);
1081 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1082 const struct fetch_pack_args *args,
1083 const struct ref *wants, struct oidset *common,
1084 int *haves_to_send, int *in_vain)
1087 struct strbuf req_buf = STRBUF_INIT;
1089 if (server_supports_v2("fetch", 1))
1090 packet_buf_write(&req_buf, "command=fetch");
1091 if (server_supports_v2("agent", 0))
1092 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1093 if (args->server_options && args->server_options->nr &&
1094 server_supports_v2("server-option", 1)) {
1096 for (i = 0; i < args->server_options->nr; i++)
1097 packet_write_fmt(fd_out, "server-option=%s",
1098 args->server_options->items[i].string);
1101 packet_buf_delim(&req_buf);
1102 if (args->use_thin_pack)
1103 packet_buf_write(&req_buf, "thin-pack");
1104 if (args->no_progress)
1105 packet_buf_write(&req_buf, "no-progress");
1106 if (args->include_tag)
1107 packet_buf_write(&req_buf, "include-tag");
1108 if (prefer_ofs_delta)
1109 packet_buf_write(&req_buf, "ofs-delta");
1111 /* Add shallow-info and deepen request */
1112 if (server_supports_feature("fetch", "shallow", 0))
1113 add_shallow_requests(&req_buf, args);
1114 else if (is_repository_shallow() || args->deepen)
1115 die(_("Server does not support shallow requests"));
1118 if (server_supports_feature("fetch", "filter", 0) &&
1119 args->filter_options.choice) {
1120 print_verbose(args, _("Server supports filter"));
1121 packet_buf_write(&req_buf, "filter %s",
1122 args->filter_options.filter_spec);
1123 } else if (args->filter_options.choice) {
1124 warning("filtering not recognized by server, ignoring");
1128 add_wants(wants, &req_buf);
1130 if (args->no_dependents) {
1131 packet_buf_write(&req_buf, "done");
1134 /* Add all of the common commits we've found in previous rounds */
1135 add_common(&req_buf, common);
1137 /* Add initial haves */
1138 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1142 packet_buf_flush(&req_buf);
1143 write_or_die(fd_out, req_buf.buf, req_buf.len);
1145 strbuf_release(&req_buf);
1150 * Processes a section header in a server's response and checks if it matches
1151 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1152 * not consumed); if 0, the line will be consumed and the function will die if
1153 * the section header doesn't match what was expected.
1155 static int process_section_header(struct packet_reader *reader,
1156 const char *section, int peek)
1160 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1161 die("error reading section header '%s'", section);
1163 ret = !strcmp(reader->line, section);
1167 die("expected '%s', received '%s'",
1168 section, reader->line);
1169 packet_reader_read(reader);
1175 static int process_acks(struct fetch_negotiator *negotiator,
1176 struct packet_reader *reader,
1177 struct oidset *common)
1180 int received_ready = 0;
1181 int received_ack = 0;
1183 process_section_header(reader, "acknowledgments", 0);
1184 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1187 if (!strcmp(reader->line, "NAK"))
1190 if (skip_prefix(reader->line, "ACK ", &arg)) {
1191 struct object_id oid;
1192 if (!get_oid_hex(arg, &oid)) {
1193 struct commit *commit;
1194 oidset_insert(common, &oid);
1195 commit = lookup_commit(&oid);
1196 negotiator->ack(negotiator, commit);
1201 if (!strcmp(reader->line, "ready")) {
1206 die("unexpected acknowledgment line: '%s'", reader->line);
1209 if (reader->status != PACKET_READ_FLUSH &&
1210 reader->status != PACKET_READ_DELIM)
1211 die("error processing acks: %d", reader->status);
1213 /* return 0 if no common, 1 if there are common, or 2 if ready */
1214 return received_ready ? 2 : (received_ack ? 1 : 0);
1217 static void receive_shallow_info(struct fetch_pack_args *args,
1218 struct packet_reader *reader)
1220 process_section_header(reader, "shallow-info", 0);
1221 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1223 struct object_id oid;
1225 if (skip_prefix(reader->line, "shallow ", &arg)) {
1226 if (get_oid_hex(arg, &oid))
1227 die(_("invalid shallow line: %s"), reader->line);
1228 register_shallow(&oid);
1231 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1232 if (get_oid_hex(arg, &oid))
1233 die(_("invalid unshallow line: %s"), reader->line);
1234 if (!lookup_object(oid.hash))
1235 die(_("object not found: %s"), reader->line);
1236 /* make sure that it is parsed as shallow */
1237 if (!parse_object(&oid))
1238 die(_("error in object: %s"), reader->line);
1239 if (unregister_shallow(&oid))
1240 die(_("no shallow found: %s"), reader->line);
1243 die(_("expected shallow/unshallow, got %s"), reader->line);
1246 if (reader->status != PACKET_READ_FLUSH &&
1247 reader->status != PACKET_READ_DELIM)
1248 die("error processing shallow info: %d", reader->status);
1250 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1255 FETCH_CHECK_LOCAL = 0,
1262 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1264 const struct ref *orig_ref,
1265 struct ref **sought, int nr_sought,
1266 char **pack_lockfile)
1268 struct ref *ref = copy_ref_list(orig_ref);
1269 enum fetch_state state = FETCH_CHECK_LOCAL;
1270 struct oidset common = OIDSET_INIT;
1271 struct packet_reader reader;
1273 int haves_to_send = INITIAL_FLUSH;
1274 struct fetch_negotiator negotiator;
1275 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1276 packet_reader_init(&reader, fd[0], NULL, 0,
1277 PACKET_READ_CHOMP_NEWLINE);
1279 while (state != FETCH_DONE) {
1281 case FETCH_CHECK_LOCAL:
1282 sort_ref_list(&ref, ref_compare_name);
1283 QSORT(sought, nr_sought, cmp_ref_by_name);
1285 /* v2 supports these by default */
1286 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1288 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1291 /* Filter 'ref' by 'sought' and those that aren't local */
1292 mark_complete_and_common_ref(&negotiator, args, &ref);
1293 filter_refs(args, &ref, sought, nr_sought);
1294 if (everything_local(args, &ref))
1297 state = FETCH_SEND_REQUEST;
1299 for_each_ref(rev_list_insert_ref_oid, &negotiator);
1300 for_each_cached_alternate(&negotiator,
1301 insert_one_alternate_object);
1303 case FETCH_SEND_REQUEST:
1304 if (send_fetch_request(&negotiator, fd[1], args, ref,
1306 &haves_to_send, &in_vain))
1307 state = FETCH_GET_PACK;
1309 state = FETCH_PROCESS_ACKS;
1311 case FETCH_PROCESS_ACKS:
1312 /* Process ACKs/NAKs */
1313 switch (process_acks(&negotiator, &reader, &common)) {
1315 state = FETCH_GET_PACK;
1321 state = FETCH_SEND_REQUEST;
1325 case FETCH_GET_PACK:
1326 /* Check for shallow-info section */
1327 if (process_section_header(&reader, "shallow-info", 1))
1328 receive_shallow_info(args, &reader);
1331 process_section_header(&reader, "packfile", 0);
1332 if (get_pack(args, fd, pack_lockfile))
1333 die(_("git fetch-pack: fetch failed."));
1342 negotiator.release(&negotiator);
1343 oidset_clear(&common);
1347 static void fetch_pack_config(void)
1349 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1350 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1351 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1352 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1353 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1354 git_config_get_string("fetch.negotiationalgorithm",
1355 &negotiation_algorithm);
1357 git_config(git_default_config, NULL);
1360 static void fetch_pack_setup(void)
1362 static int did_setup;
1365 fetch_pack_config();
1366 if (0 <= transfer_unpack_limit)
1367 unpack_limit = transfer_unpack_limit;
1368 else if (0 <= fetch_unpack_limit)
1369 unpack_limit = fetch_unpack_limit;
1373 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1375 struct string_list names = STRING_LIST_INIT_NODUP;
1378 for (src = dst = 0; src < nr; src++) {
1379 struct string_list_item *item;
1380 item = string_list_insert(&names, ref[src]->name);
1382 continue; /* already have it */
1383 item->util = ref[src];
1385 ref[dst] = ref[src];
1388 for (src = dst; src < nr; src++)
1390 string_list_clear(&names, 0);
1394 static void update_shallow(struct fetch_pack_args *args,
1395 struct ref **sought, int nr_sought,
1396 struct shallow_info *si)
1398 struct oid_array ref = OID_ARRAY_INIT;
1402 if (args->deepen && alternate_shallow_file) {
1403 if (*alternate_shallow_file == '\0') { /* --unshallow */
1404 unlink_or_warn(git_path_shallow());
1405 rollback_lock_file(&shallow_lock);
1407 commit_lock_file(&shallow_lock);
1411 if (!si->shallow || !si->shallow->nr)
1414 if (args->cloning) {
1416 * remote is shallow, but this is a clone, there are
1417 * no objects in repo to worry about. Accept any
1418 * shallow points that exist in the pack (iow in repo
1419 * after get_pack() and reprepare_packed_git())
1421 struct oid_array extra = OID_ARRAY_INIT;
1422 struct object_id *oid = si->shallow->oid;
1423 for (i = 0; i < si->shallow->nr; i++)
1424 if (has_object_file(&oid[i]))
1425 oid_array_append(&extra, &oid[i]);
1427 setup_alternate_shallow(&shallow_lock,
1428 &alternate_shallow_file,
1430 commit_lock_file(&shallow_lock);
1432 oid_array_clear(&extra);
1436 if (!si->nr_ours && !si->nr_theirs)
1439 remove_nonexistent_theirs_shallow(si);
1440 if (!si->nr_ours && !si->nr_theirs)
1442 for (i = 0; i < nr_sought; i++)
1443 oid_array_append(&ref, &sought[i]->old_oid);
1446 if (args->update_shallow) {
1448 * remote is also shallow, .git/shallow may be updated
1449 * so all refs can be accepted. Make sure we only add
1450 * shallow roots that are actually reachable from new
1453 struct oid_array extra = OID_ARRAY_INIT;
1454 struct object_id *oid = si->shallow->oid;
1455 assign_shallow_commits_to_refs(si, NULL, NULL);
1456 if (!si->nr_ours && !si->nr_theirs) {
1457 oid_array_clear(&ref);
1460 for (i = 0; i < si->nr_ours; i++)
1461 oid_array_append(&extra, &oid[si->ours[i]]);
1462 for (i = 0; i < si->nr_theirs; i++)
1463 oid_array_append(&extra, &oid[si->theirs[i]]);
1464 setup_alternate_shallow(&shallow_lock,
1465 &alternate_shallow_file,
1467 commit_lock_file(&shallow_lock);
1468 oid_array_clear(&extra);
1469 oid_array_clear(&ref);
1474 * remote is also shallow, check what ref is safe to update
1475 * without updating .git/shallow
1477 status = xcalloc(nr_sought, sizeof(*status));
1478 assign_shallow_commits_to_refs(si, NULL, status);
1479 if (si->nr_ours || si->nr_theirs) {
1480 for (i = 0; i < nr_sought; i++)
1482 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1485 oid_array_clear(&ref);
1488 struct ref *fetch_pack(struct fetch_pack_args *args,
1489 int fd[], struct child_process *conn,
1490 const struct ref *ref,
1492 struct ref **sought, int nr_sought,
1493 struct oid_array *shallow,
1494 char **pack_lockfile,
1495 enum protocol_version version)
1497 struct ref *ref_cpy;
1498 struct shallow_info si;
1502 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1505 packet_flush(fd[1]);
1506 die(_("no matching remote head"));
1508 prepare_shallow_info(&si, shallow);
1509 if (version == protocol_v2)
1510 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1513 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1514 &si, pack_lockfile);
1515 reprepare_packed_git(the_repository);
1516 update_shallow(args, sought, nr_sought, &si);
1517 clear_shallow_info(&si);
1521 int report_unmatched_refs(struct ref **sought, int nr_sought)
1525 for (i = 0; i < nr_sought; i++) {
1528 switch (sought[i]->match_status) {
1531 case REF_NOT_MATCHED:
1532 error(_("no such remote ref %s"), sought[i]->name);
1534 case REF_UNADVERTISED_NOT_ALLOWED:
1535 error(_("Server does not allow request for unadvertised object %s"),