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