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"
25
26static int transfer_unpack_limit = -1;
27static int fetch_unpack_limit = -1;
28static int unpack_limit = 100;
29static int prefer_ofs_delta = 1;
30static int no_done;
31static int deepen_since_ok;
32static int deepen_not_ok;
33static int fetch_fsck_objects = -1;
34static int transfer_fsck_objects = -1;
35static int agent_supported;
36static int server_supports_filtering;
37static struct lock_file shallow_lock;
38static const char *alternate_shallow_file;
39static char *negotiation_algorithm;
40static struct strbuf fsck_msg_types = STRBUF_INIT;
41
42/* Remember to update object flag allocation in object.h */
43#define COMPLETE (1U << 0)
44#define ALTERNATE (1U << 1)
45
46/*
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
49 */
50#define MAX_IN_VAIN 256
51
52static 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
57static unsigned int allow_unadvertised_object_request;
58
59__attribute__((format (printf, 2, 3)))
60static inline void print_verbose(const struct fetch_pack_args *args,
61 const char *fmt, ...)
62{
63 va_list params;
64
65 if (!args->verbose)
66 return;
67
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
70 va_end(params);
71 fputc('\n', stderr);
72}
73
74struct alternate_object_cache {
75 struct object **items;
76 size_t nr, alloc;
77};
78
79static void cache_one_alternate(const struct object_id *oid,
80 void *vcache)
81{
82 struct alternate_object_cache *cache = vcache;
83 struct object *obj = parse_object(the_repository, oid);
84
85 if (!obj || (obj->flags & ALTERNATE))
86 return;
87
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
91}
92
93static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
95 struct object *))
96{
97 static int initialized;
98 static struct alternate_object_cache cache;
99 size_t i;
100
101 if (!initialized) {
102 for_each_alternate_ref(cache_one_alternate, &cache);
103 initialized = 1;
104 }
105
106 for (i = 0; i < cache.nr; i++)
107 cb(negotiator, cache.items[i]);
108}
109
110static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
111 const char *refname,
112 const struct object_id *oid)
113{
114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
116 refname, 0);
117
118 if (o && o->type == OBJ_COMMIT)
119 negotiator->add_tip(negotiator, (struct commit *)o);
120
121 return 0;
122}
123
124static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
126{
127 return rev_list_insert_ref(cb_data, refname, oid);
128}
129
130enum ack_type {
131 NAK = 0,
132 ACK,
133 ACK_continue,
134 ACK_common,
135 ACK_ready
136};
137
138static void consume_shallow_list(struct fetch_pack_args *args, int fd)
139{
140 if (args->stateless_rpc && args->deepen) {
141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
144 */
145 char *line;
146 while ((line = packet_read_line(fd, NULL))) {
147 if (starts_with(line, "shallow "))
148 continue;
149 if (starts_with(line, "unshallow "))
150 continue;
151 die(_("git fetch-pack: expected shallow list"));
152 }
153 }
154}
155
156static enum ack_type get_ack(int fd, struct object_id *result_oid)
157{
158 int len;
159 char *line = packet_read_line(fd, &len);
160 const char *arg;
161
162 if (!line)
163 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
164 if (!strcmp(line, "NAK"))
165 return NAK;
166 if (skip_prefix(line, "ACK ", &arg)) {
167 if (!get_oid_hex(arg, result_oid)) {
168 arg += 40;
169 len -= arg - line;
170 if (len < 1)
171 return ACK;
172 if (strstr(arg, "continue"))
173 return ACK_continue;
174 if (strstr(arg, "common"))
175 return ACK_common;
176 if (strstr(arg, "ready"))
177 return ACK_ready;
178 return ACK;
179 }
180 }
181 if (skip_prefix(line, "ERR ", &arg))
182 die(_("remote error: %s"), arg);
183 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
184}
185
186static void send_request(struct fetch_pack_args *args,
187 int fd, struct strbuf *buf)
188{
189 if (args->stateless_rpc) {
190 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
191 packet_flush(fd);
192 } else
193 write_or_die(fd, buf->buf, buf->len);
194}
195
196static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
197 struct object *obj)
198{
199 rev_list_insert_ref(negotiator, NULL, &obj->oid);
200}
201
202#define INITIAL_FLUSH 16
203#define PIPESAFE_FLUSH 32
204#define LARGE_FLUSH 16384
205
206static int next_flush(int stateless_rpc, int count)
207{
208 if (stateless_rpc) {
209 if (count < LARGE_FLUSH)
210 count <<= 1;
211 else
212 count = count * 11 / 10;
213 } else {
214 if (count < PIPESAFE_FLUSH)
215 count <<= 1;
216 else
217 count += PIPESAFE_FLUSH;
218 }
219 return count;
220}
221
222static void mark_tips(struct fetch_negotiator *negotiator,
223 const struct oid_array *negotiation_tips)
224{
225 int i;
226
227 if (!negotiation_tips) {
228 for_each_ref(rev_list_insert_ref_oid, negotiator);
229 return;
230 }
231
232 for (i = 0; i < negotiation_tips->nr; i++)
233 rev_list_insert_ref(negotiator, NULL,
234 &negotiation_tips->oid[i]);
235 return;
236}
237
238static int find_common(struct fetch_negotiator *negotiator,
239 struct fetch_pack_args *args,
240 int fd[2], struct object_id *result_oid,
241 struct ref *refs)
242{
243 int fetching;
244 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
245 const struct object_id *oid;
246 unsigned in_vain = 0;
247 int got_continue = 0;
248 int got_ready = 0;
249 struct strbuf req_buf = STRBUF_INIT;
250 size_t state_len = 0;
251
252 if (args->stateless_rpc && multi_ack == 1)
253 die(_("--stateless-rpc requires multi_ack_detailed"));
254
255 mark_tips(negotiator, args->negotiation_tips);
256 for_each_cached_alternate(negotiator, insert_one_alternate_object);
257
258 fetching = 0;
259 for ( ; refs ; refs = refs->next) {
260 struct object_id *remote = &refs->old_oid;
261 const char *remote_hex;
262 struct object *o;
263
264 /*
265 * If that object is complete (i.e. it is an ancestor of a
266 * local ref), we tell them we have it but do not have to
267 * tell them about its ancestors, which they already know
268 * about.
269 *
270 * We use lookup_object here because we are only
271 * interested in the case we *know* the object is
272 * reachable and we have already scanned it.
273 */
274 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
275 (o->flags & COMPLETE)) {
276 continue;
277 }
278
279 remote_hex = oid_to_hex(remote);
280 if (!fetching) {
281 struct strbuf c = STRBUF_INIT;
282 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
283 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
284 if (no_done) strbuf_addstr(&c, " no-done");
285 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
286 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
287 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
288 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
289 if (args->no_progress) strbuf_addstr(&c, " no-progress");
290 if (args->include_tag) strbuf_addstr(&c, " include-tag");
291 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
292 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
293 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
294 if (agent_supported) strbuf_addf(&c, " agent=%s",
295 git_user_agent_sanitized());
296 if (args->filter_options.choice)
297 strbuf_addstr(&c, " filter");
298 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
299 strbuf_release(&c);
300 } else
301 packet_buf_write(&req_buf, "want %s\n", remote_hex);
302 fetching++;
303 }
304
305 if (!fetching) {
306 strbuf_release(&req_buf);
307 packet_flush(fd[1]);
308 return 1;
309 }
310
311 if (is_repository_shallow(the_repository))
312 write_shallow_commits(&req_buf, 1, NULL);
313 if (args->depth > 0)
314 packet_buf_write(&req_buf, "deepen %d", args->depth);
315 if (args->deepen_since) {
316 timestamp_t max_age = approxidate(args->deepen_since);
317 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
318 }
319 if (args->deepen_not) {
320 int i;
321 for (i = 0; i < args->deepen_not->nr; i++) {
322 struct string_list_item *s = args->deepen_not->items + i;
323 packet_buf_write(&req_buf, "deepen-not %s", s->string);
324 }
325 }
326 if (server_supports_filtering && args->filter_options.choice)
327 packet_buf_write(&req_buf, "filter %s",
328 args->filter_options.filter_spec);
329 packet_buf_flush(&req_buf);
330 state_len = req_buf.len;
331
332 if (args->deepen) {
333 char *line;
334 const char *arg;
335 struct object_id oid;
336
337 send_request(args, fd[1], &req_buf);
338 while ((line = packet_read_line(fd[0], NULL))) {
339 if (skip_prefix(line, "shallow ", &arg)) {
340 if (get_oid_hex(arg, &oid))
341 die(_("invalid shallow line: %s"), line);
342 register_shallow(the_repository, &oid);
343 continue;
344 }
345 if (skip_prefix(line, "unshallow ", &arg)) {
346 if (get_oid_hex(arg, &oid))
347 die(_("invalid unshallow line: %s"), line);
348 if (!lookup_object(the_repository, oid.hash))
349 die(_("object not found: %s"), line);
350 /* make sure that it is parsed as shallow */
351 if (!parse_object(the_repository, &oid))
352 die(_("error in object: %s"), line);
353 if (unregister_shallow(&oid))
354 die(_("no shallow found: %s"), line);
355 continue;
356 }
357 die(_("expected shallow/unshallow, got %s"), line);
358 }
359 } else if (!args->stateless_rpc)
360 send_request(args, fd[1], &req_buf);
361
362 if (!args->stateless_rpc) {
363 /* If we aren't using the stateless-rpc interface
364 * we don't need to retain the headers.
365 */
366 strbuf_setlen(&req_buf, 0);
367 state_len = 0;
368 }
369
370 flushes = 0;
371 retval = -1;
372 if (args->no_dependents)
373 goto done;
374 while ((oid = negotiator->next(negotiator))) {
375 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
376 print_verbose(args, "have %s", oid_to_hex(oid));
377 in_vain++;
378 if (flush_at <= ++count) {
379 int ack;
380
381 packet_buf_flush(&req_buf);
382 send_request(args, fd[1], &req_buf);
383 strbuf_setlen(&req_buf, state_len);
384 flushes++;
385 flush_at = next_flush(args->stateless_rpc, count);
386
387 /*
388 * We keep one window "ahead" of the other side, and
389 * will wait for an ACK only on the next one
390 */
391 if (!args->stateless_rpc && count == INITIAL_FLUSH)
392 continue;
393
394 consume_shallow_list(args, fd[0]);
395 do {
396 ack = get_ack(fd[0], result_oid);
397 if (ack)
398 print_verbose(args, _("got %s %d %s"), "ack",
399 ack, oid_to_hex(result_oid));
400 switch (ack) {
401 case ACK:
402 flushes = 0;
403 multi_ack = 0;
404 retval = 0;
405 goto done;
406 case ACK_common:
407 case ACK_ready:
408 case ACK_continue: {
409 struct commit *commit =
410 lookup_commit(the_repository,
411 result_oid);
412 int was_common;
413
414 if (!commit)
415 die(_("invalid commit %s"), oid_to_hex(result_oid));
416 was_common = negotiator->ack(negotiator, commit);
417 if (args->stateless_rpc
418 && ack == ACK_common
419 && !was_common) {
420 /* We need to replay the have for this object
421 * on the next RPC request so the peer knows
422 * it is in common with us.
423 */
424 const char *hex = oid_to_hex(result_oid);
425 packet_buf_write(&req_buf, "have %s\n", hex);
426 state_len = req_buf.len;
427 /*
428 * Reset in_vain because an ack
429 * for this commit has not been
430 * seen.
431 */
432 in_vain = 0;
433 } else if (!args->stateless_rpc
434 || ack != ACK_common)
435 in_vain = 0;
436 retval = 0;
437 got_continue = 1;
438 if (ack == ACK_ready)
439 got_ready = 1;
440 break;
441 }
442 }
443 } while (ack);
444 flushes--;
445 if (got_continue && MAX_IN_VAIN < in_vain) {
446 print_verbose(args, _("giving up"));
447 break; /* give up */
448 }
449 if (got_ready)
450 break;
451 }
452 }
453done:
454 if (!got_ready || !no_done) {
455 packet_buf_write(&req_buf, "done\n");
456 send_request(args, fd[1], &req_buf);
457 }
458 print_verbose(args, _("done"));
459 if (retval != 0) {
460 multi_ack = 0;
461 flushes++;
462 }
463 strbuf_release(&req_buf);
464
465 if (!got_ready || !no_done)
466 consume_shallow_list(args, fd[0]);
467 while (flushes || multi_ack) {
468 int ack = get_ack(fd[0], result_oid);
469 if (ack) {
470 print_verbose(args, _("got %s (%d) %s"), "ack",
471 ack, oid_to_hex(result_oid));
472 if (ack == ACK)
473 return 0;
474 multi_ack = 1;
475 continue;
476 }
477 flushes--;
478 }
479 /* it is no error to fetch into a completely empty repo */
480 return count ? retval : 0;
481}
482
483static struct commit_list *complete;
484
485static int mark_complete(const struct object_id *oid)
486{
487 struct object *o = parse_object(the_repository, oid);
488
489 while (o && o->type == OBJ_TAG) {
490 struct tag *t = (struct tag *) o;
491 if (!t->tagged)
492 break; /* broken repository */
493 o->flags |= COMPLETE;
494 o = parse_object(the_repository, &t->tagged->oid);
495 }
496 if (o && o->type == OBJ_COMMIT) {
497 struct commit *commit = (struct commit *)o;
498 if (!(commit->object.flags & COMPLETE)) {
499 commit->object.flags |= COMPLETE;
500 commit_list_insert(commit, &complete);
501 }
502 }
503 return 0;
504}
505
506static int mark_complete_oid(const char *refname, const struct object_id *oid,
507 int flag, void *cb_data)
508{
509 return mark_complete(oid);
510}
511
512static void mark_recent_complete_commits(struct fetch_pack_args *args,
513 timestamp_t cutoff)
514{
515 while (complete && cutoff <= complete->item->date) {
516 print_verbose(args, _("Marking %s as complete"),
517 oid_to_hex(&complete->item->object.oid));
518 pop_most_recent_commit(&complete, COMPLETE);
519 }
520}
521
522static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
523{
524 for (; refs; refs = refs->next)
525 oidset_insert(oids, &refs->old_oid);
526}
527
528static int tip_oids_contain(struct oidset *tip_oids,
529 struct ref *unmatched, struct ref *newlist,
530 const struct object_id *id)
531{
532 /*
533 * Note that this only looks at the ref lists the first time it's
534 * called. This works out in filter_refs() because even though it may
535 * add to "newlist" between calls, the additions will always be for
536 * oids that are already in the set.
537 */
538 if (!tip_oids->map.map.tablesize) {
539 add_refs_to_oidset(tip_oids, unmatched);
540 add_refs_to_oidset(tip_oids, newlist);
541 }
542 return oidset_contains(tip_oids, id);
543}
544
545static void filter_refs(struct fetch_pack_args *args,
546 struct ref **refs,
547 struct ref **sought, int nr_sought)
548{
549 struct ref *newlist = NULL;
550 struct ref **newtail = &newlist;
551 struct ref *unmatched = NULL;
552 struct ref *ref, *next;
553 struct oidset tip_oids = OIDSET_INIT;
554 int i;
555
556 i = 0;
557 for (ref = *refs; ref; ref = next) {
558 int keep = 0;
559 next = ref->next;
560
561 if (starts_with(ref->name, "refs/") &&
562 check_refname_format(ref->name, 0))
563 ; /* trash */
564 else {
565 while (i < nr_sought) {
566 int cmp = strcmp(ref->name, sought[i]->name);
567 if (cmp < 0)
568 break; /* definitely do not have it */
569 else if (cmp == 0) {
570 keep = 1; /* definitely have it */
571 sought[i]->match_status = REF_MATCHED;
572 }
573 i++;
574 }
575
576 if (!keep && args->fetch_all &&
577 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
578 keep = 1;
579 }
580
581 if (keep) {
582 *newtail = ref;
583 ref->next = NULL;
584 newtail = &ref->next;
585 } else {
586 ref->next = unmatched;
587 unmatched = ref;
588 }
589 }
590
591 /* Append unmatched requests to the list */
592 for (i = 0; i < nr_sought; i++) {
593 struct object_id oid;
594 const char *p;
595
596 ref = sought[i];
597 if (ref->match_status != REF_NOT_MATCHED)
598 continue;
599 if (parse_oid_hex(ref->name, &oid, &p) ||
600 *p != '\0' ||
601 oidcmp(&oid, &ref->old_oid))
602 continue;
603
604 if ((allow_unadvertised_object_request &
605 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
606 tip_oids_contain(&tip_oids, unmatched, newlist,
607 &ref->old_oid)) {
608 ref->match_status = REF_MATCHED;
609 *newtail = copy_ref(ref);
610 newtail = &(*newtail)->next;
611 } else {
612 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
613 }
614 }
615
616 oidset_clear(&tip_oids);
617 for (ref = unmatched; ref; ref = next) {
618 next = ref->next;
619 free(ref);
620 }
621
622 *refs = newlist;
623}
624
625static void mark_alternate_complete(struct fetch_negotiator *unused,
626 struct object *obj)
627{
628 mark_complete(&obj->oid);
629}
630
631struct loose_object_iter {
632 struct oidset *loose_object_set;
633 struct ref *refs;
634};
635
636/*
637 * If the number of refs is not larger than the number of loose objects,
638 * this function stops inserting.
639 */
640static int add_loose_objects_to_set(const struct object_id *oid,
641 const char *path,
642 void *data)
643{
644 struct loose_object_iter *iter = data;
645 oidset_insert(iter->loose_object_set, oid);
646 if (iter->refs == NULL)
647 return 1;
648
649 iter->refs = iter->refs->next;
650 return 0;
651}
652
653/*
654 * Mark recent commits available locally and reachable from a local ref as
655 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
656 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
657 * thus do not need COMMON_REF marks).
658 *
659 * The cutoff time for recency is determined by this heuristic: it is the
660 * earliest commit time of the objects in refs that are commits and that we know
661 * the commit time of.
662 */
663static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
664 struct fetch_pack_args *args,
665 struct ref **refs)
666{
667 struct ref *ref;
668 int old_save_commit_buffer = save_commit_buffer;
669 timestamp_t cutoff = 0;
670 struct oidset loose_oid_set = OIDSET_INIT;
671 int use_oidset = 0;
672 struct loose_object_iter iter = {&loose_oid_set, *refs};
673
674 /* Enumerate all loose objects or know refs are not so many. */
675 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
676 &iter, 0);
677
678 save_commit_buffer = 0;
679
680 for (ref = *refs; ref; ref = ref->next) {
681 struct object *o;
682 unsigned int flags = OBJECT_INFO_QUICK;
683
684 if (use_oidset &&
685 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
686 /*
687 * I know this does not exist in the loose form,
688 * so check if it exists in a non-loose form.
689 */
690 flags |= OBJECT_INFO_IGNORE_LOOSE;
691 }
692
693 if (!has_object_file_with_flags(&ref->old_oid, flags))
694 continue;
695 o = parse_object(the_repository, &ref->old_oid);
696 if (!o)
697 continue;
698
699 /* We already have it -- which may mean that we were
700 * in sync with the other side at some time after
701 * that (it is OK if we guess wrong here).
702 */
703 if (o->type == OBJ_COMMIT) {
704 struct commit *commit = (struct commit *)o;
705 if (!cutoff || cutoff < commit->date)
706 cutoff = commit->date;
707 }
708 }
709
710 oidset_clear(&loose_oid_set);
711
712 if (!args->no_dependents) {
713 if (!args->deepen) {
714 for_each_ref(mark_complete_oid, NULL);
715 for_each_cached_alternate(NULL, mark_alternate_complete);
716 commit_list_sort_by_date(&complete);
717 if (cutoff)
718 mark_recent_complete_commits(args, cutoff);
719 }
720
721 /*
722 * Mark all complete remote refs as common refs.
723 * Don't mark them common yet; the server has to be told so first.
724 */
725 for (ref = *refs; ref; ref = ref->next) {
726 struct object *o = deref_tag(the_repository,
727 lookup_object(the_repository,
728 ref->old_oid.hash),
729 NULL, 0);
730
731 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
732 continue;
733
734 negotiator->known_common(negotiator,
735 (struct commit *)o);
736 }
737 }
738
739 save_commit_buffer = old_save_commit_buffer;
740}
741
742/*
743 * Returns 1 if every object pointed to by the given remote refs is available
744 * locally and reachable from a local ref, and 0 otherwise.
745 */
746static int everything_local(struct fetch_pack_args *args,
747 struct ref **refs)
748{
749 struct ref *ref;
750 int retval;
751
752 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
753 const struct object_id *remote = &ref->old_oid;
754 struct object *o;
755
756 o = lookup_object(the_repository, remote->hash);
757 if (!o || !(o->flags & COMPLETE)) {
758 retval = 0;
759 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
760 ref->name);
761 continue;
762 }
763 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
764 ref->name);
765 }
766
767 return retval;
768}
769
770static int sideband_demux(int in, int out, void *data)
771{
772 int *xd = data;
773 int ret;
774
775 ret = recv_sideband("fetch-pack", xd[0], out);
776 close(out);
777 return ret;
778}
779
780static int get_pack(struct fetch_pack_args *args,
781 int xd[2], char **pack_lockfile)
782{
783 struct async demux;
784 int do_keep = args->keep_pack;
785 const char *cmd_name;
786 struct pack_header header;
787 int pass_header = 0;
788 struct child_process cmd = CHILD_PROCESS_INIT;
789 int ret;
790
791 memset(&demux, 0, sizeof(demux));
792 if (use_sideband) {
793 /* xd[] is talking with upload-pack; subprocess reads from
794 * xd[0], spits out band#2 to stderr, and feeds us band#1
795 * through demux->out.
796 */
797 demux.proc = sideband_demux;
798 demux.data = xd;
799 demux.out = -1;
800 demux.isolate_sigpipe = 1;
801 if (start_async(&demux))
802 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
803 }
804 else
805 demux.out = xd[0];
806
807 if (!args->keep_pack && unpack_limit) {
808
809 if (read_pack_header(demux.out, &header))
810 die(_("protocol error: bad pack header"));
811 pass_header = 1;
812 if (ntohl(header.hdr_entries) < unpack_limit)
813 do_keep = 0;
814 else
815 do_keep = 1;
816 }
817
818 if (alternate_shallow_file) {
819 argv_array_push(&cmd.args, "--shallow-file");
820 argv_array_push(&cmd.args, alternate_shallow_file);
821 }
822
823 if (do_keep || args->from_promisor) {
824 if (pack_lockfile)
825 cmd.out = -1;
826 cmd_name = "index-pack";
827 argv_array_push(&cmd.args, cmd_name);
828 argv_array_push(&cmd.args, "--stdin");
829 if (!args->quiet && !args->no_progress)
830 argv_array_push(&cmd.args, "-v");
831 if (args->use_thin_pack)
832 argv_array_push(&cmd.args, "--fix-thin");
833 if (do_keep && (args->lock_pack || unpack_limit)) {
834 char hostname[HOST_NAME_MAX + 1];
835 if (xgethostname(hostname, sizeof(hostname)))
836 xsnprintf(hostname, sizeof(hostname), "localhost");
837 argv_array_pushf(&cmd.args,
838 "--keep=fetch-pack %"PRIuMAX " on %s",
839 (uintmax_t)getpid(), hostname);
840 }
841 if (args->check_self_contained_and_connected)
842 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
843 if (args->from_promisor)
844 argv_array_push(&cmd.args, "--promisor");
845 }
846 else {
847 cmd_name = "unpack-objects";
848 argv_array_push(&cmd.args, cmd_name);
849 if (args->quiet || args->no_progress)
850 argv_array_push(&cmd.args, "-q");
851 args->check_self_contained_and_connected = 0;
852 }
853
854 if (pass_header)
855 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
856 ntohl(header.hdr_version),
857 ntohl(header.hdr_entries));
858 if (fetch_fsck_objects >= 0
859 ? fetch_fsck_objects
860 : transfer_fsck_objects >= 0
861 ? transfer_fsck_objects
862 : 0) {
863 if (args->from_promisor)
864 /*
865 * We cannot use --strict in index-pack because it
866 * checks both broken objects and links, but we only
867 * want to check for broken objects.
868 */
869 argv_array_push(&cmd.args, "--fsck-objects");
870 else
871 argv_array_pushf(&cmd.args, "--strict%s",
872 fsck_msg_types.buf);
873 }
874
875 cmd.in = demux.out;
876 cmd.git_cmd = 1;
877 if (start_command(&cmd))
878 die(_("fetch-pack: unable to fork off %s"), cmd_name);
879 if (do_keep && pack_lockfile) {
880 *pack_lockfile = index_pack_lockfile(cmd.out);
881 close(cmd.out);
882 }
883
884 if (!use_sideband)
885 /* Closed by start_command() */
886 xd[0] = -1;
887
888 ret = finish_command(&cmd);
889 if (!ret || (args->check_self_contained_and_connected && ret == 1))
890 args->self_contained_and_connected =
891 args->check_self_contained_and_connected &&
892 ret == 0;
893 else
894 die(_("%s failed"), cmd_name);
895 if (use_sideband && finish_async(&demux))
896 die(_("error in sideband demultiplexer"));
897 return 0;
898}
899
900static int cmp_ref_by_name(const void *a_, const void *b_)
901{
902 const struct ref *a = *((const struct ref **)a_);
903 const struct ref *b = *((const struct ref **)b_);
904 return strcmp(a->name, b->name);
905}
906
907static struct ref *do_fetch_pack(struct fetch_pack_args *args,
908 int fd[2],
909 const struct ref *orig_ref,
910 struct ref **sought, int nr_sought,
911 struct shallow_info *si,
912 char **pack_lockfile)
913{
914 struct ref *ref = copy_ref_list(orig_ref);
915 struct object_id oid;
916 const char *agent_feature;
917 int agent_len;
918 struct fetch_negotiator negotiator;
919 fetch_negotiator_init(&negotiator, negotiation_algorithm);
920
921 sort_ref_list(&ref, ref_compare_name);
922 QSORT(sought, nr_sought, cmp_ref_by_name);
923
924 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
925 die(_("Server does not support shallow clients"));
926 if (args->depth > 0 || args->deepen_since || args->deepen_not)
927 args->deepen = 1;
928 if (server_supports("multi_ack_detailed")) {
929 print_verbose(args, _("Server supports multi_ack_detailed"));
930 multi_ack = 2;
931 if (server_supports("no-done")) {
932 print_verbose(args, _("Server supports no-done"));
933 if (args->stateless_rpc)
934 no_done = 1;
935 }
936 }
937 else if (server_supports("multi_ack")) {
938 print_verbose(args, _("Server supports multi_ack"));
939 multi_ack = 1;
940 }
941 if (server_supports("side-band-64k")) {
942 print_verbose(args, _("Server supports side-band-64k"));
943 use_sideband = 2;
944 }
945 else if (server_supports("side-band")) {
946 print_verbose(args, _("Server supports side-band"));
947 use_sideband = 1;
948 }
949 if (server_supports("allow-tip-sha1-in-want")) {
950 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
951 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
952 }
953 if (server_supports("allow-reachable-sha1-in-want")) {
954 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
955 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
956 }
957 if (!server_supports("thin-pack"))
958 args->use_thin_pack = 0;
959 if (!server_supports("no-progress"))
960 args->no_progress = 0;
961 if (!server_supports("include-tag"))
962 args->include_tag = 0;
963 if (server_supports("ofs-delta"))
964 print_verbose(args, _("Server supports ofs-delta"));
965 else
966 prefer_ofs_delta = 0;
967
968 if (server_supports("filter")) {
969 server_supports_filtering = 1;
970 print_verbose(args, _("Server supports filter"));
971 } else if (args->filter_options.choice) {
972 warning("filtering not recognized by server, ignoring");
973 }
974
975 if ((agent_feature = server_feature_value("agent", &agent_len))) {
976 agent_supported = 1;
977 if (agent_len)
978 print_verbose(args, _("Server version is %.*s"),
979 agent_len, agent_feature);
980 }
981 if (server_supports("deepen-since"))
982 deepen_since_ok = 1;
983 else if (args->deepen_since)
984 die(_("Server does not support --shallow-since"));
985 if (server_supports("deepen-not"))
986 deepen_not_ok = 1;
987 else if (args->deepen_not)
988 die(_("Server does not support --shallow-exclude"));
989 if (!server_supports("deepen-relative") && args->deepen_relative)
990 die(_("Server does not support --deepen"));
991
992 mark_complete_and_common_ref(&negotiator, args, &ref);
993 filter_refs(args, &ref, sought, nr_sought);
994 if (everything_local(args, &ref)) {
995 packet_flush(fd[1]);
996 goto all_done;
997 }
998 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
999 if (!args->keep_pack)
1000 /* When cloning, it is not unusual to have
1001 * no common commit.
1002 */
1003 warning(_("no common commits"));
1004
1005 if (args->stateless_rpc)
1006 packet_flush(fd[1]);
1007 if (args->deepen)
1008 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1009 NULL);
1010 else if (si->nr_ours || si->nr_theirs)
1011 alternate_shallow_file = setup_temporary_shallow(si->shallow);
1012 else
1013 alternate_shallow_file = NULL;
1014 if (get_pack(args, fd, pack_lockfile))
1015 die(_("git fetch-pack: fetch failed."));
1016
1017 all_done:
1018 negotiator.release(&negotiator);
1019 return ref;
1020}
1021
1022static void add_shallow_requests(struct strbuf *req_buf,
1023 const struct fetch_pack_args *args)
1024{
1025 if (is_repository_shallow(the_repository))
1026 write_shallow_commits(req_buf, 1, NULL);
1027 if (args->depth > 0)
1028 packet_buf_write(req_buf, "deepen %d", args->depth);
1029 if (args->deepen_since) {
1030 timestamp_t max_age = approxidate(args->deepen_since);
1031 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1032 }
1033 if (args->deepen_not) {
1034 int i;
1035 for (i = 0; i < args->deepen_not->nr; i++) {
1036 struct string_list_item *s = args->deepen_not->items + i;
1037 packet_buf_write(req_buf, "deepen-not %s", s->string);
1038 }
1039 }
1040}
1041
1042static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1043{
1044 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1045
1046 for ( ; wants ; wants = wants->next) {
1047 const struct object_id *remote = &wants->old_oid;
1048 struct object *o;
1049
1050 /*
1051 * If that object is complete (i.e. it is an ancestor of a
1052 * local ref), we tell them we have it but do not have to
1053 * tell them about its ancestors, which they already know
1054 * about.
1055 *
1056 * We use lookup_object here because we are only
1057 * interested in the case we *know* the object is
1058 * reachable and we have already scanned it.
1059 */
1060 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
1061 (o->flags & COMPLETE)) {
1062 continue;
1063 }
1064
1065 if (!use_ref_in_want || wants->exact_oid)
1066 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1067 else
1068 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1069 }
1070}
1071
1072static void add_common(struct strbuf *req_buf, struct oidset *common)
1073{
1074 struct oidset_iter iter;
1075 const struct object_id *oid;
1076 oidset_iter_init(common, &iter);
1077
1078 while ((oid = oidset_iter_next(&iter))) {
1079 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1080 }
1081}
1082
1083static int add_haves(struct fetch_negotiator *negotiator,
1084 struct strbuf *req_buf,
1085 int *haves_to_send, int *in_vain)
1086{
1087 int ret = 0;
1088 int haves_added = 0;
1089 const struct object_id *oid;
1090
1091 while ((oid = negotiator->next(negotiator))) {
1092 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1093 if (++haves_added >= *haves_to_send)
1094 break;
1095 }
1096
1097 *in_vain += haves_added;
1098 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1099 /* Send Done */
1100 packet_buf_write(req_buf, "done\n");
1101 ret = 1;
1102 }
1103
1104 /* Increase haves to send on next round */
1105 *haves_to_send = next_flush(1, *haves_to_send);
1106
1107 return ret;
1108}
1109
1110static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1111 const struct fetch_pack_args *args,
1112 const struct ref *wants, struct oidset *common,
1113 int *haves_to_send, int *in_vain)
1114{
1115 int ret = 0;
1116 struct strbuf req_buf = STRBUF_INIT;
1117
1118 if (server_supports_v2("fetch", 1))
1119 packet_buf_write(&req_buf, "command=fetch");
1120 if (server_supports_v2("agent", 0))
1121 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1122 if (args->server_options && args->server_options->nr &&
1123 server_supports_v2("server-option", 1)) {
1124 int i;
1125 for (i = 0; i < args->server_options->nr; i++)
1126 packet_write_fmt(fd_out, "server-option=%s",
1127 args->server_options->items[i].string);
1128 }
1129
1130 packet_buf_delim(&req_buf);
1131 if (args->use_thin_pack)
1132 packet_buf_write(&req_buf, "thin-pack");
1133 if (args->no_progress)
1134 packet_buf_write(&req_buf, "no-progress");
1135 if (args->include_tag)
1136 packet_buf_write(&req_buf, "include-tag");
1137 if (prefer_ofs_delta)
1138 packet_buf_write(&req_buf, "ofs-delta");
1139
1140 /* Add shallow-info and deepen request */
1141 if (server_supports_feature("fetch", "shallow", 0))
1142 add_shallow_requests(&req_buf, args);
1143 else if (is_repository_shallow(the_repository) || args->deepen)
1144 die(_("Server does not support shallow requests"));
1145
1146 /* Add filter */
1147 if (server_supports_feature("fetch", "filter", 0) &&
1148 args->filter_options.choice) {
1149 print_verbose(args, _("Server supports filter"));
1150 packet_buf_write(&req_buf, "filter %s",
1151 args->filter_options.filter_spec);
1152 } else if (args->filter_options.choice) {
1153 warning("filtering not recognized by server, ignoring");
1154 }
1155
1156 /* add wants */
1157 add_wants(wants, &req_buf);
1158
1159 if (args->no_dependents) {
1160 packet_buf_write(&req_buf, "done");
1161 ret = 1;
1162 } else {
1163 /* Add all of the common commits we've found in previous rounds */
1164 add_common(&req_buf, common);
1165
1166 /* Add initial haves */
1167 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
1168 }
1169
1170 /* Send request */
1171 packet_buf_flush(&req_buf);
1172 write_or_die(fd_out, req_buf.buf, req_buf.len);
1173
1174 strbuf_release(&req_buf);
1175 return ret;
1176}
1177
1178/*
1179 * Processes a section header in a server's response and checks if it matches
1180 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1181 * not consumed); if 0, the line will be consumed and the function will die if
1182 * the section header doesn't match what was expected.
1183 */
1184static int process_section_header(struct packet_reader *reader,
1185 const char *section, int peek)
1186{
1187 int ret;
1188
1189 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1190 die(_("error reading section header '%s'"), section);
1191
1192 ret = !strcmp(reader->line, section);
1193
1194 if (!peek) {
1195 if (!ret)
1196 die(_("expected '%s', received '%s'"),
1197 section, reader->line);
1198 packet_reader_read(reader);
1199 }
1200
1201 return ret;
1202}
1203
1204static int process_acks(struct fetch_negotiator *negotiator,
1205 struct packet_reader *reader,
1206 struct oidset *common)
1207{
1208 /* received */
1209 int received_ready = 0;
1210 int received_ack = 0;
1211
1212 process_section_header(reader, "acknowledgments", 0);
1213 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1214 const char *arg;
1215
1216 if (!strcmp(reader->line, "NAK"))
1217 continue;
1218
1219 if (skip_prefix(reader->line, "ACK ", &arg)) {
1220 struct object_id oid;
1221 if (!get_oid_hex(arg, &oid)) {
1222 struct commit *commit;
1223 oidset_insert(common, &oid);
1224 commit = lookup_commit(the_repository, &oid);
1225 negotiator->ack(negotiator, commit);
1226 }
1227 continue;
1228 }
1229
1230 if (!strcmp(reader->line, "ready")) {
1231 received_ready = 1;
1232 continue;
1233 }
1234
1235 die(_("unexpected acknowledgment line: '%s'"), reader->line);
1236 }
1237
1238 if (reader->status != PACKET_READ_FLUSH &&
1239 reader->status != PACKET_READ_DELIM)
1240 die(_("error processing acks: %d"), reader->status);
1241
1242 /* return 0 if no common, 1 if there are common, or 2 if ready */
1243 return received_ready ? 2 : (received_ack ? 1 : 0);
1244}
1245
1246static void receive_shallow_info(struct fetch_pack_args *args,
1247 struct packet_reader *reader)
1248{
1249 process_section_header(reader, "shallow-info", 0);
1250 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1251 const char *arg;
1252 struct object_id oid;
1253
1254 if (skip_prefix(reader->line, "shallow ", &arg)) {
1255 if (get_oid_hex(arg, &oid))
1256 die(_("invalid shallow line: %s"), reader->line);
1257 register_shallow(the_repository, &oid);
1258 continue;
1259 }
1260 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1261 if (get_oid_hex(arg, &oid))
1262 die(_("invalid unshallow line: %s"), reader->line);
1263 if (!lookup_object(the_repository, oid.hash))
1264 die(_("object not found: %s"), reader->line);
1265 /* make sure that it is parsed as shallow */
1266 if (!parse_object(the_repository, &oid))
1267 die(_("error in object: %s"), reader->line);
1268 if (unregister_shallow(&oid))
1269 die(_("no shallow found: %s"), reader->line);
1270 continue;
1271 }
1272 die(_("expected shallow/unshallow, got %s"), reader->line);
1273 }
1274
1275 if (reader->status != PACKET_READ_FLUSH &&
1276 reader->status != PACKET_READ_DELIM)
1277 die(_("error processing shallow info: %d"), reader->status);
1278
1279 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1280 args->deepen = 1;
1281}
1282
1283static void receive_wanted_refs(struct packet_reader *reader,
1284 struct ref **sought, int nr_sought)
1285{
1286 process_section_header(reader, "wanted-refs", 0);
1287 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1288 struct object_id oid;
1289 const char *end;
1290 int i;
1291
1292 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1293 die(_("expected wanted-ref, got '%s'"), reader->line);
1294
1295 for (i = 0; i < nr_sought; i++) {
1296 if (!strcmp(end, sought[i]->name)) {
1297 oidcpy(&sought[i]->old_oid, &oid);
1298 break;
1299 }
1300 }
1301
1302 if (i == nr_sought)
1303 die(_("unexpected wanted-ref: '%s'"), reader->line);
1304 }
1305
1306 if (reader->status != PACKET_READ_DELIM)
1307 die(_("error processing wanted refs: %d"), reader->status);
1308}
1309
1310enum fetch_state {
1311 FETCH_CHECK_LOCAL = 0,
1312 FETCH_SEND_REQUEST,
1313 FETCH_PROCESS_ACKS,
1314 FETCH_GET_PACK,
1315 FETCH_DONE,
1316};
1317
1318static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1319 int fd[2],
1320 const struct ref *orig_ref,
1321 struct ref **sought, int nr_sought,
1322 char **pack_lockfile)
1323{
1324 struct ref *ref = copy_ref_list(orig_ref);
1325 enum fetch_state state = FETCH_CHECK_LOCAL;
1326 struct oidset common = OIDSET_INIT;
1327 struct packet_reader reader;
1328 int in_vain = 0;
1329 int haves_to_send = INITIAL_FLUSH;
1330 struct fetch_negotiator negotiator;
1331 fetch_negotiator_init(&negotiator, negotiation_algorithm);
1332 packet_reader_init(&reader, fd[0], NULL, 0,
1333 PACKET_READ_CHOMP_NEWLINE);
1334
1335 while (state != FETCH_DONE) {
1336 switch (state) {
1337 case FETCH_CHECK_LOCAL:
1338 sort_ref_list(&ref, ref_compare_name);
1339 QSORT(sought, nr_sought, cmp_ref_by_name);
1340
1341 /* v2 supports these by default */
1342 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1343 use_sideband = 2;
1344 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1345 args->deepen = 1;
1346
1347 /* Filter 'ref' by 'sought' and those that aren't local */
1348 mark_complete_and_common_ref(&negotiator, args, &ref);
1349 filter_refs(args, &ref, sought, nr_sought);
1350 if (everything_local(args, &ref))
1351 state = FETCH_DONE;
1352 else
1353 state = FETCH_SEND_REQUEST;
1354
1355 mark_tips(&negotiator, args->negotiation_tips);
1356 for_each_cached_alternate(&negotiator,
1357 insert_one_alternate_object);
1358 break;
1359 case FETCH_SEND_REQUEST:
1360 if (send_fetch_request(&negotiator, fd[1], args, ref,
1361 &common,
1362 &haves_to_send, &in_vain))
1363 state = FETCH_GET_PACK;
1364 else
1365 state = FETCH_PROCESS_ACKS;
1366 break;
1367 case FETCH_PROCESS_ACKS:
1368 /* Process ACKs/NAKs */
1369 switch (process_acks(&negotiator, &reader, &common)) {
1370 case 2:
1371 state = FETCH_GET_PACK;
1372 break;
1373 case 1:
1374 in_vain = 0;
1375 /* fallthrough */
1376 default:
1377 state = FETCH_SEND_REQUEST;
1378 break;
1379 }
1380 break;
1381 case FETCH_GET_PACK:
1382 /* Check for shallow-info section */
1383 if (process_section_header(&reader, "shallow-info", 1))
1384 receive_shallow_info(args, &reader);
1385
1386 if (process_section_header(&reader, "wanted-refs", 1))
1387 receive_wanted_refs(&reader, sought, nr_sought);
1388
1389 /* get the pack */
1390 process_section_header(&reader, "packfile", 0);
1391 if (get_pack(args, fd, pack_lockfile))
1392 die(_("git fetch-pack: fetch failed."));
1393
1394 state = FETCH_DONE;
1395 break;
1396 case FETCH_DONE:
1397 continue;
1398 }
1399 }
1400
1401 negotiator.release(&negotiator);
1402 oidset_clear(&common);
1403 return ref;
1404}
1405
1406static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1407{
1408 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1409 const char *path;
1410
1411 if (git_config_pathname(&path, var, value))
1412 return 1;
1413 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1414 fsck_msg_types.len ? ',' : '=', path);
1415 free((char *)path);
1416 return 0;
1417 }
1418
1419 if (skip_prefix(var, "fetch.fsck.", &var)) {
1420 if (is_valid_msg_type(var, value))
1421 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1422 fsck_msg_types.len ? ',' : '=', var, value);
1423 else
1424 warning("Skipping unknown msg id '%s'", var);
1425 return 0;
1426 }
1427
1428 return git_default_config(var, value, cb);
1429}
1430
1431static void fetch_pack_config(void)
1432{
1433 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1434 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1435 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1436 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1437 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1438 git_config_get_string("fetch.negotiationalgorithm",
1439 &negotiation_algorithm);
1440
1441 git_config(fetch_pack_config_cb, NULL);
1442}
1443
1444static void fetch_pack_setup(void)
1445{
1446 static int did_setup;
1447 if (did_setup)
1448 return;
1449 fetch_pack_config();
1450 if (0 <= transfer_unpack_limit)
1451 unpack_limit = transfer_unpack_limit;
1452 else if (0 <= fetch_unpack_limit)
1453 unpack_limit = fetch_unpack_limit;
1454 did_setup = 1;
1455}
1456
1457static int remove_duplicates_in_refs(struct ref **ref, int nr)
1458{
1459 struct string_list names = STRING_LIST_INIT_NODUP;
1460 int src, dst;
1461
1462 for (src = dst = 0; src < nr; src++) {
1463 struct string_list_item *item;
1464 item = string_list_insert(&names, ref[src]->name);
1465 if (item->util)
1466 continue; /* already have it */
1467 item->util = ref[src];
1468 if (src != dst)
1469 ref[dst] = ref[src];
1470 dst++;
1471 }
1472 for (src = dst; src < nr; src++)
1473 ref[src] = NULL;
1474 string_list_clear(&names, 0);
1475 return dst;
1476}
1477
1478static void update_shallow(struct fetch_pack_args *args,
1479 struct ref **sought, int nr_sought,
1480 struct shallow_info *si)
1481{
1482 struct oid_array ref = OID_ARRAY_INIT;
1483 int *status;
1484 int i;
1485
1486 if (args->deepen && alternate_shallow_file) {
1487 if (*alternate_shallow_file == '\0') { /* --unshallow */
1488 unlink_or_warn(git_path_shallow(the_repository));
1489 rollback_lock_file(&shallow_lock);
1490 } else
1491 commit_lock_file(&shallow_lock);
1492 return;
1493 }
1494
1495 if (!si->shallow || !si->shallow->nr)
1496 return;
1497
1498 if (args->cloning) {
1499 /*
1500 * remote is shallow, but this is a clone, there are
1501 * no objects in repo to worry about. Accept any
1502 * shallow points that exist in the pack (iow in repo
1503 * after get_pack() and reprepare_packed_git())
1504 */
1505 struct oid_array extra = OID_ARRAY_INIT;
1506 struct object_id *oid = si->shallow->oid;
1507 for (i = 0; i < si->shallow->nr; i++)
1508 if (has_object_file(&oid[i]))
1509 oid_array_append(&extra, &oid[i]);
1510 if (extra.nr) {
1511 setup_alternate_shallow(&shallow_lock,
1512 &alternate_shallow_file,
1513 &extra);
1514 commit_lock_file(&shallow_lock);
1515 }
1516 oid_array_clear(&extra);
1517 return;
1518 }
1519
1520 if (!si->nr_ours && !si->nr_theirs)
1521 return;
1522
1523 remove_nonexistent_theirs_shallow(si);
1524 if (!si->nr_ours && !si->nr_theirs)
1525 return;
1526 for (i = 0; i < nr_sought; i++)
1527 oid_array_append(&ref, &sought[i]->old_oid);
1528 si->ref = &ref;
1529
1530 if (args->update_shallow) {
1531 /*
1532 * remote is also shallow, .git/shallow may be updated
1533 * so all refs can be accepted. Make sure we only add
1534 * shallow roots that are actually reachable from new
1535 * refs.
1536 */
1537 struct oid_array extra = OID_ARRAY_INIT;
1538 struct object_id *oid = si->shallow->oid;
1539 assign_shallow_commits_to_refs(si, NULL, NULL);
1540 if (!si->nr_ours && !si->nr_theirs) {
1541 oid_array_clear(&ref);
1542 return;
1543 }
1544 for (i = 0; i < si->nr_ours; i++)
1545 oid_array_append(&extra, &oid[si->ours[i]]);
1546 for (i = 0; i < si->nr_theirs; i++)
1547 oid_array_append(&extra, &oid[si->theirs[i]]);
1548 setup_alternate_shallow(&shallow_lock,
1549 &alternate_shallow_file,
1550 &extra);
1551 commit_lock_file(&shallow_lock);
1552 oid_array_clear(&extra);
1553 oid_array_clear(&ref);
1554 return;
1555 }
1556
1557 /*
1558 * remote is also shallow, check what ref is safe to update
1559 * without updating .git/shallow
1560 */
1561 status = xcalloc(nr_sought, sizeof(*status));
1562 assign_shallow_commits_to_refs(si, NULL, status);
1563 if (si->nr_ours || si->nr_theirs) {
1564 for (i = 0; i < nr_sought; i++)
1565 if (status[i])
1566 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1567 }
1568 free(status);
1569 oid_array_clear(&ref);
1570}
1571
1572static int iterate_ref_map(void *cb_data, struct object_id *oid)
1573{
1574 struct ref **rm = cb_data;
1575 struct ref *ref = *rm;
1576
1577 if (!ref)
1578 return -1; /* end of the list */
1579 *rm = ref->next;
1580 oidcpy(oid, &ref->old_oid);
1581 return 0;
1582}
1583
1584struct ref *fetch_pack(struct fetch_pack_args *args,
1585 int fd[], struct child_process *conn,
1586 const struct ref *ref,
1587 const char *dest,
1588 struct ref **sought, int nr_sought,
1589 struct oid_array *shallow,
1590 char **pack_lockfile,
1591 enum protocol_version version)
1592{
1593 struct ref *ref_cpy;
1594 struct shallow_info si;
1595
1596 fetch_pack_setup();
1597 if (nr_sought)
1598 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1599
1600 if (!ref) {
1601 packet_flush(fd[1]);
1602 die(_("no matching remote head"));
1603 }
1604 prepare_shallow_info(&si, shallow);
1605 if (version == protocol_v2)
1606 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1607 pack_lockfile);
1608 else
1609 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1610 &si, pack_lockfile);
1611 reprepare_packed_git(the_repository);
1612
1613 if (!args->cloning && args->deepen) {
1614 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1615 struct ref *iterator = ref_cpy;
1616 opt.shallow_file = alternate_shallow_file;
1617 if (args->deepen)
1618 opt.is_deepening_fetch = 1;
1619 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1620 error(_("remote did not send all necessary objects"));
1621 free_refs(ref_cpy);
1622 ref_cpy = NULL;
1623 rollback_lock_file(&shallow_lock);
1624 goto cleanup;
1625 }
1626 args->connectivity_checked = 1;
1627 }
1628
1629 update_shallow(args, sought, nr_sought, &si);
1630cleanup:
1631 clear_shallow_info(&si);
1632 return ref_cpy;
1633}
1634
1635int report_unmatched_refs(struct ref **sought, int nr_sought)
1636{
1637 int i, ret = 0;
1638
1639 for (i = 0; i < nr_sought; i++) {
1640 if (!sought[i])
1641 continue;
1642 switch (sought[i]->match_status) {
1643 case REF_MATCHED:
1644 continue;
1645 case REF_NOT_MATCHED:
1646 error(_("no such remote ref %s"), sought[i]->name);
1647 break;
1648 case REF_UNADVERTISED_NOT_ALLOWED:
1649 error(_("Server does not allow request for unadvertised object %s"),
1650 sought[i]->name);
1651 break;
1652 }
1653 ret = 1;
1654 }
1655 return ret;
1656}