]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewDiff.pm
viewdiff: support diff-highlighting w/o coderepo
[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);
16 use PublicInbox::Git qw(git_unquote);
17
18 sub DSTATE_INIT () { 0 }
19 sub DSTATE_STAT () { 1 } # TODO
20 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
21 sub DSTATE_HUNK () { 3 } # /^@@ /
22 sub DSTATE_CTX () { 4 } # /^ /
23 sub DSTATE_ADD () { 5 } # /^\+/
24 sub DSTATE_DEL () { 6 } # /^\-/
25 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
26
27 my $OID_NULL = '0{7,40}';
28 my $OID_BLOB = '[a-f0-9]{7,40}';
29 my $PATH_A = '"?a/.+|/dev/null';
30 my $PATH_B = '"?b/.+|/dev/null';
31
32 sub to_html ($$) {
33         $_[0]->linkify_1($_[1]);
34         $_[0]->linkify_2(ascii_html($_[1]));
35 }
36
37 # link to line numbers in blobs
38 sub diff_hunk ($$$$) {
39         my ($dctx, $spfx, $ca, $cb) = @_;
40         my $oid_a = $dctx->{oid_a};
41         my $oid_b = $dctx->{oid_b};
42
43         (defined($spfx) && defined($oid_a) && defined($oid_b)) or
44                 return "@@ $ca $cb @@";
45
46         my ($n) = ($ca =~ /^-(\d+)/);
47         $n = defined($n) ? do { ++$n; "#n$n" } : '';
48
49         my $rv = qq(@@ <a\nhref=$spfx$oid_a/s/$dctx->{Q}$n>$ca</a>);
50
51         ($n) = ($cb =~ /^\+(\d+)/);
52         $n = defined($n) ? do { ++$n; "#n$n" } : '';
53
54         $rv .= qq( <a\nhref=$spfx$oid_b/s/$dctx->{Q}$n>$cb</a> @@);
55 }
56
57 sub oid ($$$) {
58         my ($dctx, $spfx, $oid) = @_;
59         defined($spfx) ? qq(<a\nhref=$spfx$oid/s/$dctx->{Q}>$oid</a>) : $oid;
60 }
61
62 sub flush_diff ($$$$) {
63         my ($dst, $spfx, $linkify, $diff) = @_;
64         my $state = DSTATE_INIT;
65         my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
66
67         foreach my $s (@$diff) {
68                 if ($s =~ /^ /) {
69                         if ($state == DSTATE_HUNK || $state == DSTATE_ADD ||
70                             $state == DSTATE_DEL || $state == DSTATE_HEAD) {
71                                 $$dst .= "</span><span\nclass=ctx>";
72                                 $state = DSTATE_CTX;
73                         }
74                         $$dst .= to_html($linkify, $s);
75                 } elsif ($s =~ /^-- $/) { # email signature begins
76                         if ($state != DSTATE_INIT) {
77                                 $state = DSTATE_INIT;
78                                 $$dst .= '</span>';
79                         }
80                         $$dst .= $s;
81                 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
82                         if ($state != DSTATE_HEAD) {
83                                 my ($pa, $pb) = ($1, $2);
84                                 $$dst .= '</span>' if $state != DSTATE_INIT;
85                                 $$dst .= "<span\nclass=head>";
86                                 $state = DSTATE_HEAD;
87                                 $pa = (split('/', git_unquote($pa), 2))[1];
88                                 $pb = (split('/', git_unquote($pb), 2))[1];
89                                 $dctx = {
90                                         Q => "?b=".uri_escape_utf8($pb, UNSAFE),
91                                 };
92                                 if ($pa ne $pb) {
93                                         $dctx->{Q} .=
94                                              "&a=".uri_escape_utf8($pa, UNSAFE);
95                                 }
96                         }
97                         $$dst .= to_html($linkify, $s);
98                 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
99                         $$dst .= $1 . oid($dctx, $spfx, $2);
100                         $$dst .= to_html($linkify, $s) ;
101                 } elsif ($s =~ s/^index ($OID_NULL)(\.\.$OID_BLOB)\b//o) {
102                         $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
103                         $$dst .= to_html($linkify, $s);
104                 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
105                         $dctx->{oid_a} = $1;
106                         $dctx->{oid_b} = $2;
107                         $$dst .= to_html($linkify, $s);
108                 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
109                         my ($ca, $cb) = ($1, $2);
110                         if ($state == DSTATE_HEAD || $state == DSTATE_CTX ||
111                             $state == DSTATE_ADD || $state == DSTATE_DEL) {
112                                 $$dst .= "</span><span\nclass=hunk>";
113                                 $state = DSTATE_HUNK;
114                                 $$dst .= diff_hunk($dctx, $spfx, $ca, $cb);
115                         } else {
116                                 $$dst .= to_html($linkify, "@@ $ca $cb @@");
117                         }
118                         $$dst .= to_html($linkify, $s);
119                 } elsif ($s =~ m!^--- $PATH_A!) {
120                         if ($state == DSTATE_INIT) { # color only (no oid link)
121                                 $state = DSTATE_HEAD;
122                                 $$dst .= "<span\nclass=head>";
123                         }
124                         $$dst .= to_html($linkify, $s);
125                 } elsif ($s =~ m!^\+{3} $PATH_B!)  {
126                         if ($state == DSTATE_INIT) { # color only (no oid link)
127                                 $state = DSTATE_HEAD;
128                                 $$dst .= "<span\nclass=head>";
129                         }
130                         $$dst .= to_html($linkify, $s);
131                 } elsif ($s =~ /^\+/) {
132                         if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
133                                 $$dst .= "</span><span\nclass=add>";
134                                 $state = DSTATE_ADD;
135                         }
136                         $$dst .= to_html($linkify, $s);
137                 } elsif ($s =~ /^-/) {
138                         if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
139                                 $$dst .= "</span><span\nclass=del>";
140                                 $state = DSTATE_DEL;
141                         }
142                         $$dst .= to_html($linkify, $s);
143                 # ignore the following lines in headers:
144                 } elsif ($s =~ /^(?:dis)similarity index/ ||
145                          $s =~ /^(?:old|new) mode/ ||
146                          $s =~ /^(?:deleted|new) file mode/ ||
147                          $s =~ /^(?:copy|rename) (?:from|to) / ||
148                          $s =~ /^(?:dis)?similarity index /) {
149                         $$dst .= to_html($linkify, $s);
150                 } else {
151                         if ($state != DSTATE_INIT) {
152                                 $$dst .= '</span>';
153                                 $state = DSTATE_INIT;
154                         }
155                         $$dst .= to_html($linkify, $s);
156                 }
157         }
158         @$diff = ();
159         $$dst .= '</span>' if $state != DSTATE_INIT;
160         undef;
161 }
162
163 1;