]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewDiff.pm
view: perform highlighting for space-prefixed diffs
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
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.
8
9 package PublicInbox::ViewDiff;
10 use strict;
11 use warnings;
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 from_attr);
16 use PublicInbox::Git qw(git_unquote);
17
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 } # /^\-/
26
27 # maps the DSTATE_* to CSS class names compatible with what cgit uses:
28 my @state2class = (
29         '', # init
30         '', # stat
31         'head',
32         '', # ctx
33         'add',
34         'del'
35 );
36
37 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
38
39 my $OID_NULL = '0{7,40}';
40 my $OID_BLOB = '[a-f0-9]{7,40}';
41 my $PATH_A = '"?a/.+|/dev/null';
42 my $PATH_B = '"?b/.+|/dev/null';
43
44 sub to_html ($$) {
45         $_[0]->linkify_1($_[1]);
46         $_[0]->linkify_2(ascii_html($_[1]));
47 }
48
49 # link to line numbers in blobs
50 sub diff_hunk ($$$$) {
51         my ($dctx, $spfx, $ca, $cb) = @_;
52         my $oid_a = $dctx->{oid_a};
53         my $oid_b = $dctx->{oid_b};
54
55         (defined($spfx) && defined($oid_a) && defined($oid_b)) or
56                 return "@@ $ca $cb @@";
57
58         my ($n) = ($ca =~ /^-(\d+)/);
59         $n = defined($n) ? do { ++$n; "#n$n" } : '';
60
61         my $rv = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
62
63         ($n) = ($cb =~ /^\+(\d+)/);
64         $n = defined($n) ? do { ++$n; "#n$n" } : '';
65
66         $rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
67 }
68
69 sub oid ($$$) {
70         my ($dctx, $spfx, $oid) = @_;
71         defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
72 }
73
74 sub to_state ($$$) {
75         my ($dst, $state, $new_state) = @_;
76         $$dst .= '</span>' if $state2class[$state];
77         $_[1] = $new_state;
78         my $class = $state2class[$new_state] or return;
79         $$dst .= qq(<span\nclass="$class">);
80 }
81
82 sub anchor0 ($$$$$) {
83         my ($dst, $ctx, $linkify, $fn, $rest) = @_;
84
85         my $orig = $fn;
86
87         # normal git diffstat output is impossible to parse reliably
88         # without --numstat, and that isn't the default for format-patch.
89         # So only do best-effort handling of renames for common cases;
90         # which works well in practice. If projects put "=>", or trailing
91         # spaces in filenames, oh well :P
92         $fn =~ s/ +\z//s;
93         $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
94         $fn = git_unquote($fn);
95
96         # long filenames will require us to walk backwards in anchor1
97         if ($fn =~ s!\A\.\.\./?!!) {
98                 my $lp = $ctx->{-long_path} ||= {};
99                 $lp->{$fn} = qr/\Q$fn\E\z/s;
100         }
101
102         if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
103                 $ctx->{-anchors}->{$attr} = 1;
104                 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
105                         ascii_html($orig) . '</a>'.
106                         to_html($linkify, $rest);
107                 return 1;
108         }
109         undef;
110 }
111
112 sub anchor1 ($$$$$) {
113         my ($dst, $ctx, $linkify, $pb, $s) = @_;
114         my $attr = to_attr($ctx->{-apfx}.$pb) or return;
115         my $line = to_html($linkify, $s);
116
117         my $ok = delete $ctx->{-anchors}->{$attr};
118
119         # unlikely, check the end of all long path names we captured:
120         unless ($ok) {
121                 my $lp = $ctx->{-long_path} or return;
122                 foreach my $fn (keys %$lp) {
123                         $pb =~ $lp->{$fn} or next;
124
125                         delete $lp->{$fn};
126                         $attr = to_attr($ctx->{-apfx}.$fn) or return;
127                         $ok = delete $ctx->{-anchors}->{$attr} or return;
128                         last;
129                 }
130         }
131         if ($ok && $line =~ s/^diff //) {
132                 $$dst .= "<a\nhref=#i$attr\nid=$attr>diff</a> ".$line;
133                 return 1;
134         }
135         undef
136 }
137
138 sub flush_diff ($$$) {
139         my ($dst, $ctx, $linkify) = @_;
140         my $diff = $ctx->{-diff};
141         my $spfx = $ctx->{-spfx};
142         my $state = DSTATE_INIT;
143         my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
144         my $dpfx = $ctx->{-dpfx}; # leading spaces for interdiff
145         my $dpfx_re = qr/\A$dpfx/ if defined $dpfx;
146
147         foreach my $s (@$diff) {
148                 if (defined($dpfx)) {
149                         $s =~ s/$dpfx_re//;
150                         $$dst .= $dpfx;
151                 }
152                 if ($s =~ /^---$/) {
153                         to_state($dst, $state, DSTATE_STAT);
154                         $$dst .= $s;
155                 } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) {
156                         # works for common cases, but not weird/long filenames
157                         if ($state == DSTATE_STAT &&
158                                         $s =~ /^ (.+)( +\| .*\z)/s) {
159                                 anchor0($dst, $ctx, $linkify, $1, $2) and next;
160                         } elsif ($state2class[$state]) {
161                                 to_state($dst, $state, DSTATE_CTX);
162                         }
163                         $$dst .= to_html($linkify, $s);
164                 } elsif ($s =~ /^-- $/) { # email signature begins
165                         $state == DSTATE_INIT or
166                                 to_state($dst, $state, DSTATE_INIT);
167                         $$dst .= $s;
168                 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
169                         my ($pa, $pb) = ($1, $2);
170                         if ($state != DSTATE_HEAD) {
171                                 to_state($dst, $state, DSTATE_HEAD);
172                         }
173                         $pa = (split('/', git_unquote($pa), 2))[1];
174                         $pb = (split('/', git_unquote($pb), 2))[1];
175                         $dctx = {
176                                 Q => "?b=".uri_escape_utf8($pb, UNSAFE),
177                         };
178                         if ($pa ne $pb) {
179                                 $dctx->{Q} .= '&amp;a='.
180                                         uri_escape_utf8($pa, UNSAFE);
181                         }
182                         anchor1($dst, $ctx, $linkify, $pb, $s) and next;
183                         $$dst .= to_html($linkify, $s);
184                 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
185                         $$dst .= $1 . oid($dctx, $spfx, $2);
186                         $dctx = { Q => '' };
187                         $$dst .= to_html($linkify, $s) ;
188                 } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
189                         $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
190                         $dctx = { Q => '' };
191                         $$dst .= to_html($linkify, $s);
192                 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
193                         $dctx->{oid_a} = $1;
194                         $dctx->{oid_b} = $2;
195                         $$dst .= to_html($linkify, $s);
196                 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
197                         $$dst .= '</span>' if $state2class[$state];
198                         $$dst .= qq(<span\nclass="hunk">);
199                         $$dst .= diff_hunk($dctx, $spfx, $1, $2);
200                         $$dst .= '</span>';
201                         $state = DSTATE_CTX;
202                         $$dst .= to_html($linkify, $s);
203                 } elsif ($s =~ m!^--- (?:$PATH_A)! ||
204                          $s =~ m!^\+{3} (?:$PATH_B)!)  {
205                         # color only (no oid link) if missing dctx->{oid_*}
206                         $state <= DSTATE_STAT and
207                                 to_state($dst, $state, DSTATE_HEAD);
208                         $$dst .= to_html($linkify, $s);
209                 } elsif ($s =~ /^\+/) {
210                         if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
211                                 to_state($dst, $state, DSTATE_ADD);
212                         }
213                         $$dst .= to_html($linkify, $s);
214                 } elsif ($s =~ /^-/) {
215                         if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
216                                 to_state($dst, $state, DSTATE_DEL);
217                         }
218                         $$dst .= to_html($linkify, $s);
219                 # ignore the following lines in headers:
220                 } elsif ($s =~ /^(?:dis)similarity index/ ||
221                          $s =~ /^(?:old|new) mode/ ||
222                          $s =~ /^(?:deleted|new) file mode/ ||
223                          $s =~ /^(?:copy|rename) (?:from|to) / ||
224                          $s =~ /^(?:dis)?similarity index /) {
225                         $$dst .= to_html($linkify, $s);
226                 } else {
227                         $state <= DSTATE_STAT or
228                                 to_state($dst, $state, DSTATE_INIT);
229                         $$dst .= to_html($linkify, $s);
230                 }
231         }
232         @$diff = ();
233         $$dst .= '</span>' if $state2class[$state];
234         undef;
235 }
236
237 1;