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>
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.
9 package PublicInbox::ViewDiff;
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);
19 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
21 my $OID_NULL = '0{7,}';
22 my $OID_BLOB = '[a-f0-9]{7,}';
25 my $FN = qr!(?:"?[^/\n]+/[^\n]+|/dev/null)!;
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)
38 # old mode || new mode || copy|rename|deleted|...
40 )? # end of optional stuff, everything below is required
41 ^index\x20($OID_BLOB)\.\.($OID_BLOB)$ANY*$LF
43 ^\+{3}\x20($FN)$LF)/msx;
44 my $IS_OID = qr/\A$OID_BLOB\z/s;
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)};
51 if (defined($spfx) && defined($oid_a) && defined($oid_b)) {
52 my ($n) = ($ca =~ /^-([0-9]+)/);
53 $n = defined($n) ? "#n$n" : '';
55 $$dst .= qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
57 ($n) = ($cb =~ /^\+([0-9]+)/);
58 $n = defined($n) ? "#n$n" : '';
59 $$dst .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
61 $$dst .= "@@ $ca $cb @@";
66 my ($dctx, $spfx, $oid) = @_;
67 defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
70 # returns true if diffstat anchor written, false otherwise
72 my ($dst, $ctx, $fn, $rest) = @_;
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);
85 # long filenames will require us to check in anchor1()
86 push(@{$ctx->{-long_path}}, $fn) if $fn =~ s!\A\.\.\./?!!;
88 if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
89 $ctx->{-anchors}->{$attr} = 1;
90 my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
91 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
92 ascii_html($orig) . '</a>' . $spaces .
93 $ctx->{-linkify}->to_html($rest);
99 # returns "diff --git" anchor destination, undef otherwise
102 my $attr = to_attr($ctx->{-apfx}.$pb) or return;
104 my $ok = delete $ctx->{-anchors}->{$attr};
106 # unlikely, check the end of long path names we captured,
107 # assume diffstat and diff output follow the same order,
108 # and ignore different ordering (could be malicious input)
110 my $fn = shift(@{$ctx->{-long_path}}) or return;
111 $pb =~ /\Q$fn\E\z/s or return;
112 $attr = to_attr($ctx->{-apfx}.$fn) or return;
113 $ok = delete $ctx->{-anchors}->{$attr} or return;
115 $ok ? "<a\nhref=#i$attr\nid=$attr>diff</a> --git" : undef
118 sub diff_header ($$$) {
119 my ($x, $ctx, $top) = @_;
120 my (undef, undef, $pa, $pb) = splice(@$top, 0, 4); # ignore oid_{a,b}
121 my $spfx = $ctx->{-spfx};
122 my $dctx = { spfx => $spfx };
124 # get rid of leading "a/" or "b/" (or whatever --{src,dst}-prefix are)
125 $pa = (split('/', git_unquote($pa), 2))[1] if $pa ne '/dev/null';
126 $pb = (split('/', git_unquote($pb), 2))[1] if $pb ne '/dev/null';
127 if ($pa eq $pb && $pb ne '/dev/null') {
128 $dctx->{Q} = "?b=".uri_escape_utf8($pb, UNSAFE);
131 if ($pb ne '/dev/null') {
132 push @q, 'b='.uri_escape_utf8($pb, UNSAFE);
134 if ($pa ne '/dev/null') {
135 push @q, 'a='.uri_escape_utf8($pa, UNSAFE);
137 $dctx->{Q} = '?'.join('&', @q);
140 # linkify early and all at once, since we know the following
141 # subst ops on $$x won't need further escaping:
142 $$x = $ctx->{-linkify}->to_html($$x);
144 # no need to capture oid_a and oid_b on add/delete,
145 # we just linkify OIDs directly via s///e in conditional
146 if (($$x =~ s/$NULL_TO_BLOB/$1 . oid($dctx, $spfx, $2)/e) ||
147 ($$x =~ s/$BLOB_TO_NULL/
148 'index ' . oid($dctx, $spfx, $1) . $2/e)) {
149 } elsif ($$x =~ $BLOB_TO_BLOB) {
150 # modification-only, not add/delete:
151 # linkify hunk headers later using oid_a and oid_b
152 @$dctx{qw(oid_a oid_b)} = ($1, $2);
154 warn "BUG? <$$x> had no ^index line";
156 $$x =~ s!^diff --git!anchor1($ctx, $pb) // 'diff --git'!ems;
157 my $dst = $ctx->{obuf};
158 $$dst .= qq(<span\nclass="head">);
164 sub diff_before_or_after ($$) {
166 my $linkify = $ctx->{-linkify};
167 my $dst = $ctx->{obuf};
168 my $anchors = exists($ctx->{-anchors}) ? 1 : 0;
169 for my $y (split(/(^---\n)/sm, $$x)) {
170 if ($y =~ /\A---\n\z/s) {
171 $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
173 } elsif ($anchors == 3 && $y =~ /^ [0-9]+ files? changed, /sm) {
174 # ok, looks like a diffstat, go line-by-line:
175 for my $l (split(/^/m, $y)) {
176 if ($l =~ /^ (.+)( +\| .*\z)/s) {
177 anchor0($dst, $ctx, $1, $2) and next;
179 $$dst .= $linkify->to_html($l);
181 } else { # commit message, notes, etc
182 $$dst .= $linkify->to_html($y);
187 # callers must do CRLF => LF conversion before calling this
188 sub flush_diff ($$) {
189 my ($ctx, $cur) = @_;
191 my @top = split($EXTRACT_DIFFS, $$cur);
192 undef $$cur; # free memory
194 my $linkify = $ctx->{-linkify};
195 my $dst = $ctx->{obuf};
196 my $dctx; # {}, keys: Q, oid_a, oid_b
198 while (defined(my $x = shift @top)) {
199 if (scalar(@top) >= 4 &&
200 $top[1] =~ $IS_OID &&
201 $top[0] =~ $IS_OID) {
202 $dctx = diff_header(\$x, $ctx, \@top);
206 # Quiet "Complex regular subexpression recursion limit"
207 # warning. Perl will truncate matches upon hitting
208 # that limit, giving us more (and shorter) scalars than
209 # would be ideal, but otherwise it's harmless.
211 # We could replace the `+' metacharacter with `{1,100}'
212 # to limit the matches ourselves to 100, but we can
213 # let Perl do it for us, quietly.
214 no warnings 'regexp';
216 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
218 (?:^@@ [^\n]+\n))/xsm, $x)) {
219 if (!defined($dctx)) {
221 } elsif ($s =~ s/\A@@ (\S+) (\S+) @@//) {
222 $$dst .= qq(<span\nclass="hunk">);
223 diff_hunk($dst, $dctx, $1, $2);
224 $$dst .= $linkify->to_html($s);
226 } elsif ($s =~ /\A\+/) {
227 $$dst .= qq(<span\nclass="add">);
228 $$dst .= $linkify->to_html($s);
230 } elsif ($s =~ /\A-- $/sm) { # email sig starts
233 } elsif ($s =~ /\A-/) {
234 $$dst .= qq(<span\nclass="del">);
235 $$dst .= $linkify->to_html($s);
238 $$dst .= $linkify->to_html($s);
241 diff_before_or_after($ctx, \$after) unless $dctx;
243 diff_before_or_after($ctx, \$x);