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