]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
Merge remote-tracking branch 'origin/inboxdir'
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018-2019 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::InboxWritable;
9 use PublicInbox::Config;
10 use PublicInbox::V2Writable;
11 use PublicInbox::Import;
12 use PublicInbox::Spawn qw(spawn);
13 use Cwd 'abs_path';
14 use File::Copy 'cp'; # preserves permissions:
15 my $usage = "Usage: public-inbox-convert OLD NEW\n";
16 my $jobs;
17 my $index = 1;
18 my %opts = (
19         '--jobs|j=i' => \$jobs,
20         '--index!' => \$index,
21 );
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);
30 my $old;
31 if ($config) {
32         $config->each_inbox(sub {
33                 $old = $_[0] if abs_path($_[0]->{inboxdir}) eq $old_dir;
34         });
35 }
36 unless ($old) {
37         warn "W: $old_dir not configured in " .
38                 PublicInbox::Config::default_file() . "\n";
39         $old = {
40                 inboxdir => $old_dir,
41                 name => 'ignored',
42                 address => [ 'old@example.com' ],
43         };
44         $old = PublicInbox::Inbox->new($old);
45 }
46 $old = PublicInbox::InboxWritable->new($old);
47 if (($old->{version} || 1) >= 2) {
48         die "Only conversion from v1 inboxes is supported\n";
49 }
50 my $new = { %$old };
51 $new->{inboxdir} = abs_path($new_dir);
52 $new->{version} = 2;
53 $new = PublicInbox::InboxWritable->new($new);
54 my $v2w;
55 $old->umask_prepare;
56
57 sub link_or_copy ($$) {
58         my ($src, $dst) = @_;
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";
62 }
63
64 $old->with_umask(sub {
65         my $old_cfg = "$old->{inboxdir}/config";
66         local $ENV{GIT_CONFIG} = $old_cfg;
67         my $new_cfg = "$new->{inboxdir}/all.git/config";
68         $v2w = PublicInbox::V2Writable->new($new, 1);
69         $v2w->init_inbox($jobs);
70         unlink $new_cfg;
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);
76                         $src->mm_alt or next;
77                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
78                         $dst = $dst->{filename};
79                         $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
80                 }
81         }
82         my $desc = "$old->{inboxdir}/description";
83         link_or_copy($desc, "$new->{inboxdir}/description") if -e $desc;
84         my $clone = "$old->{inboxdir}/cloneurl";
85         if (-e $clone) {
86                 warn <<"";
87 $clone may not be valid after migrating to v2, not copying
88
89         }
90 });
91 my $state = '';
92 my ($prev, $from);
93 my $head = $old->{ref_head} || 'HEAD';
94 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
95 $v2w->idx_init;
96 my $im = $v2w->importer;
97 my ($r, $w) = $im->gfi_start;
98 my $h = '[0-9a-f]';
99 my %D;
100 my $last;
101 while (<$rd>) {
102         if ($_ eq "blob\n") {
103                 $state = 'blob';
104         } elsif (/^commit /) {
105                 $state = 'commit';
106         } elsif (/^data ([0-9]+)/) {
107                 my $len = $1;
108                 $w->print($_) or $im->wfail;
109                 while ($len) {
110                         my $n = read($rd, my $tmp, $len) or die "read: $!";
111                         warn "$n != $len\n" if $n != $len;
112                         $len -= $n;
113                         $w->print($tmp) or $im->wfail;
114                 }
115                 next;
116         } elsif ($state eq 'commit') {
117                 if (m{^M 100644 :([0-9]+) (${h}{2}/${h}{38})}o) {
118                         my ($mark, $path) = ($1, $2);
119                         $D{$path} = $mark;
120                         if ($last && $last ne 'm') {
121                                 $w->print("D $last\n") or $im->wfail;
122                         }
123                         $w->print("M 100644 :$mark m\n") or $im->wfail;
124                         $last = 'm';
125                         next;
126                 }
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;
132                         }
133                         $w->print("M 100644 :$mark d\n") or $im->wfail;
134                         $last = 'd';
135                         next;
136                 }
137                 if (m{^from (:[0-9]+)}) {
138                         $prev = $from;
139                         $from = $1;
140                         # no next
141                 }
142         }
143         last if $_ eq "done\n";
144         $w->print($_) or $im->wfail;
145 }
146 $w = $r = undef;
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";
150 my $mm = $old->mm;
151 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
152 $v2w->done;
153 if ($index) {
154         $v2w->index_sync;
155         $v2w->done;
156 }