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