Merge branch 'pw/rebase-x-sanity-check'
[git.git] / fetch-pack.c
blob08b3b356faeabdeeff90918142f7225ce5460e8c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "lockfile.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "exec-cmd.h"
10 #include "pack.h"
11 #include "sideband.h"
12 #include "fetch-pack.h"
13 #include "remote.h"
14 #include "run-command.h"
15 #include "connect.h"
16 #include "transport.h"
17 #include "version.h"
18 #include "sha1-array.h"
19 #include "oidset.h"
20 #include "packfile.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
24 #include "fsck.h"
26 static int transfer_unpack_limit = -1;
27 static int fetch_unpack_limit = -1;
28 static int unpack_limit = 100;
29 static int prefer_ofs_delta = 1;
30 static int no_done;
31 static int deepen_since_ok;
32 static int deepen_not_ok;
33 static int fetch_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int agent_supported;
36 static int server_supports_filtering;
37 static struct lock_file shallow_lock;
38 static const char *alternate_shallow_file;
39 static char *negotiation_algorithm;
40 static struct strbuf fsck_msg_types = STRBUF_INIT;
42 /* Remember to update object flag allocation in object.h */
43 #define COMPLETE (1U << 0)
44 #define ALTERNATE (1U << 1)
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 int multi_ack, use_sideband;
53 /* Allow specifying sha1 if it is a ref tip. */
54 #define ALLOW_TIP_SHA1 01
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 #define ALLOW_REACHABLE_SHA1 02
57 static unsigned int allow_unadvertised_object_request;
59 __attribute__((format (printf, 2, 3)))
60 static inline void print_verbose(const struct fetch_pack_args *args,
61 const char *fmt, ...)
63 va_list params;
65 if (!args->verbose)
66 return;
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
70 va_end(params);
71 fputc('\n', stderr);
74 struct alternate_object_cache {
75 struct object **items;
76 size_t nr, alloc;
79 static void cache_one_alternate(const struct object_id *oid,
80 void *vcache)
82 struct alternate_object_cache *cache = vcache;
83 struct object *obj = parse_object(the_repository, oid);
85 if (!obj || (obj->flags & ALTERNATE))
86 return;
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
93 static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
95 struct object *))
97 static int initialized;
98 static struct alternate_object_cache cache;
99 size_t i;
101 if (!initialized) {
102 for_each_alternate_ref(cache_one_alternate, &cache);
103 initialized = 1;
106 for (i = 0; i < cache.nr; i++)
107 cb(negotiator, cache.items[i]);
110 static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
111 const char *refname,
112 const struct object_id *oid)
114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
116 refname, 0);
118 if (o && o->type == OBJ_COMMIT)
119 negotiator->add_tip(negotiator, (struct commit *)o);
121 return 0;
124 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
127 return rev_list_insert_ref(cb_data, refname, oid);
130 enum ack_type {
131 NAK = 0,
132 ACK,
133 ACK_continue,
134 ACK_common,
135 ACK_ready
138 static void consume_shallow_list(struct fetch_pack_args *args,
139 struct packet_reader *reader)
141 if (args->stateless_rpc && args->deepen) {
142 /* If we sent a depth we will get back "duplicate"
143 * shallow and unshallow commands every time there
144 * is a block of have lines exchanged.
146 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
147 if (starts_with(reader->line, "shallow "))
148 continue;
149 if (starts_with(reader->line, "unshallow "))
150 continue;
151 die(_("git fetch-pack: expected shallow list"));
153 if (reader->status != PACKET_READ_FLUSH)
154 die(_("git fetch-pack: expected a flush packet after shallow list"));
158 static enum ack_type get_ack(struct packet_reader *reader,
159 struct object_id *result_oid)
161 int len;
162 const char *arg;
164 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
165 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
166 len = reader->pktlen;
168 if (!strcmp(reader->line, "NAK"))
169 return NAK;
170 if (skip_prefix(reader->line, "ACK ", &arg)) {
171 if (!get_oid_hex(arg, result_oid)) {
172 arg += 40;
173 len -= arg - reader->line;
174 if (len < 1)
175 return ACK;
176 if (strstr(arg, "continue"))
177 return ACK_continue;
178 if (strstr(arg, "common"))
179 return ACK_common;
180 if (strstr(arg, "ready"))
181 return ACK_ready;
182 return ACK;
185 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
188 static void send_request(struct fetch_pack_args *args,
189 int fd, struct strbuf *buf)
191 if (args->stateless_rpc) {
192 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
193 packet_flush(fd);
194 } else
195 write_or_die(fd, buf->buf, buf->len);
198 static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
199 struct object *obj)
201 rev_list_insert_ref(negotiator, NULL, &obj->oid);
204 #define INITIAL_FLUSH 16
205 #define PIPESAFE_FLUSH 32
206 #define LARGE_FLUSH 16384
208 static int next_flush(int stateless_rpc, int count)
210 if (stateless_rpc) {
211 if (count < LARGE_FLUSH)
212 count <<= 1;
213 else
214 count = count * 11 / 10;
215 } else {
216 if (count < PIPESAFE_FLUSH)
217 count <<= 1;
218 else
219 count += PIPESAFE_FLUSH;
221 return count;
224 static void mark_tips(struct fetch_negotiator *negotiator,
225 const struct oid_array *negotiation_tips)
227 int i;
229 if (!negotiation_tips) {
230 for_each_ref(rev_list_insert_ref_oid, negotiator);
231 return;
234 for (i = 0; i < negotiation_tips->nr; i++)
235 rev_list_insert_ref(negotiator, NULL,
236 &negotiation_tips->oid[i]);
237 return;
240 static int find_common(struct fetch_negotiator *negotiator,
241 struct fetch_pack_args *args,
242 int fd[2], struct object_id *result_oid,
243 struct ref *refs)
245 int fetching;
246 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
247 const struct object_id *oid;
248 unsigned in_vain = 0;
249 int got_continue = 0;
250 int got_ready = 0;
251 struct strbuf req_buf = STRBUF_INIT;
252 size_t state_len = 0;
253 struct packet_reader reader;
255 if (args->stateless_rpc && multi_ack == 1)
256 die(_("--stateless-rpc requires multi_ack_detailed"));
258 packet_reader_init(&reader, fd[0], NULL, 0,
259 PACKET_READ_CHOMP_NEWLINE |
260 PACKET_READ_DIE_ON_ERR_PACKET);
262 if (!args->no_dependents) {
263 mark_tips(negotiator, args->negotiation_tips);
264 for_each_cached_alternate(negotiator, insert_one_alternate_object);
267 fetching = 0;
268 for ( ; refs ; refs = refs->next) {
269 struct object_id *remote = &refs->old_oid;
270 const char *remote_hex;
271 struct object *o;
274 * If that object is complete (i.e. it is an ancestor of a
275 * local ref), we tell them we have it but do not have to
276 * tell them about its ancestors, which they already know
277 * about.
279 * We use lookup_object here because we are only
280 * interested in the case we *know* the object is
281 * reachable and we have already scanned it.
283 * Do this only if args->no_dependents is false (if it is true,
284 * we cannot trust the object flags).
286 if (!args->no_dependents &&
287 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
288 (o->flags & COMPLETE)) {
289 continue;
292 remote_hex = oid_to_hex(remote);
293 if (!fetching) {
294 struct strbuf c = STRBUF_INIT;
295 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
296 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
297 if (no_done) strbuf_addstr(&c, " no-done");
298 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
299 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
300 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
301 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
302 if (args->no_progress) strbuf_addstr(&c, " no-progress");
303 if (args->include_tag) strbuf_addstr(&c, " include-tag");
304 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
305 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
306 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
307 if (agent_supported) strbuf_addf(&c, " agent=%s",
308 git_user_agent_sanitized());
309 if (args->filter_options.choice)
310 strbuf_addstr(&c, " filter");
311 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
312 strbuf_release(&c);
313 } else
314 packet_buf_write(&req_buf, "want %s\n", remote_hex);
315 fetching++;
318 if (!fetching) {
319 strbuf_release(&req_buf);
320 packet_flush(fd[1]);
321 return 1;
324 if (is_repository_shallow(the_repository))
325 write_shallow_commits(&req_buf, 1, NULL);
326 if (args->depth > 0)
327 packet_buf_write(&req_buf, "deepen %d", args->depth);
328 if (args->deepen_since) {
329 timestamp_t max_age = approxidate(args->deepen_since);
330 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
332 if (args->deepen_not) {
333 int i;
334 for (i = 0; i < args->deepen_not->nr; i++) {
335 struct string_list_item *s = args->deepen_not->items + i;
336 packet_buf_write(&req_buf, "deepen-not %s", s->string);
339 if (server_supports_filtering && args->filter_options.choice) {
340 struct strbuf expanded_filter_spec = STRBUF_INIT;
341 expand_list_objects_filter_spec(&args->filter_options,
342 &expanded_filter_spec);
343 packet_buf_write(&req_buf, "filter %s",
344 expanded_filter_spec.buf);
345 strbuf_release(&expanded_filter_spec);
347 packet_buf_flush(&req_buf);
348 state_len = req_buf.len;
350 if (args->deepen) {
351 const char *arg;
352 struct object_id oid;
354 send_request(args, fd[1], &req_buf);
355 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
356 if (skip_prefix(reader.line, "shallow ", &arg)) {
357 if (get_oid_hex(arg, &oid))
358 die(_("invalid shallow line: %s"), reader.line);
359 register_shallow(the_repository, &oid);
360 continue;
362 if (skip_prefix(reader.line, "unshallow ", &arg)) {
363 if (get_oid_hex(arg, &oid))
364 die(_("invalid unshallow line: %s"), reader.line);
365 if (!lookup_object(the_repository, oid.hash))
366 die(_("object not found: %s"), reader.line);
367 /* make sure that it is parsed as shallow */
368 if (!parse_object(the_repository, &oid))
369 die(_("error in object: %s"), reader.line);
370 if (unregister_shallow(&oid))
371 die(_("no shallow found: %s"), reader.line);
372 continue;
374 die(_("expected shallow/unshallow, got %s"), reader.line);
376 } else if (!args->stateless_rpc)
377 send_request(args, fd[1], &req_buf);
379 if (!args->stateless_rpc) {
380 /* If we aren't using the stateless-rpc interface
381 * we don't need to retain the headers.
383 strbuf_setlen(&req_buf, 0);
384 state_len = 0;
387 flushes = 0;
388 retval = -1;
389 if (args->no_dependents)
390 goto done;
391 while ((oid = negotiator->next(negotiator))) {
392 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
393 print_verbose(args, "have %s", oid_to_hex(oid));
394 in_vain++;
395 if (flush_at <= ++count) {
396 int ack;
398 packet_buf_flush(&req_buf);
399 send_request(args, fd[1], &req_buf);
400 strbuf_setlen(&req_buf, state_len);
401 flushes++;
402 flush_at = next_flush(args->stateless_rpc, count);
405 * We keep one window "ahead" of the other side, and
406 * will wait for an ACK only on the next one
408 if (!args->stateless_rpc && count == INITIAL_FLUSH)
409 continue;
411 consume_shallow_list(args, &reader);
412 do {
413 ack = get_ack(&reader, result_oid);
414 if (ack)
415 print_verbose(args, _("got %s %d %s"), "ack",
416 ack, oid_to_hex(result_oid));
417 switch (ack) {
418 case ACK:
419 flushes = 0;
420 multi_ack = 0;
421 retval = 0;
422 goto done;
423 case ACK_common:
424 case ACK_ready:
425 case ACK_continue: {
426 struct commit *commit =
427 lookup_commit(the_repository,
428 result_oid);
429 int was_common;
431 if (!commit)
432 die(_("invalid commit %s"), oid_to_hex(result_oid));
433 was_common = negotiator->ack(negotiator, commit);
434 if (args->stateless_rpc
435 && ack == ACK_common
436 && !was_common) {
437 /* We need to replay the have for this object
438 * on the next RPC request so the peer knows
439 * it is in common with us.
441 const char *hex = oid_to_hex(result_oid);
442 packet_buf_write(&req_buf, "have %s\n", hex);
443 state_len = req_buf.len;
445 * Reset in_vain because an ack
446 * for this commit has not been
447 * seen.
449 in_vain = 0;
450 } else if (!args->stateless_rpc
451 || ack != ACK_common)
452 in_vain = 0;
453 retval = 0;
454 got_continue = 1;
455 if (ack == ACK_ready)
456 got_ready = 1;
457 break;
460 } while (ack);
461 flushes--;
462 if (got_continue && MAX_IN_VAIN < in_vain) {
463 print_verbose(args, _("giving up"));
464 break; /* give up */
466 if (got_ready)
467 break;
470 done:
471 if (!got_ready || !no_done) {
472 packet_buf_write(&req_buf, "done\n");
473 send_request(args, fd[1], &req_buf);
475 print_verbose(args, _("done"));
476 if (retval != 0) {
477 multi_ack = 0;
478 flushes++;
480 strbuf_release(&req_buf);
482 if (!got_ready || !no_done)
483 consume_shallow_list(args, &reader);
484 while (flushes || multi_ack) {
485 int ack = get_ack(&reader, result_oid);
486 if (ack) {
487 print_verbose(args, _("got %s (%d) %s"), "ack",
488 ack, oid_to_hex(result_oid));
489 if (ack == ACK)
490 return 0;
491 multi_ack = 1;
492 continue;
494 flushes--;
496 /* it is no error to fetch into a completely empty repo */
497 return count ? retval : 0;
500 static struct commit_list *complete;
502 static int mark_complete(const struct object_id *oid)
504 struct object *o = parse_object(the_repository, oid);
506 while (o && o->type == OBJ_TAG) {
507 struct tag *t = (struct tag *) o;
508 if (!t->tagged)
509 break; /* broken repository */
510 o->flags |= COMPLETE;
511 o = parse_object(the_repository, &t->tagged->oid);
513 if (o && o->type == OBJ_COMMIT) {
514 struct commit *commit = (struct commit *)o;
515 if (!(commit->object.flags & COMPLETE)) {
516 commit->object.flags |= COMPLETE;
517 commit_list_insert(commit, &complete);
520 return 0;
523 static int mark_complete_oid(const char *refname, const struct object_id *oid,
524 int flag, void *cb_data)
526 return mark_complete(oid);
529 static void mark_recent_complete_commits(struct fetch_pack_args *args,
530 timestamp_t cutoff)
532 while (complete && cutoff <= complete->item->date) {
533 print_verbose(args, _("Marking %s as complete"),
534 oid_to_hex(&complete->item->object.oid));
535 pop_most_recent_commit(&complete, COMPLETE);
539 static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
541 for (; refs; refs = refs->next)
542 oidset_insert(oids, &refs->old_oid);
545 static int is_unmatched_ref(const struct ref *ref)
547 struct object_id oid;
548 const char *p;
549 return ref->match_status == REF_NOT_MATCHED &&
550 !parse_oid_hex(ref->name, &oid, &p) &&
551 *p == '\0' &&
552 oideq(&oid, &ref->old_oid);
555 static void filter_refs(struct fetch_pack_args *args,
556 struct ref **refs,
557 struct ref **sought, int nr_sought)
559 struct ref *newlist = NULL;
560 struct ref **newtail = &newlist;
561 struct ref *unmatched = NULL;
562 struct ref *ref, *next;
563 struct oidset tip_oids = OIDSET_INIT;
564 int i;
565 int strict = !(allow_unadvertised_object_request &
566 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
568 i = 0;
569 for (ref = *refs; ref; ref = next) {
570 int keep = 0;
571 next = ref->next;
573 if (starts_with(ref->name, "refs/") &&
574 check_refname_format(ref->name, 0))
575 ; /* trash */
576 else {
577 while (i < nr_sought) {
578 int cmp = strcmp(ref->name, sought[i]->name);
579 if (cmp < 0)
580 break; /* definitely do not have it */
581 else if (cmp == 0) {
582 keep = 1; /* definitely have it */
583 sought[i]->match_status = REF_MATCHED;
585 i++;
588 if (!keep && args->fetch_all &&
589 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
590 keep = 1;
593 if (keep) {
594 *newtail = ref;
595 ref->next = NULL;
596 newtail = &ref->next;
597 } else {
598 ref->next = unmatched;
599 unmatched = ref;
603 if (strict) {
604 for (i = 0; i < nr_sought; i++) {
605 ref = sought[i];
606 if (!is_unmatched_ref(ref))
607 continue;
609 add_refs_to_oidset(&tip_oids, unmatched);
610 add_refs_to_oidset(&tip_oids, newlist);
611 break;
615 /* Append unmatched requests to the list */
616 for (i = 0; i < nr_sought; i++) {
617 ref = sought[i];
618 if (!is_unmatched_ref(ref))
619 continue;
621 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
622 ref->match_status = REF_MATCHED;
623 *newtail = copy_ref(ref);
624 newtail = &(*newtail)->next;
625 } else {
626 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
630 oidset_clear(&tip_oids);
631 for (ref = unmatched; ref; ref = next) {
632 next = ref->next;
633 free(ref);
636 *refs = newlist;
639 static void mark_alternate_complete(struct fetch_negotiator *unused,
640 struct object *obj)
642 mark_complete(&obj->oid);
645 struct loose_object_iter {
646 struct oidset *loose_object_set;
647 struct ref *refs;
651 * Mark recent commits available locally and reachable from a local ref as
652 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
653 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
654 * thus do not need COMMON_REF marks).
656 * The cutoff time for recency is determined by this heuristic: it is the
657 * earliest commit time of the objects in refs that are commits and that we know
658 * the commit time of.
660 static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
661 struct fetch_pack_args *args,
662 struct ref **refs)
664 struct ref *ref;
665 int old_save_commit_buffer = save_commit_buffer;
666 timestamp_t cutoff = 0;
668 save_commit_buffer = 0;
670 for (ref = *refs; ref; ref = ref->next) {
671 struct object *o;
673 if (!has_object_file_with_flags(&ref->old_oid,
674 OBJECT_INFO_QUICK))
675 continue;
676 o = parse_object(the_repository, &ref->old_oid);
677 if (!o)
678 continue;
680 /* We already have it -- which may mean that we were
681 * in sync with the other side at some time after
682 * that (it is OK if we guess wrong here).
684 if (o->type == OBJ_COMMIT) {
685 struct commit *commit = (struct commit *)o;
686 if (!cutoff || cutoff < commit->date)
687 cutoff = commit->date;
691 if (!args->deepen) {
692 for_each_ref(mark_complete_oid, NULL);
693 for_each_cached_alternate(NULL, mark_alternate_complete);
694 commit_list_sort_by_date(&complete);
695 if (cutoff)
696 mark_recent_complete_commits(args, cutoff);
700 * Mark all complete remote refs as common refs.
701 * Don't mark them common yet; the server has to be told so first.
703 for (ref = *refs; ref; ref = ref->next) {
704 struct object *o = deref_tag(the_repository,
705 lookup_object(the_repository,
706 ref->old_oid.hash),
707 NULL, 0);
709 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
710 continue;
712 negotiator->known_common(negotiator,
713 (struct commit *)o);
716 save_commit_buffer = old_save_commit_buffer;
720 * Returns 1 if every object pointed to by the given remote refs is available
721 * locally and reachable from a local ref, and 0 otherwise.
723 static int everything_local(struct fetch_pack_args *args,
724 struct ref **refs)
726 struct ref *ref;
727 int retval;
729 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
730 const struct object_id *remote = &ref->old_oid;
731 struct object *o;
733 o = lookup_object(the_repository, remote->hash);
734 if (!o || !(o->flags & COMPLETE)) {
735 retval = 0;
736 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
737 ref->name);
738 continue;
740 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
741 ref->name);
744 return retval;
747 static int sideband_demux(int in, int out, void *data)
749 int *xd = data;
750 int ret;
752 ret = recv_sideband("fetch-pack", xd[0], out);
753 close(out);
754 return ret;
757 static int get_pack(struct fetch_pack_args *args,
758 int xd[2], char **pack_lockfile)
760 struct async demux;
761 int do_keep = args->keep_pack;
762 const char *cmd_name;
763 struct pack_header header;
764 int pass_header = 0;
765 struct child_process cmd = CHILD_PROCESS_INIT;
766 int ret;
768 memset(&demux, 0, sizeof(demux));
769 if (use_sideband) {
770 /* xd[] is talking with upload-pack; subprocess reads from
771 * xd[0], spits out band#2 to stderr, and feeds us band#1
772 * through demux->out.
774 demux.proc = sideband_demux;
775 demux.data = xd;
776 demux.out = -1;
777 demux.isolate_sigpipe = 1;
778 if (start_async(&demux))
779 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
781 else
782 demux.out = xd[0];
784 if (!args->keep_pack && unpack_limit) {
786 if (read_pack_header(demux.out, &header))
787 die(_("protocol error: bad pack header"));
788 pass_header = 1;
789 if (ntohl(header.hdr_entries) < unpack_limit)
790 do_keep = 0;
791 else
792 do_keep = 1;
795 if (alternate_shallow_file) {
796 argv_array_push(&cmd.args, "--shallow-file");
797 argv_array_push(&cmd.args, alternate_shallow_file);
800 if (do_keep || args->from_promisor) {
801 if (pack_lockfile)
802 cmd.out = -1;
803 cmd_name = "index-pack";
804 argv_array_push(&cmd.args, cmd_name);
805 argv_array_push(&cmd.args, "--stdin");
806 if (!args->quiet && !args->no_progress)
807 argv_array_push(&cmd.args, "-v");
808 if (args->use_thin_pack)
809 argv_array_push(&cmd.args, "--fix-thin");
810 if (do_keep && (args->lock_pack || unpack_limit)) {
811 char hostname[HOST_NAME_MAX + 1];
812 if (xgethostname(hostname, sizeof(hostname)))
813 xsnprintf(hostname, sizeof(hostname), "localhost");
814 argv_array_pushf(&cmd.args,
815 "--keep=fetch-pack %"PRIuMAX " on %s",
816 (uintmax_t)getpid(), hostname);
818 if (args->check_self_contained_and_connected)
819 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
820 if (args->from_promisor)
821 argv_array_push(&cmd.args, "--promisor");
823 else {
824 cmd_name = "unpack-objects";
825 argv_array_push(&cmd.args, cmd_name);
826 if (args->quiet || args->no_progress)
827 argv_array_push(&cmd.args, "-q");
828 args->check_self_contained_and_connected = 0;
831 if (pass_header)
832 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
833 ntohl(header.hdr_version),
834 ntohl(header.hdr_entries));
835 if (fetch_fsck_objects >= 0
836 ? fetch_fsck_objects
837 : transfer_fsck_objects >= 0
838 ? transfer_fsck_objects
839 : 0) {
840 if (args->from_promisor)
842 * We cannot use --strict in index-pack because it
843 * checks both broken objects and links, but we only
844 * want to check for broken objects.
846 argv_array_push(&cmd.args, "--fsck-objects");
847 else
848 argv_array_pushf(&cmd.args, "--strict%s",
849 fsck_msg_types.buf);
852 cmd.in = demux.out;
853 cmd.git_cmd = 1;
854 if (start_command(&cmd))
855 die(_("fetch-pack: unable to fork off %s"), cmd_name);
856 if (do_keep && pack_lockfile) {
857 *pack_lockfile = index_pack_lockfile(cmd.out);
858 close(cmd.out);
861 if (!use_sideband)
862 /* Closed by start_command() */
863 xd[0] = -1;
865 ret = finish_command(&cmd);
866 if (!ret || (args->check_self_contained_and_connected && ret == 1))
867 args->self_contained_and_connected =
868 args->check_self_contained_and_connected &&
869 ret == 0;
870 else
871 die(_("%s failed"), cmd_name);
872 if (use_sideband && finish_async(&demux))
873 die(_("error in sideband demultiplexer"));
874 return 0;
877 static int cmp_ref_by_name(const void *a_, const void *b_)
879 const struct ref *a = *((const struct ref **)a_);
880 const struct ref *b = *((const struct ref **)b_);
881 return strcmp(a->name, b->name);
884 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
885 int fd[2],
886 const struct ref *orig_ref,
887 struct ref **sought, int nr_sought,
888 struct shallow_info *si,
889 char **pack_lockfile)
891 struct ref *ref = copy_ref_list(orig_ref);
892 struct object_id oid;
893 const char *agent_feature;
894 int agent_len;
895 struct fetch_negotiator negotiator;
896 fetch_negotiator_init(&negotiator, negotiation_algorithm);
898 sort_ref_list(&ref, ref_compare_name);
899 QSORT(sought, nr_sought, cmp_ref_by_name);
901 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
902 die(_("Server does not support shallow clients"));
903 if (args->depth > 0 || args->deepen_since || args->deepen_not)
904 args->deepen = 1;
905 if (server_supports("multi_ack_detailed")) {
906 print_verbose(args, _("Server supports multi_ack_detailed"));
907 multi_ack = 2;
908 if (server_supports("no-done")) {
909 print_verbose(args, _("Server supports no-done"));
910 if (args->stateless_rpc)
911 no_done = 1;
914 else if (server_supports("multi_ack")) {
915 print_verbose(args, _("Server supports multi_ack"));
916 multi_ack = 1;
918 if (server_supports("side-band-64k")) {
919 print_verbose(args, _("Server supports side-band-64k"));
920 use_sideband = 2;
922 else if (server_supports("side-band")) {
923 print_verbose(args, _("Server supports side-band"));
924 use_sideband = 1;
926 if (server_supports("allow-tip-sha1-in-want")) {
927 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
928 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
930 if (server_supports("allow-reachable-sha1-in-want")) {
931 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
932 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
934 if (!server_supports("thin-pack"))
935 args->use_thin_pack = 0;
936 if (!server_supports("no-progress"))
937 args->no_progress = 0;
938 if (!server_supports("include-tag"))
939 args->include_tag = 0;
940 if (server_supports("ofs-delta"))
941 print_verbose(args, _("Server supports ofs-delta"));
942 else
943 prefer_ofs_delta = 0;
945 if (server_supports("filter")) {
946 server_supports_filtering = 1;
947 print_verbose(args, _("Server supports filter"));
948 } else if (args->filter_options.choice) {
949 warning("filtering not recognized by server, ignoring");
952 if ((agent_feature = server_feature_value("agent", &agent_len))) {
953 agent_supported = 1;
954 if (agent_len)
955 print_verbose(args, _("Server version is %.*s"),
956 agent_len, agent_feature);
958 if (server_supports("deepen-since"))
959 deepen_since_ok = 1;
960 else if (args->deepen_since)
961 die(_("Server does not support --shallow-since"));
962 if (server_supports("deepen-not"))
963 deepen_not_ok = 1;
964 else if (args->deepen_not)
965 die(_("Server does not support --shallow-exclude"));
966 if (!server_supports("deepen-relative") && args->deepen_relative)
967 die(_("Server does not support --deepen"));
969 if (!args->no_dependents) {
970 mark_complete_and_common_ref(&negotiator, args, &ref);
971 filter_refs(args, &ref, sought, nr_sought);
972 if (everything_local(args, &ref)) {
973 packet_flush(fd[1]);
974 goto all_done;
976 } else {
977 filter_refs(args, &ref, sought, nr_sought);
979 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
980 if (!args->keep_pack)
981 /* When cloning, it is not unusual to have
982 * no common commit.
984 warning(_("no common commits"));
986 if (args->stateless_rpc)
987 packet_flush(fd[1]);
988 if (args->deepen)
989 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
990 NULL);
991 else if (si->nr_ours || si->nr_theirs)
992 alternate_shallow_file = setup_temporary_shallow(si->shallow);
993 else
994 alternate_shallow_file = NULL;
995 if (get_pack(args, fd, pack_lockfile))
996 die(_("git fetch-pack: fetch failed."));
998 all_done:
999 negotiator.release(&negotiator);
1000 return ref;
1003 static void add_shallow_requests(struct strbuf *req_buf,
1004 const struct fetch_pack_args *args)
1006 if (is_repository_shallow(the_repository))
1007 write_shallow_commits(req_buf, 1, NULL);
1008 if (args->depth > 0)
1009 packet_buf_write(req_buf, "deepen %d", args->depth);
1010 if (args->deepen_since) {
1011 timestamp_t max_age = approxidate(args->deepen_since);
1012 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1014 if (args->deepen_not) {
1015 int i;
1016 for (i = 0; i < args->deepen_not->nr; i++) {
1017 struct string_list_item *s = args->deepen_not->items + i;
1018 packet_buf_write(req_buf, "deepen-not %s", s->string);
1021 if (args->deepen_relative)
1022 packet_buf_write(req_buf, "deepen-relative\n");
1025 static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
1027 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1029 for ( ; wants ; wants = wants->next) {
1030 const struct object_id *remote = &wants->old_oid;
1031 struct object *o;
1034 * If that object is complete (i.e. it is an ancestor of a
1035 * local ref), we tell them we have it but do not have to
1036 * tell them about its ancestors, which they already know
1037 * about.
1039 * We use lookup_object here because we are only
1040 * interested in the case we *know* the object is
1041 * reachable and we have already scanned it.
1043 * Do this only if args->no_dependents is false (if it is true,
1044 * we cannot trust the object flags).
1046 if (!no_dependents &&
1047 ((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1048 (o->flags & COMPLETE)) {
1049 continue;
1052 if (!use_ref_in_want || wants->exact_oid)
1053 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1054 else
1055 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1059 static void add_common(struct strbuf *req_buf, struct oidset *common)
1061 struct oidset_iter iter;
1062 const struct object_id *oid;
1063 oidset_iter_init(common, &iter);
1065 while ((oid = oidset_iter_next(&iter))) {
1066 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1070 static int add_haves(struct fetch_negotiator *negotiator,
1071 struct strbuf *req_buf,
1072 int *haves_to_send, int *in_vain)
1074 int ret = 0;
1075 int haves_added = 0;
1076 const struct object_id *oid;
1078 while ((oid = negotiator->next(negotiator))) {
1079 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1080 if (++haves_added >= *haves_to_send)
1081 break;
1084 *in_vain += haves_added;
1085 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1086 /* Send Done */
1087 packet_buf_write(req_buf, "done\n");
1088 ret = 1;
1091 /* Increase haves to send on next round */
1092 *haves_to_send = next_flush(1, *haves_to_send);
1094 return ret;
1097 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1098 const struct fetch_pack_args *args,
1099 const struct ref *wants, struct oidset *common,
1100 int *haves_to_send, int *in_vain,
1101 int sideband_all)
1103 int ret = 0;
1104 struct strbuf req_buf = STRBUF_INIT;
1106 if (server_supports_v2("fetch", 1))
1107 packet_buf_write(&req_buf, "command=fetch");
1108 if (server_supports_v2("agent", 0))
1109 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1110 if (args->server_options && args->server_options->nr &&
1111 server_supports_v2("server-option", 1)) {
1112 int i;
1113 for (i = 0; i < args->server_options->nr; i++)
1114 packet_write_fmt(fd_out, "server-option=%s",
1115 args->server_options->items[i].string);
1118 packet_buf_delim(&req_buf);
1119 if (args->use_thin_pack)
1120 packet_buf_write(&req_buf, "thin-pack");
1121 if (args->no_progress)
1122 packet_buf_write(&req_buf, "no-progress");
1123 if (args->include_tag)
1124 packet_buf_write(&req_buf, "include-tag");
1125 if (prefer_ofs_delta)
1126 packet_buf_write(&req_buf, "ofs-delta");
1127 if (sideband_all)
1128 packet_buf_write(&req_buf, "sideband-all");
1130 /* Add shallow-info and deepen request */
1131 if (server_supports_feature("fetch", "shallow", 0))
1132 add_shallow_requests(&req_buf, args);
1133 else if (is_repository_shallow(the_repository) || args->deepen)
1134 die(_("Server does not support shallow requests"));
1136 /* Add filter */
1137 if (server_supports_feature("fetch", "filter", 0) &&
1138 args->filter_options.choice) {
1139 struct strbuf expanded_filter_spec = STRBUF_INIT;
1140 print_verbose(args, _("Server supports filter"));
1141 expand_list_objects_filter_spec(&args->filter_options,
1142 &expanded_filter_spec);
1143 packet_buf_write(&req_buf, "filter %s",
1144 expanded_filter_spec.buf);
1145 strbuf_release(&expanded_filter_spec);
1146 } else if (args->filter_options.choice) {
1147 warning("filtering not recognized by server, ignoring");
1150 /* add wants */
1151 add_wants(args->no_dependents, wants, &req_buf);
1153 if (args->no_dependents) {
1154 packet_buf_write(&req_buf, "done");
1155 ret = 1;
1156 } else {
1157 /* Add all of the common commits we've found in previous rounds */
1158 add_common(&req_buf, common);
1160 /* Add initial haves */
1161 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1164 /* Send request */
1165 packet_buf_flush(&req_buf);
1166 write_or_die(fd_out, req_buf.buf, req_buf.len);
1168 strbuf_release(&req_buf);
1169 return ret;
1173 * Processes a section header in a server's response and checks if it matches
1174 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1175 * not consumed); if 0, the line will be consumed and the function will die if
1176 * the section header doesn't match what was expected.
1178 static int process_section_header(struct packet_reader *reader,
1179 const char *section, int peek)
1181 int ret;
1183 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1184 die(_("error reading section header '%s'"), section);
1186 ret = !strcmp(reader->line, section);
1188 if (!peek) {
1189 if (!ret)
1190 die(_("expected '%s', received '%s'"),
1191 section, reader->line);
1192 packet_reader_read(reader);
1195 return ret;
1198 static int process_acks(struct fetch_negotiator *negotiator,
1199 struct packet_reader *reader,
1200 struct oidset *common)
1202 /* received */
1203 int received_ready = 0;
1204 int received_ack = 0;
1206 process_section_header(reader, "acknowledgments", 0);
1207 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1208 const char *arg;
1210 if (!strcmp(reader->line, "NAK"))
1211 continue;
1213 if (skip_prefix(reader->line, "ACK ", &arg)) {
1214 struct object_id oid;
1215 if (!get_oid_hex(arg, &oid)) {
1216 struct commit *commit;
1217 oidset_insert(common, &oid);
1218 commit = lookup_commit(the_repository, &oid);
1219 negotiator->ack(negotiator, commit);
1221 continue;
1224 if (!strcmp(reader->line, "ready")) {
1225 received_ready = 1;
1226 continue;
1229 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1232 if (reader->status != PACKET_READ_FLUSH &&
1233 reader->status != PACKET_READ_DELIM)
1234 die(_("error processing acks: %d"), reader->status);
1237 * If an "acknowledgments" section is sent, a packfile is sent if and
1238 * only if "ready" was sent in this section. The other sections
1239 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1240 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1241 * otherwise.
1243 if (received_ready && reader->status != PACKET_READ_DELIM)
1244 die(_("expected packfile to be sent after 'ready'"));
1245 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1246 die(_("expected no other sections to be sent after no 'ready'"));
1248 /* return 0 if no common, 1 if there are common, or 2 if ready */
1249 return received_ready ? 2 : (received_ack ? 1 : 0);
1252 static void receive_shallow_info(struct fetch_pack_args *args,
1253 struct packet_reader *reader)
1255 int line_received = 0;
1257 process_section_header(reader, "shallow-info", 0);
1258 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1259 const char *arg;
1260 struct object_id oid;
1262 if (skip_prefix(reader->line, "shallow ", &arg)) {
1263 if (get_oid_hex(arg, &oid))
1264 die(_("invalid shallow line: %s"), reader->line);
1265 register_shallow(the_repository, &oid);
1266 line_received = 1;
1267 continue;
1269 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1270 if (get_oid_hex(arg, &oid))
1271 die(_("invalid unshallow line: %s"), reader->line);
1272 if (!lookup_object(the_repository, oid.hash))
1273 die(_("object not found: %s"), reader->line);
1274 /* make sure that it is parsed as shallow */
1275 if (!parse_object(the_repository, &oid))
1276 die(_("error in object: %s"), reader->line);
1277 if (unregister_shallow(&oid))
1278 die(_("no shallow found: %s"), reader->line);
1279 line_received = 1;
1280 continue;
1282 die(_("expected shallow/unshallow, got %s"), reader->line);
1285 if (reader->status != PACKET_READ_FLUSH &&
1286 reader->status != PACKET_READ_DELIM)
1287 die(_("error processing shallow info: %d"), reader->status);
1289 if (line_received) {
1290 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1291 NULL);
1292 args->deepen = 1;
1296 static void receive_wanted_refs(struct packet_reader *reader,
1297 struct ref **sought, int nr_sought)
1299 process_section_header(reader, "wanted-refs", 0);
1300 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1301 struct object_id oid;
1302 const char *end;
1303 int i;
1305 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1306 die(_("expected wanted-ref, got '%s'"), reader->line);
1308 for (i = 0; i < nr_sought; i++) {
1309 if (!strcmp(end, sought[i]->name)) {
1310 oidcpy(&sought[i]->old_oid, &oid);
1311 break;
1315 if (i == nr_sought)
1316 die(_("unexpected wanted-ref: '%s'"), reader->line);
1319 if (reader->status != PACKET_READ_DELIM)
1320 die(_("error processing wanted refs: %d"), reader->status);
1323 enum fetch_state {
1324 FETCH_CHECK_LOCAL = 0,
1325 FETCH_SEND_REQUEST,
1326 FETCH_PROCESS_ACKS,
1327 FETCH_GET_PACK,
1328 FETCH_DONE,
1331 static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1332 int fd[2],
1333 const struct ref *orig_ref,
1334 struct ref **sought, int nr_sought,
1335 char **pack_lockfile)
1337 struct ref *ref = copy_ref_list(orig_ref);
1338 enum fetch_state state = FETCH_CHECK_LOCAL;
1339 struct oidset common = OIDSET_INIT;
1340 struct packet_reader reader;
1341 int in_vain = 0;
1342 int haves_to_send = INITIAL_FLUSH;
1343 struct fetch_negotiator negotiator;
1344 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1345 packet_reader_init(&reader, fd[0], NULL, 0,
1346 PACKET_READ_CHOMP_NEWLINE |
1347 PACKET_READ_DIE_ON_ERR_PACKET);
1348 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1349 server_supports_feature("fetch", "sideband-all", 0)) {
1350 reader.use_sideband = 1;
1351 reader.me = "fetch-pack";
1354 while (state != FETCH_DONE) {
1355 switch (state) {
1356 case FETCH_CHECK_LOCAL:
1357 sort_ref_list(&ref, ref_compare_name);
1358 QSORT(sought, nr_sought, cmp_ref_by_name);
1360 /* v2 supports these by default */
1361 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1362 use_sideband = 2;
1363 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1364 args->deepen = 1;
1366 /* Filter 'ref' by 'sought' and those that aren't local */
1367 if (!args->no_dependents) {
1368 mark_complete_and_common_ref(&negotiator, args, &ref);
1369 filter_refs(args, &ref, sought, nr_sought);
1370 if (everything_local(args, &ref))
1371 state = FETCH_DONE;
1372 else
1373 state = FETCH_SEND_REQUEST;
1375 mark_tips(&negotiator, args->negotiation_tips);
1376 for_each_cached_alternate(&negotiator,
1377 insert_one_alternate_object);
1378 } else {
1379 filter_refs(args, &ref, sought, nr_sought);
1380 state = FETCH_SEND_REQUEST;
1382 break;
1383 case FETCH_SEND_REQUEST:
1384 if (send_fetch_request(&negotiator, fd[1], args, ref,
1385 &common,
1386 &haves_to_send, &in_vain,
1387 reader.use_sideband))
1388 state = FETCH_GET_PACK;
1389 else
1390 state = FETCH_PROCESS_ACKS;
1391 break;
1392 case FETCH_PROCESS_ACKS:
1393 /* Process ACKs/NAKs */
1394 switch (process_acks(&negotiator, &reader, &common)) {
1395 case 2:
1396 state = FETCH_GET_PACK;
1397 break;
1398 case 1:
1399 in_vain = 0;
1400 /* fallthrough */
1401 default:
1402 state = FETCH_SEND_REQUEST;
1403 break;
1405 break;
1406 case FETCH_GET_PACK:
1407 /* Check for shallow-info section */
1408 if (process_section_header(&reader, "shallow-info", 1))
1409 receive_shallow_info(args, &reader);
1411 if (process_section_header(&reader, "wanted-refs", 1))
1412 receive_wanted_refs(&reader, sought, nr_sought);
1414 /* get the pack */
1415 process_section_header(&reader, "packfile", 0);
1416 if (get_pack(args, fd, pack_lockfile))
1417 die(_("git fetch-pack: fetch failed."));
1419 state = FETCH_DONE;
1420 break;
1421 case FETCH_DONE:
1422 continue;
1426 negotiator.release(&negotiator);
1427 oidset_clear(&common);
1428 return ref;
1431 static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1433 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1434 const char *path;
1436 if (git_config_pathname(&path, var, value))
1437 return 1;
1438 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1439 fsck_msg_types.len ? ',' : '=', path);
1440 free((char *)path);
1441 return 0;
1444 if (skip_prefix(var, "fetch.fsck.", &var)) {
1445 if (is_valid_msg_type(var, value))
1446 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1447 fsck_msg_types.len ? ',' : '=', var, value);
1448 else
1449 warning("Skipping unknown msg id '%s'", var);
1450 return 0;
1453 return git_default_config(var, value, cb);
1456 static void fetch_pack_config(void)
1458 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1459 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1460 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1461 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1462 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1463 git_config_get_string("fetch.negotiationalgorithm",
1464 &negotiation_algorithm);
1466 git_config(fetch_pack_config_cb, NULL);
1469 static void fetch_pack_setup(void)
1471 static int did_setup;
1472 if (did_setup)
1473 return;
1474 fetch_pack_config();
1475 if (0 <= transfer_unpack_limit)
1476 unpack_limit = transfer_unpack_limit;
1477 else if (0 <= fetch_unpack_limit)
1478 unpack_limit = fetch_unpack_limit;
1479 did_setup = 1;
1482 static int remove_duplicates_in_refs(struct ref **ref, int nr)
1484 struct string_list names = STRING_LIST_INIT_NODUP;
1485 int src, dst;
1487 for (src = dst = 0; src < nr; src++) {
1488 struct string_list_item *item;
1489 item = string_list_insert(&names, ref[src]->name);
1490 if (item->util)
1491 continue; /* already have it */
1492 item->util = ref[src];
1493 if (src != dst)
1494 ref[dst] = ref[src];
1495 dst++;
1497 for (src = dst; src < nr; src++)
1498 ref[src] = NULL;
1499 string_list_clear(&names, 0);
1500 return dst;
1503 static void update_shallow(struct fetch_pack_args *args,
1504 struct ref **sought, int nr_sought,
1505 struct shallow_info *si)
1507 struct oid_array ref = OID_ARRAY_INIT;
1508 int *status;
1509 int i;
1511 if (args->deepen && alternate_shallow_file) {
1512 if (*alternate_shallow_file == '\0') { /* --unshallow */
1513 unlink_or_warn(git_path_shallow(the_repository));
1514 rollback_lock_file(&shallow_lock);
1515 } else
1516 commit_lock_file(&shallow_lock);
1517 return;
1520 if (!si->shallow || !si->shallow->nr)
1521 return;
1523 if (args->cloning) {
1525 * remote is shallow, but this is a clone, there are
1526 * no objects in repo to worry about. Accept any
1527 * shallow points that exist in the pack (iow in repo
1528 * after get_pack() and reprepare_packed_git())
1530 struct oid_array extra = OID_ARRAY_INIT;
1531 struct object_id *oid = si->shallow->oid;
1532 for (i = 0; i < si->shallow->nr; i++)
1533 if (has_object_file(&oid[i]))
1534 oid_array_append(&extra, &oid[i]);
1535 if (extra.nr) {
1536 setup_alternate_shallow(&shallow_lock,
1537 &alternate_shallow_file,
1538 &extra);
1539 commit_lock_file(&shallow_lock);
1541 oid_array_clear(&extra);
1542 return;
1545 if (!si->nr_ours && !si->nr_theirs)
1546 return;
1548 remove_nonexistent_theirs_shallow(si);
1549 if (!si->nr_ours && !si->nr_theirs)
1550 return;
1551 for (i = 0; i < nr_sought; i++)
1552 oid_array_append(&ref, &sought[i]->old_oid);
1553 si->ref = &ref;
1555 if (args->update_shallow) {
1557 * remote is also shallow, .git/shallow may be updated
1558 * so all refs can be accepted. Make sure we only add
1559 * shallow roots that are actually reachable from new
1560 * refs.
1562 struct oid_array extra = OID_ARRAY_INIT;
1563 struct object_id *oid = si->shallow->oid;
1564 assign_shallow_commits_to_refs(si, NULL, NULL);
1565 if (!si->nr_ours && !si->nr_theirs) {
1566 oid_array_clear(&ref);
1567 return;
1569 for (i = 0; i < si->nr_ours; i++)
1570 oid_array_append(&extra, &oid[si->ours[i]]);
1571 for (i = 0; i < si->nr_theirs; i++)
1572 oid_array_append(&extra, &oid[si->theirs[i]]);
1573 setup_alternate_shallow(&shallow_lock,
1574 &alternate_shallow_file,
1575 &extra);
1576 commit_lock_file(&shallow_lock);
1577 oid_array_clear(&extra);
1578 oid_array_clear(&ref);
1579 return;
1583 * remote is also shallow, check what ref is safe to update
1584 * without updating .git/shallow
1586 status = xcalloc(nr_sought, sizeof(*status));
1587 assign_shallow_commits_to_refs(si, NULL, status);
1588 if (si->nr_ours || si->nr_theirs) {
1589 for (i = 0; i < nr_sought; i++)
1590 if (status[i])
1591 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1593 free(status);
1594 oid_array_clear(&ref);
1597 static int iterate_ref_map(void *cb_data, struct object_id *oid)
1599 struct ref **rm = cb_data;
1600 struct ref *ref = *rm;
1602 if (!ref)
1603 return -1; /* end of the list */
1604 *rm = ref->next;
1605 oidcpy(oid, &ref->old_oid);
1606 return 0;
1609 struct ref *fetch_pack(struct fetch_pack_args *args,
1610 int fd[], struct child_process *conn,
1611 const struct ref *ref,
1612 const char *dest,
1613 struct ref **sought, int nr_sought,
1614 struct oid_array *shallow,
1615 char **pack_lockfile,
1616 enum protocol_version version)
1618 struct ref *ref_cpy;
1619 struct shallow_info si;
1621 fetch_pack_setup();
1622 if (nr_sought)
1623 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1625 if (args->no_dependents && !args->filter_options.choice) {
1627 * The protocol does not support requesting that only the
1628 * wanted objects be sent, so approximate this by setting a
1629 * "blob:none" filter if no filter is already set. This works
1630 * for all object types: note that wanted blobs will still be
1631 * sent because they are directly specified as a "want".
1633 * NEEDSWORK: Add an option in the protocol to request that
1634 * only the wanted objects be sent, and implement it.
1636 parse_list_objects_filter(&args->filter_options, "blob:none");
1639 if (version != protocol_v2 && !ref) {
1640 packet_flush(fd[1]);
1641 die(_("no matching remote head"));
1643 prepare_shallow_info(&si, shallow);
1644 if (version == protocol_v2)
1645 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1646 pack_lockfile);
1647 else
1648 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1649 &si, pack_lockfile);
1650 reprepare_packed_git(the_repository);
1652 if (!args->cloning && args->deepen) {
1653 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1654 struct ref *iterator = ref_cpy;
1655 opt.shallow_file = alternate_shallow_file;
1656 if (args->deepen)
1657 opt.is_deepening_fetch = 1;
1658 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1659 error(_("remote did not send all necessary objects"));
1660 free_refs(ref_cpy);
1661 ref_cpy = NULL;
1662 rollback_lock_file(&shallow_lock);
1663 goto cleanup;
1665 args->connectivity_checked = 1;
1668 update_shallow(args, sought, nr_sought, &si);
1669 cleanup:
1670 clear_shallow_info(&si);
1671 return ref_cpy;
1674 int report_unmatched_refs(struct ref **sought, int nr_sought)
1676 int i, ret = 0;
1678 for (i = 0; i < nr_sought; i++) {
1679 if (!sought[i])
1680 continue;
1681 switch (sought[i]->match_status) {
1682 case REF_MATCHED:
1683 continue;
1684 case REF_NOT_MATCHED:
1685 error(_("no such remote ref %s"), sought[i]->name);
1686 break;
1687 case REF_UNADVERTISED_NOT_ALLOWED:
1688 error(_("Server does not allow request for unadvertised object %s"),
1689 sought[i]->name);
1690 break;
1692 ret = 1;
1694 return ret;