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