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