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