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