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