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