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,40}';
22 my $OID_BLOB = '[a-f0-9]{7,40}';
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) ? do { ++$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) ? do { ++$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 walk backwards in anchor1
86 if ($fn =~ s!\A\.\.\./?!!) {
87 $ctx->{-long_path}->{$fn} = qr/\Q$fn\E\z/s;
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);
101 # returns "diff --git" anchor destination, undef otherwise
104 my $attr = to_attr($ctx->{-apfx}.$pb) or return;
106 my $ok = delete $ctx->{-anchors}->{$attr};
108 # unlikely, check the end of all long path names we captured:
110 my $lp = $ctx->{-long_path} or return;
111 foreach my $fn (keys %$lp) {
112 $pb =~ $lp->{$fn} or next;
115 $attr = to_attr($ctx->{-apfx}.$fn) or return;
116 $ok = delete $ctx->{-anchors}->{$attr} or return;
120 $ok ? "<a\nhref=#i$attr\nid=$attr>diff</a> --git" : undef
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 };
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);
136 if ($pb ne '/dev/null') {
137 push @q, 'b='.uri_escape_utf8($pb, UNSAFE);
139 if ($pa ne '/dev/null') {
140 push @q, 'a='.uri_escape_utf8($pa, UNSAFE);
142 $dctx->{Q} = '?'.join('&', @q);
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);
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);
159 warn "BUG? <$$x> had no ^index line";
161 $$x =~ s!^diff --git!anchor1($ctx, $pb) // 'diff --git'!ems;
162 my $dst = $ctx->{obuf};
163 $$dst .= qq(<span\nclass="head">);
169 sub diff_before_or_after ($$) {
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;
182 $$dst .= $linkify->to_html($l);
184 } else { # commit message, notes, etc
185 $$dst .= $linkify->to_html($y);
190 # callers must do CRLF => LF conversion before calling this
191 sub flush_diff ($$) {
192 my ($ctx, $cur) = @_;
194 my @top = split($EXTRACT_DIFFS, $$cur);
195 undef $$cur; # free memory
197 my $linkify = $ctx->{-linkify};
198 my $dst = $ctx->{obuf};
199 my $dctx; # {}, keys: Q, oid_a, oid_b
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);
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.
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';
219 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
221 (?:^@@ [^\n]+\n))/xsm, $x)) {
222 if (!defined($dctx)) {
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);
229 } elsif ($s =~ /\A\+/) {
230 $$dst .= qq(<span\nclass="add">);
231 $$dst .= $linkify->to_html($s);
233 } elsif ($s =~ /\A-- $/sm) { # email sig starts
236 } elsif ($s =~ /\A-/) {
237 $$dst .= qq(<span\nclass="del">);
238 $$dst .= $linkify->to_html($s);
241 $$dst .= $linkify->to_html($s);
244 diff_before_or_after($ctx, \$after) unless $dctx;
246 diff_before_or_after($ctx, \$x);