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