1 # Copyright (C) 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 uri_escape_path);
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 $OID_NULL = '0{7,}';
19 my $OID_BLOB = '[a-f0-9]{7,}';
22 my $FN = qr!(?:"?[^/\n]+/[^\n]+|/dev/null)!;
24 # cf. git diff.c :: get_compact_summary
25 my $DIFFSTAT_COMMENT =
26 qr/(?: *\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\))? *\z/s;
27 my $NULL_TO_BLOB = qr/^(index $OID_NULL\.\.)($OID_BLOB)\b/ms;
28 my $BLOB_TO_NULL = qr/^index ($OID_BLOB)(\.\.$OID_NULL)\b/ms;
29 my $BLOB_TO_BLOB = qr/^index ($OID_BLOB)\.\.($OID_BLOB)/ms;
30 our $EXTRACT_DIFFS = qr/(
31 (?: # begin header stuff, don't capture filenames, here,
32 # but instead wait for the --- and +++ lines.
33 (?:^diff\x20--git\x20$FN\x20$FN$LF)
35 # old mode || new mode || copy|rename|deleted|...
37 )? # end of optional stuff, everything below is required
38 ^index\x20($OID_BLOB)\.\.($OID_BLOB)$ANY*$LF
40 ^\+{3}\x20($FN)$LF)/msx;
41 our $IS_OID = qr/\A$OID_BLOB\z/s;
44 # '/' + $URI::Escape::Unsafe{RFC3986}
45 uri_escape_utf8($_[0], "^A-Za-z0-9\-\._~/");
48 # link to line numbers in blobs
50 my ($dctx, $ca, $cb) = @_;
51 my ($oid_a, $oid_b, $spfx) = @$dctx{qw(oid_a oid_b spfx)};
53 if (defined($spfx) && defined($oid_a) && defined($oid_b)) {
54 my $n = ($ca =~ /^-([0-9]+)/) ? "#n$1" : '';
55 my $x = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
57 $n = ($cb =~ /^\+([0-9]+)/) ? "#n$1" : '';
58 $x .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
65 my ($dctx, $spfx, $oid) = @_;
66 defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
69 # returns true if diffstat anchor written, false otherwise
71 my ($dst, $ctx, $fn, $rest) = @_;
75 # normal git diffstat output is impossible to parse reliably
76 # without --numstat, and that isn't the default for format-patch.
77 # So only do best-effort handling of renames for common cases;
78 # which works well in practice. If projects put "=>", or trailing
79 # spaces in filenames, oh well :P
80 $fn =~ s/$DIFFSTAT_COMMENT//;
81 $fn =~ s/\{(?:.+) => (.+)\}/$1/ or $fn =~ s/.* => (.+)/$1/;
82 $fn = git_unquote($fn);
84 # long filenames will require us to check in anchor1()
85 push(@{$ctx->{-long_path}}, $fn) if $fn =~ s!\A\.\.\./?!!;
87 if (defined(my $attr = to_attr($ctx->{-apfx}.$fn))) {
88 $ctx->{-anchors}->{$attr} = 1;
89 my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
90 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
91 ascii_html($orig) . '</a>' . $spaces .
92 $ctx->{-linkify}->to_html($rest);
98 # returns "diff --git" anchor destination, undef otherwise
101 my $attr = to_attr($ctx->{-apfx}.$pb) // return;
103 my $ok = delete $ctx->{-anchors}->{$attr};
105 # unlikely, check the end of long path names we captured,
106 # assume diffstat and diff output follow the same order,
107 # and ignore different ordering (could be malicious input)
109 my $fn = shift(@{$ctx->{-long_path}}) // return;
110 $pb =~ /\Q$fn\E\z/s or return;
111 $attr = to_attr($ctx->{-apfx}.$fn) // return;
112 $ok = delete $ctx->{-anchors}->{$attr} // return;
114 $ok ? "<a\nhref=#i$attr\nid=$attr>diff</a> --git" : undef
117 sub diff_header ($$$) {
118 my ($x, $ctx, $top) = @_;
119 my (undef, undef, $pa, $pb) = splice(@$top, 0, 4); # ignore oid_{a,b}
120 my $spfx = $ctx->{-spfx};
121 my $dctx = { spfx => $spfx };
123 # get rid of leading "a/" or "b/" (or whatever --{src,dst}-prefix are)
124 $pa = (split(m'/', git_unquote($pa), 2))[1] if $pa ne '/dev/null';
125 $pb = (split(m'/', git_unquote($pb), 2))[1] if $pb ne '/dev/null';
126 if ($pa eq $pb && $pb ne '/dev/null') {
127 $dctx->{Q} = '?b='.uri_escape_path($pb);
130 if ($pb ne '/dev/null') {
131 push @q, 'b='.uri_escape_path($pb);
133 if ($pa ne '/dev/null') {
134 push @q, 'a='.uri_escape_path($pa);
136 $dctx->{Q} = '?'.join('&', @q);
139 # linkify early and all at once, since we know the following
140 # subst ops on $$x won't need further escaping:
141 $$x = $ctx->{-linkify}->to_html($$x);
143 # no need to capture oid_a and oid_b on add/delete,
144 # we just linkify OIDs directly via s///e in conditional
145 if ($$x =~ s/$NULL_TO_BLOB/$1 . oid($dctx, $spfx, $2)/e) {
146 push @{$ctx->{-qry}->{dfpost}}, $2;
147 } elsif ($$x =~ s/$BLOB_TO_NULL/'index '.oid($dctx, $spfx, $1).$2/e) {
148 push @{$ctx->{-qry}->{dfpre}}, $1;
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);
153 push @{$ctx->{-qry}->{dfpre}}, $1;
154 push @{$ctx->{-qry}->{dfpost}}, $2;
156 warn "BUG? <$$x> had no ^index line";
158 $$x =~ s!^diff --git!anchor1($ctx, $pb) // 'diff --git'!ems;
159 my $dst = $ctx->{obuf};
160 $$dst .= qq(<span\nclass="head">);
166 sub diff_before_or_after ($$) {
168 if (exists $ctx->{-anchors} && $$x =~ /\A(.*?) # likely "---\n"
170 ((?:^\x20(?:[^\n]+?)(?:\x20+\|\x20[^\n]*\n))+)
171 (\x20[0-9]+\x20files?\x20)changed,([^\n]+\n)
172 (.*?)\z/msx) { # notes, commit message, etc
173 my @x = ($5, $4, $3, $2, $1);
174 my $lnk = $ctx->{-linkify};
175 $$x = $lnk->to_html(pop @x); # uninteresting prefix
176 for my $l (split(/^/m, pop(@x))) { # per-file diffstat lines
177 $l =~ /^ (.+)( +\| .*\z)/s and
178 anchor0($x, $ctx, $1, $2) and next;
179 $$x .= $lnk->to_html($l);
181 $$x .= pop @x; # $3 /^ \d+ files? /
182 my $ch = $ctx->{changed_href} // '#related';
183 $$x .= qq(<a href="$ch">changed</a>,);
184 $$x .= ascii_html(pop @x); # $4: insertions/deletions
185 $$x .= $lnk->to_html(pop @x); # notes, commit message, etc
187 $ctx->{-linkify}->to_html($$x);
191 # callers must do CRLF => LF conversion before calling this
192 sub flush_diff ($$) {
193 my ($ctx, $cur) = @_;
195 my @top = split($EXTRACT_DIFFS, $$cur);
196 undef $$cur; # free memory
198 my $linkify = $ctx->{-linkify};
199 my $dst = $ctx->{obuf};
200 my $dctx; # {}, keys: Q, oid_a, oid_b
202 while (defined(my $x = shift @top)) {
203 if (scalar(@top) >= 4 &&
204 $top[1] =~ $IS_OID &&
205 $top[0] =~ $IS_OID) {
206 $dctx = diff_header(\$x, $ctx, \@top);
210 # Quiet "Complex regular subexpression recursion limit"
211 # warning. Perl will truncate matches upon hitting
212 # that limit, giving us more (and shorter) scalars than
213 # would be ideal, but otherwise it's harmless.
215 # We could replace the `+' metacharacter with `{1,100}'
216 # to limit the matches ourselves to 100, but we can
217 # let Perl do it for us, quietly.
218 no warnings 'regexp';
220 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
222 (?:^@@ [^\n]+\n))/xsm, $x)) {
223 if (!defined($dctx)) {
225 } elsif ($s =~ s/\A@@ (\S+) (\S+) @@//) {
226 $$dst .= qq(<span\nclass="hunk">);
227 $$dst .= diff_hunk($dctx, $1, $2);
228 $$dst .= $linkify->to_html($s);
230 } elsif ($s =~ /\A\+/) {
231 $$dst .= qq(<span\nclass="add">);
232 $$dst .= $linkify->to_html($s);
234 } elsif ($s =~ /\A-- $/sm) { # email sig starts
237 } elsif ($s =~ /\A-/) {
238 $$dst .= qq(<span\nclass="del">);
239 $$dst .= $linkify->to_html($s);
242 $$dst .= $linkify->to_html($s);
245 $$dst .= diff_before_or_after($ctx, \$after) if !$dctx;
247 $$dst .= diff_before_or_after($ctx, \$x);