1 # Copyright (C) 2019 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 base 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 # keep track of state so we can avoid redundant HTML tags for
19 # identically-classed lines
20 sub DSTATE_INIT () { 0 }
21 sub DSTATE_STAT () { 1 }
22 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
23 sub DSTATE_CTX () { 3 } # /^ /
24 sub DSTATE_ADD () { 4 } # /^\+/
25 sub DSTATE_DEL () { 5 } # /^\-/
27 # maps the DSTATE_* to CSS class names compatible with what cgit uses:
37 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
39 my $OID_NULL = '0{7,40}';
40 my $OID_BLOB = '[a-f0-9]{7,40}';
41 my $PATH_X = '"?[^/]+/.+|/dev/null';
43 # cf. git diff.c :: get_compact_summary
44 my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
46 # link to line numbers in blobs
47 sub diff_hunk ($$$$) {
48 my ($dctx, $spfx, $ca, $cb) = @_;
49 my $oid_a = $dctx->{oid_a};
50 my $oid_b = $dctx->{oid_b};
52 (defined($spfx) && defined($oid_a) && defined($oid_b)) or
53 return "@@ $ca $cb @@";
55 my ($n) = ($ca =~ /^-([0-9]+)/);
56 $n = defined($n) ? do { ++$n; "#n$n" } : '';
58 my $rv = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
60 ($n) = ($cb =~ /^\+([0-9]+)/);
61 $n = defined($n) ? do { ++$n; "#n$n" } : '';
63 $rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
67 my ($dctx, $spfx, $oid) = @_;
68 defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
72 my ($dst, $state, $new_state) = @_;
73 $$dst .= '</span>' if $state2class[$state];
75 my $class = $state2class[$new_state] or return;
76 $$dst .= qq(<span\nclass="$class">);
80 my ($dst, $ctx, $linkify, $fn, $rest) = @_;
84 # normal git diffstat output is impossible to parse reliably
85 # without --numstat, and that isn't the default for format-patch.
86 # So only do best-effort handling of renames for common cases;
87 # which works well in practice. If projects put "=>", or trailing
88 # spaces in filenames, oh well :P
89 $fn =~ s/(?: *$DIFFSTAT_COMMENT)? *\z//so;
90 $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
91 $fn = git_unquote($fn);
93 # long filenames will require us to walk backwards in anchor1
94 if ($fn =~ s!\A\.\.\./?!!) {
95 my $lp = $ctx->{-long_path} ||= {};
96 $lp->{$fn} = qr/\Q$fn\E\z/s;
99 if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
100 $ctx->{-anchors}->{$attr} = 1;
101 my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
102 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
103 ascii_html($orig) . '</a>' . $spaces .
104 $linkify->to_html($rest);
110 sub anchor1 ($$$$$) {
111 my ($dst, $ctx, $linkify, $pb, $s) = @_;
112 my $attr = to_attr($ctx->{-apfx}.$pb) or return;
113 my $line = $linkify->to_html($s);
115 my $ok = delete $ctx->{-anchors}->{$attr};
117 # unlikely, check the end of all long path names we captured:
119 my $lp = $ctx->{-long_path} or return;
120 foreach my $fn (keys %$lp) {
121 $pb =~ $lp->{$fn} or next;
124 $attr = to_attr($ctx->{-apfx}.$fn) or return;
125 $ok = delete $ctx->{-anchors}->{$attr} or return;
129 if ($ok && $line =~ s/^diff //) {
130 $$dst .= "<a\nhref=#i$attr\nid=$attr>diff</a> ".$line;
136 sub flush_diff ($$$) {
137 my ($dst, $ctx, $linkify) = @_;
138 my $diff = $ctx->{-diff};
139 my $spfx = $ctx->{-spfx};
140 my $state = DSTATE_INIT;
141 my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
143 foreach my $s (@$diff) {
145 to_state($dst, $state, DSTATE_STAT);
147 } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) {
148 # works for common cases, but not weird/long filenames
149 if ($state == DSTATE_STAT &&
150 $s =~ /^ (.+)( +\| .*\z)/s) {
151 anchor0($dst, $ctx, $linkify, $1, $2) and next;
152 } elsif ($state2class[$state]) {
153 to_state($dst, $state, DSTATE_CTX);
155 $$dst .= $linkify->to_html($s);
156 } elsif ($s =~ /^-- $/) { # email signature begins
157 $state == DSTATE_INIT or
158 to_state($dst, $state, DSTATE_INIT);
160 } elsif ($s =~ m!^diff --git ($PATH_X) ($PATH_X)$!o) {
161 my ($pa, $pb) = ($1, $2);
162 if ($state != DSTATE_HEAD) {
163 to_state($dst, $state, DSTATE_HEAD);
165 $pa = (split('/', git_unquote($pa), 2))[1];
166 $pb = (split('/', git_unquote($pb), 2))[1];
168 Q => "?b=".uri_escape_utf8($pb, UNSAFE),
171 $dctx->{Q} .= '&a='.
172 uri_escape_utf8($pa, UNSAFE);
174 anchor1($dst, $ctx, $linkify, $pb, $s) and next;
175 $$dst .= $linkify->to_html($s);
176 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
177 $$dst .= $1 . oid($dctx, $spfx, $2);
179 $$dst .= $linkify->to_html($s) ;
180 } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
181 $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
183 $$dst .= $linkify->to_html($s);
184 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
187 $$dst .= $linkify->to_html($s);
188 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
189 $$dst .= '</span>' if $state2class[$state];
190 $$dst .= qq(<span\nclass="hunk">);
191 $$dst .= diff_hunk($dctx, $spfx, $1, $2);
194 $$dst .= $linkify->to_html($s);
195 } elsif ($s =~ m!^--- (?:$PATH_X)!o ||
196 $s =~ m!^\+{3} (?:$PATH_X)!o) {
197 # color only (no oid link) if missing dctx->{oid_*}
198 $state <= DSTATE_STAT and
199 to_state($dst, $state, DSTATE_HEAD);
200 $$dst .= $linkify->to_html($s);
201 } elsif ($s =~ /^\+/) {
202 if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
203 to_state($dst, $state, DSTATE_ADD);
205 $$dst .= $linkify->to_html($s);
206 } elsif ($s =~ /^-/) {
207 if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
208 to_state($dst, $state, DSTATE_DEL);
210 $$dst .= $linkify->to_html($s);
211 # ignore the following lines in headers:
212 } elsif ($s =~ /^(?:dis)similarity index/ ||
213 $s =~ /^(?:old|new) mode/ ||
214 $s =~ /^(?:deleted|new) file mode/ ||
215 $s =~ /^(?:copy|rename) (?:from|to) / ||
216 $s =~ /^(?:dis)?similarity index /) {
217 $$dst .= $linkify->to_html($s);
219 $state <= DSTATE_STAT or
220 to_state($dst, $state, DSTATE_INIT);
221 $$dst .= $linkify->to_html($s);
225 $$dst .= '</span>' if $state2class[$state];