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