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