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