2 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <http://www.gnu.org/licenses/agpl-3.0.txt>
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
8 use PublicInbox::Inbox;
9 use PublicInbox::Config;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Spawn qw(spawn);
13 my $usage = "Usage: public-inbox-convert OLD NEW\n";
17 '--jobs|j=i' => \$jobs,
18 '--index!' => \$index,
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);
29 $config->each_inbox(sub {
30 $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
33 warn "W: $old_dir not configured in " .
34 PublicInbox::Config::default_file() . "\n";
38 address => [ 'old@example.com' ],
40 $old = PublicInbox::Inbox->new($old);
42 if (($old->{version} || 1) >= 2) {
43 die "Only conversion from v1 inboxes is supported\n";
46 delete $new->{altid}; # TODO: support altid for v2
47 $new->{mainrepo} = $new_dir;
49 $new = PublicInbox::Inbox->new($new);
50 my $v2w = PublicInbox::V2Writable->new($new, 1);
51 $v2w->init_inbox($jobs);
54 my $head = $old->{ref_head} || 'HEAD';
55 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
57 my $im = $v2w->importer;
58 my ($r, $w) = $im->gfi_start;
64 } elsif (/^commit /) {
66 } elsif (/^data (\d+)/) {
68 $w->print($_) or $im->wfail;
70 my $n = read($rd, my $tmp, $len) or die "read: $!";
71 warn "$n != $len\n" if $n != $len;
73 $w->print($tmp) or $im->wfail;
76 } elsif ($state eq 'commit') {
77 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
78 my ($mark, $path) = ($1, $2);
80 $w->print("M 100644 :$mark m\n") or $im->wfail;
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;
89 if (m{^from (:\d+)}) {
94 } elsif ($_ eq "done\n") {
97 $w->print($_) or $im->wfail;
100 close $rd or die "close fast-export: $!\n";
101 waitpid($pid, 0) or die "waitpid failed: $!\n";
102 $? == 0 or die "fast-export failed: $?\n";
104 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;