blob: 203e9b19530ea79d1ba1fcb3e289414b452217da (
plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#!/usr/bin/env perl
use warnings;
sub quote {
my $token = shift;
$token =~ s/'/''/g;
return "'$token'";
}
sub fail {
my $reason = shift;
print "set-register e fail " . quote("diff-parse.pl: $reason");
exit;
}
my $begin;
my $end;
while (defined $ARGV[0]) {
if ($ARGV[0] eq "--") {
shift;
last;
}
if ($ARGV[0] =~ m{^(BEGIN|END)$}) {
if (not defined $ARGV[1]) {
fail "missing argument to $ARGV[0]";
}
if ($ARGV[0] eq "BEGIN") {
$begin = $ARGV[1];
} else {
$end = $ARGV[1];
}
shift, shift;
next;
}
fail "unknown argument: $ARGV[0]";
}
# Inputs
our $directory = $ENV{PWD};
our $strip;
our $in_file;
our $in_file_line;
our $version = "+";
eval $begin if defined $begin;
$in_file = "$directory/$in_file" if defined $in_file;
# Outputs
our $diff_line = 0;
our $commit;
our $file;
our $file_line;
our $diff_line_text;
my $other_version;
if ($version eq "+") {
$other_version = "-";
} else {
$other_version = "+";
}
my $is_recursive_diff = 0;
my $state = "header";
my $fallback_file;
my $other_file;
my $other_file_line;
sub strip {
my $is_recursive_diff = shift;
my $f = shift;
my $effective_strip;
if (defined $strip) {
$effective_strip = $strip;
} else {
# A "diff -r" or "git diff" adds "diff" lines to
# the output. If no such line is present, we have
# a plain diff between files (not directories), so
# there should be no need to strip the directory.
$effective_strip = $is_recursive_diff ? 1 : 0;
}
if ($f !~ m{^/}) {
$f =~ s,^([^/]+/+){$effective_strip},, or fail "directory prefix underflow";
$f = "$directory/$f";
}
return $f;
}
while (<STDIN>) {
$diff_line++;
s/^(> )*//g;
$diff_line_text = $_;
if (m{^commit (\w+)}) {
$commit = $1;
next;
}
if (m{^diff\b}) {
$state = "header";
$is_recursive_diff = 1;
if (m{^diff -\S* (\S+) (\S+)$}) {
$fallback_file = strip $is_recursive_diff, ($version eq "+" ? $2 : $1);
}
next;
}
if ($state eq "header") {
if (m{^[$version]{3} ([^\t\n]+)}) {
$file = strip $is_recursive_diff, $1;
next;
}
if (m{^[$other_version]{3} ([^\t\n]+)}) {
$other_file = strip $is_recursive_diff, $1;
next;
}
}
if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
$state = "contents";
$file_line = ($version eq "+" ? $2 : $1) - 1;
$other_file_line = ($version eq "+" ? $1 : $2) - 1;
} else {
my $iscontext = m{^[ ]};
if (m{^[ $version]}) {
$file_line++ if defined $file_line;
}
if (m{^[ $other_version]}) {
$other_file_line++ if defined $other_file_line;
}
}
if (defined $in_file and defined $file and $file eq $in_file) {
if (defined $in_file_line and defined $file_line and $file_line >= $in_file_line) {
last;
}
}
}
if (not defined $file) {
$file = ($fallback_file or $other_file);
}
if (not defined $file) {
fail "missing diff header";
}
eval $end if defined $end;
|