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