]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewDiff.pm
view: diffstat anchors for multi-message/attachment views
[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         if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
81                 $ctx->{-anchors}->{$attr} = 1;
82                 $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
83                         ascii_html($fn) . '</a>'.
84                         to_html($linkify, $rest);
85                 return 1;
86         }
87         undef;
88 }
89
90 sub anchor1 ($$$$$) {
91         my ($dst, $ctx, $linkify, $pb, $s) = @_;
92         my $attr = to_attr($ctx->{-apfx}.$pb) or return;
93         my $line = to_html($linkify, $s);
94
95         if (delete $ctx->{-anchors}->{$attr} && $line =~ s/^diff //) {
96                 $$dst .= "<a\nhref=#i$attr\nid=$attr>diff</a> ".$line;
97                 return 1;
98         }
99         undef
100 }
101
102 sub flush_diff ($$$) {
103         my ($dst, $ctx, $linkify) = @_;
104         my $diff = $ctx->{-diff};
105         my $spfx = $ctx->{-spfx};
106         my $state = DSTATE_INIT;
107         my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
108
109         foreach my $s (@$diff) {
110                 if ($s =~ /^---$/) {
111                         to_state($dst, $state, DSTATE_STAT);
112                         $$dst .= $s;
113                 } elsif ($s =~ /^ /) {
114                         # works for common cases, but not weird/long filenames
115                         if ($state == DSTATE_STAT &&
116                                         $s =~ /^ (\S+)(\s+\|.*\z)/s) {
117                                 anchor0($dst, $ctx, $linkify, $1, $2) and next;
118                         } elsif ($state2class[$state]) {
119                                 to_state($dst, $state, DSTATE_CTX);
120                         }
121                         $$dst .= to_html($linkify, $s);
122                 } elsif ($s =~ /^-- $/) { # email signature begins
123                         $state == DSTATE_INIT or
124                                 to_state($dst, $state, DSTATE_INIT);
125                         $$dst .= $s;
126                 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
127                         if ($state != DSTATE_HEAD) {
128                                 my ($pa, $pb) = ($1, $2);
129                                 to_state($dst, $state, DSTATE_HEAD);
130                                 $pa = (split('/', git_unquote($pa), 2))[1];
131                                 $pb = (split('/', git_unquote($pb), 2))[1];
132                                 $dctx = {
133                                         Q => "?b=".uri_escape_utf8($pb, UNSAFE),
134                                 };
135                                 if ($pa ne $pb) {
136                                         $dctx->{Q} .=
137                                              "&a=".uri_escape_utf8($pa, UNSAFE);
138                                 }
139                                 anchor1($dst, $ctx, $linkify, $pb, $s) and next;
140                         }
141                         $$dst .= to_html($linkify, $s);
142                 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
143                         $$dst .= $1 . oid($dctx, $spfx, $2);
144                         $dctx = { Q => '' };
145                         $$dst .= to_html($linkify, $s) ;
146                 } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
147                         $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
148                         $dctx = { Q => '' };
149                         $$dst .= to_html($linkify, $s);
150                 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
151                         $dctx->{oid_a} = $1;
152                         $dctx->{oid_b} = $2;
153                         $$dst .= to_html($linkify, $s);
154                 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
155                         $$dst .= '</span>' if $state2class[$state];
156                         $$dst .= qq(<span\nclass="hunk">);
157                         $$dst .= diff_hunk($dctx, $spfx, $1, $2);
158                         $$dst .= '</span>';
159                         $state = DSTATE_CTX;
160                         $$dst .= to_html($linkify, $s);
161                 } elsif ($s =~ m!^--- $PATH_A! ||
162                          $s =~ m!^\+{3} $PATH_B!)  {
163                         # color only (no oid link)
164                         $state <= DSTATE_STAT and
165                                 to_state($dst, $state, DSTATE_HEAD);
166                         $$dst .= to_html($linkify, $s);
167                 } elsif ($s =~ /^\+/) {
168                         if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
169                                 to_state($dst, $state, DSTATE_ADD);
170                         }
171                         $$dst .= to_html($linkify, $s);
172                 } elsif ($s =~ /^-/) {
173                         if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
174                                 to_state($dst, $state, DSTATE_DEL);
175                         }
176                         $$dst .= to_html($linkify, $s);
177                 # ignore the following lines in headers:
178                 } elsif ($s =~ /^(?:dis)similarity index/ ||
179                          $s =~ /^(?:old|new) mode/ ||
180                          $s =~ /^(?:deleted|new) file mode/ ||
181                          $s =~ /^(?:copy|rename) (?:from|to) / ||
182                          $s =~ /^(?:dis)?similarity index /) {
183                         $$dst .= to_html($linkify, $s);
184                 } else {
185                         $state <= DSTATE_STAT or
186                                 to_state($dst, $state, DSTATE_INIT);
187                         $$dst .= to_html($linkify, $s);
188                 }
189         }
190         @$diff = ();
191         $$dst .= '</span>' if $state2class[$state];
192         undef;
193 }
194
195 1;