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 $new->{mainrepo} = abs_path($new_dir);
50 $new = PublicInbox::InboxWritable->new($new);
53 $old->with_umask(sub {
54 local $ENV{GIT_CONFIG} = "$old->{mainrepo}/config";
55 $v2w = PublicInbox::V2Writable->new($new, 1);
56 $v2w->init_inbox($jobs);
57 chomp(my $sr = $old->git->qx('config', 'core.sharedRepository'));
59 PublicInbox::Import::run_die(['git', 'config',
60 "--file=$new->{mainrepo}/all.git/config",
61 'core.sharedRepository', $sr]);
63 if (my $alt = $new->{altid}) {
64 require PublicInbox::AltId;
65 foreach my $i (0..$#$alt) {
66 my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
68 my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
69 $dst = $dst->{filename};
70 $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
76 my $head = $old->{ref_head} || 'HEAD';
77 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
79 my $im = $v2w->importer;
80 my ($r, $w) = $im->gfi_start;
87 } elsif (/^commit /) {
89 } elsif (/^data (\d+)/) {
91 $w->print($_) or $im->wfail;
93 my $n = read($rd, my $tmp, $len) or die "read: $!";
94 warn "$n != $len\n" if $n != $len;
96 $w->print($tmp) or $im->wfail;
99 } elsif ($state eq 'commit') {
100 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
101 my ($mark, $path) = ($1, $2);
103 if ($last && $last ne 'm') {
104 $w->print("D $last\n") or $im->wfail;
106 $w->print("M 100644 :$mark m\n") or $im->wfail;
110 if (m{^D (${h}{2}/${h}{38})}o) {
111 my $mark = delete $D{$1};
112 defined $mark or die "undeleted path: $1\n";
113 if ($last && $last ne 'd') {
114 $w->print("D $last\n") or $im->wfail;
116 $w->print("M 100644 :$mark d\n") or $im->wfail;
120 if (m{^from (:\d+)}) {
126 last if $_ eq "done\n";
127 $w->print($_) or $im->wfail;
130 close $rd or die "close fast-export: $!\n";
131 waitpid($pid, 0) or die "waitpid failed: $!\n";
132 $? == 0 or die "fast-export failed: $?\n";
134 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;