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