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