]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
convert: support converting with altid defined
[public-inbox.git] / script / public-inbox-convert
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018 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 my $usage = "Usage: public-inbox-convert OLD NEW\n";
15 my $jobs;
16 my $index = 1;
17 my %opts = (
18         '--jobs|j=i' => \$jobs,
19         '--index!' => \$index,
20 );
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);
29 my $old;
30 $config->each_inbox(sub {
31         $old = $_[0] if abs_path($_[0]->{mainrepo}) eq $old_dir;
32 });
33 unless ($old) {
34         warn "W: $old_dir not configured in " .
35                 PublicInbox::Config::default_file() . "\n";
36         $old = {
37                 mainrepo => $old_dir,
38                 name => 'ignored',
39                 address => [ 'old@example.com' ],
40         };
41         $old = PublicInbox::Inbox->new($old);
42 }
43 $old = PublicInbox::InboxWritable->new($old);
44 if (($old->{version} || 1) >= 2) {
45         die "Only conversion from v1 inboxes is supported\n";
46 }
47 my $new = { %$old };
48 $new->{mainrepo} = abs_path($new_dir);
49 $new->{version} = 2;
50 $new = PublicInbox::InboxWritable->new($new);
51 my $v2w;
52 $old->umask_prepare;
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'));
58         if ($sr ne '') {
59                 PublicInbox::Import::run_die(['git', 'config',
60                         "--file=$new->{mainrepo}/all.git/config",
61                         'core.sharedRepository', $sr]);
62         }
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);
67                         $src->mm_alt or next;
68                         my $dst = PublicInbox::AltId->new($new, $alt->[$i], 1);
69                         $dst = $dst->{filename};
70                         $src->mm_alt->{dbh}->sqlite_backup_to_file($dst);
71                 }
72         }
73 });
74 my $state = '';
75 my ($prev, $from);
76 my $head = $old->{ref_head} || 'HEAD';
77 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
78 $v2w->idx_init;
79 my $im = $v2w->importer;
80 my ($r, $w) = $im->gfi_start;
81 my $h = '[0-9a-f]';
82 my %D;
83 my $last;
84 while (<$rd>) {
85         if ($_ eq "blob\n") {
86                 $state = 'blob';
87         } elsif (/^commit /) {
88                 $state = 'commit';
89         } elsif (/^data (\d+)/) {
90                 my $len = $1;
91                 $w->print($_) or $im->wfail;
92                 while ($len) {
93                         my $n = read($rd, my $tmp, $len) or die "read: $!";
94                         warn "$n != $len\n" if $n != $len;
95                         $len -= $n;
96                         $w->print($tmp) or $im->wfail;
97                 }
98                 next;
99         } elsif ($state eq 'commit') {
100                 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
101                         my ($mark, $path) = ($1, $2);
102                         $D{$path} = $mark;
103                         if ($last && $last ne 'm') {
104                                 $w->print("D $last\n") or $im->wfail;
105                         }
106                         $w->print("M 100644 :$mark m\n") or $im->wfail;
107                         $last = 'm';
108                         next;
109                 }
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;
115                         }
116                         $w->print("M 100644 :$mark d\n") or $im->wfail;
117                         $last = 'd';
118                         next;
119                 }
120                 if (m{^from (:\d+)}) {
121                         $prev = $from;
122                         $from = $1;
123                         # no next
124                 }
125         }
126         last if $_ eq "done\n";
127         $w->print($_) or $im->wfail;
128 }
129 $w = $r = undef;
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";
133 my $mm = $old->mm;
134 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
135 $v2w->done;
136 if ($index) {
137         $v2w->index_sync;
138         $v2w->done;
139 }