]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
convert: avoid redundant "done\n" statement for fast-import
[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::Inbox;
9 use PublicInbox::Config;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Spawn qw(spawn);
12 use Cwd 'abs_path';
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 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 = PublicInbox::Config->new;
27 $old_dir = abs_path($old_dir);
28 my $old;
29 $config->each_inbox(sub {
30         $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
31 });
32 unless ($old) {
33         warn "W: $old_dir not configured in " .
34                 PublicInbox::Config::default_file() . "\n";
35         $old = {
36                 mainrepo => $old_dir,
37                 name => 'ignored',
38                 address => [ 'old@example.com' ],
39         };
40         $old = PublicInbox::Inbox->new($old);
41 }
42 if (($old->{version} || 1) >= 2) {
43         die "Only conversion from v1 inboxes is supported\n";
44 }
45 my $new = { %$old };
46 delete $new->{altid}; # TODO: support altid for v2
47 $new->{mainrepo} = $new_dir;
48 $new->{version} = 2;
49 $new = PublicInbox::Inbox->new($new);
50 my $v2w = PublicInbox::V2Writable->new($new, 1);
51 $v2w->init_inbox($jobs);
52 my $state = '';
53 my ($prev, $from);
54 my $head = $old->{ref_head} || 'HEAD';
55 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
56 $v2w->idx_init;
57 my $im = $v2w->importer;
58 my ($r, $w) = $im->gfi_start;
59 my $h = '[0-9a-f]';
60 my %D;
61 while (<$rd>) {
62         if ($_ eq "blob\n") {
63                 $state = 'blob';
64         } elsif (/^commit /) {
65                 $state = 'commit';
66         } elsif (/^data (\d+)/) {
67                 my $len = $1;
68                 $w->print($_) or $im->wfail;
69                 while ($len) {
70                         my $n = read($rd, my $tmp, $len) or die "read: $!";
71                         warn "$n != $len\n" if $n != $len;
72                         $len -= $n;
73                         $w->print($tmp) or $im->wfail;
74                 }
75                 next;
76         } elsif ($state eq 'commit') {
77                 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
78                         my ($mark, $path) = ($1, $2);
79                         $D{$path} = $mark;
80                         $w->print("M 100644 :$mark m\n") or $im->wfail;
81                         next;
82                 }
83                 if (m{^D (${h}{2}/${h}{38})}o) {
84                         my $mark = delete $D{$1};
85                         defined $mark or die "undeleted path: $1\n";
86                         $w->print("M 100644 :$mark _/D\n") or $im->wfail;
87                         next;
88                 }
89                 if (m{^from (:\d+)}) {
90                         $prev = $from;
91                         $from = $1;
92                         # no next
93                 }
94         }
95         last if $_ eq "done\n";
96         $w->print($_) or $im->wfail;
97 }
98 $w = $r = undef;
99 close $rd or die "close fast-export: $!\n";
100 waitpid($pid, 0) or die "waitpid failed: $!\n";
101 $? == 0 or die "fast-export failed: $?\n";
102 my $mm = $old->mm;
103 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
104 $v2w->done;
105 if ($index) {
106         $v2w->reindex;
107         $v2w->done;
108 }