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