]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-clone
new public-inbox-{clone,fetch} commands
[public-inbox.git] / script / public-inbox-clone
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Wrapper to git clone remote public-inboxes
5 use strict;
6 use v5.10.1;
7 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
8 my $opt = {};
9 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
10 usage: public-inbox-clone INBOX_URL [DESTINATION]
11
12   clone remote public-inboxes
13
14 options:
15
16   --torsocks VAL      whether or not to wrap git and curl commands with
17                       torsocks (default: `auto')
18                       Must be one of: `auto', `no' or `yes'
19   --verbose | -v      increase verbosity (may be repeated)
20     --quiet | -q      increase verbosity (may be repeated)
21     -C DIR            chdir to specified directory
22 EOF
23 GetOptions($opt, qw(help|h quiet|q verbose|v+ C=s@ c=s@
24                 no-torsocks torsocks=s)) or die $help;
25 if ($opt->{help}) { print $help; exit };
26 require PublicInbox::Admin; # loads Config
27 PublicInbox::Admin::do_chdir(delete $opt->{C});
28 PublicInbox::Admin::setup_signals();
29 $SIG{PIPE} = 'IGNORE';
30
31 my ($url, $dst, $extra) = @ARGV;
32 die $help if !defined($url) || defined($extra);
33 defined($dst) or ($dst) = ($url =~ m!/([^/]+)/?\z!);
34 index($dst, "\n") >= 0 and die "`\\n' not allowed in `$dst'";
35
36 # n.b. this is still a truckload of code...
37 require URI;
38 require PublicInbox::LEI;
39 require PublicInbox::LeiExternal;
40 require PublicInbox::LeiMirror;
41 require PublicInbox::LeiCurl;
42 require PublicInbox::Lock;
43
44 $url = PublicInbox::LeiExternal::ext_canonicalize($url);
45 my $lei = bless {
46         env => \%ENV, opt => $opt, cmd => 'public-inbox-clone',
47         0 => *STDIN{GLOB}, 2 => *STDERR{GLOB},
48 }, 'PublicInbox::LEI';
49 open $lei->{1}, '+<&=', 1 or die "dup: $!";
50 open $lei->{3}, '.' or die "open . $!";
51 my $mrr = bless {
52         lei => $lei,
53         src => $url,
54         dst => $dst,
55 }, 'PublicInbox::LeiMirror';
56 $mrr->do_mirror;
57 $mrr->can('do_finish_mirror')->([$mrr, $lei], $$);
58 exit(($lei->{child_error} // 0) >> 8);