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