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