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 use File::Copy 'cp'; # preserves permissions:
15 my $usage = "Usage: public-inbox-convert OLD NEW\n";
19 '--jobs|j=i' => \$jobs,
20 '--index!' => \$index,
22 GetOptions(%opts) or die "bad command-line args\n$usage";
23 GetOptions(%opts) or die "bad command-line args\n$usage";
24 my $old_dir = shift or die $usage;
25 my $new_dir = shift or die $usage;
26 die "$new_dir exists\n" if -d $new_dir;
27 die "$old_dir not a directory\n" unless -d $old_dir;
28 my $config = eval { PublicInbox::Config->new };
29 $old_dir = abs_path($old_dir);
32 $config->each_inbox(sub {
33 $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
37 warn "W: $old_dir not configured in " .
38 PublicInbox::Config::default_file() . "\n";
42 address => [ 'old@example.com' ],
44 $old = PublicInbox::Inbox->new($old);
46 $old = PublicInbox::InboxWritable->new($old);
47 if (($old->{version} || 1) >= 2) {
48 die "Only conversion from v1 inboxes is supported\n";
51 $new->{mainrepo} = abs_path($new_dir);
53 $new = PublicInbox::InboxWritable->new($new);
57 sub link_or_copy ($$) {
59 link($src, $dst) and return;
60 $!{EXDEV} or warn "link $src, $dst failed: $!, trying cp\n";
61 cp($src, $dst) or die "cp $src, $dst failed: $!\n";
64 $old->with_umask(sub {
65 my $old_cfg = "$old->{mainrepo}/config";
66 local $ENV{GIT_CONFIG} = $old_cfg;
67 my $new_cfg = "$new->{mainrepo}/all.git/config";
68 $v2w = PublicInbox::V2Writable->new($new, 1);
69 $v2w->init_inbox($jobs);
71 link_or_copy($old_cfg, $new_cfg);
72 if (my $alt = $new->{altid}) {
73 require PublicInbox::AltId;
74 foreach my $i (0..$#$alt) {
75 my $src = PublicInbox::AltId->new($old, $alt->[$i], 0);
77 my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
78 $dst = $dst->{filename};
79 $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
82 my $desc = "$old->{mainrepo}/description";
83 link_or_copy($desc, "$new->{mainrepo}/description") if -e $desc;
84 my $clone = "$old->{mainrepo}/cloneurl";
87 $clone may not be valid after migrating to v2, not copying
93 my $head = $old->{ref_head} || 'HEAD';
94 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
96 my $im = $v2w->importer;
97 my ($r, $w) = $im->gfi_start;
102 if ($_ eq "blob\n") {
104 } elsif (/^commit /) {
106 } elsif (/^data (\d+)/) {
108 $w->print($_) or $im->wfail;
110 my $n = read($rd, my $tmp, $len) or die "read: $!";
111 warn "$n != $len\n" if $n != $len;
113 $w->print($tmp) or $im->wfail;
116 } elsif ($state eq 'commit') {
117 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
118 my ($mark, $path) = ($1, $2);
120 if ($last && $last ne 'm') {
121 $w->print("D $last\n") or $im->wfail;
123 $w->print("M 100644 :$mark m\n") or $im->wfail;
127 if (m{^D (${h}{2}/${h}{38})}o) {
128 my $mark = delete $D{$1};
129 defined $mark or die "undeleted path: $1\n";
130 if ($last && $last ne 'd') {
131 $w->print("D $last\n") or $im->wfail;
133 $w->print("M 100644 :$mark d\n") or $im->wfail;
137 if (m{^from (:\d+)}) {
143 last if $_ eq "done\n";
144 $w->print($_) or $im->wfail;
147 close $rd or die "close fast-export: $!\n";
148 waitpid($pid, 0) or die "waitpid failed: $!\n";
149 $? == 0 or die "fast-export failed: $?\n";
151 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;