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