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