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