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