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