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