]> git.ipfire.org Git - thirdparty/git.git/blame - fetch-pack.c
fetch-pack: add a deref_without_lazy_fetch_extended()
[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
ece9aea2 850/*
b664e9ff
JT
851 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
852 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
853 * stored there. (It must be freed by the caller.)
ece9aea2 854 */
745f7a8c 855static int get_pack(struct fetch_pack_args *args,
9da69a65 856 int xd[2], struct string_list *pack_lockfiles,
b664e9ff 857 struct strvec *index_pack_args,
5476e1ef
JT
858 struct ref **sought, int nr_sought,
859 struct oidset *gitmodules_oids)
745f7a8c
NTND
860{
861 struct async demux;
745f7a8c 862 int do_keep = args->keep_pack;
984a43b9
JK
863 const char *cmd_name;
864 struct pack_header header;
865 int pass_header = 0;
d3180279 866 struct child_process cmd = CHILD_PROCESS_INIT;
5476e1ef 867 int fsck_objects = 0;
c6807a40 868 int ret;
745f7a8c
NTND
869
870 memset(&demux, 0, sizeof(demux));
871 if (use_sideband) {
872 /* xd[] is talking with upload-pack; subprocess reads from
873 * xd[0], spits out band#2 to stderr, and feeds us band#1
874 * through demux->out.
875 */
876 demux.proc = sideband_demux;
877 demux.data = xd;
878 demux.out = -1;
df857572 879 demux.isolate_sigpipe = 1;
745f7a8c 880 if (start_async(&demux))
1dd73e20 881 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
745f7a8c
NTND
882 }
883 else
884 demux.out = xd[0];
885
2aec3bc4 886 if (!args->keep_pack && unpack_limit && !index_pack_args) {
745f7a8c
NTND
887
888 if (read_pack_header(demux.out, &header))
1dd73e20 889 die(_("protocol error: bad pack header"));
984a43b9 890 pass_header = 1;
745f7a8c
NTND
891 if (ntohl(header.hdr_entries) < unpack_limit)
892 do_keep = 0;
893 else
894 do_keep = 1;
895 }
896
6035d6aa 897 if (alternate_shallow_file) {
ef8d7ac4
JK
898 strvec_push(&cmd.args, "--shallow-file");
899 strvec_push(&cmd.args, alternate_shallow_file);
6035d6aa
NTND
900 }
901
5476e1ef
JT
902 if (fetch_fsck_objects >= 0
903 ? fetch_fsck_objects
904 : transfer_fsck_objects >= 0
905 ? transfer_fsck_objects
906 : 0)
907 fsck_objects = 1;
908
909 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
910 if (pack_lockfiles || fsck_objects)
745f7a8c 911 cmd.out = -1;
984a43b9 912 cmd_name = "index-pack";
ef8d7ac4
JK
913 strvec_push(&cmd.args, cmd_name);
914 strvec_push(&cmd.args, "--stdin");
745f7a8c 915 if (!args->quiet && !args->no_progress)
ef8d7ac4 916 strvec_push(&cmd.args, "-v");
745f7a8c 917 if (args->use_thin_pack)
ef8d7ac4 918 strvec_push(&cmd.args, "--fix-thin");
2aec3bc4 919 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) {
da25bdb7 920 char hostname[HOST_NAME_MAX + 1];
5781a9a2 921 if (xgethostname(hostname, sizeof(hostname)))
984a43b9 922 xsnprintf(hostname, sizeof(hostname), "localhost");
ef8d7ac4 923 strvec_pushf(&cmd.args,
f6d8942b
JK
924 "--keep=fetch-pack %"PRIuMAX " on %s",
925 (uintmax_t)getpid(), hostname);
745f7a8c 926 }
b664e9ff 927 if (!index_pack_args && args->check_self_contained_and_connected)
ef8d7ac4 928 strvec_push(&cmd.args, "--check-self-contained-and-connected");
dd4b732d
JT
929 else
930 /*
931 * We cannot perform any connectivity checks because
932 * not all packs have been downloaded; let the caller
933 * have this responsibility.
934 */
935 args->check_self_contained_and_connected = 0;
1b03df5f
JT
936
937 if (args->from_promisor)
938 /*
9d7fa3be 939 * create_promisor_file() may be called afterwards but
1b03df5f
JT
940 * we still need index-pack to know that this is a
941 * promisor pack. For example, if transfer.fsckobjects
942 * is true, index-pack needs to know that .gitmodules
943 * is a promisor object (so that it won't complain if
944 * it is missing).
945 */
ef8d7ac4 946 strvec_push(&cmd.args, "--promisor");
745f7a8c
NTND
947 }
948 else {
984a43b9 949 cmd_name = "unpack-objects";
ef8d7ac4 950 strvec_push(&cmd.args, cmd_name);
745f7a8c 951 if (args->quiet || args->no_progress)
ef8d7ac4 952 strvec_push(&cmd.args, "-q");
c6807a40 953 args->check_self_contained_and_connected = 0;
745f7a8c 954 }
984a43b9
JK
955
956 if (pass_header)
ef8d7ac4 957 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
f6d8942b 958 ntohl(header.hdr_version),
984a43b9 959 ntohl(header.hdr_entries));
5476e1ef 960 if (fsck_objects) {
b664e9ff 961 if (args->from_promisor || index_pack_args)
98a2ea46
JT
962 /*
963 * We cannot use --strict in index-pack because it
964 * checks both broken objects and links, but we only
965 * want to check for broken objects.
966 */
ef8d7ac4 967 strvec_push(&cmd.args, "--fsck-objects");
98a2ea46 968 else
ef8d7ac4 969 strvec_pushf(&cmd.args, "--strict%s",
f6d8942b 970 fsck_msg_types.buf);
98a2ea46 971 }
745f7a8c 972
b664e9ff
JT
973 if (index_pack_args) {
974 int i;
975
976 for (i = 0; i < cmd.args.nr; i++)
977 strvec_push(index_pack_args, cmd.args.v[i]);
978 }
979
2a4aed42
JK
980 sigchain_push(SIGPIPE, SIG_IGN);
981
745f7a8c
NTND
982 cmd.in = demux.out;
983 cmd.git_cmd = 1;
984 if (start_command(&cmd))
1dd73e20 985 die(_("fetch-pack: unable to fork off %s"), cmd_name);
5476e1ef
JT
986 if (do_keep && (pack_lockfiles || fsck_objects)) {
987 int is_well_formed;
988 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
989
990 if (!is_well_formed)
991 die(_("fetch-pack: invalid index-pack output"));
6031af38
RS
992 if (pack_lockfile)
993 string_list_append_nodup(pack_lockfiles, pack_lockfile);
5476e1ef 994 parse_gitmodules_oids(cmd.out, gitmodules_oids);
745f7a8c
NTND
995 close(cmd.out);
996 }
997
37cb1dd6
JL
998 if (!use_sideband)
999 /* Closed by start_command() */
1000 xd[0] = -1;
1001
c6807a40
NTND
1002 ret = finish_command(&cmd);
1003 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1004 args->self_contained_and_connected =
1005 args->check_self_contained_and_connected &&
1006 ret == 0;
1007 else
1dd73e20 1008 die(_("%s failed"), cmd_name);
745f7a8c 1009 if (use_sideband && finish_async(&demux))
1dd73e20 1010 die(_("error in sideband demultiplexer"));
5374a290 1011
2a4aed42
JK
1012 sigchain_pop(SIGPIPE);
1013
5374a290
JT
1014 /*
1015 * Now that index-pack has succeeded, write the promisor file using the
1016 * obtained .keep filename if necessary
1017 */
9da69a65 1018 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
9d7fa3be 1019 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
5374a290 1020
745f7a8c
NTND
1021 return 0;
1022}
1023
f2db854d
JH
1024static int cmp_ref_by_name(const void *a_, const void *b_)
1025{
1026 const struct ref *a = *((const struct ref **)a_);
1027 const struct ref *b = *((const struct ref **)b_);
1028 return strcmp(a->name, b->name);
1029}
1030
745f7a8c
NTND
1031static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1032 int fd[2],
1033 const struct ref *orig_ref,
f2db854d 1034 struct ref **sought, int nr_sought,
beea4152 1035 struct shallow_info *si,
9da69a65 1036 struct string_list *pack_lockfiles)
745f7a8c 1037{
aaf633c2 1038 struct repository *r = the_repository;
745f7a8c 1039 struct ref *ref = copy_ref_list(orig_ref);
1b283377 1040 struct object_id oid;
745f7a8c
NTND
1041 const char *agent_feature;
1042 int agent_len;
603960b5
JT
1043 struct fetch_negotiator negotiator_alloc;
1044 struct fetch_negotiator *negotiator;
1045
9dfa8dbe 1046 negotiator = &negotiator_alloc;
4dfd0925
RC
1047 if (args->refetch) {
1048 fetch_negotiator_init_noop(negotiator);
1049 } else {
1050 fetch_negotiator_init(r, negotiator);
1051 }
745f7a8c
NTND
1052
1053 sort_ref_list(&ref, ref_compare_name);
9ed0d8d6 1054 QSORT(sought, nr_sought, cmp_ref_by_name);
745f7a8c 1055
0e042971
NTND
1056 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1057 agent_supported = 1;
1058 if (agent_len)
1059 print_verbose(args, _("Server version is %.*s"),
1060 agent_len, agent_feature);
1061 }
1062
1e905bbc
JS
1063 if (!server_supports("session-id"))
1064 advertise_sid = 0;
1065
5a88583b
NTND
1066 if (server_supports("shallow"))
1067 print_verbose(args, _("Server supports %s"), "shallow");
aaf633c2 1068 else if (args->depth > 0 || is_repository_shallow(r))
1dd73e20 1069 die(_("Server does not support shallow clients"));
a45a2600 1070 if (args->depth > 0 || args->deepen_since || args->deepen_not)
79891cb9 1071 args->deepen = 1;
745f7a8c 1072 if (server_supports("multi_ack_detailed")) {
0778b293 1073 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
745f7a8c
NTND
1074 multi_ack = 2;
1075 if (server_supports("no-done")) {
0778b293 1076 print_verbose(args, _("Server supports %s"), "no-done");
745f7a8c
NTND
1077 if (args->stateless_rpc)
1078 no_done = 1;
1079 }
1080 }
1081 else if (server_supports("multi_ack")) {
0778b293 1082 print_verbose(args, _("Server supports %s"), "multi_ack");
745f7a8c
NTND
1083 multi_ack = 1;
1084 }
1085 if (server_supports("side-band-64k")) {
0778b293 1086 print_verbose(args, _("Server supports %s"), "side-band-64k");
745f7a8c
NTND
1087 use_sideband = 2;
1088 }
1089 else if (server_supports("side-band")) {
0778b293 1090 print_verbose(args, _("Server supports %s"), "side-band");
745f7a8c
NTND
1091 use_sideband = 1;
1092 }
6e7b66ee 1093 if (server_supports("allow-tip-sha1-in-want")) {
0778b293 1094 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
7199c093 1095 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
6e7b66ee 1096 }
68ee6289 1097 if (server_supports("allow-reachable-sha1-in-want")) {
0778b293 1098 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
68ee6289
FM
1099 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1100 }
5a88583b
NTND
1101 if (server_supports("thin-pack"))
1102 print_verbose(args, _("Server supports %s"), "thin-pack");
1103 else
745f7a8c 1104 args->use_thin_pack = 0;
5a88583b
NTND
1105 if (server_supports("no-progress"))
1106 print_verbose(args, _("Server supports %s"), "no-progress");
1107 else
745f7a8c 1108 args->no_progress = 0;
5a88583b
NTND
1109 if (server_supports("include-tag"))
1110 print_verbose(args, _("Server supports %s"), "include-tag");
1111 else
745f7a8c 1112 args->include_tag = 0;
0d789a5b 1113 if (server_supports("ofs-delta"))
0778b293 1114 print_verbose(args, _("Server supports %s"), "ofs-delta");
0d789a5b 1115 else
745f7a8c
NTND
1116 prefer_ofs_delta = 0;
1117
640d8b72
JH
1118 if (server_supports("filter")) {
1119 server_supports_filtering = 1;
0778b293 1120 print_verbose(args, _("Server supports %s"), "filter");
640d8b72
JH
1121 } else if (args->filter_options.choice) {
1122 warning("filtering not recognized by server, ignoring");
1123 }
1124
5a88583b
NTND
1125 if (server_supports("deepen-since")) {
1126 print_verbose(args, _("Server supports %s"), "deepen-since");
508ea882 1127 deepen_since_ok = 1;
5a88583b 1128 } else if (args->deepen_since)
508ea882 1129 die(_("Server does not support --shallow-since"));
5a88583b
NTND
1130 if (server_supports("deepen-not")) {
1131 print_verbose(args, _("Server supports %s"), "deepen-not");
a45a2600 1132 deepen_not_ok = 1;
5a88583b 1133 } else if (args->deepen_not)
a45a2600 1134 die(_("Server does not support --shallow-exclude"));
5a88583b
NTND
1135 if (server_supports("deepen-relative"))
1136 print_verbose(args, _("Server supports %s"), "deepen-relative");
1137 else if (args->deepen_relative)
cccf74e2 1138 die(_("Server does not support --deepen"));
48bf1415 1139 if (!server_supports_hash(the_hash_algo->name, NULL))
1140 die(_("Server does not support this repository's object format"));
745f7a8c 1141
9dfa8dbe
JT
1142 mark_complete_and_common_ref(negotiator, args, &ref);
1143 filter_refs(args, &ref, sought, nr_sought);
4dfd0925 1144 if (!args->refetch && everything_local(args, &ref)) {
9dfa8dbe
JT
1145 packet_flush(fd[1]);
1146 goto all_done;
745f7a8c 1147 }
603960b5 1148 if (find_common(negotiator, args, fd, &oid, ref) < 0)
745f7a8c
NTND
1149 if (!args->keep_pack)
1150 /* When cloning, it is not unusual to have
1151 * no common commit.
1152 */
1dd73e20 1153 warning(_("no common commits"));
745f7a8c
NTND
1154
1155 if (args->stateless_rpc)
1156 packet_flush(fd[1]);
79891cb9 1157 if (args->deepen)
1a30f5a2
NTND
1158 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1159 NULL);
4fe788b1
LL
1160 else if (si->nr_ours || si->nr_theirs) {
1161 if (args->reject_shallow_remote)
1162 die(_("source repository is shallow, reject to clone."));
beea4152 1163 alternate_shallow_file = setup_temporary_shallow(si->shallow);
4fe788b1 1164 } else
6da8bdcb 1165 alternate_shallow_file = NULL;
5476e1ef 1166 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
3745e269 1167 &fsck_options.gitmodules_found))
1dd73e20 1168 die(_("git fetch-pack: fetch failed."));
3745e269
ÆAB
1169 if (fsck_finish(&fsck_options))
1170 die("fsck failed");
745f7a8c
NTND
1171
1172 all_done:
603960b5
JT
1173 if (negotiator)
1174 negotiator->release(negotiator);
745f7a8c
NTND
1175 return ref;
1176}
1177
f7e20501
BW
1178static void add_shallow_requests(struct strbuf *req_buf,
1179 const struct fetch_pack_args *args)
1180{
00624d60 1181 if (is_repository_shallow(the_repository))
f7e20501
BW
1182 write_shallow_commits(req_buf, 1, NULL);
1183 if (args->depth > 0)
1184 packet_buf_write(req_buf, "deepen %d", args->depth);
1185 if (args->deepen_since) {
1186 timestamp_t max_age = approxidate(args->deepen_since);
1187 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1188 }
1189 if (args->deepen_not) {
1190 int i;
1191 for (i = 0; i < args->deepen_not->nr; i++) {
1192 struct string_list_item *s = args->deepen_not->items + i;
1193 packet_buf_write(req_buf, "deepen-not %s", s->string);
1194 }
1195 }
5056cf4a
JT
1196 if (args->deepen_relative)
1197 packet_buf_write(req_buf, "deepen-relative\n");
f7e20501
BW
1198}
1199
9dfa8dbe 1200static void add_wants(const struct ref *wants, struct strbuf *req_buf)
685fbd32 1201{
73302051
BW
1202 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1203
685fbd32
BW
1204 for ( ; wants ; wants = wants->next) {
1205 const struct object_id *remote = &wants->old_oid;
685fbd32
BW
1206 struct object *o;
1207
1208 /*
1209 * If that object is complete (i.e. it is an ancestor of a
1210 * local ref), we tell them we have it but do not have to
1211 * tell them about its ancestors, which they already know
1212 * about.
1213 *
1214 * We use lookup_object here because we are only
1215 * interested in the case we *know* the object is
1216 * reachable and we have already scanned it.
1217 */
9dfa8dbe 1218 if (((o = lookup_object(the_repository, remote)) != NULL) &&
685fbd32
BW
1219 (o->flags & COMPLETE)) {
1220 continue;
1221 }
1222
73302051
BW
1223 if (!use_ref_in_want || wants->exact_oid)
1224 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1225 else
1226 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
685fbd32
BW
1227 }
1228}
1229
1230static void add_common(struct strbuf *req_buf, struct oidset *common)
1231{
1232 struct oidset_iter iter;
1233 const struct object_id *oid;
1234 oidset_iter_init(common, &iter);
1235
1236 while ((oid = oidset_iter_next(&iter))) {
1237 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1238 }
1239}
1240
ec062838
JT
1241static int add_haves(struct fetch_negotiator *negotiator,
1242 struct strbuf *req_buf,
57c3451b 1243 int *haves_to_send)
685fbd32 1244{
685fbd32
BW
1245 int haves_added = 0;
1246 const struct object_id *oid;
1247
ec062838 1248 while ((oid = negotiator->next(negotiator))) {
685fbd32
BW
1249 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1250 if (++haves_added >= *haves_to_send)
1251 break;
1252 }
1253
685fbd32
BW
1254 /* Increase haves to send on next round */
1255 *haves_to_send = next_flush(1, *haves_to_send);
1256
57c3451b 1257 return haves_added;
685fbd32
BW
1258}
1259
6871d0ce
JT
1260static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1261 const struct string_list *server_options)
685fbd32 1262{
4b831208 1263 const char *hash_name;
685fbd32
BW
1264
1265 if (server_supports_v2("fetch", 1))
6871d0ce 1266 packet_buf_write(req_buf, "command=fetch");
685fbd32 1267 if (server_supports_v2("agent", 0))
6871d0ce 1268 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1e905bbc 1269 if (advertise_sid && server_supports_v2("session-id", 0))
6871d0ce
JT
1270 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1271 if (server_options && server_options->nr &&
5e3548ef
BW
1272 server_supports_v2("server-option", 1)) {
1273 int i;
6871d0ce
JT
1274 for (i = 0; i < server_options->nr; i++)
1275 packet_buf_write(req_buf, "server-option=%s",
1276 server_options->items[i].string);
5e3548ef 1277 }
685fbd32 1278
4b831208 1279 if (server_feature_v2("object-format", &hash_name)) {
1280 int hash_algo = hash_algo_by_name(hash_name);
1281 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1282 die(_("mismatched algorithms: client %s; server %s"),
1283 the_hash_algo->name, hash_name);
6871d0ce 1284 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
4b831208 1285 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1286 die(_("the server does not support algorithm '%s'"),
1287 the_hash_algo->name);
1288 }
6871d0ce
JT
1289 packet_buf_delim(req_buf);
1290}
1291
1292static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1293 struct fetch_pack_args *args,
1294 const struct ref *wants, struct oidset *common,
1295 int *haves_to_send, int *in_vain,
1296 int sideband_all, int seen_ack)
1297{
1298 int haves_added;
1299 int done_sent = 0;
1300 struct strbuf req_buf = STRBUF_INIT;
1301
1302 write_fetch_command_and_capabilities(&req_buf, args->server_options);
4b831208 1303
685fbd32
BW
1304 if (args->use_thin_pack)
1305 packet_buf_write(&req_buf, "thin-pack");
1306 if (args->no_progress)
1307 packet_buf_write(&req_buf, "no-progress");
1308 if (args->include_tag)
1309 packet_buf_write(&req_buf, "include-tag");
1310 if (prefer_ofs_delta)
1311 packet_buf_write(&req_buf, "ofs-delta");
0bbc0bc5
JT
1312 if (sideband_all)
1313 packet_buf_write(&req_buf, "sideband-all");
685fbd32 1314
f7e20501
BW
1315 /* Add shallow-info and deepen request */
1316 if (server_supports_feature("fetch", "shallow", 0))
1317 add_shallow_requests(&req_buf, args);
00624d60 1318 else if (is_repository_shallow(the_repository) || args->deepen)
f7e20501
BW
1319 die(_("Server does not support shallow requests"));
1320
ba95710a
JT
1321 /* Add filter */
1322 if (server_supports_feature("fetch", "filter", 0) &&
1323 args->filter_options.choice) {
cf9ceb5a
MD
1324 const char *spec =
1325 expand_list_objects_filter_spec(&args->filter_options);
ba95710a 1326 print_verbose(args, _("Server supports filter"));
cf9ceb5a 1327 packet_buf_write(&req_buf, "filter %s", spec);
ba95710a
JT
1328 } else if (args->filter_options.choice) {
1329 warning("filtering not recognized by server, ignoring");
1330 }
1331
dd4b732d
JT
1332 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1333 int i;
1334 struct strbuf to_send = STRBUF_INIT;
1335
1336 for (i = 0; i < uri_protocols.nr; i++) {
1337 const char *s = uri_protocols.items[i].string;
1338
1339 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1340 if (to_send.len)
1341 strbuf_addch(&to_send, ',');
1342 strbuf_addstr(&to_send, s);
1343 }
1344 }
1345 if (to_send.len) {
1346 packet_buf_write(&req_buf, "packfile-uris %s",
1347 to_send.buf);
1348 strbuf_release(&to_send);
1349 }
1350 }
1351
685fbd32 1352 /* add wants */
9dfa8dbe 1353 add_wants(wants, &req_buf);
685fbd32 1354
9dfa8dbe
JT
1355 /* Add all of the common commits we've found in previous rounds */
1356 add_common(&req_buf, common);
685fbd32 1357
57c3451b
JT
1358 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1359 *in_vain += haves_added;
1360 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1361 /* Send Done */
1362 packet_buf_write(&req_buf, "done\n");
1363 done_sent = 1;
1364 }
685fbd32
BW
1365
1366 /* Send request */
1367 packet_buf_flush(&req_buf);
37c80012
JK
1368 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1369 die_errno(_("unable to write request to remote"));
685fbd32
BW
1370
1371 strbuf_release(&req_buf);
57c3451b 1372 return done_sent;
685fbd32
BW
1373}
1374
1375/*
1376 * Processes a section header in a server's response and checks if it matches
1377 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1378 * not consumed); if 0, the line will be consumed and the function will die if
1379 * the section header doesn't match what was expected.
1380 */
1381static int process_section_header(struct packet_reader *reader,
1382 const char *section, int peek)
1383{
1384 int ret;
1385
1386 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
bbb19a8b 1387 die(_("error reading section header '%s'"), section);
685fbd32
BW
1388
1389 ret = !strcmp(reader->line, section);
1390
1391 if (!peek) {
1392 if (!ret)
bbb19a8b 1393 die(_("expected '%s', received '%s'"),
685fbd32
BW
1394 section, reader->line);
1395 packet_reader_read(reader);
1396 }
1397
1398 return ret;
1399}
1400
81025703
JT
1401static int process_ack(struct fetch_negotiator *negotiator,
1402 struct packet_reader *reader,
1403 struct object_id *common_oid,
1404 int *received_ready)
685fbd32 1405{
685fbd32
BW
1406 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1407 const char *arg;
1408
1409 if (!strcmp(reader->line, "NAK"))
1410 continue;
1411
1412 if (skip_prefix(reader->line, "ACK ", &arg)) {
81025703 1413 if (!get_oid_hex(arg, common_oid)) {
685fbd32 1414 struct commit *commit;
81025703 1415 commit = lookup_commit(the_repository, common_oid);
603960b5
JT
1416 if (negotiator)
1417 negotiator->ack(negotiator, commit);
685fbd32 1418 }
81025703 1419 return 1;
685fbd32
BW
1420 }
1421
1422 if (!strcmp(reader->line, "ready")) {
81025703 1423 *received_ready = 1;
685fbd32
BW
1424 continue;
1425 }
1426
bbb19a8b 1427 die(_("unexpected acknowledgment line: '%s'"), reader->line);
685fbd32
BW
1428 }
1429
1430 if (reader->status != PACKET_READ_FLUSH &&
1431 reader->status != PACKET_READ_DELIM)
bbb19a8b 1432 die(_("error processing acks: %d"), reader->status);
685fbd32 1433
5400b2a2
JT
1434 /*
1435 * If an "acknowledgments" section is sent, a packfile is sent if and
1436 * only if "ready" was sent in this section. The other sections
1437 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1438 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1439 * otherwise.
1440 */
81025703 1441 if (*received_ready && reader->status != PACKET_READ_DELIM)
3d3c23b3
BS
1442 /*
1443 * TRANSLATORS: The parameter will be 'ready', a protocol
1444 * keyword.
1445 */
1446 die(_("expected packfile to be sent after '%s'"), "ready");
81025703 1447 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
3d3c23b3
BS
1448 /*
1449 * TRANSLATORS: The parameter will be 'ready', a protocol
1450 * keyword.
1451 */
1452 die(_("expected no other sections to be sent after no '%s'"), "ready");
5400b2a2 1453
81025703 1454 return 0;
685fbd32
BW
1455}
1456
f7e20501 1457static void receive_shallow_info(struct fetch_pack_args *args,
1339078f
JT
1458 struct packet_reader *reader,
1459 struct oid_array *shallows,
1460 struct shallow_info *si)
f7e20501 1461{
1339078f 1462 int unshallow_received = 0;
bd0b42ae 1463
f7e20501
BW
1464 process_section_header(reader, "shallow-info", 0);
1465 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1466 const char *arg;
1467 struct object_id oid;
1468
1469 if (skip_prefix(reader->line, "shallow ", &arg)) {
1470 if (get_oid_hex(arg, &oid))
1471 die(_("invalid shallow line: %s"), reader->line);
1339078f 1472 oid_array_append(shallows, &oid);
f7e20501
BW
1473 continue;
1474 }
1475 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1476 if (get_oid_hex(arg, &oid))
1477 die(_("invalid unshallow line: %s"), reader->line);
d0229abd 1478 if (!lookup_object(the_repository, &oid))
f7e20501
BW
1479 die(_("object not found: %s"), reader->line);
1480 /* make sure that it is parsed as shallow */
109cd76d 1481 if (!parse_object(the_repository, &oid))
f7e20501
BW
1482 die(_("error in object: %s"), reader->line);
1483 if (unregister_shallow(&oid))
1484 die(_("no shallow found: %s"), reader->line);
1339078f 1485 unshallow_received = 1;
f7e20501
BW
1486 continue;
1487 }
1488 die(_("expected shallow/unshallow, got %s"), reader->line);
1489 }
1490
1491 if (reader->status != PACKET_READ_FLUSH &&
1492 reader->status != PACKET_READ_DELIM)
bbb19a8b 1493 die(_("error processing shallow info: %d"), reader->status);
f7e20501 1494
1339078f
JT
1495 if (args->deepen || unshallow_received) {
1496 /*
1497 * Treat these as shallow lines caused by our depth settings.
1498 * In v0, these lines cannot cause refs to be rejected; do the
1499 * same.
1500 */
1501 int i;
1502
1503 for (i = 0; i < shallows->nr; i++)
1504 register_shallow(the_repository, &shallows->oid[i]);
bd0b42ae
JT
1505 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1506 NULL);
1507 args->deepen = 1;
1339078f
JT
1508 } else if (shallows->nr) {
1509 /*
1510 * Treat these as shallow lines caused by the remote being
1511 * shallow. In v0, remote refs that reach these objects are
1512 * rejected (unless --update-shallow is set); do the same.
1513 */
1514 prepare_shallow_info(si, shallows);
4fe788b1
LL
1515 if (si->nr_ours || si->nr_theirs) {
1516 if (args->reject_shallow_remote)
1517 die(_("source repository is shallow, reject to clone."));
1339078f
JT
1518 alternate_shallow_file =
1519 setup_temporary_shallow(si->shallow);
4fe788b1 1520 } else
1339078f 1521 alternate_shallow_file = NULL;
380ebab2 1522 } else {
1523 alternate_shallow_file = NULL;
bd0b42ae 1524 }
f7e20501
BW
1525}
1526
b7643009
JT
1527static int cmp_name_ref(const void *name, const void *ref)
1528{
1529 return strcmp(name, (*(struct ref **)ref)->name);
1530}
1531
e2842b39
JT
1532static void receive_wanted_refs(struct packet_reader *reader,
1533 struct ref **sought, int nr_sought)
73302051
BW
1534{
1535 process_section_header(reader, "wanted-refs", 0);
1536 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1537 struct object_id oid;
1538 const char *end;
b7643009 1539 struct ref **found;
73302051
BW
1540
1541 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
bbb19a8b 1542 die(_("expected wanted-ref, got '%s'"), reader->line);
73302051 1543
b7643009
JT
1544 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1545 cmp_name_ref);
1546 if (!found)
bbb19a8b 1547 die(_("unexpected wanted-ref: '%s'"), reader->line);
b7643009 1548 oidcpy(&(*found)->old_oid, &oid);
73302051
BW
1549 }
1550
1551 if (reader->status != PACKET_READ_DELIM)
bbb19a8b 1552 die(_("error processing wanted refs: %d"), reader->status);
73302051
BW
1553}
1554
dd4b732d
JT
1555static void receive_packfile_uris(struct packet_reader *reader,
1556 struct string_list *uris)
1557{
1558 process_section_header(reader, "packfile-uris", 0);
1559 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1560 if (reader->pktlen < the_hash_algo->hexsz ||
1561 reader->line[the_hash_algo->hexsz] != ' ')
1562 die("expected '<hash> <uri>', got: %s\n", reader->line);
1563
1564 string_list_append(uris, reader->line);
1565 }
1566 if (reader->status != PACKET_READ_DELIM)
1567 die("expected DELIM");
1568}
1569
685fbd32
BW
1570enum fetch_state {
1571 FETCH_CHECK_LOCAL = 0,
1572 FETCH_SEND_REQUEST,
1573 FETCH_PROCESS_ACKS,
1574 FETCH_GET_PACK,
1575 FETCH_DONE,
1576};
1577
9c1e657a 1578static void do_check_stateless_delimiter(int stateless_rpc,
b0df0c16
DL
1579 struct packet_reader *reader)
1580{
9c1e657a 1581 check_stateless_delimiter(stateless_rpc, reader,
b0df0c16
DL
1582 _("git fetch-pack: expected response end packet"));
1583}
1584
685fbd32
BW
1585static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1586 int fd[2],
1587 const struct ref *orig_ref,
1588 struct ref **sought, int nr_sought,
1339078f
JT
1589 struct oid_array *shallows,
1590 struct shallow_info *si,
9da69a65 1591 struct string_list *pack_lockfiles)
685fbd32 1592{
aaf633c2 1593 struct repository *r = the_repository;
685fbd32
BW
1594 struct ref *ref = copy_ref_list(orig_ref);
1595 enum fetch_state state = FETCH_CHECK_LOCAL;
1596 struct oidset common = OIDSET_INIT;
1597 struct packet_reader reader;
5fc31180 1598 int in_vain = 0, negotiation_started = 0;
685fbd32 1599 int haves_to_send = INITIAL_FLUSH;
603960b5
JT
1600 struct fetch_negotiator negotiator_alloc;
1601 struct fetch_negotiator *negotiator;
4fa3f00a 1602 int seen_ack = 0;
81025703
JT
1603 struct object_id common_oid;
1604 int received_ready = 0;
dd4b732d
JT
1605 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1606 int i;
b664e9ff 1607 struct strvec index_pack_args = STRVEC_INIT;
603960b5 1608
9dfa8dbe 1609 negotiator = &negotiator_alloc;
4dfd0925
RC
1610 if (args->refetch)
1611 fetch_negotiator_init_noop(negotiator);
1612 else
1613 fetch_negotiator_init(r, negotiator);
603960b5 1614
685fbd32 1615 packet_reader_init(&reader, fd[0], NULL, 0,
2d103c31
MS
1616 PACKET_READ_CHOMP_NEWLINE |
1617 PACKET_READ_DIE_ON_ERR_PACKET);
07c3c2aa
JT
1618 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1619 server_supports_feature("fetch", "sideband-all", 0)) {
0bbc0bc5
JT
1620 reader.use_sideband = 1;
1621 reader.me = "fetch-pack";
1622 }
685fbd32
BW
1623
1624 while (state != FETCH_DONE) {
1625 switch (state) {
1626 case FETCH_CHECK_LOCAL:
1627 sort_ref_list(&ref, ref_compare_name);
1628 QSORT(sought, nr_sought, cmp_ref_by_name);
1629
1630 /* v2 supports these by default */
1631 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1632 use_sideband = 2;
f7e20501
BW
1633 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1634 args->deepen = 1;
685fbd32 1635
685fbd32 1636 /* Filter 'ref' by 'sought' and those that aren't local */
9dfa8dbe
JT
1637 mark_complete_and_common_ref(negotiator, args, &ref);
1638 filter_refs(args, &ref, sought, nr_sought);
4dfd0925 1639 if (!args->refetch && everything_local(args, &ref))
9dfa8dbe
JT
1640 state = FETCH_DONE;
1641 else
685fbd32 1642 state = FETCH_SEND_REQUEST;
9dfa8dbe
JT
1643
1644 mark_tips(negotiator, args->negotiation_tips);
1645 for_each_cached_alternate(negotiator,
1646 insert_one_alternate_object);
685fbd32
BW
1647 break;
1648 case FETCH_SEND_REQUEST:
5fc31180
JS
1649 if (!negotiation_started) {
1650 negotiation_started = 1;
1651 trace2_region_enter("fetch-pack",
1652 "negotiation_v2",
1653 the_repository);
1654 }
603960b5 1655 if (send_fetch_request(negotiator, fd[1], args, ref,
ec062838 1656 &common,
0bbc0bc5 1657 &haves_to_send, &in_vain,
4fa3f00a
JT
1658 reader.use_sideband,
1659 seen_ack))
685fbd32
BW
1660 state = FETCH_GET_PACK;
1661 else
1662 state = FETCH_PROCESS_ACKS;
1663 break;
1664 case FETCH_PROCESS_ACKS:
1665 /* Process ACKs/NAKs */
81025703
JT
1666 process_section_header(&reader, "acknowledgments", 0);
1667 while (process_ack(negotiator, &reader, &common_oid,
1668 &received_ready)) {
1669 in_vain = 0;
1670 seen_ack = 1;
1671 oidset_insert(&common, &common_oid);
1672 }
1673 if (received_ready) {
b0df0c16
DL
1674 /*
1675 * Don't check for response delimiter; get_pack() will
1676 * read the rest of this response.
1677 */
685fbd32 1678 state = FETCH_GET_PACK;
81025703 1679 } else {
9c1e657a 1680 do_check_stateless_delimiter(args->stateless_rpc, &reader);
685fbd32 1681 state = FETCH_SEND_REQUEST;
685fbd32
BW
1682 }
1683 break;
1684 case FETCH_GET_PACK:
5fc31180
JS
1685 trace2_region_leave("fetch-pack",
1686 "negotiation_v2",
1687 the_repository);
f7e20501
BW
1688 /* Check for shallow-info section */
1689 if (process_section_header(&reader, "shallow-info", 1))
1339078f 1690 receive_shallow_info(args, &reader, shallows, si);
f7e20501 1691
73302051 1692 if (process_section_header(&reader, "wanted-refs", 1))
e2842b39 1693 receive_wanted_refs(&reader, sought, nr_sought);
73302051 1694
dd4b732d 1695 /* get the pack(s) */
88e9b1e3
IF
1696 if (git_env_bool("GIT_TRACE_REDACT", 1))
1697 reader.options |= PACKET_READ_REDACT_URI_PATH;
dd4b732d
JT
1698 if (process_section_header(&reader, "packfile-uris", 1))
1699 receive_packfile_uris(&reader, &packfile_uris);
88e9b1e3
IF
1700 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1701 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1702
685fbd32 1703 process_section_header(&reader, "packfile", 0);
ae1a7eef
JK
1704
1705 /*
1706 * this is the final request we'll make of the server;
1707 * do a half-duplex shutdown to indicate that they can
1708 * hang up as soon as the pack is sent.
1709 */
1710 close(fd[1]);
1711 fd[1] = -1;
1712
dd4b732d 1713 if (get_pack(args, fd, pack_lockfiles,
b664e9ff 1714 packfile_uris.nr ? &index_pack_args : NULL,
3745e269 1715 sought, nr_sought, &fsck_options.gitmodules_found))
685fbd32 1716 die(_("git fetch-pack: fetch failed."));
9c1e657a 1717 do_check_stateless_delimiter(args->stateless_rpc, &reader);
685fbd32
BW
1718
1719 state = FETCH_DONE;
1720 break;
1721 case FETCH_DONE:
1722 continue;
1723 }
1724 }
1725
dd4b732d 1726 for (i = 0; i < packfile_uris.nr; i++) {
b664e9ff 1727 int j;
dd4b732d
JT
1728 struct child_process cmd = CHILD_PROCESS_INIT;
1729 char packname[GIT_MAX_HEXSZ + 1];
1730 const char *uri = packfile_uris.items[i].string +
1731 the_hash_algo->hexsz + 1;
1732
ef8d7ac4
JK
1733 strvec_push(&cmd.args, "http-fetch");
1734 strvec_pushf(&cmd.args, "--packfile=%.*s",
f6d8942b
JK
1735 (int) the_hash_algo->hexsz,
1736 packfile_uris.items[i].string);
b664e9ff
JT
1737 for (j = 0; j < index_pack_args.nr; j++)
1738 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1739 index_pack_args.v[j]);
ef8d7ac4 1740 strvec_push(&cmd.args, uri);
dd4b732d
JT
1741 cmd.git_cmd = 1;
1742 cmd.no_stdin = 1;
1743 cmd.out = -1;
1744 if (start_command(&cmd))
1745 die("fetch-pack: unable to spawn http-fetch");
1746
1747 if (read_in_full(cmd.out, packname, 5) < 0 ||
1748 memcmp(packname, "keep\t", 5))
1749 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1750
1751 if (read_in_full(cmd.out, packname,
1752 the_hash_algo->hexsz + 1) < 0 ||
1753 packname[the_hash_algo->hexsz] != '\n')
1754 die("fetch-pack: expected hash then LF at end of http-fetch output");
1755
1756 packname[the_hash_algo->hexsz] = '\0';
1757
3745e269 1758 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
5476e1ef 1759
dd4b732d
JT
1760 close(cmd.out);
1761
1762 if (finish_command(&cmd))
1763 die("fetch-pack: unable to finish http-fetch");
1764
1765 if (memcmp(packfile_uris.items[i].string, packname,
1766 the_hash_algo->hexsz))
1767 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1768 uri, (int) the_hash_algo->hexsz,
1769 packfile_uris.items[i].string);
1770
1771 string_list_append_nodup(pack_lockfiles,
1772 xstrfmt("%s/pack/pack-%s.keep",
1773 get_object_directory(),
1774 packname));
1775 }
1776 string_list_clear(&packfile_uris, 0);
b664e9ff 1777 strvec_clear(&index_pack_args);
dd4b732d 1778
3745e269
ÆAB
1779 if (fsck_finish(&fsck_options))
1780 die("fsck failed");
dd4b732d 1781
603960b5
JT
1782 if (negotiator)
1783 negotiator->release(negotiator);
dd4b732d 1784
685fbd32
BW
1785 oidset_clear(&common);
1786 return ref;
1787}
1788
1362df0d
ÆAB
1789static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1790{
1791 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1792 const char *path;
1793
1794 if (git_config_pathname(&path, var, value))
1795 return 1;
1796 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1797 fsck_msg_types.len ? ',' : '=', path);
1798 free((char *)path);
1799 return 0;
1800 }
1801
1802 if (skip_prefix(var, "fetch.fsck.", &var)) {
1803 if (is_valid_msg_type(var, value))
1804 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1805 fsck_msg_types.len ? ',' : '=', var, value);
1806 else
1807 warning("Skipping unknown msg id '%s'", var);
1808 return 0;
1809 }
1810
1811 return git_default_config(var, value, cb);
1812}
1813
f44af51d 1814static void fetch_pack_config(void)
745f7a8c 1815{
f44af51d
TA
1816 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1817 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1818 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1819 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1820 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1e905bbc 1821 git_config_get_bool("transfer.advertisesid", &advertise_sid);
dd4b732d
JT
1822 if (!uri_protocols.nr) {
1823 char *str;
1824
1825 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1826 string_list_split(&uri_protocols, str, ',', -1);
1827 free(str);
1828 }
1829 }
745f7a8c 1830
1362df0d 1831 git_config(fetch_pack_config_cb, NULL);
745f7a8c
NTND
1832}
1833
745f7a8c
NTND
1834static void fetch_pack_setup(void)
1835{
1836 static int did_setup;
1837 if (did_setup)
1838 return;
f44af51d 1839 fetch_pack_config();
745f7a8c
NTND
1840 if (0 <= transfer_unpack_limit)
1841 unpack_limit = transfer_unpack_limit;
1842 else if (0 <= fetch_unpack_limit)
1843 unpack_limit = fetch_unpack_limit;
1844 did_setup = 1;
1845}
1846
f2db854d
JH
1847static int remove_duplicates_in_refs(struct ref **ref, int nr)
1848{
1849 struct string_list names = STRING_LIST_INIT_NODUP;
1850 int src, dst;
1851
1852 for (src = dst = 0; src < nr; src++) {
1853 struct string_list_item *item;
1854 item = string_list_insert(&names, ref[src]->name);
1855 if (item->util)
1856 continue; /* already have it */
1857 item->util = ref[src];
1858 if (src != dst)
1859 ref[dst] = ref[src];
1860 dst++;
1861 }
1862 for (src = dst; src < nr; src++)
1863 ref[src] = NULL;
1864 string_list_clear(&names, 0);
1865 return dst;
1866}
1867
beea4152 1868static void update_shallow(struct fetch_pack_args *args,
e2842b39 1869 struct ref **sought, int nr_sought,
beea4152 1870 struct shallow_info *si)
a796ccee 1871{
910650d2 1872 struct oid_array ref = OID_ARRAY_INIT;
4820a33b 1873 int *status;
beea4152
NTND
1874 int i;
1875
79891cb9 1876 if (args->deepen && alternate_shallow_file) {
a796ccee 1877 if (*alternate_shallow_file == '\0') { /* --unshallow */
102de880 1878 unlink_or_warn(git_path_shallow(the_repository));
37b9dcab 1879 rollback_shallow_file(the_repository, &shallow_lock);
a796ccee 1880 } else
37b9dcab 1881 commit_shallow_file(the_repository, &shallow_lock);
23311f35 1882 alternate_shallow_file = NULL;
a796ccee
NTND
1883 return;
1884 }
beea4152
NTND
1885
1886 if (!si->shallow || !si->shallow->nr)
1887 return;
1888
beea4152
NTND
1889 if (args->cloning) {
1890 /*
1891 * remote is shallow, but this is a clone, there are
1892 * no objects in repo to worry about. Accept any
1893 * shallow points that exist in the pack (iow in repo
1894 * after get_pack() and reprepare_packed_git())
1895 */
910650d2 1896 struct oid_array extra = OID_ARRAY_INIT;
ee3051bd 1897 struct object_id *oid = si->shallow->oid;
beea4152 1898 for (i = 0; i < si->shallow->nr; i++)
ee3051bd 1899 if (has_object_file(&oid[i]))
910650d2 1900 oid_array_append(&extra, &oid[i]);
beea4152
NTND
1901 if (extra.nr) {
1902 setup_alternate_shallow(&shallow_lock,
1903 &alternate_shallow_file,
1904 &extra);
37b9dcab 1905 commit_shallow_file(the_repository, &shallow_lock);
23311f35 1906 alternate_shallow_file = NULL;
beea4152 1907 }
910650d2 1908 oid_array_clear(&extra);
beea4152
NTND
1909 return;
1910 }
4820a33b
NTND
1911
1912 if (!si->nr_ours && !si->nr_theirs)
1913 return;
1914
1915 remove_nonexistent_theirs_shallow(si);
4820a33b
NTND
1916 if (!si->nr_ours && !si->nr_theirs)
1917 return;
e2842b39
JT
1918 for (i = 0; i < nr_sought; i++)
1919 oid_array_append(&ref, &sought[i]->old_oid);
4820a33b
NTND
1920 si->ref = &ref;
1921
48d25cae
NTND
1922 if (args->update_shallow) {
1923 /*
1924 * remote is also shallow, .git/shallow may be updated
1925 * so all refs can be accepted. Make sure we only add
1926 * shallow roots that are actually reachable from new
1927 * refs.
1928 */
910650d2 1929 struct oid_array extra = OID_ARRAY_INIT;
ee3051bd 1930 struct object_id *oid = si->shallow->oid;
48d25cae
NTND
1931 assign_shallow_commits_to_refs(si, NULL, NULL);
1932 if (!si->nr_ours && !si->nr_theirs) {
910650d2 1933 oid_array_clear(&ref);
48d25cae
NTND
1934 return;
1935 }
1936 for (i = 0; i < si->nr_ours; i++)
910650d2 1937 oid_array_append(&extra, &oid[si->ours[i]]);
48d25cae 1938 for (i = 0; i < si->nr_theirs; i++)
910650d2 1939 oid_array_append(&extra, &oid[si->theirs[i]]);
48d25cae
NTND
1940 setup_alternate_shallow(&shallow_lock,
1941 &alternate_shallow_file,
1942 &extra);
37b9dcab 1943 commit_shallow_file(the_repository, &shallow_lock);
910650d2 1944 oid_array_clear(&extra);
1945 oid_array_clear(&ref);
23311f35 1946 alternate_shallow_file = NULL;
48d25cae
NTND
1947 return;
1948 }
1949
4820a33b
NTND
1950 /*
1951 * remote is also shallow, check what ref is safe to update
1952 * without updating .git/shallow
1953 */
ca56dadb 1954 CALLOC_ARRAY(status, nr_sought);
4820a33b
NTND
1955 assign_shallow_commits_to_refs(si, NULL, status);
1956 if (si->nr_ours || si->nr_theirs) {
e2842b39 1957 for (i = 0; i < nr_sought; i++)
4820a33b 1958 if (status[i])
e2842b39 1959 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
4820a33b
NTND
1960 }
1961 free(status);
910650d2 1962 oid_array_clear(&ref);
a796ccee
NTND
1963}
1964
9fec7b21 1965static const struct object_id *iterate_ref_map(void *cb_data)
cf1e7c07
JT
1966{
1967 struct ref **rm = cb_data;
1968 struct ref *ref = *rm;
1969
1970 if (!ref)
9fec7b21 1971 return NULL;
cf1e7c07 1972 *rm = ref->next;
9fec7b21 1973 return &ref->old_oid;
cf1e7c07
JT
1974}
1975
745f7a8c 1976struct ref *fetch_pack(struct fetch_pack_args *args,
0f804b0b 1977 int fd[],
745f7a8c 1978 const struct ref *ref,
f2db854d 1979 struct ref **sought, int nr_sought,
910650d2 1980 struct oid_array *shallow,
9da69a65 1981 struct string_list *pack_lockfiles,
685fbd32 1982 enum protocol_version version)
745f7a8c 1983{
745f7a8c 1984 struct ref *ref_cpy;
beea4152 1985 struct shallow_info si;
1339078f 1986 struct oid_array shallows_scratch = OID_ARRAY_INIT;
745f7a8c
NTND
1987
1988 fetch_pack_setup();
f2db854d
JH
1989 if (nr_sought)
1990 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
745f7a8c 1991
01775651 1992 if (version != protocol_v2 && !ref) {
745f7a8c 1993 packet_flush(fd[1]);
1dd73e20 1994 die(_("no matching remote head"));
745f7a8c 1995 }
1e7d440b
JT
1996 if (version == protocol_v2) {
1997 if (shallow->nr)
1998 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1999 memset(&si, 0, sizeof(si));
685fbd32 2000 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1339078f 2001 &shallows_scratch, &si,
9da69a65 2002 pack_lockfiles);
1e7d440b
JT
2003 } else {
2004 prepare_shallow_info(&si, shallow);
685fbd32 2005 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
9da69a65 2006 &si, pack_lockfiles);
1e7d440b 2007 }
a49d2834 2008 reprepare_packed_git(the_repository);
cf1e7c07
JT
2009
2010 if (!args->cloning && args->deepen) {
2011 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2012 struct ref *iterator = ref_cpy;
2013 opt.shallow_file = alternate_shallow_file;
2014 if (args->deepen)
2015 opt.is_deepening_fetch = 1;
2016 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2017 error(_("remote did not send all necessary objects"));
2018 free_refs(ref_cpy);
2019 ref_cpy = NULL;
37b9dcab 2020 rollback_shallow_file(the_repository, &shallow_lock);
cf1e7c07
JT
2021 goto cleanup;
2022 }
2023 args->connectivity_checked = 1;
2024 }
2025
e2842b39 2026 update_shallow(args, sought, nr_sought, &si);
cf1e7c07 2027cleanup:
beea4152 2028 clear_shallow_info(&si);
1339078f 2029 oid_array_clear(&shallows_scratch);
745f7a8c
NTND
2030 return ref_cpy;
2031}
e860d96b 2032
9c1e657a
JT
2033static int add_to_object_array(const struct object_id *oid, void *data)
2034{
2035 struct object_array *a = data;
2036
2037 add_object_array(lookup_object(the_repository, oid), "", a);
2038 return 0;
2039}
2040
2041static void clear_common_flag(struct oidset *s)
2042{
2043 struct oidset_iter iter;
2044 const struct object_id *oid;
2045 oidset_iter_init(s, &iter);
2046
2047 while ((oid = oidset_iter_next(&iter))) {
2048 struct object *obj = lookup_object(the_repository, oid);
2049 obj->flags &= ~COMMON;
2050 }
2051}
2052
2053void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2054 const struct string_list *server_options,
2055 int stateless_rpc,
2056 int fd[],
2057 struct oidset *acked_commits)
2058{
2059 struct fetch_negotiator negotiator;
2060 struct packet_reader reader;
2061 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2062 struct strbuf req_buf = STRBUF_INIT;
2063 int haves_to_send = INITIAL_FLUSH;
2064 int in_vain = 0;
2065 int seen_ack = 0;
2066 int last_iteration = 0;
2067 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2068
2069 fetch_negotiator_init(the_repository, &negotiator);
2070 mark_tips(&negotiator, negotiation_tips);
2071
2072 packet_reader_init(&reader, fd[0], NULL, 0,
2073 PACKET_READ_CHOMP_NEWLINE |
2074 PACKET_READ_DIE_ON_ERR_PACKET);
2075
2076 oid_array_for_each((struct oid_array *) negotiation_tips,
2077 add_to_object_array,
2078 &nt_object_array);
2079
2080 while (!last_iteration) {
2081 int haves_added;
2082 struct object_id common_oid;
2083 int received_ready = 0;
2084
2085 strbuf_reset(&req_buf);
2086 write_fetch_command_and_capabilities(&req_buf, server_options);
2087
2088 packet_buf_write(&req_buf, "wait-for-done");
2089
2090 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2091 in_vain += haves_added;
2092 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2093 last_iteration = 1;
2094
2095 /* Send request */
2096 packet_buf_flush(&req_buf);
2097 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2098 die_errno(_("unable to write request to remote"));
2099
2100 /* Process ACKs/NAKs */
2101 process_section_header(&reader, "acknowledgments", 0);
2102 while (process_ack(&negotiator, &reader, &common_oid,
2103 &received_ready)) {
2104 struct commit *commit = lookup_commit(the_repository,
2105 &common_oid);
2106 if (commit) {
2107 timestamp_t generation;
2108
2109 parse_commit_or_die(commit);
2110 commit->object.flags |= COMMON;
2111 generation = commit_graph_generation(commit);
2112 if (generation < min_generation)
2113 min_generation = generation;
2114 }
2115 in_vain = 0;
2116 seen_ack = 1;
2117 oidset_insert(acked_commits, &common_oid);
2118 }
2119 if (received_ready)
2120 die(_("unexpected 'ready' from remote"));
2121 else
2122 do_check_stateless_delimiter(stateless_rpc, &reader);
2123 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2124 REACH_SCRATCH, 0,
2125 min_generation))
2126 last_iteration = 1;
2127 }
2128 clear_common_flag(acked_commits);
2129 strbuf_release(&req_buf);
2130}
2131
e860d96b
MM
2132int report_unmatched_refs(struct ref **sought, int nr_sought)
2133{
2134 int i, ret = 0;
2135
2136 for (i = 0; i < nr_sought; i++) {
d56583de 2137 if (!sought[i])
e860d96b 2138 continue;
d56583de
MM
2139 switch (sought[i]->match_status) {
2140 case REF_MATCHED:
2141 continue;
2142 case REF_NOT_MATCHED:
2143 error(_("no such remote ref %s"), sought[i]->name);
2144 break;
2145 case REF_UNADVERTISED_NOT_ALLOWED:
2146 error(_("Server does not allow request for unadvertised object %s"),
2147 sought[i]->name);
2148 break;
2149 }
e860d96b
MM
2150 ret = 1;
2151 }
2152 return ret;
2153}