]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewDiff.pm
view_diff: minor coding style fixes
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # used by PublicInbox::View
5 # This adds CSS spans for diff highlighting.
6 # It also generates links for ViewVCS + SolverGit to show
7 # (or reconstruct) blobs.
8
9 package PublicInbox::ViewDiff;
10 use strict;
11 use v5.10.1;
12 use parent qw(Exporter);
13 our @EXPORT_OK = qw(flush_diff);
14 use URI::Escape qw(uri_escape_utf8);
15 use PublicInbox::Hval qw(ascii_html to_attr);
16 use PublicInbox::Git qw(git_unquote);
17
18 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
19
20 my $OID_NULL = '0{7,}';
21 my $OID_BLOB = '[a-f0-9]{7,}';
22 my $LF = qr!\n!;
23 my $ANY = qr![^\n]!;
24 my $FN = qr!(?:"?[^/\n]+/[^\n]+|/dev/null)!;
25
26 # cf. git diff.c :: get_compact_summary
27 my $DIFFSTAT_COMMENT =
28         qr/(?: *\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\))? *\z/s;
29 my $NULL_TO_BLOB = qr/^(index $OID_NULL\.\.)($OID_BLOB)\b/ms;
30 my $BLOB_TO_NULL = qr/^index ($OID_BLOB)(\.\.$OID_NULL)\b/ms;
31 my $BLOB_TO_BLOB = qr/^index ($OID_BLOB)\.\.($OID_BLOB)/ms;
32 our $EXTRACT_DIFFS = qr/(
33                 (?:     # begin header stuff, don't capture filenames, here,
34                         # but instead wait for the --- and +++ lines.
35                         (?:^diff\x20--git\x20$FN\x20$FN$LF)
36
37                         # old mode || new mode || copy|rename|deleted|...
38                         (?:^[a-z]$ANY+$LF)*
39                 )? # end of optional stuff, everything below is required
40                 ^index\x20($OID_BLOB)\.\.($OID_BLOB)$ANY*$LF
41                 ^---\x20($FN)$LF
42                 ^\+{3}\x20($FN)$LF)/msx;
43 our $IS_OID = qr/\A$OID_BLOB\z/s;
44
45 # link to line numbers in blobs
46 sub diff_hunk ($$$$) {
47         my ($dst, $dctx, $ca, $cb) = @_;
48         my ($oid_a, $oid_b, $spfx) = @$dctx{qw(oid_a oid_b spfx)};
49
50         if (defined($spfx) && defined($oid_a) && defined($oid_b)) {
51                 my ($n) = ($ca =~ /^-([0-9]+)/);
52                 $n = defined($n) ? "#n$n" : '';
53
54                 $$dst .= qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
55
56                 ($n) = ($cb =~ /^\+([0-9]+)/);
57                 $n = defined($n) ? "#n$n" : '';
58                 $$dst .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
59         } else {
60                 $$dst .= "@@ $ca $cb @@";
61         }
62 }
63
64 sub oid ($$$) {
65         my ($dctx, $spfx, $oid) = @_;
66         defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
67 }
68
69 # returns true if diffstat anchor written, false otherwise
70 sub anchor0 ($$$$) {
71         my ($dst, $ctx, $fn, $rest) = @_;
72
73         my $orig = $fn;
74
75         # normal git diffstat output is impossible to parse reliably
76         # without --numstat, and that isn't the default for format-patch.
77         # So only do best-effort handling of renames for common cases;
78         # which works well in practice. If projects put "=>", or trailing
79         # spaces in filenames, oh well :P
80         $fn =~ s/$DIFFSTAT_COMMENT//;
81         $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
82         $fn = git_unquote($fn);
83
84         # long filenames will require us to check in anchor1()
85         push(@{$ctx->{-long_path}}, $fn) if $fn =~ s!\A\.\.\./?!!;
86
87         if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
88                 $ctx->{-anchors}->{$attr} = 1;
89                 my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
90                 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
91                         ascii_html($orig) . '</a>' . $spaces .
92                         $ctx->{-linkify}->to_html($rest);
93                 return 1;
94         }
95         undef;
96 }
97
98 # returns "diff --git" anchor destination, undef otherwise
99 sub anchor1 ($$) {
100         my ($ctx, $pb) = @_;
101         my $attr = to_attr($ctx->{-apfx}.$pb) or return;
102
103         my $ok = delete $ctx->{-anchors}->{$attr};
104
105         # unlikely, check the end of long path names we captured,
106         # assume diffstat and diff output follow the same order,
107         # and ignore different ordering (could be malicious input)
108         unless ($ok) {
109                 my $fn = shift(@{$ctx->{-long_path}}) or return;
110                 $pb =~ /\Q$fn\E\z/s or return;
111                 $attr = to_attr($ctx->{-apfx}.$fn) or return;
112                 $ok = delete $ctx->{-anchors}->{$attr} or return;
113         }
114         $ok ? "<a\nhref=#i$attr\nid=$attr>diff</a> --git" : undef
115 }
116
117 sub diff_header ($$$) {
118         my ($x, $ctx, $top) = @_;
119         my (undef, undef, $pa, $pb) = splice(@$top, 0, 4); # ignore oid_{a,b}
120         my $spfx = $ctx->{-spfx};
121         my $dctx = { spfx => $spfx };
122
123         # get rid of leading "a/" or "b/" (or whatever --{src,dst}-prefix are)
124         $pa = (split(m'/', git_unquote($pa), 2))[1] if $pa ne '/dev/null';
125         $pb = (split(m'/', git_unquote($pb), 2))[1] if $pb ne '/dev/null';
126         if ($pa eq $pb && $pb ne '/dev/null') {
127                 $dctx->{Q} = "?b=".uri_escape_utf8($pb, UNSAFE);
128         } else {
129                 my @q;
130                 if ($pb ne '/dev/null') {
131                         push @q, 'b='.uri_escape_utf8($pb, UNSAFE);
132                 }
133                 if ($pa ne '/dev/null') {
134                         push @q, 'a='.uri_escape_utf8($pa, UNSAFE);
135                 }
136                 $dctx->{Q} = '?'.join('&amp;', @q);
137         }
138
139         # linkify early and all at once, since we know the following
140         # subst ops on $$x won't need further escaping:
141         $$x = $ctx->{-linkify}->to_html($$x);
142
143         # no need to capture oid_a and oid_b on add/delete,
144         # we just linkify OIDs directly via s///e in conditional
145         if (($$x =~ s/$NULL_TO_BLOB/$1 . oid($dctx, $spfx, $2)/e) ||
146                 ($$x =~ s/$BLOB_TO_NULL/
147                         'index ' . oid($dctx, $spfx, $1) . $2/e)) {
148         } elsif ($$x =~ $BLOB_TO_BLOB) {
149                 # modification-only, not add/delete:
150                 # linkify hunk headers later using oid_a and oid_b
151                 @$dctx{qw(oid_a oid_b)} = ($1, $2);
152         } else {
153                 warn "BUG? <$$x> had no ^index line";
154         }
155         $$x =~ s!^diff --git!anchor1($ctx, $pb) // 'diff --git'!ems;
156         my $dst = $ctx->{obuf};
157         $$dst .= qq(<span\nclass="head">);
158         $$dst .= $$x;
159         $$dst .= '</span>';
160         $dctx;
161 }
162
163 sub diff_before_or_after ($$) {
164         my ($ctx, $x) = @_;
165         my $linkify = $ctx->{-linkify};
166         my $dst = $ctx->{obuf};
167         my $anchors = exists($ctx->{-anchors}) ? 1 : 0;
168         for my $y (split(/(^---\n)/sm, $$x)) {
169                 if ($y =~ /\A---\n\z/s) {
170                         $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
171                         $anchors |= 2;
172                 } elsif ($anchors == 3 && $y =~ /^ [0-9]+ files? changed, /sm) {
173                         # ok, looks like a diffstat, go line-by-line:
174                         for my $l (split(/^/m, $y)) {
175                                 if ($l =~ /^ (.+)( +\| .*\z)/s) {
176                                         anchor0($dst, $ctx, $1, $2) and next;
177                                 }
178                                 $$dst .= $linkify->to_html($l);
179                         }
180                 } else { # commit message, notes, etc
181                         $$dst .= $linkify->to_html($y);
182                 }
183         }
184 }
185
186 # callers must do CRLF => LF conversion before calling this
187 sub flush_diff ($$) {
188         my ($ctx, $cur) = @_;
189
190         my @top = split($EXTRACT_DIFFS, $$cur);
191         undef $$cur; # free memory
192
193         my $linkify = $ctx->{-linkify};
194         my $dst = $ctx->{obuf};
195         my $dctx; # {}, keys: Q, oid_a, oid_b
196
197         while (defined(my $x = shift @top)) {
198                 if (scalar(@top) >= 4 &&
199                                 $top[1] =~ $IS_OID &&
200                                 $top[0] =~ $IS_OID) {
201                         $dctx = diff_header(\$x, $ctx, \@top);
202                 } elsif ($dctx) {
203                         my $after = '';
204
205                         # Quiet "Complex regular subexpression recursion limit"
206                         # warning.  Perl will truncate matches upon hitting
207                         # that limit, giving us more (and shorter) scalars than
208                         # would be ideal, but otherwise it's harmless.
209                         #
210                         # We could replace the `+' metacharacter with `{1,100}'
211                         # to limit the matches ourselves to 100, but we can
212                         # let Perl do it for us, quietly.
213                         no warnings 'regexp';
214
215                         for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
216                                         (?:(?:^-[^\n]*\n)+)|
217                                         (?:^@@ [^\n]+\n))/xsm, $x)) {
218                                 if (!defined($dctx)) {
219                                         $after .= $s;
220                                 } elsif ($s =~ s/\A@@ (\S+) (\S+) @@//) {
221                                         $$dst .= qq(<span\nclass="hunk">);
222                                         diff_hunk($dst, $dctx, $1, $2);
223                                         $$dst .= $linkify->to_html($s);
224                                         $$dst .= '</span>';
225                                 } elsif ($s =~ /\A\+/) {
226                                         $$dst .= qq(<span\nclass="add">);
227                                         $$dst .= $linkify->to_html($s);
228                                         $$dst .= '</span>';
229                                 } elsif ($s =~ /\A-- $/sm) { # email sig starts
230                                         $dctx = undef;
231                                         $after .= $s;
232                                 } elsif ($s =~ /\A-/) {
233                                         $$dst .= qq(<span\nclass="del">);
234                                         $$dst .= $linkify->to_html($s);
235                                         $$dst .= '</span>';
236                                 } else {
237                                         $$dst .= $linkify->to_html($s);
238                                 }
239                         }
240                         diff_before_or_after($ctx, \$after) unless $dctx;
241                 } else {
242                         diff_before_or_after($ctx, \$x);
243                 }
244         }
245 }
246
247 1;