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