]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
9bee5e7a323b437eaa0c6077458879a0b2464301
[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::MIME;
8 use PublicInbox::InboxWritable;
9 use PublicInbox::Config;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Import;
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 or die $usage;
24 my $new_dir = shift 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 = eval { 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} || 1) >= 2) {
47         die "Only conversion from v1 inboxes is supported\n";
48 }
49 my $new = { %$old };
50 $new->{inboxdir} = abs_path($new_dir);
51 $new->{version} = 2;
52 $new = PublicInbox::InboxWritable->new($new);
53 my $v2w;
54 $old->umask_prepare;
55
56 sub link_or_copy ($$) {
57         my ($src, $dst) = @_;
58         link($src, $dst) and return;
59         $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
60         cp($src, $dst) or die "cp $src, $dst failed: $!\n";
61 }
62
63 $old->with_umask(sub {
64         my $old_cfg = "$old->{inboxdir}/config";
65         local $ENV{GIT_CONFIG} = $old_cfg;
66         my $new_cfg = "$new->{inboxdir}/all.git/config";
67         $v2w = PublicInbox::V2Writable->new($new, 1);
68         $v2w->init_inbox($jobs);
69         unlink $new_cfg;
70         link_or_copy($old_cfg, $new_cfg);
71         if (my $alt = $new->{altid}) {
72                 require PublicInbox::AltId;
73                 foreach my $i (0..$#$alt) {
74                         my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
75                         $src->mm_alt or next;
76                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
77                         $dst = $dst->{filename};
78                         $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
79                 }
80         }
81         my $desc = "$old->{inboxdir}/description";
82         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
83         my $clone = "$old->{inboxdir}/cloneurl";
84         if (-e $clone) {
85                 warn <<"";
86 $clone may not be valid after migrating to v2, not copying
87
88         }
89 });
90 my $state = '';
91 my ($prev, $from);
92 my $head = $old->{ref_head} || 'HEAD';
93 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
94 $v2w->idx_init;
95 my $im = $v2w->importer;
96 my ($r, $w) = $im->gfi_start;
97 my $h = '[0-9a-f]';
98 my %D;
99 my $last;
100 while (<$rd>) {
101         if ($_ eq "blob\n") {
102                 $state = 'blob';
103         } elsif (/^commit /) {
104                 $state = 'commit';
105         } elsif (/^data ([0-9]+)/) {
106                 my $len = $1;
107                 $w->print($_) or $im->wfail;
108                 while ($len) {
109                         my $n = read($rd, my $tmp, $len) or die "read: $!";
110                         warn "$n != $len\n" if $n != $len;
111                         $len -= $n;
112                         $w->print($tmp) or $im->wfail;
113                 }
114                 next;
115         } elsif ($state eq 'commit') {
116                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
117                         my ($mark, $path) = ($1, $2);
118                         $D{$path} = $mark;
119                         if ($last && $last ne 'm') {
120                                 $w->print("D $last\n") or $im->wfail;
121                         }
122                         $w->print("M 100644 :$mark m\n") or $im->wfail;
123                         $last = 'm';
124                         next;
125                 }
126                 if (m{^D (${h}{2}/${h}{38})}o) {
127                         my $mark = delete $D{$1};
128                         defined $mark or die "undeleted path: $1\n";
129                         if ($last && $last ne 'd') {
130                                 $w->print("D $last\n") or $im->wfail;
131                         }
132                         $w->print("M 100644 :$mark d\n") or $im->wfail;
133                         $last = 'd';
134                         next;
135                 }
136                 if (m{^from (:[0-9]+)}) {
137                         $prev = $from;
138                         $from = $1;
139                         # no next
140                 }
141         }
142         last if $_ eq "done\n";
143         $w->print($_) or $im->wfail;
144 }
145 $w = $r = undef;
146 close $rd or die "close fast-export: $!\n";
147 waitpid($pid, 0) or die "waitpid failed: $!\n";
148 $? == 0 or die "fast-export failed: $?\n";
149 my $mm = $old->mm;
150 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
151 $v2w->done;
152 if ($index) {
153         $v2w->index_sync;
154         $v2w->done;
155 }