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