]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
convert: describe the release of fast-import pipes
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
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>
4 use strict;
5 use warnings;
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 use PublicInbox::InboxWritable;
8 use PublicInbox::Config;
9 use PublicInbox::Admin;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Git;
12 use PublicInbox::Spawn qw(spawn);
13 use Cwd 'abs_path';
14 use File::Copy 'cp'; # preserves permissions:
15 my $usage = "Usage: public-inbox-convert OLD NEW\n";
16 my $jobs;
17 my $index = 1;
18 my %opts = (
19         '--jobs|j=i' => \$jobs,
20         '--index!' => \$index,
21 );
22 GetOptions(%opts) or die "bad command-line args\n$usage";
23 my $old_dir = shift(@ARGV) or die $usage;
24 my $new_dir = shift(@ARGV) or die $usage;
25 die "$new_dir exists\n" if -d $new_dir;
26 die "$old_dir not a directory\n" unless -d $old_dir;
27 my $config = PublicInbox::Config->new;
28 $old_dir = abs_path($old_dir);
29 my $old;
30 if ($config) {
31         $config->each_inbox(sub {
32                 $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
33         });
34 }
35 unless ($old) {
36         warn "W: $old_dir not configured in " .
37                 PublicInbox::Config::default_file() . "\n";
38         $old = {
39                 inboxdir => $old_dir,
40                 name => 'ignored',
41                 address => [ 'old@example.com' ],
42         };
43         $old = PublicInbox::Inbox->new($old);
44 }
45 $old = PublicInbox::InboxWritable->new($old);
46 if ($old->version >= 2) {
47         die "Only conversion from v1 inboxes is supported\n";
48 }
49
50 $old->{indexlevel} //= PublicInbox::Admin::detect_indexlevel($old);
51 if ($index) {
52         my $mods = {};
53         PublicInbox::Admin::scan_ibx_modules($mods, $old);
54         PublicInbox::Admin::require_or_die(keys %$mods);
55 }
56
57 my $new = { %$old };
58 $new->{inboxdir} = abs_path($new_dir);
59 $new->{version} = 2;
60 $new = PublicInbox::InboxWritable->new($new);
61 my $v2w;
62 $old->umask_prepare;
63
64 sub link_or_copy ($$) {
65         my ($src, $dst) = @_;
66         link($src, $dst) and return;
67         $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
68         cp($src, $dst) or die "cp $src, $dst failed: $!\n";
69 }
70
71 $old->with_umask(sub {
72         my $old_cfg = "$old->{inboxdir}/config";
73         local $ENV{GIT_CONFIG} = $old_cfg;
74         my $new_cfg = "$new->{inboxdir}/all.git/config";
75         $v2w = PublicInbox::V2Writable->new($new, 1);
76         $v2w->init_inbox($jobs);
77         unlink $new_cfg;
78         link_or_copy($old_cfg, $new_cfg);
79         if (my $alt = $new->{altid}) {
80                 require PublicInbox::AltId;
81                 foreach my $i (0..$#$alt) {
82                         my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
83                         $src->mm_alt or next;
84                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
85                         $dst = $dst->{filename};
86                         $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
87                 }
88         }
89         my $desc = "$old->{inboxdir}/description";
90         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
91         my $clone = "$old->{inboxdir}/cloneurl";
92         if (-e $clone) {
93                 warn <<"";
94 $clone may not be valid after migrating to v2, not copying
95
96         }
97 });
98 my $state = '';
99 my $head = $old->{ref_head} || 'HEAD';
100 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
101 $v2w->idx_init;
102 my $im = $v2w->importer;
103 my ($r, $w) = $im->gfi_start;
104 my $h = '[0-9a-f]';
105 my %D;
106 my $last;
107 while (<$rd>) {
108         if ($_ eq "blob\n") {
109                 $state = 'blob';
110         } elsif (/^commit /) {
111                 $state = 'commit';
112         } elsif (/^data ([0-9]+)/) {
113                 my $len = $1;
114                 print $w $_ or $im->wfail;
115                 while ($len) {
116                         my $n = read($rd, my $tmp, $len) or die "read: $!";
117                         warn "$n != $len\n" if $n != $len;
118                         $len -= $n;
119                         print $w $tmp or $im->wfail;
120                 }
121                 next;
122         } elsif ($state eq 'commit') {
123                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
124                         my ($mark, $path) = ($1, $2);
125                         $D{$path} = $mark;
126                         if ($last && $last ne 'm') {
127                                 print $w "D $last\n" or $im->wfail;
128                         }
129                         print $w "M 100644 :$mark m\n" or $im->wfail;
130                         $last = 'm';
131                         next;
132                 }
133                 if (m{^D (${h}{2}/${h}{38})}o) {
134                         my $mark = delete $D{$1};
135                         defined $mark or die "undeleted path: $1\n";
136                         if ($last && $last ne 'd') {
137                                 print $w "D $last\n" or $im->wfail;
138                         }
139                         print $w "M 100644 :$mark d\n" or $im->wfail;
140                         $last = 'd';
141                         next;
142                 }
143         }
144         last if $_ eq "done\n";
145         print $w $_ or $im->wfail;
146 }
147 close $rd or die "close fast-export: $!\n";
148 waitpid($pid, 0) or die "waitpid failed: $!\n";
149 $? == 0 or die "fast-export failed: $?\n";
150 $r = $w = undef; # v2w->done does the actual close and error checking
151 $v2w->done;
152 if (my $mm = $old->mm) {
153         $old->cleanup;
154         $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3");
155
156         # we want to trigger a reindex, not a from scratch index if
157         # we're reusing the msgmap from an existing v1 installation.
158         $v2w->idx_init;
159         my $epoch0 = PublicInbox::Git->new($v2w->git_init(0));
160         chop(my $cmt = $epoch0->qx(qw(rev-parse --verify), $head));
161         $v2w->last_epoch_commit(0, $cmt);
162 }
163 $v2w->index_sync({reindex => 1}) if $index;
164 $v2w->done;