]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-convert
v2: respect core.sharedRepository in git configs
[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 delete $new->{altid}; # TODO: support altid for v2
49 $new->{mainrepo} = abs_path($new_dir);
50 $new->{version} = 2;
51 $new = PublicInbox::InboxWritable->new($new);
52 my $v2w;
53 $old->umask_prepare;
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'));
59         if ($sr ne '') {
60                 PublicInbox::Import::run_die(['git', 'config',
61                         "--file=$new->{mainrepo}/all.git/config",
62                         'core.sharedRepository', $sr]);
63         }
64 });
65 my $state = '';
66 my ($prev, $from);
67 my $head = $old->{ref_head} || 'HEAD';
68 my ($rd, $pid) = $old->git->popen(qw(fast-export --use-done-feature), $head);
69 $v2w->idx_init;
70 my $im = $v2w->importer;
71 my ($r, $w) = $im->gfi_start;
72 my $h = '[0-9a-f]';
73 my %D;
74 while (<$rd>) {
75         if ($_ eq "blob\n") {
76                 $state = 'blob';
77         } elsif (/^commit /) {
78                 $state = 'commit';
79         } elsif (/^data (\d+)/) {
80                 my $len = $1;
81                 $w->print($_) or $im->wfail;
82                 while ($len) {
83                         my $n = read($rd, my $tmp, $len) or die "read: $!";
84                         warn "$n != $len\n" if $n != $len;
85                         $len -= $n;
86                         $w->print($tmp) or $im->wfail;
87                 }
88                 next;
89         } elsif ($state eq 'commit') {
90                 if (m{^M 100644 :(\d+) (${h}{2}/${h}{38})}o) {
91                         my ($mark, $path) = ($1, $2);
92                         $D{$path} = $mark;
93                         $w->print("M 100644 :$mark m\n") or $im->wfail;
94                         next;
95                 }
96                 if (m{^D (${h}{2}/${h}{38})}o) {
97                         my $mark = delete $D{$1};
98                         defined $mark or die "undeleted path: $1\n";
99                         $w->print("M 100644 :$mark _/D\n") or $im->wfail;
100                         next;
101                 }
102                 if (m{^from (:\d+)}) {
103                         $prev = $from;
104                         $from = $1;
105                         # no next
106                 }
107         }
108         last if $_ eq "done\n";
109         $w->print($_) or $im->wfail;
110 }
111 $w = $r = undef;
112 close $rd or die "close fast-export: $!\n";
113 waitpid($pid, 0) or die "waitpid failed: $!\n";
114 $? == 0 or die "fast-export failed: $?\n";
115 my $mm = $old->mm;
116 $mm->{dbh}->sqlite_backup_to_file("$new_dir/msgmap.sqlite3") if $mm;
117 $v2w->done;
118 if ($index) {
119         $v2w->reindex;
120         $v2w->done;
121 }