]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
v2: one file, really
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018 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 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 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 = PublicInbox::Config->new;
28 $old_dir = abs_path($old_dir);
29 my $old;
30 $config->each_inbox(sub {
31         $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
32 });
33 unless ($old) {
34         warn "W: $old_dir not configured in " .
35                 PublicInbox::Config::default_file() . "\n";
36         $old = {
37                 mainrepo => $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} || 1) >= 2) {
45         die "Only conversion from v1 inboxes is supported\n";
46 }
47 my $new = { %$old };
48 delete $new->{altid}; # TODO: support altid for v2
49 $new->{mainrepo} = abs_path($new_dir);
50 $new->{version} = 2;
51 $new = PublicInbox::InboxWritable->new($new);
52 my $v2w;
53 $old->umask_prepare;
54 $old->with_umask(sub {
55         local $ENV{GIT_CONFIG} = "$old->{mainrepo}/config";
56         $v2w = PublicInbox::V2Writable->new($new, 1);
57         $v2w->init_inbox($jobs);
58         chomp(my $sr = $old->git->qx('config', 'core.sharedRepository'));
59         if ($sr ne '') {
60                 PublicInbox::Import::run_die(['git', 'config',
61                         "--file=$new->{mainrepo}/all.git/config",
62                         'core.sharedRepository', $sr]);
63         }
64 });
65 my $state = '';
66 my ($prev, $from);
67 my $head = $old->{ref_head} || 'HEAD';
68 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
69 $v2w->idx_init;
70 my $im = $v2w->importer;
71 my ($r, $w) = $im->gfi_start;
72 my $h = '[0-9a-f]';
73 my %D;
74 my $last;
75 while (<$rd>) {
76         if ($_ eq "blob\n") {
77                 $state = 'blob';
78         } elsif (/^commit /) {
79                 $state = 'commit';
80         } elsif (/^data (\d+)/) {
81                 my $len = $1;
82                 $w->print($_) or $im->wfail;
83                 while ($len) {
84                         my $n = read($rd, my $tmp, $len) or die "read: $!";
85                         warn "$n != $len\n" if $n != $len;
86                         $len -= $n;
87                         $w->print($tmp) or $im->wfail;
88                 }
89                 next;
90         } elsif ($state eq 'commit') {
91                 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
92                         my ($mark, $path) = ($1, $2);
93                         $D{$path} = $mark;
94                         if ($last && $last ne 'm') {
95                                 $w->print("D $last\n") or $im->wfail;
96                         }
97                         $w->print("M 100644 :$mark m\n") or $im->wfail;
98                         $last = 'm';
99                         next;
100                 }
101                 if (m{^D (${h}{2}/${h}{38})}o) {
102                         my $mark = delete $D{$1};
103                         defined $mark or die "undeleted path: $1\n";
104                         if ($last && $last ne 'd') {
105                                 $w->print("D $last\n") or $im->wfail;
106                         }
107                         $w->print("M 100644 :$mark d\n") or $im->wfail;
108                         $last = 'd';
109                         next;
110                 }
111                 if (m{^from (:\d+)}) {
112                         $prev = $from;
113                         $from = $1;
114                         # no next
115                 }
116         }
117         last if $_ eq "done\n";
118         $w->print($_) or $im->wfail;
119 }
120 $w = $r = undef;
121 close $rd or die "close fast-export: $!\n";
122 waitpid($pid, 0) or die "waitpid failed: $!\n";
123 $? == 0 or die "fast-export failed: $?\n";
124 my $mm = $old->mm;
125 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
126 $v2w->done;
127 if ($index) {
128         $v2w->reindex;
129         $v2w->done;
130 }