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>
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;
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);
18 my $UNSAFE = "^A-Za-z0-9\-\._~/"; # '/' + $URI::Escape::Unsafe{RFC3986}
19 my $OID_NULL = '0{7,}';
20 my $OID_BLOB = '[a-f0-9]{7,}';
23 my $FN = qr!(?:"?[^/\n]+/[^\n]+|/dev/null)!;
25 # cf. git diff.c :: get_compact_summary
26 my $DIFFSTAT_COMMENT =
27 qr/(?: *\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\))? *\z/s;
28 my $NULL_TO_BLOB = qr/^(index $OID_NULL\.\.)($OID_BLOB)\b/ms;
29 my $BLOB_TO_NULL = qr/^index ($OID_BLOB)(\.\.$OID_NULL)\b/ms;
30 my $BLOB_TO_BLOB = qr/^index ($OID_BLOB)\.\.($OID_BLOB)/ms;
31 our $EXTRACT_DIFFS = qr/(
32 (?: # begin header stuff, don't capture filenames, here,
33 # but instead wait for the --- and +++ lines.
34 (?:^diff\x20--git\x20$FN\x20$FN$LF)
36 # old mode || new mode || copy|rename|deleted|...
38 )? # end of optional stuff, everything below is required
39 ^index\x20($OID_BLOB)\.\.($OID_BLOB)$ANY*$LF
41 ^\+{3}\x20($FN)$LF)/msx;
42 our $IS_OID = qr/\A$OID_BLOB\z/s;
44 # link to line numbers in blobs
45 sub diff_hunk ($$$$) {
46 my ($dst, $dctx, $ca, $cb) = @_;
47 my ($oid_a, $oid_b, $spfx) = @$dctx{qw(oid_a oid_b spfx)};
49 if (defined($spfx) && defined($oid_a) && defined($oid_b)) {
50 my ($n) = ($ca =~ /^-([0-9]+)/);
51 $n = defined($n) ? "#n$n" : '';
53 $$dst .= qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
55 ($n) = ($cb =~ /^\+([0-9]+)/);
56 $n = defined($n) ? "#n$n" : '';
57 $$dst .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
59 $$dst .= "@@ $ca $cb @@";
64 my ($dctx, $spfx, $oid) = @_;
65 defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
68 # returns true if diffstat anchor written, false otherwise
70 my ($dst, $ctx, $fn, $rest) = @_;
74 # normal git diffstat output is impossible to parse reliably
75 # without --numstat, and that isn't the default for format-patch.
76 # So only do best-effort handling of renames for common cases;
77 # which works well in practice. If projects put "=>", or trailing
78 # spaces in filenames, oh well :P
79 $fn =~ s/$DIFFSTAT_COMMENT//;
80 $fn =~ s/\{(?:.+) => (.+)\}/$1/ or $fn =~ s/.* => (.+)/$1/;
81 $fn = git_unquote($fn);
83 # long filenames will require us to check in anchor1()
84 push(@{$ctx->{-long_path}}, $fn) if $fn =~ s!\A\.\.\./?!!;
86 if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
87 $ctx->{-anchors}->{$attr} = 1;
88 my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
89 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
90 ascii_html($orig) . '</a>' . $spaces .
91 $ctx->{-linkify}->to_html($rest);
97 # returns "diff --git" anchor destination, undef otherwise
100 my $attr = to_attr($ctx->{-apfx}.$pb) or return;
102 my $ok = delete $ctx->{-anchors}->{$attr};
104 # unlikely, check the end of long path names we captured,
105 # assume diffstat and diff output follow the same order,
106 # and ignore different ordering (could be malicious input)
108 my $fn = shift(@{$ctx->{-long_path}}) or return;
109 $pb =~ /\Q$fn\E\z/s or return;
110 $attr = to_attr($ctx->{-apfx}.$fn) or return;
111 $ok = delete $ctx->{-anchors}->{$attr} or return;
113 $ok ? "<a\nhref=#i$attr\nid=$attr>diff</a> --git" : undef
116 sub diff_header ($$$) {
117 my ($x, $ctx, $top) = @_;
118 my (undef, undef, $pa, $pb) = splice(@$top, 0, 4); # ignore oid_{a,b}
119 my $spfx = $ctx->{-spfx};
120 my $dctx = { spfx => $spfx };
122 # get rid of leading "a/" or "b/" (or whatever --{src,dst}-prefix are)
123 $pa = (split(m'/', git_unquote($pa), 2))[1] if $pa ne '/dev/null';
124 $pb = (split(m'/', git_unquote($pb), 2))[1] if $pb ne '/dev/null';
125 if ($pa eq $pb && $pb ne '/dev/null') {
126 $dctx->{Q} = "?b=".uri_escape_utf8($pb, $UNSAFE);
129 if ($pb ne '/dev/null') {
130 push @q, 'b='.uri_escape_utf8($pb, $UNSAFE);
132 if ($pa ne '/dev/null') {
133 push @q, 'a='.uri_escape_utf8($pa, $UNSAFE);
135 $dctx->{Q} = '?'.join('&', @q);
138 # linkify early and all at once, since we know the following
139 # subst ops on $$x won't need further escaping:
140 $$x = $ctx->{-linkify}->to_html($$x);
142 # no need to capture oid_a and oid_b on add/delete,
143 # we just linkify OIDs directly via s///e in conditional
144 if (($$x =~ s/$NULL_TO_BLOB/$1 . oid($dctx, $spfx, $2)/e) ||
145 ($$x =~ s/$BLOB_TO_NULL/
146 'index ' . oid($dctx, $spfx, $1) . $2/e)) {
147 } elsif ($$x =~ $BLOB_TO_BLOB) {
148 # modification-only, not add/delete:
149 # linkify hunk headers later using oid_a and oid_b
150 @$dctx{qw(oid_a oid_b)} = ($1, $2);
152 warn "BUG? <$$x> had no ^index line";
154 $$x =~ s!^diff --git!anchor1($ctx, $pb) // 'diff --git'!ems;
155 my $dst = $ctx->{obuf};
156 $$dst .= qq(<span\nclass="head">);
162 sub diff_before_or_after ($$) {
164 my $linkify = $ctx->{-linkify};
165 my $dst = $ctx->{obuf};
166 my $anchors = exists($ctx->{-anchors}) ? 1 : 0;
167 for my $y (split(/(^---\n)/sm, $$x)) {
168 if ($y =~ /\A---\n\z/s) {
169 $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
171 } elsif ($anchors == 3 && $y =~ /^ [0-9]+ files? changed, /sm) {
172 # ok, looks like a diffstat, go line-by-line:
173 for my $l (split(/^/m, $y)) {
174 if ($l =~ /^ (.+)( +\| .*\z)/s) {
175 anchor0($dst, $ctx, $1, $2) and next;
177 $$dst .= $linkify->to_html($l);
179 } else { # commit message, notes, etc
180 $$dst .= $linkify->to_html($y);
185 # callers must do CRLF => LF conversion before calling this
186 sub flush_diff ($$) {
187 my ($ctx, $cur) = @_;
189 my @top = split($EXTRACT_DIFFS, $$cur);
190 undef $$cur; # free memory
192 my $linkify = $ctx->{-linkify};
193 my $dst = $ctx->{obuf};
194 my $dctx; # {}, keys: Q, oid_a, oid_b
196 while (defined(my $x = shift @top)) {
197 if (scalar(@top) >= 4 &&
198 $top[1] =~ $IS_OID &&
199 $top[0] =~ $IS_OID) {
200 $dctx = diff_header(\$x, $ctx, \@top);
204 # Quiet "Complex regular subexpression recursion limit"
205 # warning. Perl will truncate matches upon hitting
206 # that limit, giving us more (and shorter) scalars than
207 # would be ideal, but otherwise it's harmless.
209 # We could replace the `+' metacharacter with `{1,100}'
210 # to limit the matches ourselves to 100, but we can
211 # let Perl do it for us, quietly.
212 no warnings 'regexp';
214 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
216 (?:^@@ [^\n]+\n))/xsm, $x)) {
217 if (!defined($dctx)) {
219 } elsif ($s =~ s/\A@@ (\S+) (\S+) @@//) {
220 $$dst .= qq(<span\nclass="hunk">);
221 diff_hunk($dst, $dctx, $1, $2);
222 $$dst .= $linkify->to_html($s);
224 } elsif ($s =~ /\A\+/) {
225 $$dst .= qq(<span\nclass="add">);
226 $$dst .= $linkify->to_html($s);
228 } elsif ($s =~ /\A-- $/sm) { # email sig starts
231 } elsif ($s =~ /\A-/) {
232 $$dst .= qq(<span\nclass="del">);
233 $$dst .= $linkify->to_html($s);
236 $$dst .= $linkify->to_html($s);
239 diff_before_or_after($ctx, \$after) unless $dctx;
241 diff_before_or_after($ctx, \$x);