]> git.ipfire.org Git - thirdparty/git.git/blame - fetch-pack.c
branch: add test for -m renaming multiple config sections
[thirdparty/git.git] / fetch-pack.c
CommitLineData
745f7a8c 1#include "cache.h"
697cc8ef 2#include "lockfile.h"
745f7a8c
NTND
3#include "refs.h"
4#include "pkt-line.h"
5#include "commit.h"
6#include "tag.h"
7#include "exec_cmd.h"
8#include "pack.h"
9#include "sideband.h"
10#include "fetch-pack.h"
11#include "remote.h"
12#include "run-command.h"
47a59185 13#include "connect.h"
745f7a8c
NTND
14#include "transport.h"
15#include "version.h"
099327b5 16#include "prio-queue.h"
beea4152 17#include "sha1-array.h"
fdb69d33 18#include "oidset.h"
745f7a8c
NTND
19
20static int transfer_unpack_limit = -1;
21static int fetch_unpack_limit = -1;
22static int unpack_limit = 100;
23static int prefer_ofs_delta = 1;
24static int no_done;
508ea882 25static int deepen_since_ok;
a45a2600 26static int deepen_not_ok;
745f7a8c
NTND
27static int fetch_fsck_objects = -1;
28static int transfer_fsck_objects = -1;
29static int agent_supported;
6035d6aa
NTND
30static struct lock_file shallow_lock;
31static const char *alternate_shallow_file;
745f7a8c 32
208acbfb 33/* Remember to update object flag allocation in object.h */
745f7a8c
NTND
34#define COMPLETE (1U << 0)
35#define COMMON (1U << 1)
36#define COMMON_REF (1U << 2)
37#define SEEN (1U << 3)
38#define POPPED (1U << 4)
41a078c6 39#define ALTERNATE (1U << 5)
745f7a8c
NTND
40
41static int marked;
42
43/*
44 * After sending this many "have"s if we do not get any new ACK , we
45 * give up traversing our history.
46 */
47#define MAX_IN_VAIN 256
48
099327b5 49static struct prio_queue rev_list = { compare_commits_by_commit_date };
7199c093
FM
50static int non_common_revs, multi_ack, use_sideband;
51/* Allow specifying sha1 if it is a ref tip. */
52#define ALLOW_TIP_SHA1 01
68ee6289
FM
53/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54#define ALLOW_REACHABLE_SHA1 02
7199c093 55static unsigned int allow_unadvertised_object_request;
745f7a8c 56
0d789a5b
NTND
57__attribute__((format (printf, 2, 3)))
58static inline void print_verbose(const struct fetch_pack_args *args,
59 const char *fmt, ...)
60{
61 va_list params;
62
63 if (!args->verbose)
64 return;
65
66 va_start(params, fmt);
67 vfprintf(stderr, fmt, params);
68 va_end(params);
69 fputc('\n', stderr);
70}
71
41a078c6
JK
72struct alternate_object_cache {
73 struct object **items;
74 size_t nr, alloc;
75};
76
77static void cache_one_alternate(const char *refname,
78 const struct object_id *oid,
79 void *vcache)
80{
81 struct alternate_object_cache *cache = vcache;
c251c83d 82 struct object *obj = parse_object(oid);
41a078c6
JK
83
84 if (!obj || (obj->flags & ALTERNATE))
85 return;
86
87 obj->flags |= ALTERNATE;
88 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
89 cache->items[cache->nr++] = obj;
90}
91
92static void for_each_cached_alternate(void (*cb)(struct object *))
93{
94 static int initialized;
95 static struct alternate_object_cache cache;
96 size_t i;
97
98 if (!initialized) {
99 for_each_alternate_ref(cache_one_alternate, &cache);
100 initialized = 1;
101 }
102
103 for (i = 0; i < cache.nr; i++)
104 cb(cache.items[i]);
105}
106
745f7a8c
NTND
107static void rev_list_push(struct commit *commit, int mark)
108{
109 if (!(commit->object.flags & mark)) {
110 commit->object.flags |= mark;
111
0064053b
JK
112 if (parse_commit(commit))
113 return;
745f7a8c 114
099327b5 115 prio_queue_put(&rev_list, commit);
745f7a8c
NTND
116
117 if (!(commit->object.flags & COMMON))
118 non_common_revs++;
119 }
120}
121
1b283377 122static int rev_list_insert_ref(const char *refname, const struct object_id *oid)
745f7a8c 123{
c251c83d 124 struct object *o = deref_tag(parse_object(oid), refname, 0);
745f7a8c
NTND
125
126 if (o && o->type == OBJ_COMMIT)
127 rev_list_push((struct commit *)o, SEEN);
128
129 return 0;
130}
131
b1b49c6e
MH
132static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
133 int flag, void *cb_data)
745f7a8c 134{
1b283377 135 return rev_list_insert_ref(refname, oid);
b1b49c6e
MH
136}
137
c50fb6ce
MH
138static int clear_marks(const char *refname, const struct object_id *oid,
139 int flag, void *cb_data)
745f7a8c 140{
c251c83d 141 struct object *o = deref_tag(parse_object(oid), refname, 0);
745f7a8c
NTND
142
143 if (o && o->type == OBJ_COMMIT)
144 clear_commit_marks((struct commit *)o,
145 COMMON | COMMON_REF | SEEN | POPPED);
146 return 0;
147}
148
149/*
150 This function marks a rev and its ancestors as common.
151 In some cases, it is desirable to mark only the ancestors (for example
152 when only the server does not yet know that they are common).
153*/
154
155static void mark_common(struct commit *commit,
156 int ancestors_only, int dont_parse)
157{
158 if (commit != NULL && !(commit->object.flags & COMMON)) {
159 struct object *o = (struct object *)commit;
160
161 if (!ancestors_only)
162 o->flags |= COMMON;
163
164 if (!(o->flags & SEEN))
165 rev_list_push(commit, SEEN);
166 else {
167 struct commit_list *parents;
168
169 if (!ancestors_only && !(o->flags & POPPED))
170 non_common_revs--;
171 if (!o->parsed && !dont_parse)
172 if (parse_commit(commit))
173 return;
174
175 for (parents = commit->parents;
176 parents;
177 parents = parents->next)
178 mark_common(parents->item, 0, dont_parse);
179 }
180 }
181}
182
183/*
184 Get the next rev to send, ignoring the common.
185*/
186
1b283377 187static const struct object_id *get_rev(void)
745f7a8c
NTND
188{
189 struct commit *commit = NULL;
190
191 while (commit == NULL) {
192 unsigned int mark;
193 struct commit_list *parents;
194
099327b5 195 if (rev_list.nr == 0 || non_common_revs == 0)
745f7a8c
NTND
196 return NULL;
197
099327b5 198 commit = prio_queue_get(&rev_list);
0064053b 199 parse_commit(commit);
745f7a8c
NTND
200 parents = commit->parents;
201
202 commit->object.flags |= POPPED;
203 if (!(commit->object.flags & COMMON))
204 non_common_revs--;
205
206 if (commit->object.flags & COMMON) {
207 /* do not send "have", and ignore ancestors */
208 commit = NULL;
209 mark = COMMON | SEEN;
210 } else if (commit->object.flags & COMMON_REF)
211 /* send "have", and ignore ancestors */
212 mark = COMMON | SEEN;
213 else
214 /* send "have", also for its ancestors */
215 mark = SEEN;
216
217 while (parents) {
218 if (!(parents->item->object.flags & SEEN))
219 rev_list_push(parents->item, mark);
220 if (mark & COMMON)
221 mark_common(parents->item, 1, 0);
222 parents = parents->next;
223 }
745f7a8c
NTND
224 }
225
1b283377 226 return &commit->object.oid;
745f7a8c
NTND
227}
228
229enum ack_type {
230 NAK = 0,
231 ACK,
232 ACK_continue,
233 ACK_common,
234 ACK_ready
235};
236
237static void consume_shallow_list(struct fetch_pack_args *args, int fd)
238{
79891cb9 239 if (args->stateless_rpc && args->deepen) {
745f7a8c
NTND
240 /* If we sent a depth we will get back "duplicate"
241 * shallow and unshallow commands every time there
242 * is a block of have lines exchanged.
243 */
74543a04
JK
244 char *line;
245 while ((line = packet_read_line(fd, NULL))) {
59556548 246 if (starts_with(line, "shallow "))
745f7a8c 247 continue;
59556548 248 if (starts_with(line, "unshallow "))
745f7a8c 249 continue;
1dd73e20 250 die(_("git fetch-pack: expected shallow list"));
745f7a8c
NTND
251 }
252 }
253}
254
1b283377 255static enum ack_type get_ack(int fd, struct object_id *result_oid)
745f7a8c 256{
74543a04
JK
257 int len;
258 char *line = packet_read_line(fd, &len);
82e56767 259 const char *arg;
745f7a8c
NTND
260
261 if (!len)
1dd73e20 262 die(_("git fetch-pack: expected ACK/NAK, got EOF"));
745f7a8c
NTND
263 if (!strcmp(line, "NAK"))
264 return NAK;
82e56767 265 if (skip_prefix(line, "ACK ", &arg)) {
1b283377 266 if (!get_oid_hex(arg, result_oid)) {
82e56767
JK
267 arg += 40;
268 len -= arg - line;
269 if (len < 1)
030e9dd6 270 return ACK;
82e56767 271 if (strstr(arg, "continue"))
745f7a8c 272 return ACK_continue;
82e56767 273 if (strstr(arg, "common"))
745f7a8c 274 return ACK_common;
82e56767 275 if (strstr(arg, "ready"))
745f7a8c
NTND
276 return ACK_ready;
277 return ACK;
278 }
279 }
8e2c7bef
JT
280 if (skip_prefix(line, "ERR ", &arg))
281 die(_("remote error: %s"), arg);
dfbfb9f3 282 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
745f7a8c
NTND
283}
284
285static void send_request(struct fetch_pack_args *args,
286 int fd, struct strbuf *buf)
287{
288 if (args->stateless_rpc) {
289 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
290 packet_flush(fd);
291 } else
cdf4fb8e 292 write_or_die(fd, buf->buf, buf->len);
745f7a8c
NTND
293}
294
41a078c6 295static void insert_one_alternate_object(struct object *obj)
745f7a8c 296{
1b283377 297 rev_list_insert_ref(NULL, &obj->oid);
745f7a8c
NTND
298}
299
300#define INITIAL_FLUSH 16
301#define PIPESAFE_FLUSH 32
da470981 302#define LARGE_FLUSH 16384
745f7a8c
NTND
303
304static int next_flush(struct fetch_pack_args *args, int count)
305{
da470981
JT
306 if (args->stateless_rpc) {
307 if (count < LARGE_FLUSH)
308 count <<= 1;
309 else
310 count = count * 11 / 10;
311 } else {
312 if (count < PIPESAFE_FLUSH)
313 count <<= 1;
314 else
315 count += PIPESAFE_FLUSH;
316 }
745f7a8c
NTND
317 return count;
318}
319
320static int find_common(struct fetch_pack_args *args,
1b283377 321 int fd[2], struct object_id *result_oid,
745f7a8c
NTND
322 struct ref *refs)
323{
324 int fetching;
325 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
1b283377 326 const struct object_id *oid;
745f7a8c
NTND
327 unsigned in_vain = 0;
328 int got_continue = 0;
329 int got_ready = 0;
330 struct strbuf req_buf = STRBUF_INIT;
331 size_t state_len = 0;
332
333 if (args->stateless_rpc && multi_ack == 1)
1dd73e20 334 die(_("--stateless-rpc requires multi_ack_detailed"));
745f7a8c
NTND
335 if (marked)
336 for_each_ref(clear_marks, NULL);
337 marked = 1;
338
b1b49c6e 339 for_each_ref(rev_list_insert_ref_oid, NULL);
41a078c6 340 for_each_cached_alternate(insert_one_alternate_object);
745f7a8c
NTND
341
342 fetching = 0;
343 for ( ; refs ; refs = refs->next) {
1b283377 344 struct object_id *remote = &refs->old_oid;
745f7a8c
NTND
345 const char *remote_hex;
346 struct object *o;
347
348 /*
349 * If that object is complete (i.e. it is an ancestor of a
350 * local ref), we tell them we have it but do not have to
351 * tell them about its ancestors, which they already know
352 * about.
353 *
354 * We use lookup_object here because we are only
355 * interested in the case we *know* the object is
356 * reachable and we have already scanned it.
357 */
1b283377 358 if (((o = lookup_object(remote->hash)) != NULL) &&
745f7a8c
NTND
359 (o->flags & COMPLETE)) {
360 continue;
361 }
362
1b283377 363 remote_hex = oid_to_hex(remote);
745f7a8c
NTND
364 if (!fetching) {
365 struct strbuf c = STRBUF_INIT;
366 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
367 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
368 if (no_done) strbuf_addstr(&c, " no-done");
369 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
370 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
cccf74e2 371 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
745f7a8c
NTND
372 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
373 if (args->no_progress) strbuf_addstr(&c, " no-progress");
374 if (args->include_tag) strbuf_addstr(&c, " include-tag");
375 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
508ea882 376 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
a45a2600 377 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
745f7a8c
NTND
378 if (agent_supported) strbuf_addf(&c, " agent=%s",
379 git_user_agent_sanitized());
380 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
381 strbuf_release(&c);
382 } else
383 packet_buf_write(&req_buf, "want %s\n", remote_hex);
384 fetching++;
385 }
386
387 if (!fetching) {
388 strbuf_release(&req_buf);
389 packet_flush(fd[1]);
390 return 1;
391 }
392
393 if (is_repository_shallow())
1a30f5a2 394 write_shallow_commits(&req_buf, 1, NULL);
745f7a8c
NTND
395 if (args->depth > 0)
396 packet_buf_write(&req_buf, "deepen %d", args->depth);
508ea882 397 if (args->deepen_since) {
dddbad72 398 timestamp_t max_age = approxidate(args->deepen_since);
cb71f8bd 399 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
508ea882 400 }
a45a2600
NTND
401 if (args->deepen_not) {
402 int i;
403 for (i = 0; i < args->deepen_not->nr; i++) {
404 struct string_list_item *s = args->deepen_not->items + i;
405 packet_buf_write(&req_buf, "deepen-not %s", s->string);
406 }
407 }
745f7a8c
NTND
408 packet_buf_flush(&req_buf);
409 state_len = req_buf.len;
410
79891cb9 411 if (args->deepen) {
74543a04 412 char *line;
ae021d87 413 const char *arg;
1b283377 414 struct object_id oid;
745f7a8c
NTND
415
416 send_request(args, fd[1], &req_buf);
74543a04 417 while ((line = packet_read_line(fd[0], NULL))) {
ae021d87 418 if (skip_prefix(line, "shallow ", &arg)) {
1b283377 419 if (get_oid_hex(arg, &oid))
1dd73e20 420 die(_("invalid shallow line: %s"), line);
e92b848c 421 register_shallow(&oid);
745f7a8c
NTND
422 continue;
423 }
ae021d87 424 if (skip_prefix(line, "unshallow ", &arg)) {
1b283377 425 if (get_oid_hex(arg, &oid))
1dd73e20 426 die(_("invalid unshallow line: %s"), line);
1b283377 427 if (!lookup_object(oid.hash))
1dd73e20 428 die(_("object not found: %s"), line);
745f7a8c 429 /* make sure that it is parsed as shallow */
c251c83d 430 if (!parse_object(&oid))
1dd73e20 431 die(_("error in object: %s"), line);
e92b848c 432 if (unregister_shallow(&oid))
1dd73e20 433 die(_("no shallow found: %s"), line);
745f7a8c
NTND
434 continue;
435 }
1dd73e20 436 die(_("expected shallow/unshallow, got %s"), line);
745f7a8c
NTND
437 }
438 } else if (!args->stateless_rpc)
439 send_request(args, fd[1], &req_buf);
440
441 if (!args->stateless_rpc) {
442 /* If we aren't using the stateless-rpc interface
443 * we don't need to retain the headers.
444 */
445 strbuf_setlen(&req_buf, 0);
446 state_len = 0;
447 }
448
449 flushes = 0;
450 retval = -1;
1b283377 451 while ((oid = get_rev())) {
452 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
453 print_verbose(args, "have %s", oid_to_hex(oid));
745f7a8c
NTND
454 in_vain++;
455 if (flush_at <= ++count) {
456 int ack;
457
458 packet_buf_flush(&req_buf);
459 send_request(args, fd[1], &req_buf);
460 strbuf_setlen(&req_buf, state_len);
461 flushes++;
462 flush_at = next_flush(args, count);
463
464 /*
465 * We keep one window "ahead" of the other side, and
466 * will wait for an ACK only on the next one
467 */
468 if (!args->stateless_rpc && count == INITIAL_FLUSH)
469 continue;
470
471 consume_shallow_list(args, fd[0]);
472 do {
1b283377 473 ack = get_ack(fd[0], result_oid);
0d789a5b 474 if (ack)
1dd73e20 475 print_verbose(args, _("got %s %d %s"), "ack",
1b283377 476 ack, oid_to_hex(result_oid));
745f7a8c
NTND
477 switch (ack) {
478 case ACK:
479 flushes = 0;
480 multi_ack = 0;
481 retval = 0;
482 goto done;
483 case ACK_common:
484 case ACK_ready:
485 case ACK_continue: {
486 struct commit *commit =
bc83266a 487 lookup_commit(result_oid);
745f7a8c 488 if (!commit)
1b283377 489 die(_("invalid commit %s"), oid_to_hex(result_oid));
745f7a8c
NTND
490 if (args->stateless_rpc
491 && ack == ACK_common
492 && !(commit->object.flags & COMMON)) {
493 /* We need to replay the have for this object
494 * on the next RPC request so the peer knows
495 * it is in common with us.
496 */
1b283377 497 const char *hex = oid_to_hex(result_oid);
745f7a8c
NTND
498 packet_buf_write(&req_buf, "have %s\n", hex);
499 state_len = req_buf.len;
06b3d386
JT
500 /*
501 * Reset in_vain because an ack
502 * for this commit has not been
503 * seen.
504 */
505 in_vain = 0;
506 } else if (!args->stateless_rpc
507 || ack != ACK_common)
508 in_vain = 0;
745f7a8c
NTND
509 mark_common(commit, 0, 1);
510 retval = 0;
745f7a8c
NTND
511 got_continue = 1;
512 if (ack == ACK_ready) {
099327b5 513 clear_prio_queue(&rev_list);
745f7a8c
NTND
514 got_ready = 1;
515 }
516 break;
517 }
518 }
519 } while (ack);
520 flushes--;
521 if (got_continue && MAX_IN_VAIN < in_vain) {
1dd73e20 522 print_verbose(args, _("giving up"));
745f7a8c
NTND
523 break; /* give up */
524 }
525 }
526 }
527done:
528 if (!got_ready || !no_done) {
529 packet_buf_write(&req_buf, "done\n");
530 send_request(args, fd[1], &req_buf);
531 }
1dd73e20 532 print_verbose(args, _("done"));
745f7a8c
NTND
533 if (retval != 0) {
534 multi_ack = 0;
535 flushes++;
536 }
537 strbuf_release(&req_buf);
538
ff62eca7
NTND
539 if (!got_ready || !no_done)
540 consume_shallow_list(args, fd[0]);
745f7a8c 541 while (flushes || multi_ack) {
1b283377 542 int ack = get_ack(fd[0], result_oid);
745f7a8c 543 if (ack) {
1dd73e20 544 print_verbose(args, _("got %s (%d) %s"), "ack",
1b283377 545 ack, oid_to_hex(result_oid));
745f7a8c
NTND
546 if (ack == ACK)
547 return 0;
548 multi_ack = 1;
549 continue;
550 }
551 flushes--;
552 }
553 /* it is no error to fetch into a completely empty repo */
554 return count ? retval : 0;
555}
556
557static struct commit_list *complete;
558
1b283377 559static int mark_complete(const struct object_id *oid)
745f7a8c 560{
c251c83d 561 struct object *o = parse_object(oid);
745f7a8c
NTND
562
563 while (o && o->type == OBJ_TAG) {
564 struct tag *t = (struct tag *) o;
565 if (!t->tagged)
566 break; /* broken repository */
567 o->flags |= COMPLETE;
c251c83d 568 o = parse_object(&t->tagged->oid);
745f7a8c
NTND
569 }
570 if (o && o->type == OBJ_COMMIT) {
571 struct commit *commit = (struct commit *)o;
572 if (!(commit->object.flags & COMPLETE)) {
573 commit->object.flags |= COMPLETE;
16445242 574 commit_list_insert(commit, &complete);
745f7a8c
NTND
575 }
576 }
577 return 0;
578}
579
f8ee4d85
MH
580static int mark_complete_oid(const char *refname, const struct object_id *oid,
581 int flag, void *cb_data)
582{
1b283377 583 return mark_complete(oid);
f8ee4d85
MH
584}
585
745f7a8c 586static void mark_recent_complete_commits(struct fetch_pack_args *args,
dddbad72 587 timestamp_t cutoff)
745f7a8c
NTND
588{
589 while (complete && cutoff <= complete->item->date) {
1dd73e20 590 print_verbose(args, _("Marking %s as complete"),
0d789a5b 591 oid_to_hex(&complete->item->object.oid));
745f7a8c
NTND
592 pop_most_recent_commit(&complete, COMPLETE);
593 }
594}
595
fdb69d33
JT
596static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
597{
598 for (; refs; refs = refs->next)
599 oidset_insert(oids, &refs->old_oid);
600}
601
602static int tip_oids_contain(struct oidset *tip_oids,
603 struct ref *unmatched, struct ref *newlist,
604 const struct object_id *id)
605{
606 /*
607 * Note that this only looks at the ref lists the first time it's
608 * called. This works out in filter_refs() because even though it may
609 * add to "newlist" between calls, the additions will always be for
610 * oids that are already in the set.
611 */
612 if (!tip_oids->map.tablesize) {
613 add_refs_to_oidset(tip_oids, unmatched);
614 add_refs_to_oidset(tip_oids, newlist);
615 }
616 return oidset_contains(tip_oids, id);
617}
618
745f7a8c 619static void filter_refs(struct fetch_pack_args *args,
f2db854d
JH
620 struct ref **refs,
621 struct ref **sought, int nr_sought)
745f7a8c
NTND
622{
623 struct ref *newlist = NULL;
624 struct ref **newtail = &newlist;
fdb69d33 625 struct ref *unmatched = NULL;
745f7a8c 626 struct ref *ref, *next;
fdb69d33 627 struct oidset tip_oids = OIDSET_INIT;
f2db854d 628 int i;
745f7a8c 629
f2db854d 630 i = 0;
745f7a8c
NTND
631 for (ref = *refs; ref; ref = next) {
632 int keep = 0;
633 next = ref->next;
f2db854d 634
50e19a83 635 if (starts_with(ref->name, "refs/") &&
4c224081 636 check_refname_format(ref->name, 0))
745f7a8c
NTND
637 ; /* trash */
638 else {
f2db854d
JH
639 while (i < nr_sought) {
640 int cmp = strcmp(ref->name, sought[i]->name);
745f7a8c
NTND
641 if (cmp < 0)
642 break; /* definitely do not have it */
643 else if (cmp == 0) {
644 keep = 1; /* definitely have it */
d56583de 645 sought[i]->match_status = REF_MATCHED;
745f7a8c 646 }
f2db854d 647 i++;
745f7a8c
NTND
648 }
649 }
650
f2db854d 651 if (!keep && args->fetch_all &&
79891cb9 652 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
745f7a8c
NTND
653 keep = 1;
654
655 if (keep) {
656 *newtail = ref;
657 ref->next = NULL;
658 newtail = &ref->next;
659 } else {
fdb69d33
JT
660 ref->next = unmatched;
661 unmatched = ref;
745f7a8c
NTND
662 }
663 }
664
6e7b66ee 665 /* Append unmatched requests to the list */
d56583de 666 for (i = 0; i < nr_sought; i++) {
1b283377 667 struct object_id oid;
668 const char *p;
b7916422 669
d56583de
MM
670 ref = sought[i];
671 if (ref->match_status != REF_NOT_MATCHED)
672 continue;
1b283377 673 if (parse_oid_hex(ref->name, &oid, &p) ||
674 *p != '\0' ||
675 oidcmp(&oid, &ref->old_oid))
d56583de 676 continue;
6e7b66ee 677
d56583de 678 if ((allow_unadvertised_object_request &
fdb69d33
JT
679 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
680 tip_oids_contain(&tip_oids, unmatched, newlist,
681 &ref->old_oid)) {
d56583de 682 ref->match_status = REF_MATCHED;
c3c17bf1
JK
683 *newtail = copy_ref(ref);
684 newtail = &(*newtail)->next;
d56583de
MM
685 } else {
686 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
6e7b66ee
JH
687 }
688 }
fdb69d33
JT
689
690 oidset_clear(&tip_oids);
691 for (ref = unmatched; ref; ref = next) {
692 next = ref->next;
693 free(ref);
694 }
695
745f7a8c
NTND
696 *refs = newlist;
697}
698
41a078c6 699static void mark_alternate_complete(struct object *obj)
745f7a8c 700{
1b283377 701 mark_complete(&obj->oid);
745f7a8c
NTND
702}
703
704static int everything_local(struct fetch_pack_args *args,
f2db854d
JH
705 struct ref **refs,
706 struct ref **sought, int nr_sought)
745f7a8c
NTND
707{
708 struct ref *ref;
709 int retval;
dddbad72 710 timestamp_t cutoff = 0;
745f7a8c
NTND
711
712 save_commit_buffer = 0;
713
714 for (ref = *refs; ref; ref = ref->next) {
715 struct object *o;
716
f4e54d02 717 if (!has_object_file(&ref->old_oid))
012a1bb5
JH
718 continue;
719
c251c83d 720 o = parse_object(&ref->old_oid);
745f7a8c
NTND
721 if (!o)
722 continue;
723
724 /* We already have it -- which may mean that we were
725 * in sync with the other side at some time after
726 * that (it is OK if we guess wrong here).
727 */
728 if (o->type == OBJ_COMMIT) {
729 struct commit *commit = (struct commit *)o;
730 if (!cutoff || cutoff < commit->date)
731 cutoff = commit->date;
732 }
733 }
734
79891cb9 735 if (!args->deepen) {
f8ee4d85 736 for_each_ref(mark_complete_oid, NULL);
41a078c6 737 for_each_cached_alternate(mark_alternate_complete);
16445242 738 commit_list_sort_by_date(&complete);
745f7a8c
NTND
739 if (cutoff)
740 mark_recent_complete_commits(args, cutoff);
741 }
742
743 /*
744 * Mark all complete remote refs as common refs.
745 * Don't mark them common yet; the server has to be told so first.
746 */
747 for (ref = *refs; ref; ref = ref->next) {
f4e54d02 748 struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
745f7a8c
NTND
749 NULL, 0);
750
751 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
752 continue;
753
754 if (!(o->flags & SEEN)) {
755 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
756
757 mark_common((struct commit *)o, 1, 1);
758 }
759 }
760
f2db854d 761 filter_refs(args, refs, sought, nr_sought);
745f7a8c
NTND
762
763 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
1b283377 764 const struct object_id *remote = &ref->old_oid;
745f7a8c
NTND
765 struct object *o;
766
1b283377 767 o = lookup_object(remote->hash);
745f7a8c
NTND
768 if (!o || !(o->flags & COMPLETE)) {
769 retval = 0;
1b283377 770 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
0d789a5b 771 ref->name);
745f7a8c
NTND
772 continue;
773 }
1b283377 774 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
0d789a5b 775 ref->name);
745f7a8c
NTND
776 }
777 return retval;
778}
779
780static int sideband_demux(int in, int out, void *data)
781{
782 int *xd = data;
9ff18faf 783 int ret;
745f7a8c 784
9ff18faf 785 ret = recv_sideband("fetch-pack", xd[0], out);
745f7a8c
NTND
786 close(out);
787 return ret;
788}
789
790static int get_pack(struct fetch_pack_args *args,
791 int xd[2], char **pack_lockfile)
792{
793 struct async demux;
745f7a8c 794 int do_keep = args->keep_pack;
984a43b9
JK
795 const char *cmd_name;
796 struct pack_header header;
797 int pass_header = 0;
d3180279 798 struct child_process cmd = CHILD_PROCESS_INIT;
c6807a40 799 int ret;
745f7a8c
NTND
800
801 memset(&demux, 0, sizeof(demux));
802 if (use_sideband) {
803 /* xd[] is talking with upload-pack; subprocess reads from
804 * xd[0], spits out band#2 to stderr, and feeds us band#1
805 * through demux->out.
806 */
807 demux.proc = sideband_demux;
808 demux.data = xd;
809 demux.out = -1;
df857572 810 demux.isolate_sigpipe = 1;
745f7a8c 811 if (start_async(&demux))
1dd73e20 812 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
745f7a8c
NTND
813 }
814 else
815 demux.out = xd[0];
816
745f7a8c 817 if (!args->keep_pack && unpack_limit) {
745f7a8c
NTND
818
819 if (read_pack_header(demux.out, &header))
1dd73e20 820 die(_("protocol error: bad pack header"));
984a43b9 821 pass_header = 1;
745f7a8c
NTND
822 if (ntohl(header.hdr_entries) < unpack_limit)
823 do_keep = 0;
824 else
825 do_keep = 1;
826 }
827
6035d6aa 828 if (alternate_shallow_file) {
984a43b9
JK
829 argv_array_push(&cmd.args, "--shallow-file");
830 argv_array_push(&cmd.args, alternate_shallow_file);
6035d6aa
NTND
831 }
832
745f7a8c
NTND
833 if (do_keep) {
834 if (pack_lockfile)
835 cmd.out = -1;
984a43b9
JK
836 cmd_name = "index-pack";
837 argv_array_push(&cmd.args, cmd_name);
838 argv_array_push(&cmd.args, "--stdin");
745f7a8c 839 if (!args->quiet && !args->no_progress)
984a43b9 840 argv_array_push(&cmd.args, "-v");
745f7a8c 841 if (args->use_thin_pack)
984a43b9 842 argv_array_push(&cmd.args, "--fix-thin");
745f7a8c 843 if (args->lock_pack || unpack_limit) {
da25bdb7 844 char hostname[HOST_NAME_MAX + 1];
5781a9a2 845 if (xgethostname(hostname, sizeof(hostname)))
984a43b9
JK
846 xsnprintf(hostname, sizeof(hostname), "localhost");
847 argv_array_pushf(&cmd.args,
848 "--keep=fetch-pack %"PRIuMAX " on %s",
849 (uintmax_t)getpid(), hostname);
745f7a8c 850 }
c6807a40 851 if (args->check_self_contained_and_connected)
984a43b9 852 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
745f7a8c
NTND
853 }
854 else {
984a43b9
JK
855 cmd_name = "unpack-objects";
856 argv_array_push(&cmd.args, cmd_name);
745f7a8c 857 if (args->quiet || args->no_progress)
984a43b9 858 argv_array_push(&cmd.args, "-q");
c6807a40 859 args->check_self_contained_and_connected = 0;
745f7a8c 860 }
984a43b9
JK
861
862 if (pass_header)
863 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
864 ntohl(header.hdr_version),
865 ntohl(header.hdr_entries));
745f7a8c
NTND
866 if (fetch_fsck_objects >= 0
867 ? fetch_fsck_objects
868 : transfer_fsck_objects >= 0
869 ? transfer_fsck_objects
870 : 0)
984a43b9 871 argv_array_push(&cmd.args, "--strict");
745f7a8c
NTND
872
873 cmd.in = demux.out;
874 cmd.git_cmd = 1;
875 if (start_command(&cmd))
1dd73e20 876 die(_("fetch-pack: unable to fork off %s"), cmd_name);
745f7a8c
NTND
877 if (do_keep && pack_lockfile) {
878 *pack_lockfile = index_pack_lockfile(cmd.out);
879 close(cmd.out);
880 }
881
37cb1dd6
JL
882 if (!use_sideband)
883 /* Closed by start_command() */
884 xd[0] = -1;
885
c6807a40
NTND
886 ret = finish_command(&cmd);
887 if (!ret || (args->check_self_contained_and_connected && ret == 1))
888 args->self_contained_and_connected =
889 args->check_self_contained_and_connected &&
890 ret == 0;
891 else
1dd73e20 892 die(_("%s failed"), cmd_name);
745f7a8c 893 if (use_sideband && finish_async(&demux))
1dd73e20 894 die(_("error in sideband demultiplexer"));
745f7a8c
NTND
895 return 0;
896}
897
f2db854d
JH
898static int cmp_ref_by_name(const void *a_, const void *b_)
899{
900 const struct ref *a = *((const struct ref **)a_);
901 const struct ref *b = *((const struct ref **)b_);
902 return strcmp(a->name, b->name);
903}
904
745f7a8c
NTND
905static struct ref *do_fetch_pack(struct fetch_pack_args *args,
906 int fd[2],
907 const struct ref *orig_ref,
f2db854d 908 struct ref **sought, int nr_sought,
beea4152 909 struct shallow_info *si,
745f7a8c
NTND
910 char **pack_lockfile)
911{
912 struct ref *ref = copy_ref_list(orig_ref);
1b283377 913 struct object_id oid;
745f7a8c
NTND
914 const char *agent_feature;
915 int agent_len;
916
917 sort_ref_list(&ref, ref_compare_name);
9ed0d8d6 918 QSORT(sought, nr_sought, cmp_ref_by_name);
745f7a8c 919
eb86a507 920 if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
1dd73e20 921 die(_("Server does not support shallow clients"));
a45a2600 922 if (args->depth > 0 || args->deepen_since || args->deepen_not)
79891cb9 923 args->deepen = 1;
745f7a8c 924 if (server_supports("multi_ack_detailed")) {
1dd73e20 925 print_verbose(args, _("Server supports multi_ack_detailed"));
745f7a8c
NTND
926 multi_ack = 2;
927 if (server_supports("no-done")) {
1dd73e20 928 print_verbose(args, _("Server supports no-done"));
745f7a8c
NTND
929 if (args->stateless_rpc)
930 no_done = 1;
931 }
932 }
933 else if (server_supports("multi_ack")) {
1dd73e20 934 print_verbose(args, _("Server supports multi_ack"));
745f7a8c
NTND
935 multi_ack = 1;
936 }
937 if (server_supports("side-band-64k")) {
1dd73e20 938 print_verbose(args, _("Server supports side-band-64k"));
745f7a8c
NTND
939 use_sideband = 2;
940 }
941 else if (server_supports("side-band")) {
1dd73e20 942 print_verbose(args, _("Server supports side-band"));
745f7a8c
NTND
943 use_sideband = 1;
944 }
6e7b66ee 945 if (server_supports("allow-tip-sha1-in-want")) {
1dd73e20 946 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
7199c093 947 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
6e7b66ee 948 }
68ee6289 949 if (server_supports("allow-reachable-sha1-in-want")) {
1dd73e20 950 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
68ee6289
FM
951 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
952 }
745f7a8c
NTND
953 if (!server_supports("thin-pack"))
954 args->use_thin_pack = 0;
955 if (!server_supports("no-progress"))
956 args->no_progress = 0;
957 if (!server_supports("include-tag"))
958 args->include_tag = 0;
0d789a5b 959 if (server_supports("ofs-delta"))
1dd73e20 960 print_verbose(args, _("Server supports ofs-delta"));
0d789a5b 961 else
745f7a8c
NTND
962 prefer_ofs_delta = 0;
963
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
f2db854d 981 if (everything_local(args, &ref, sought, nr_sought)) {
745f7a8c
NTND
982 packet_flush(fd[1]);
983 goto all_done;
984 }
1b283377 985 if (find_common(args, fd, &oid, ref) < 0)
745f7a8c
NTND
986 if (!args->keep_pack)
987 /* When cloning, it is not unusual to have
988 * no common commit.
989 */
1dd73e20 990 warning(_("no common commits"));
745f7a8c
NTND
991
992 if (args->stateless_rpc)
993 packet_flush(fd[1]);
79891cb9 994 if (args->deepen)
1a30f5a2
NTND
995 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
996 NULL);
4820a33b 997 else if (si->nr_ours || si->nr_theirs)
beea4152 998 alternate_shallow_file = setup_temporary_shallow(si->shallow);
6da8bdcb
NTND
999 else
1000 alternate_shallow_file = NULL;
745f7a8c 1001 if (get_pack(args, fd, pack_lockfile))
1dd73e20 1002 die(_("git fetch-pack: fetch failed."));
745f7a8c
NTND
1003
1004 all_done:
1005 return ref;
1006}
1007
f44af51d 1008static void fetch_pack_config(void)
745f7a8c 1009{
f44af51d
TA
1010 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1011 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1012 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1013 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1014 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
745f7a8c 1015
f44af51d 1016 git_config(git_default_config, NULL);
745f7a8c
NTND
1017}
1018
745f7a8c
NTND
1019static void fetch_pack_setup(void)
1020{
1021 static int did_setup;
1022 if (did_setup)
1023 return;
f44af51d 1024 fetch_pack_config();
745f7a8c
NTND
1025 if (0 <= transfer_unpack_limit)
1026 unpack_limit = transfer_unpack_limit;
1027 else if (0 <= fetch_unpack_limit)
1028 unpack_limit = fetch_unpack_limit;
1029 did_setup = 1;
1030}
1031
f2db854d
JH
1032static int remove_duplicates_in_refs(struct ref **ref, int nr)
1033{
1034 struct string_list names = STRING_LIST_INIT_NODUP;
1035 int src, dst;
1036
1037 for (src = dst = 0; src < nr; src++) {
1038 struct string_list_item *item;
1039 item = string_list_insert(&names, ref[src]->name);
1040 if (item->util)
1041 continue; /* already have it */
1042 item->util = ref[src];
1043 if (src != dst)
1044 ref[dst] = ref[src];
1045 dst++;
1046 }
1047 for (src = dst; src < nr; src++)
1048 ref[src] = NULL;
1049 string_list_clear(&names, 0);
1050 return dst;
1051}
1052
beea4152 1053static void update_shallow(struct fetch_pack_args *args,
4820a33b 1054 struct ref **sought, int nr_sought,
beea4152 1055 struct shallow_info *si)
a796ccee 1056{
910650d2 1057 struct oid_array ref = OID_ARRAY_INIT;
4820a33b 1058 int *status;
beea4152
NTND
1059 int i;
1060
79891cb9 1061 if (args->deepen && alternate_shallow_file) {
a796ccee 1062 if (*alternate_shallow_file == '\0') { /* --unshallow */
f932729c 1063 unlink_or_warn(git_path_shallow());
a796ccee
NTND
1064 rollback_lock_file(&shallow_lock);
1065 } else
1066 commit_lock_file(&shallow_lock);
1067 return;
1068 }
beea4152
NTND
1069
1070 if (!si->shallow || !si->shallow->nr)
1071 return;
1072
beea4152
NTND
1073 if (args->cloning) {
1074 /*
1075 * remote is shallow, but this is a clone, there are
1076 * no objects in repo to worry about. Accept any
1077 * shallow points that exist in the pack (iow in repo
1078 * after get_pack() and reprepare_packed_git())
1079 */
910650d2 1080 struct oid_array extra = OID_ARRAY_INIT;
ee3051bd 1081 struct object_id *oid = si->shallow->oid;
beea4152 1082 for (i = 0; i < si->shallow->nr; i++)
ee3051bd 1083 if (has_object_file(&oid[i]))
910650d2 1084 oid_array_append(&extra, &oid[i]);
beea4152
NTND
1085 if (extra.nr) {
1086 setup_alternate_shallow(&shallow_lock,
1087 &alternate_shallow_file,
1088 &extra);
1089 commit_lock_file(&shallow_lock);
1090 }
910650d2 1091 oid_array_clear(&extra);
beea4152
NTND
1092 return;
1093 }
4820a33b
NTND
1094
1095 if (!si->nr_ours && !si->nr_theirs)
1096 return;
1097
1098 remove_nonexistent_theirs_shallow(si);
4820a33b
NTND
1099 if (!si->nr_ours && !si->nr_theirs)
1100 return;
1101 for (i = 0; i < nr_sought; i++)
910650d2 1102 oid_array_append(&ref, &sought[i]->old_oid);
4820a33b
NTND
1103 si->ref = &ref;
1104
48d25cae
NTND
1105 if (args->update_shallow) {
1106 /*
1107 * remote is also shallow, .git/shallow may be updated
1108 * so all refs can be accepted. Make sure we only add
1109 * shallow roots that are actually reachable from new
1110 * refs.
1111 */
910650d2 1112 struct oid_array extra = OID_ARRAY_INIT;
ee3051bd 1113 struct object_id *oid = si->shallow->oid;
48d25cae
NTND
1114 assign_shallow_commits_to_refs(si, NULL, NULL);
1115 if (!si->nr_ours && !si->nr_theirs) {
910650d2 1116 oid_array_clear(&ref);
48d25cae
NTND
1117 return;
1118 }
1119 for (i = 0; i < si->nr_ours; i++)
910650d2 1120 oid_array_append(&extra, &oid[si->ours[i]]);
48d25cae 1121 for (i = 0; i < si->nr_theirs; i++)
910650d2 1122 oid_array_append(&extra, &oid[si->theirs[i]]);
48d25cae
NTND
1123 setup_alternate_shallow(&shallow_lock,
1124 &alternate_shallow_file,
1125 &extra);
1126 commit_lock_file(&shallow_lock);
910650d2 1127 oid_array_clear(&extra);
1128 oid_array_clear(&ref);
48d25cae
NTND
1129 return;
1130 }
1131
4820a33b
NTND
1132 /*
1133 * remote is also shallow, check what ref is safe to update
1134 * without updating .git/shallow
1135 */
1136 status = xcalloc(nr_sought, sizeof(*status));
1137 assign_shallow_commits_to_refs(si, NULL, status);
1138 if (si->nr_ours || si->nr_theirs) {
1139 for (i = 0; i < nr_sought; i++)
1140 if (status[i])
1141 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
1142 }
1143 free(status);
910650d2 1144 oid_array_clear(&ref);
a796ccee
NTND
1145}
1146
745f7a8c
NTND
1147struct ref *fetch_pack(struct fetch_pack_args *args,
1148 int fd[], struct child_process *conn,
1149 const struct ref *ref,
1150 const char *dest,
f2db854d 1151 struct ref **sought, int nr_sought,
910650d2 1152 struct oid_array *shallow,
745f7a8c
NTND
1153 char **pack_lockfile)
1154{
745f7a8c 1155 struct ref *ref_cpy;
beea4152 1156 struct shallow_info si;
745f7a8c
NTND
1157
1158 fetch_pack_setup();
f2db854d
JH
1159 if (nr_sought)
1160 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
745f7a8c
NTND
1161
1162 if (!ref) {
1163 packet_flush(fd[1]);
1dd73e20 1164 die(_("no matching remote head"));
745f7a8c 1165 }
beea4152
NTND
1166 prepare_shallow_info(&si, shallow);
1167 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1168 &si, pack_lockfile);
745f7a8c 1169 reprepare_packed_git();
4820a33b 1170 update_shallow(args, sought, nr_sought, &si);
beea4152 1171 clear_shallow_info(&si);
745f7a8c
NTND
1172 return ref_cpy;
1173}
e860d96b
MM
1174
1175int report_unmatched_refs(struct ref **sought, int nr_sought)
1176{
1177 int i, ret = 0;
1178
1179 for (i = 0; i < nr_sought; i++) {
d56583de 1180 if (!sought[i])
e860d96b 1181 continue;
d56583de
MM
1182 switch (sought[i]->match_status) {
1183 case REF_MATCHED:
1184 continue;
1185 case REF_NOT_MATCHED:
1186 error(_("no such remote ref %s"), sought[i]->name);
1187 break;
1188 case REF_UNADVERTISED_NOT_ALLOWED:
1189 error(_("Server does not allow request for unadvertised object %s"),
1190 sought[i]->name);
1191 break;
1192 }
e860d96b
MM
1193 ret = 1;
1194 }
1195 return ret;
1196}