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