]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewDiff.pm
view: enable naming hints for raw blob downloads
[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($oid_a) && defined($oid_b)) or return "@@ $ca $cb @@";
44
45         my ($n) = ($ca =~ /^-(\d+)/);
46         $n = defined($n) ? do { ++$n; "#n$n" } : '';
47
48         my $rv = qq(@@ <a\nhref=$spfx$oid_a/s$dctx->{Q}$n>$ca</a>);
49
50         ($n) = ($cb =~ /^\+(\d+)/);
51         $n = defined($n) ? do { ++$n; "#n$n" } : '';
52
53         $rv .= qq( <a\nhref=$spfx$oid_b/s$dctx->{Q}$n>$cb</a> @@);
54 }
55
56 sub flush_diff ($$$$) {
57         my ($dst, $spfx, $linkify, $diff) = @_;
58         my $state = DSTATE_INIT;
59         my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
60
61         foreach my $s (@$diff) {
62                 if ($s =~ /^ /) {
63                         if ($state == DSTATE_HUNK || $state == DSTATE_ADD ||
64                             $state == DSTATE_DEL || $state == DSTATE_HEAD) {
65                                 $$dst .= "</span><span\nclass=ctx>";
66                                 $state = DSTATE_CTX;
67                         }
68                         $$dst .= to_html($linkify, $s);
69                 } elsif ($s =~ /^-- $/) { # email signature begins
70                         if ($state != DSTATE_INIT) {
71                                 $state = DSTATE_INIT;
72                                 $$dst .= '</span>';
73                         }
74                         $$dst .= $s;
75                 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
76                         if ($state != DSTATE_HEAD) {
77                                 my ($pa, $pb) = ($1, $2);
78                                 $$dst .= '</span>' if $state != DSTATE_INIT;
79                                 $$dst .= "<span\nclass=head>";
80                                 $state = DSTATE_HEAD;
81                                 $pa = (split('/', git_unquote($pa), 2))[1];
82                                 $pb = (split('/', git_unquote($pb), 2))[1];
83                                 $dctx = {
84                                         Q => "?b=".uri_escape_utf8($pb, UNSAFE),
85                                 };
86                                 if ($pa ne $pb) {
87                                         $dctx->{Q} .=
88                                              "&a=".uri_escape_utf8($pa, UNSAFE);
89                                 }
90                         }
91                         $$dst .= to_html($linkify, $s);
92                 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
93                         $$dst .= qq($1<a\nhref=$spfx$2/s$dctx->{Q}>$2</a>);
94                         $$dst .= to_html($linkify, $s) ;
95                 } elsif ($s =~ s/^index ($OID_NULL)(\.\.$OID_BLOB)\b//o) {
96                         $$dst .= 'index ';
97                         $$dst .= qq(<a\nhref=$spfx$1/s$dctx->{Q}>$1</a>$2);
98                         $$dst .= to_html($linkify, $s);
99                 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
100                         $dctx->{oid_a} = $1;
101                         $dctx->{oid_b} = $2;
102                         $$dst .= to_html($linkify, $s);
103                 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
104                         my ($ca, $cb) = ($1, $2);
105                         if ($state == DSTATE_HEAD || $state == DSTATE_CTX ||
106                             $state == DSTATE_ADD || $state == DSTATE_DEL) {
107                                 $$dst .= "</span><span\nclass=hunk>";
108                                 $state = DSTATE_HUNK;
109                                 $$dst .= diff_hunk($dctx, $spfx, $ca, $cb);
110                         } else {
111                                 $$dst .= to_html($linkify, "@@ $ca $cb @@");
112                         }
113                         $$dst .= to_html($linkify, $s);
114                 } elsif ($s =~ m!^--- $PATH_A!) {
115                         if ($state == DSTATE_INIT) { # color only (no oid link)
116                                 $state = DSTATE_HEAD;
117                                 $$dst .= "<span\nclass=head>";
118                         }
119                         $$dst .= to_html($linkify, $s);
120                 } elsif ($s =~ m!^\+{3} $PATH_B!)  {
121                         if ($state == DSTATE_INIT) { # color only (no oid link)
122                                 $state = DSTATE_HEAD;
123                                 $$dst .= "<span\nclass=head>";
124                         }
125                         $$dst .= to_html($linkify, $s);
126                 } elsif ($s =~ /^\+/) {
127                         if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
128                                 $$dst .= "</span><span\nclass=add>";
129                                 $state = DSTATE_ADD;
130                         }
131                         $$dst .= to_html($linkify, $s);
132                 } elsif ($s =~ /^-/) {
133                         if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
134                                 $$dst .= "</span><span\nclass=del>";
135                                 $state = DSTATE_DEL;
136                         }
137                         $$dst .= to_html($linkify, $s);
138                 # ignore the following lines in headers:
139                 } elsif ($s =~ /^(?:dis)similarity index/ ||
140                          $s =~ /^(?:old|new) mode/ ||
141                          $s =~ /^(?:deleted|new) file mode/ ||
142                          $s =~ /^(?:copy|rename) (?:from|to) / ||
143                          $s =~ /^(?:dis)?similarity index /) {
144                         $$dst .= to_html($linkify, $s);
145                 } else {
146                         if ($state != DSTATE_INIT) {
147                                 $$dst .= '</span>';
148                                 $state = DSTATE_INIT;
149                         }
150                         $$dst .= to_html($linkify, $s);
151                 }
152         }
153         @$diff = ();
154         $$dst .= '</span>' if $state != DSTATE_INIT;
155         undef;
156 }
157
158 1;