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