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