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::InboxWritable;
9 use PublicInbox::Config;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Import;
12 use PublicInbox::Spawn qw(spawn);
14 my $usage = "Usage: public-inbox-convert OLD NEW\n";
18 '--jobs|j=i' => \$jobs,
19 '--index!' => \$index,
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);
30 $config->each_inbox(sub {
31 $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
34 warn "W: $old_dir not configured in " .
35 PublicInbox::Config::default_file() . "\n";
39 address => [ 'old@example.com' ],
41 $old = PublicInbox::Inbox->new($old);
43 $old = PublicInbox::InboxWritable->new($old);
44 if (($old->{version} || 1) >= 2) {
45 die "Only conversion from v1 inboxes is supported\n";
48 delete $new->{altid}; # TODO: support altid for v2
49 $new->{mainrepo} = abs_path($new_dir);
51 $new = PublicInbox::InboxWritable->new($new);
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'));
60 PublicInbox::Import::run_die(['git', 'config',
61 "--file=$new->{mainrepo}/all.git/config",
62 'core.sharedRepository', $sr]);
67 my $head = $old->{ref_head} || 'HEAD';
68 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
70 my $im = $v2w->importer;
71 my ($r, $w) = $im->gfi_start;
78 } elsif (/^commit /) {
80 } elsif (/^data (\d+)/) {
82 $w->print($_) or $im->wfail;
84 my $n = read($rd, my $tmp, $len) or die "read: $!";
85 warn "$n != $len\n" if $n != $len;
87 $w->print($tmp) or $im->wfail;
90 } elsif ($state eq 'commit') {
91 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
92 my ($mark, $path) = ($1, $2);
94 if ($last && $last ne 'm') {
95 $w->print("D $last\n") or $im->wfail;
97 $w->print("M 100644 :$mark m\n") or $im->wfail;
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;
107 $w->print("M 100644 :$mark d\n") or $im->wfail;
111 if (m{^from (:\d+)}) {
117 last if $_ eq "done\n";
118 $w->print($_) or $im->wfail;
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";
125 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;