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