]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
No ext_urls
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <http://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
8 usage: public-inbox-convert [options] OLD NEW
9
10   convert v1 format inboxes to v2
11
12 options:
13
14   --no-index          do not index after conversion
15   --jobs=NUM          set shards (NUM=0)
16   --verbose | -v      increase verbosity (may be repeated)
17
18 index options (see public-inbox-index(1) man page for full description):
19
20   --no-fsync          speed up indexing, risk corruption on power outage
21   -L LEVEL            `basic', `medium', or `full' (default: full)
22   --compact | -c      run public-inbox-compact(1) after indexing
23   --sequential-shard  index Xapian shards sequentially for slow storage
24   --batch-size=BYTES  flush changes to OS after a given number of bytes
25   --max-size=BYTES    do not index messages larger than the given size
26
27 See public-inbox-convert(1) man page for full documentation.
28 EOF
29
30 my $opt = {
31         index => 1,
32         # index defaults:
33         quiet => -1, compact => 0, maxsize => undef, fsync => 1,
34         reindex => 1, # we always reindex
35 };
36 GetOptions($opt, qw(jobs|j=i index! help|h C=s@),
37                 # index options
38                 qw(verbose|v+ rethread compact|c+ fsync|sync!
39                 indexlevel|index-level|L=s max_size|max-size=s
40                 batch_size|batch-size=s
41                 sequential-shard|seq-shard
42                 )) or die $help;
43 if ($opt->{help}) { print $help; exit 0 };
44 require PublicInbox::Admin;
45 PublicInbox::Admin::do_chdir(delete $opt->{C});
46 my $old_dir = shift(@ARGV) // '';
47 my $new_dir = shift(@ARGV) // '';
48 die $help if (scalar(@ARGV) || $new_dir eq '' || $old_dir eq '');
49 die "$new_dir exists\n" if -d $new_dir;
50 die "$old_dir not a directory\n" unless -d $old_dir;
51
52 require PublicInbox::Config;
53 require PublicInbox::InboxWritable;
54
55 my $cfg = PublicInbox::Config->new;
56 my @old = PublicInbox::Admin::resolve_inboxes([$old_dir], undef, $cfg);
57 @old > 1 and die "BUG: resolved several inboxes from $old_dir:\n",
58                 map { "\t$_->{inboxdir}\n" } @old;
59 my $old = PublicInbox::InboxWritable->new($old[0]);
60 if (delete $old->{-unconfigured}) {
61         warn "W: $old_dir not configured in " .
62                 PublicInbox::Config::default_file() . "\n";
63 }
64 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
65
66 my $detected = PublicInbox::Admin::detect_indexlevel($old);
67 $old->{indexlevel} //= $detected;
68 my $env;
69 if ($opt->{'index'}) {
70         my $mods = {};
71         PublicInbox::Admin::scan_ibx_modules($mods, $old);
72         PublicInbox::Admin::require_or_die(keys %$mods);
73         PublicInbox::Admin::progress_prepare($opt);
74         $env = PublicInbox::Admin::index_prepare($opt, $cfg);
75 }
76 local %ENV = (%$env, %ENV) if $env;
77 my $new = { %$old };
78 $new->{inboxdir} = $cfg->rel2abs_collapsed($new_dir);
79 $new->{version} = 2;
80 $new = PublicInbox::InboxWritable->new($new, { nproc => $opt->{jobs} });
81 $new->{-no_fsync} = 1 if !$opt->{fsync};
82 my $v2w;
83
84 sub link_or_copy ($$) {
85         my ($src, $dst) = @_;
86         link($src, $dst) and return;
87         $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
88         require File::Copy; # preserves permissions:
89         File::Copy::cp($src, $dst) or die "cp $src, $dst failed: $!\n";
90 }
91
92 $old->with_umask(sub {
93         my $old_cfg = "$old->{inboxdir}/config";
94         local $ENV{GIT_CONFIG} = $old_cfg;
95         my $new_cfg = "$new->{inboxdir}/all.git/config";
96         $v2w = $new->importer(1);
97         $v2w->init_inbox(delete $opt->{jobs});
98         unlink $new_cfg;
99         link_or_copy($old_cfg, $new_cfg);
100         if (my $alt = $new->{altid}) {
101                 require PublicInbox::AltId;
102                 foreach my $i (0..$#$alt) {
103                         my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
104                         $src = $src->mm_alt or next;
105                         $src = $src->{dbh}->sqlite_db_filename;
106                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
107                         $dst->mm_alt->{dbh}->sqlite_backup_from_file($src);
108                 }
109         }
110         my $desc = "$old->{inboxdir}/description";
111         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
112         my $clone = "$old->{inboxdir}/cloneurl";
113         if (-e $clone) {
114                 warn <<"";
115 $clone may not be valid after migrating to v2, not copying
116
117         }
118 });
119 my $state = '';
120 my $head = $old->{ref_head} || 'HEAD';
121 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
122 $v2w->idx_init($opt);
123 my $im = $v2w->importer;
124 my ($r, $w) = $im->gfi_start;
125 my $h = '[0-9a-f]';
126 my %D;
127 my $last;
128 while (<$rd>) {
129         if ($_ eq "blob\n") {
130                 $state = 'blob';
131         } elsif (/^commit /) {
132                 $state = 'commit';
133         } elsif (/^data ([0-9]+)/) {
134                 my $len = $1;
135                 print $w $_ or $im->wfail;
136                 while ($len) {
137                         my $n = read($rd, my $tmp, $len) or die "read: $!";
138                         warn "$n != $len\n" if $n != $len;
139                         $len -= $n;
140                         print $w $tmp or $im->wfail;
141                 }
142                 next;
143         } elsif ($state eq 'commit') {
144                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
145                         my ($mark, $path) = ($1, $2);
146                         $D{$path} = $mark;
147                         if ($last && $last ne 'm') {
148                                 print $w "D $last\n" or $im->wfail;
149                         }
150                         print $w "M 100644 :$mark m\n" or $im->wfail;
151                         $last = 'm';
152                         next;
153                 }
154                 if (m{^D (${h}{2}/${h}{38})}o) {
155                         my $mark = delete $D{$1};
156                         defined $mark or die "undeleted path: $1\n";
157                         if ($last && $last ne 'd') {
158                                 print $w "D $last\n" or $im->wfail;
159                         }
160                         print $w "M 100644 :$mark d\n" or $im->wfail;
161                         $last = 'd';
162                         next;
163                 }
164         }
165         last if $_ eq "done\n";
166         print $w $_ or $im->wfail;
167 }
168 close $rd or die "close fast-export: $!\n";
169 waitpid($pid, 0) or die "waitpid failed: $!\n";
170 $? == 0 or die "fast-export failed: $?\n";
171 $r = $w = undef; # v2w->done does the actual close and error checking
172 $v2w->done;
173 if (my $old_mm = $old->mm) {
174         $old->cleanup;
175         $old_mm = $old_mm->{dbh}->sqlite_db_filename;
176
177         # we want to trigger a reindex, not a from scratch index if
178         # we're reusing the msgmap from an existing v1 installation.
179         $v2w->idx_init($opt);
180         $v2w->{mm}->{dbh}->sqlite_backup_from_file($old_mm);
181
182         my $epoch0 = PublicInbox::Git->new($v2w->{mg}->add_epoch(0));
183         chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
184         $v2w->last_epoch_commit(0, $cmt);
185 }
186 $v2w->index_sync($opt) if delete $opt->{'index'};
187 $v2w->done;