2 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <http://www.gnu.org/licenses/agpl-3.0.txt>
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 my $usage = 'Usage: public-inbox-convert [options] OLD NEW';
8 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
11 convert v1 format inboxes to v2
15 --no-index do not index after conversion
16 --jobs=NUM set shards (NUM=0)
17 --verbose | -v increase verbosity (may be repeated)
18 --help | -? show this help
20 index options (see public-inbox-index(1) manpage for full description):
22 --no-fsync speed up indexing, risk corruption on power outage
23 -L LEVEL `basic', `medium', or `full' (default: full)
24 --compact | -c run public-inbox-compact(1) after indexing
25 --sequential-shard index Xapian shards sequentially for slow storage
26 --batch-size=BYTES flush changes to OS after a given number of bytes
27 --max-size=BYTES do not index messages larger than the given size
29 See public-inbox-convert(1) man page for full documentation.
35 quiet => -1, compact => 0, maxsize => undef, fsync => 1,
36 reindex => 1, # we always reindex
38 GetOptions($opt, qw(jobs|j=i index! help|?),
40 qw(verbose|v+ rethread compact|c+ fsync|sync!
41 indexlevel|index-level|L=s max_size|max-size=s
42 batch_size|batch-size=s
43 sequential_shard|sequential-shard|seq-shard
45 bad command-line args\n$usage
47 if ($opt->{help}) { print $help; exit 0 };
48 my $old_dir = shift(@ARGV) or die $usage;
49 my $new_dir = shift(@ARGV) or die $usage;
50 die "$new_dir exists\n" if -d $new_dir;
51 die "$old_dir not a directory\n" unless -d $old_dir;
54 Cwd->import('abs_path');
55 require PublicInbox::Config;
56 require PublicInbox::InboxWritable;
58 $old_dir = abs_path($old_dir);
59 my $cfg = PublicInbox::Config->new;
61 $cfg->each_inbox(sub {
62 $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
65 $old = PublicInbox::InboxWritable->new($old);
67 warn "W: $old_dir not configured in " .
68 PublicInbox::Config::default_file() . "\n";
69 $old = PublicInbox::InboxWritable->new({
72 -primary_address => 'old@example.com',
73 address => [ 'old@example.com' ],
76 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
78 require PublicInbox::Admin;
79 $old->{indexlevel} //= PublicInbox::Admin::detect_indexlevel($old);
81 if ($opt->{'index'}) {
83 PublicInbox::Admin::scan_ibx_modules($mods, $old);
84 PublicInbox::Admin::require_or_die(keys %$mods);
85 PublicInbox::Admin::progress_prepare($opt);
86 $env = PublicInbox::Admin::index_prepare($opt, $cfg);
88 local %ENV = (%$env, %ENV) if $env;
90 $new->{inboxdir} = abs_path($new_dir);
92 $new = PublicInbox::InboxWritable->new($new, { nproc => $opt->{jobs} });
93 $new->{-no_fsync} = 1 if !$opt->{fsync};
97 sub link_or_copy ($$) {
99 link($src, $dst) and return;
100 $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
101 require File::Copy; # preserves permissions:
102 File::Copy::cp($src, $dst) or die "cp $src, $dst failed: $!\n";
105 $old->with_umask(sub {
106 my $old_cfg = "$old->{inboxdir}/config";
107 local $ENV{GIT_CONFIG} = $old_cfg;
108 my $new_cfg = "$new->{inboxdir}/all.git/config";
109 $v2w = $new->importer(1);
110 $v2w->init_inbox(delete $opt->{jobs});
112 link_or_copy($old_cfg, $new_cfg);
113 if (my $alt = $new->{altid}) {
114 require PublicInbox::AltId;
115 foreach my $i (0..$#$alt) {
116 my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
117 $src->mm_alt or next;
118 my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
119 $dst = $dst->{filename};
120 $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
123 my $desc = "$old->{inboxdir}/description";
124 link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
125 my $clone = "$old->{inboxdir}/cloneurl";
128 $clone may not be valid after migrating to v2, not copying
133 my $head = $old->{ref_head} || 'HEAD';
134 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
135 $v2w->idx_init($opt);
136 my $im = $v2w->importer;
137 my ($r, $w) = $im->gfi_start;
142 if ($_ eq "blob\n") {
144 } elsif (/^commit /) {
146 } elsif (/^data ([0-9]+)/) {
148 print $w $_ or $im->wfail;
150 my $n = read($rd, my $tmp, $len) or die "read: $!";
151 warn "$n != $len\n" if $n != $len;
153 print $w $tmp or $im->wfail;
156 } elsif ($state eq 'commit') {
157 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
158 my ($mark, $path) = ($1, $2);
160 if ($last && $last ne 'm') {
161 print $w "D $last\n" or $im->wfail;
163 print $w "M 100644 :$mark m\n" or $im->wfail;
167 if (m{^D (${h}{2}/${h}{38})}o) {
168 my $mark = delete $D{$1};
169 defined $mark or die "undeleted path: $1\n";
170 if ($last && $last ne 'd') {
171 print $w "D $last\n" or $im->wfail;
173 print $w "M 100644 :$mark d\n" or $im->wfail;
178 last if $_ eq "done\n";
179 print $w $_ or $im->wfail;
181 close $rd or die "close fast-export: $!\n";
182 waitpid($pid, 0) or die "waitpid failed: $!\n";
183 $? == 0 or die "fast-export failed: $?\n";
184 $r = $w = undef; # v2w->done does the actual close and error checking
186 if (my $mm = $old->mm) {
188 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3");
190 # we want to trigger a reindex, not a from scratch index if
191 # we're reusing the msgmap from an existing v1 installation.
192 $v2w->idx_init($opt);
193 my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
194 chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
195 $v2w->last_epoch_commit(0, $cmt);
197 $v2w->index_sync($opt) if delete $opt->{'index'};