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