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