df6a8dc2775cb3ec0d2375b3be34217f9342eb48
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
4#include "commit.h"
5#include "tag.h"
6#include "exec_cmd.h"
7#include "pack.h"
8#include "sideband.h"
9#include "fetch-pack.h"
10#include "remote.h"
11#include "run-command.h"
12
13static int transfer_unpack_limit = -1;
14static int fetch_unpack_limit = -1;
15static int unpack_limit = 100;
16static int prefer_ofs_delta = 1;
17static int fetch_fsck_objects;
18static struct fetch_pack_args args = {
19 /* .uploadpack = */ "git-upload-pack",
20};
21
22static const char fetch_pack_usage[] =
23"git fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
24
25#define COMPLETE (1U << 0)
26#define COMMON (1U << 1)
27#define COMMON_REF (1U << 2)
28#define SEEN (1U << 3)
29#define POPPED (1U << 4)
30
31static int marked;
32
33/*
34 * After sending this many "have"s if we do not get any new ACK , we
35 * give up traversing our history.
36 */
37#define MAX_IN_VAIN 256
38
39static struct commit_list *rev_list;
40static int non_common_revs, multi_ack, use_sideband;
41
42static void rev_list_push(struct commit *commit, int mark)
43{
44 if (!(commit->object.flags & mark)) {
45 commit->object.flags |= mark;
46
47 if (!(commit->object.parsed))
48 if (parse_commit(commit))
49 return;
50
51 insert_by_date(commit, &rev_list);
52
53 if (!(commit->object.flags & COMMON))
54 non_common_revs++;
55 }
56}
57
58static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
59{
60 struct object *o = deref_tag(parse_object(sha1), path, 0);
61
62 if (o && o->type == OBJ_COMMIT)
63 rev_list_push((struct commit *)o, SEEN);
64
65 return 0;
66}
67
68static int clear_marks(const char *path, const unsigned char *sha1, int flag, void *cb_data)
69{
70 struct object *o = deref_tag(parse_object(sha1), path, 0);
71
72 if (o && o->type == OBJ_COMMIT)
73 clear_commit_marks((struct commit *)o,
74 COMMON | COMMON_REF | SEEN | POPPED);
75 return 0;
76}
77
78/*
79 This function marks a rev and its ancestors as common.
80 In some cases, it is desirable to mark only the ancestors (for example
81 when only the server does not yet know that they are common).
82*/
83
84static void mark_common(struct commit *commit,
85 int ancestors_only, int dont_parse)
86{
87 if (commit != NULL && !(commit->object.flags & COMMON)) {
88 struct object *o = (struct object *)commit;
89
90 if (!ancestors_only)
91 o->flags |= COMMON;
92
93 if (!(o->flags & SEEN))
94 rev_list_push(commit, SEEN);
95 else {
96 struct commit_list *parents;
97
98 if (!ancestors_only && !(o->flags & POPPED))
99 non_common_revs--;
100 if (!o->parsed && !dont_parse)
101 if (parse_commit(commit))
102 return;
103
104 for (parents = commit->parents;
105 parents;
106 parents = parents->next)
107 mark_common(parents->item, 0, dont_parse);
108 }
109 }
110}
111
112/*
113 Get the next rev to send, ignoring the common.
114*/
115
116static const unsigned char *get_rev(void)
117{
118 struct commit *commit = NULL;
119
120 while (commit == NULL) {
121 unsigned int mark;
122 struct commit_list *parents;
123
124 if (rev_list == NULL || non_common_revs == 0)
125 return NULL;
126
127 commit = rev_list->item;
128 if (!commit->object.parsed)
129 parse_commit(commit);
130 parents = commit->parents;
131
132 commit->object.flags |= POPPED;
133 if (!(commit->object.flags & COMMON))
134 non_common_revs--;
135
136 if (commit->object.flags & COMMON) {
137 /* do not send "have", and ignore ancestors */
138 commit = NULL;
139 mark = COMMON | SEEN;
140 } else if (commit->object.flags & COMMON_REF)
141 /* send "have", and ignore ancestors */
142 mark = COMMON | SEEN;
143 else
144 /* send "have", also for its ancestors */
145 mark = SEEN;
146
147 while (parents) {
148 if (!(parents->item->object.flags & SEEN))
149 rev_list_push(parents->item, mark);
150 if (mark & COMMON)
151 mark_common(parents->item, 1, 0);
152 parents = parents->next;
153 }
154
155 rev_list = rev_list->next;
156 }
157
158 return commit->object.sha1;
159}
160
161enum ack_type {
162 NAK = 0,
163 ACK,
164 ACK_continue,
165 ACK_common,
166 ACK_ready
167};
168
169static void consume_shallow_list(int fd)
170{
171 if (args.stateless_rpc && args.depth > 0) {
172 /* If we sent a depth we will get back "duplicate"
173 * shallow and unshallow commands every time there
174 * is a block of have lines exchanged.
175 */
176 char line[1000];
177 while (packet_read_line(fd, line, sizeof(line))) {
178 if (!prefixcmp(line, "shallow "))
179 continue;
180 if (!prefixcmp(line, "unshallow "))
181 continue;
182 die("git fetch-pack: expected shallow list");
183 }
184 }
185}
186
187static enum ack_type get_ack(int fd, unsigned char *result_sha1)
188{
189 static char line[1000];
190 int len = packet_read_line(fd, line, sizeof(line));
191
192 if (!len)
193 die("git fetch-pack: expected ACK/NAK, got EOF");
194 if (line[len-1] == '\n')
195 line[--len] = 0;
196 if (!strcmp(line, "NAK"))
197 return NAK;
198 if (!prefixcmp(line, "ACK ")) {
199 if (!get_sha1_hex(line+4, result_sha1)) {
200 if (strstr(line+45, "continue"))
201 return ACK_continue;
202 if (strstr(line+45, "common"))
203 return ACK_common;
204 if (strstr(line+45, "ready"))
205 return ACK_ready;
206 return ACK;
207 }
208 }
209 die("git fetch_pack: expected ACK/NAK, got '%s'", line);
210}
211
212static void send_request(int fd, struct strbuf *buf)
213{
214 if (args.stateless_rpc) {
215 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
216 packet_flush(fd);
217 } else
218 safe_write(fd, buf->buf, buf->len);
219}
220
221static int find_common(int fd[2], unsigned char *result_sha1,
222 struct ref *refs)
223{
224 int fetching;
225 int count = 0, flushes = 0, retval;
226 const unsigned char *sha1;
227 unsigned in_vain = 0;
228 int got_continue = 0;
229 struct strbuf req_buf = STRBUF_INIT;
230 size_t state_len = 0;
231
232 if (args.stateless_rpc && multi_ack == 1)
233 die("--stateless-rpc requires multi_ack_detailed");
234 if (marked)
235 for_each_ref(clear_marks, NULL);
236 marked = 1;
237
238 for_each_ref(rev_list_insert_ref, NULL);
239
240 fetching = 0;
241 for ( ; refs ; refs = refs->next) {
242 unsigned char *remote = refs->old_sha1;
243 const char *remote_hex;
244 struct object *o;
245
246 /*
247 * If that object is complete (i.e. it is an ancestor of a
248 * local ref), we tell them we have it but do not have to
249 * tell them about its ancestors, which they already know
250 * about.
251 *
252 * We use lookup_object here because we are only
253 * interested in the case we *know* the object is
254 * reachable and we have already scanned it.
255 */
256 if (((o = lookup_object(remote)) != NULL) &&
257 (o->flags & COMPLETE)) {
258 continue;
259 }
260
261 remote_hex = sha1_to_hex(remote);
262 if (!fetching) {
263 struct strbuf c = STRBUF_INIT;
264 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
265 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
266 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
267 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
268 if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
269 if (args.no_progress) strbuf_addstr(&c, " no-progress");
270 if (args.include_tag) strbuf_addstr(&c, " include-tag");
271 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
272 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
273 strbuf_release(&c);
274 } else
275 packet_buf_write(&req_buf, "want %s\n", remote_hex);
276 fetching++;
277 }
278
279 if (!fetching) {
280 strbuf_release(&req_buf);
281 packet_flush(fd[1]);
282 return 1;
283 }
284
285 if (is_repository_shallow())
286 write_shallow_commits(&req_buf, 1);
287 if (args.depth > 0)
288 packet_buf_write(&req_buf, "deepen %d", args.depth);
289 packet_buf_flush(&req_buf);
290 state_len = req_buf.len;
291
292 if (args.depth > 0) {
293 char line[1024];
294 unsigned char sha1[20];
295
296 send_request(fd[1], &req_buf);
297 while (packet_read_line(fd[0], line, sizeof(line))) {
298 if (!prefixcmp(line, "shallow ")) {
299 if (get_sha1_hex(line + 8, sha1))
300 die("invalid shallow line: %s", line);
301 register_shallow(sha1);
302 continue;
303 }
304 if (!prefixcmp(line, "unshallow ")) {
305 if (get_sha1_hex(line + 10, sha1))
306 die("invalid unshallow line: %s", line);
307 if (!lookup_object(sha1))
308 die("object not found: %s", line);
309 /* make sure that it is parsed as shallow */
310 if (!parse_object(sha1))
311 die("error in object: %s", line);
312 if (unregister_shallow(sha1))
313 die("no shallow found: %s", line);
314 continue;
315 }
316 die("expected shallow/unshallow, got %s", line);
317 }
318 } else if (!args.stateless_rpc)
319 send_request(fd[1], &req_buf);
320
321 if (!args.stateless_rpc) {
322 /* If we aren't using the stateless-rpc interface
323 * we don't need to retain the headers.
324 */
325 strbuf_setlen(&req_buf, 0);
326 state_len = 0;
327 }
328
329 flushes = 0;
330 retval = -1;
331 while ((sha1 = get_rev())) {
332 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
333 if (args.verbose)
334 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
335 in_vain++;
336 if (!(31 & ++count)) {
337 int ack;
338
339 packet_buf_flush(&req_buf);
340 send_request(fd[1], &req_buf);
341 strbuf_setlen(&req_buf, state_len);
342 flushes++;
343
344 /*
345 * We keep one window "ahead" of the other side, and
346 * will wait for an ACK only on the next one
347 */
348 if (!args.stateless_rpc && count == 32)
349 continue;
350
351 consume_shallow_list(fd[0]);
352 do {
353 ack = get_ack(fd[0], result_sha1);
354 if (args.verbose && ack)
355 fprintf(stderr, "got ack %d %s\n", ack,
356 sha1_to_hex(result_sha1));
357 switch (ack) {
358 case ACK:
359 flushes = 0;
360 multi_ack = 0;
361 retval = 0;
362 goto done;
363 case ACK_common:
364 case ACK_ready:
365 case ACK_continue: {
366 struct commit *commit =
367 lookup_commit(result_sha1);
368 if (args.stateless_rpc
369 && ack == ACK_common
370 && !(commit->object.flags & COMMON)) {
371 /* We need to replay the have for this object
372 * on the next RPC request so the peer knows
373 * it is in common with us.
374 */
375 const char *hex = sha1_to_hex(result_sha1);
376 packet_buf_write(&req_buf, "have %s\n", hex);
377 state_len = req_buf.len;
378 }
379 mark_common(commit, 0, 1);
380 retval = 0;
381 in_vain = 0;
382 got_continue = 1;
383 break;
384 }
385 }
386 } while (ack);
387 flushes--;
388 if (got_continue && MAX_IN_VAIN < in_vain) {
389 if (args.verbose)
390 fprintf(stderr, "giving up\n");
391 break; /* give up */
392 }
393 }
394 }
395done:
396 packet_buf_write(&req_buf, "done\n");
397 send_request(fd[1], &req_buf);
398 if (args.verbose)
399 fprintf(stderr, "done\n");
400 if (retval != 0) {
401 multi_ack = 0;
402 flushes++;
403 }
404 strbuf_release(&req_buf);
405
406 consume_shallow_list(fd[0]);
407 while (flushes || multi_ack) {
408 int ack = get_ack(fd[0], result_sha1);
409 if (ack) {
410 if (args.verbose)
411 fprintf(stderr, "got ack (%d) %s\n", ack,
412 sha1_to_hex(result_sha1));
413 if (ack == ACK)
414 return 0;
415 multi_ack = 1;
416 continue;
417 }
418 flushes--;
419 }
420 /* it is no error to fetch into a completely empty repo */
421 return count ? retval : 0;
422}
423
424static struct commit_list *complete;
425
426static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
427{
428 struct object *o = parse_object(sha1);
429
430 while (o && o->type == OBJ_TAG) {
431 struct tag *t = (struct tag *) o;
432 if (!t->tagged)
433 break; /* broken repository */
434 o->flags |= COMPLETE;
435 o = parse_object(t->tagged->sha1);
436 }
437 if (o && o->type == OBJ_COMMIT) {
438 struct commit *commit = (struct commit *)o;
439 commit->object.flags |= COMPLETE;
440 insert_by_date(commit, &complete);
441 }
442 return 0;
443}
444
445static void mark_recent_complete_commits(unsigned long cutoff)
446{
447 while (complete && cutoff <= complete->item->date) {
448 if (args.verbose)
449 fprintf(stderr, "Marking %s as complete\n",
450 sha1_to_hex(complete->item->object.sha1));
451 pop_most_recent_commit(&complete, COMPLETE);
452 }
453}
454
455static void filter_refs(struct ref **refs, int nr_match, char **match)
456{
457 struct ref **return_refs;
458 struct ref *newlist = NULL;
459 struct ref **newtail = &newlist;
460 struct ref *ref, *next;
461 struct ref *fastarray[32];
462
463 if (nr_match && !args.fetch_all) {
464 if (ARRAY_SIZE(fastarray) < nr_match)
465 return_refs = xcalloc(nr_match, sizeof(struct ref *));
466 else {
467 return_refs = fastarray;
468 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
469 }
470 }
471 else
472 return_refs = NULL;
473
474 for (ref = *refs; ref; ref = next) {
475 next = ref->next;
476 if (!memcmp(ref->name, "refs/", 5) &&
477 check_ref_format(ref->name + 5))
478 ; /* trash */
479 else if (args.fetch_all &&
480 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
481 *newtail = ref;
482 ref->next = NULL;
483 newtail = &ref->next;
484 continue;
485 }
486 else {
487 int order = path_match(ref->name, nr_match, match);
488 if (order) {
489 return_refs[order-1] = ref;
490 continue; /* we will link it later */
491 }
492 }
493 free(ref);
494 }
495
496 if (!args.fetch_all) {
497 int i;
498 for (i = 0; i < nr_match; i++) {
499 ref = return_refs[i];
500 if (ref) {
501 *newtail = ref;
502 ref->next = NULL;
503 newtail = &ref->next;
504 }
505 }
506 if (return_refs != fastarray)
507 free(return_refs);
508 }
509 *refs = newlist;
510}
511
512static int everything_local(struct ref **refs, int nr_match, char **match)
513{
514 struct ref *ref;
515 int retval;
516 unsigned long cutoff = 0;
517
518 save_commit_buffer = 0;
519
520 for (ref = *refs; ref; ref = ref->next) {
521 struct object *o;
522
523 o = parse_object(ref->old_sha1);
524 if (!o)
525 continue;
526
527 /* We already have it -- which may mean that we were
528 * in sync with the other side at some time after
529 * that (it is OK if we guess wrong here).
530 */
531 if (o->type == OBJ_COMMIT) {
532 struct commit *commit = (struct commit *)o;
533 if (!cutoff || cutoff < commit->date)
534 cutoff = commit->date;
535 }
536 }
537
538 if (!args.depth) {
539 for_each_ref(mark_complete, NULL);
540 if (cutoff)
541 mark_recent_complete_commits(cutoff);
542 }
543
544 /*
545 * Mark all complete remote refs as common refs.
546 * Don't mark them common yet; the server has to be told so first.
547 */
548 for (ref = *refs; ref; ref = ref->next) {
549 struct object *o = deref_tag(lookup_object(ref->old_sha1),
550 NULL, 0);
551
552 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
553 continue;
554
555 if (!(o->flags & SEEN)) {
556 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
557
558 mark_common((struct commit *)o, 1, 1);
559 }
560 }
561
562 filter_refs(refs, nr_match, match);
563
564 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
565 const unsigned char *remote = ref->old_sha1;
566 unsigned char local[20];
567 struct object *o;
568
569 o = lookup_object(remote);
570 if (!o || !(o->flags & COMPLETE)) {
571 retval = 0;
572 if (!args.verbose)
573 continue;
574 fprintf(stderr,
575 "want %s (%s)\n", sha1_to_hex(remote),
576 ref->name);
577 continue;
578 }
579
580 hashcpy(ref->new_sha1, local);
581 if (!args.verbose)
582 continue;
583 fprintf(stderr,
584 "already have %s (%s)\n", sha1_to_hex(remote),
585 ref->name);
586 }
587 return retval;
588}
589
590static int sideband_demux(int in, int out, void *data)
591{
592 int *xd = data;
593
594 int ret = recv_sideband("fetch-pack", xd[0], out);
595 close(out);
596 return ret;
597}
598
599static int get_pack(int xd[2], char **pack_lockfile)
600{
601 struct async demux;
602 const char *argv[20];
603 char keep_arg[256];
604 char hdr_arg[256];
605 const char **av;
606 int do_keep = args.keep_pack;
607 struct child_process cmd;
608
609 memset(&demux, 0, sizeof(demux));
610 if (use_sideband) {
611 /* xd[] is talking with upload-pack; subprocess reads from
612 * xd[0], spits out band#2 to stderr, and feeds us band#1
613 * through demux->out.
614 */
615 demux.proc = sideband_demux;
616 demux.data = xd;
617 demux.out = -1;
618 if (start_async(&demux))
619 die("fetch-pack: unable to fork off sideband"
620 " demultiplexer");
621 }
622 else
623 demux.out = xd[0];
624
625 memset(&cmd, 0, sizeof(cmd));
626 cmd.argv = argv;
627 av = argv;
628 *hdr_arg = 0;
629 if (!args.keep_pack && unpack_limit) {
630 struct pack_header header;
631
632 if (read_pack_header(demux.out, &header))
633 die("protocol error: bad pack header");
634 snprintf(hdr_arg, sizeof(hdr_arg),
635 "--pack_header=%"PRIu32",%"PRIu32,
636 ntohl(header.hdr_version), ntohl(header.hdr_entries));
637 if (ntohl(header.hdr_entries) < unpack_limit)
638 do_keep = 0;
639 else
640 do_keep = 1;
641 }
642
643 if (do_keep) {
644 if (pack_lockfile)
645 cmd.out = -1;
646 *av++ = "index-pack";
647 *av++ = "--stdin";
648 if (!args.quiet && !args.no_progress)
649 *av++ = "-v";
650 if (args.use_thin_pack)
651 *av++ = "--fix-thin";
652 if (args.lock_pack || unpack_limit) {
653 int s = sprintf(keep_arg,
654 "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
655 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
656 strcpy(keep_arg + s, "localhost");
657 *av++ = keep_arg;
658 }
659 }
660 else {
661 *av++ = "unpack-objects";
662 if (args.quiet)
663 *av++ = "-q";
664 }
665 if (*hdr_arg)
666 *av++ = hdr_arg;
667 if (fetch_fsck_objects)
668 *av++ = "--strict";
669 *av++ = NULL;
670
671 cmd.in = demux.out;
672 cmd.git_cmd = 1;
673 if (start_command(&cmd))
674 die("fetch-pack: unable to fork off %s", argv[0]);
675 if (do_keep && pack_lockfile) {
676 *pack_lockfile = index_pack_lockfile(cmd.out);
677 close(cmd.out);
678 }
679
680 if (finish_command(&cmd))
681 die("%s failed", argv[0]);
682 if (use_sideband && finish_async(&demux))
683 die("error in sideband demultiplexer");
684 return 0;
685}
686
687static struct ref *do_fetch_pack(int fd[2],
688 const struct ref *orig_ref,
689 int nr_match,
690 char **match,
691 char **pack_lockfile)
692{
693 struct ref *ref = copy_ref_list(orig_ref);
694 unsigned char sha1[20];
695
696 if (is_repository_shallow() && !server_supports("shallow"))
697 die("Server does not support shallow clients");
698 if (server_supports("multi_ack_detailed")) {
699 if (args.verbose)
700 fprintf(stderr, "Server supports multi_ack_detailed\n");
701 multi_ack = 2;
702 }
703 else if (server_supports("multi_ack")) {
704 if (args.verbose)
705 fprintf(stderr, "Server supports multi_ack\n");
706 multi_ack = 1;
707 }
708 if (server_supports("side-band-64k")) {
709 if (args.verbose)
710 fprintf(stderr, "Server supports side-band-64k\n");
711 use_sideband = 2;
712 }
713 else if (server_supports("side-band")) {
714 if (args.verbose)
715 fprintf(stderr, "Server supports side-band\n");
716 use_sideband = 1;
717 }
718 if (server_supports("ofs-delta")) {
719 if (args.verbose)
720 fprintf(stderr, "Server supports ofs-delta\n");
721 } else
722 prefer_ofs_delta = 0;
723 if (everything_local(&ref, nr_match, match)) {
724 packet_flush(fd[1]);
725 goto all_done;
726 }
727 if (find_common(fd, sha1, ref) < 0)
728 if (!args.keep_pack)
729 /* When cloning, it is not unusual to have
730 * no common commit.
731 */
732 warning("no common commits");
733
734 if (args.stateless_rpc)
735 packet_flush(fd[1]);
736 if (get_pack(fd, pack_lockfile))
737 die("git fetch-pack: fetch failed.");
738
739 all_done:
740 return ref;
741}
742
743static int remove_duplicates(int nr_heads, char **heads)
744{
745 int src, dst;
746
747 for (src = dst = 0; src < nr_heads; src++) {
748 /* If heads[src] is different from any of
749 * heads[0..dst], push it in.
750 */
751 int i;
752 for (i = 0; i < dst; i++) {
753 if (!strcmp(heads[i], heads[src]))
754 break;
755 }
756 if (i < dst)
757 continue;
758 if (src != dst)
759 heads[dst] = heads[src];
760 dst++;
761 }
762 return dst;
763}
764
765static int fetch_pack_config(const char *var, const char *value, void *cb)
766{
767 if (strcmp(var, "fetch.unpacklimit") == 0) {
768 fetch_unpack_limit = git_config_int(var, value);
769 return 0;
770 }
771
772 if (strcmp(var, "transfer.unpacklimit") == 0) {
773 transfer_unpack_limit = git_config_int(var, value);
774 return 0;
775 }
776
777 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
778 prefer_ofs_delta = git_config_bool(var, value);
779 return 0;
780 }
781
782 if (!strcmp(var, "fetch.fsckobjects")) {
783 fetch_fsck_objects = git_config_bool(var, value);
784 return 0;
785 }
786
787 return git_default_config(var, value, cb);
788}
789
790static struct lock_file lock;
791
792static void fetch_pack_setup(void)
793{
794 static int did_setup;
795 if (did_setup)
796 return;
797 git_config(fetch_pack_config, NULL);
798 if (0 <= transfer_unpack_limit)
799 unpack_limit = transfer_unpack_limit;
800 else if (0 <= fetch_unpack_limit)
801 unpack_limit = fetch_unpack_limit;
802 did_setup = 1;
803}
804
805int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
806{
807 int i, ret, nr_heads;
808 struct ref *ref = NULL;
809 char *dest = NULL, **heads;
810 int fd[2];
811 char *pack_lockfile = NULL;
812 char **pack_lockfile_ptr = NULL;
813 struct child_process *conn;
814
815 nr_heads = 0;
816 heads = NULL;
817 for (i = 1; i < argc; i++) {
818 const char *arg = argv[i];
819
820 if (*arg == '-') {
821 if (!prefixcmp(arg, "--upload-pack=")) {
822 args.uploadpack = arg + 14;
823 continue;
824 }
825 if (!prefixcmp(arg, "--exec=")) {
826 args.uploadpack = arg + 7;
827 continue;
828 }
829 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
830 args.quiet = 1;
831 continue;
832 }
833 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
834 args.lock_pack = args.keep_pack;
835 args.keep_pack = 1;
836 continue;
837 }
838 if (!strcmp("--thin", arg)) {
839 args.use_thin_pack = 1;
840 continue;
841 }
842 if (!strcmp("--include-tag", arg)) {
843 args.include_tag = 1;
844 continue;
845 }
846 if (!strcmp("--all", arg)) {
847 args.fetch_all = 1;
848 continue;
849 }
850 if (!strcmp("-v", arg)) {
851 args.verbose = 1;
852 continue;
853 }
854 if (!prefixcmp(arg, "--depth=")) {
855 args.depth = strtol(arg + 8, NULL, 0);
856 continue;
857 }
858 if (!strcmp("--no-progress", arg)) {
859 args.no_progress = 1;
860 continue;
861 }
862 if (!strcmp("--stateless-rpc", arg)) {
863 args.stateless_rpc = 1;
864 continue;
865 }
866 if (!strcmp("--lock-pack", arg)) {
867 args.lock_pack = 1;
868 pack_lockfile_ptr = &pack_lockfile;
869 continue;
870 }
871 usage(fetch_pack_usage);
872 }
873 dest = (char *)arg;
874 heads = (char **)(argv + i + 1);
875 nr_heads = argc - i - 1;
876 break;
877 }
878 if (!dest)
879 usage(fetch_pack_usage);
880
881 if (args.stateless_rpc) {
882 conn = NULL;
883 fd[0] = 0;
884 fd[1] = 1;
885 } else {
886 conn = git_connect(fd, (char *)dest, args.uploadpack,
887 args.verbose ? CONNECT_VERBOSE : 0);
888 }
889
890 get_remote_heads(fd[0], &ref, 0, NULL, 0, NULL);
891
892 ref = fetch_pack(&args, fd, conn, ref, dest,
893 nr_heads, heads, pack_lockfile_ptr);
894 if (pack_lockfile) {
895 printf("lock %s\n", pack_lockfile);
896 fflush(stdout);
897 }
898 close(fd[0]);
899 close(fd[1]);
900 if (finish_connect(conn))
901 ref = NULL;
902 ret = !ref;
903
904 if (!ret && nr_heads) {
905 /* If the heads to pull were given, we should have
906 * consumed all of them by matching the remote.
907 * Otherwise, 'git fetch remote no-such-ref' would
908 * silently succeed without issuing an error.
909 */
910 for (i = 0; i < nr_heads; i++)
911 if (heads[i] && heads[i][0]) {
912 error("no such remote ref %s", heads[i]);
913 ret = 1;
914 }
915 }
916 while (ref) {
917 printf("%s %s\n",
918 sha1_to_hex(ref->old_sha1), ref->name);
919 ref = ref->next;
920 }
921
922 return ret;
923}
924
925struct ref *fetch_pack(struct fetch_pack_args *my_args,
926 int fd[], struct child_process *conn,
927 const struct ref *ref,
928 const char *dest,
929 int nr_heads,
930 char **heads,
931 char **pack_lockfile)
932{
933 struct stat st;
934 struct ref *ref_cpy;
935
936 fetch_pack_setup();
937 if (&args != my_args)
938 memcpy(&args, my_args, sizeof(args));
939 if (args.depth > 0) {
940 if (stat(git_path("shallow"), &st))
941 st.st_mtime = 0;
942 }
943
944 if (heads && nr_heads)
945 nr_heads = remove_duplicates(nr_heads, heads);
946 if (!ref) {
947 packet_flush(fd[1]);
948 die("no matching remote head");
949 }
950 ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);
951
952 if (args.depth > 0) {
953 struct cache_time mtime;
954 struct strbuf sb = STRBUF_INIT;
955 char *shallow = git_path("shallow");
956 int fd;
957
958 mtime.sec = st.st_mtime;
959 mtime.nsec = ST_MTIME_NSEC(st);
960 if (stat(shallow, &st)) {
961 if (mtime.sec)
962 die("shallow file was removed during fetch");
963 } else if (st.st_mtime != mtime.sec
964#ifdef USE_NSEC
965 || ST_MTIME_NSEC(st) != mtime.nsec
966#endif
967 )
968 die("shallow file was changed during fetch");
969
970 fd = hold_lock_file_for_update(&lock, shallow,
971 LOCK_DIE_ON_ERROR);
972 if (!write_shallow_commits(&sb, 0)
973 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
974 unlink_or_warn(shallow);
975 rollback_lock_file(&lock);
976 } else {
977 commit_lock_file(&lock);
978 }
979 strbuf_release(&sb);
980 }
981
982 reprepare_packed_git();
983 return ref_cpy;
984}