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