-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-kconv-fast-import.pl
executable file
·51 lines (44 loc) · 1.12 KB
/
git-kconv-fast-import.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env perl
use strict;
use Jcode;
use Getopt::Long qw/:config no_ignore_case/;
sub usage {
die <<END;
Usage: $0 [-H | --history-only] ...FILES...
Convert legacy kanji code to utf8 in git fast-import files.
END
}
GetOptions("H|history-only", \ (my $o_history_only),
"h|help", \ (my $o_help),
"d|debug+", \ (my $o_debug = 0),
)
or usage();
$o_help and usage();
{
while (<>) {
if ($o_history_only) {
if (my $line = /^commit / .. (my ($origBytes) = /^data (\d+)/)) {
$_ = read_n_convert($origBytes) if $line =~ /E0$/ and $origBytes;
}
} else {
if (my ($origBytes) = /^data (\d+)/) {
$_ = read_n_convert($origBytes) if $origBytes;
}
}
print;
}
}
sub read_n_convert {
my ($origBytes) = @_;
my $buf = "";
unless (read(ARGV, $buf, $origBytes)) {
die $!;
}
my $converted = Jcode->new($buf)->utf8;
my $newBytes = length($converted);
print STDERR "# Changed data at $. ($origBytes => $newBytes)\n" if $o_debug;
if ($o_debug >= 2) {
print STDERR "-- $_\n" for split /\n/, $converted;
}
"data $newBytes\n$converted";
}