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 ($dst, $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 $$dst .= qq(<span\nclass="head">);
168 sub diff_before_or_after ($$$) {
169 my ($dst, $ctx, $x) = @_;
170 my $linkify = $ctx->{-linkify};
171 for my $y (split(/(^---\n)/sm, $$x)) {
172 if ($y =~ /\A---\n\z/s) {
173 $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
174 } elsif ($y =~ /^ [0-9]+ files? changed, /sm) {
175 # ok, looks like a diffstat, go line-by-line:
176 for my $l (split(/^/m, $y)) {
177 if ($l =~ /^ (.+)( +\| .*\z)/s) {
178 anchor0($dst, $ctx, $1, $2) and next;
180 $$dst .= $linkify->to_html($l);
182 } else { # commit message, notes, etc
183 $$dst .= $linkify->to_html($y);
188 # callers must do CRLF => LF conversion before calling this
189 sub flush_diff ($$$) {
190 my ($dst, $ctx, $cur) = @_;
192 my @top = split($EXTRACT_DIFFS, $$cur);
195 my $linkify = $ctx->{-linkify};
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($dst, \$x, $ctx, \@top);
205 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
207 (?:^@@ [^\n]+\n))/xsm, $x)) {
208 if (!defined($dctx)) {
210 } elsif ($s =~ s/\A@@ (\S+) (\S+) @@//) {
211 $$dst .= qq(<span\nclass="hunk">);
212 diff_hunk($dst, $dctx, $1, $2);
213 $$dst .= $linkify->to_html($s);
215 } elsif ($s =~ /\A\+/) {
216 $$dst .= qq(<span\nclass="add">);
217 $$dst .= $linkify->to_html($s);
219 } elsif ($s =~ /\A-- $/sm) { # email sig starts
222 } elsif ($s =~ /\A-/) {
223 $$dst .= qq(<span\nclass="del">);
224 $$dst .= $linkify->to_html($s);
227 $$dst .= $linkify->to_html($s);
230 diff_before_or_after($dst, $ctx, \$after) unless $dctx;
232 diff_before_or_after($dst, $ctx, \$x);