1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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.
9 package PublicInbox::ViewDiff;
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);
18 sub DSTATE_INIT () { 0 }
19 sub DSTATE_STAT () { 1 } # TODO
20 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
21 sub DSTATE_CTX () { 3 } # /^ /
22 sub DSTATE_ADD () { 4 } # /^\+/
23 sub DSTATE_DEL () { 5 } # /^\-/
33 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
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';
41 $_[0]->linkify_1($_[1]);
42 $_[0]->linkify_2(ascii_html($_[1]));
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};
51 (defined($spfx) && defined($oid_a) && defined($oid_b)) or
52 return "@@ $ca $cb @@";
54 my ($n) = ($ca =~ /^-(\d+)/);
55 $n = defined($n) ? do { ++$n; "#n$n" } : '';
57 my $rv = qq(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
59 ($n) = ($cb =~ /^\+(\d+)/);
60 $n = defined($n) ? do { ++$n; "#n$n" } : '';
62 $rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
66 my ($dctx, $spfx, $oid) = @_;
67 defined($spfx) ? qq(<a\nhref="$spfx$oid/s/$dctx->{Q}">$oid</a>) : $oid;
71 my ($dst, $state, $new_state) = @_;
72 $$dst .= '</span>' if $state2class[$state];
74 my $class = $state2class[$new_state] or return;
75 $$dst .= qq(<span\nclass="$class">);
78 sub flush_diff ($$$$) {
79 my ($dst, $spfx, $linkify, $diff) = @_;
80 my $state = DSTATE_INIT;
81 my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
83 foreach my $s (@$diff) {
85 if ($state2class[$state]) {
86 to_state($dst, $state, DSTATE_CTX);
88 $$dst .= to_html($linkify, $s);
89 } elsif ($s =~ /^-- $/) { # email signature begins
90 $state == DSTATE_INIT or
91 to_state($dst, $state, DSTATE_INIT);
93 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
94 if ($state != DSTATE_HEAD) {
95 my ($pa, $pb) = ($1, $2);
96 to_state($dst, $state, DSTATE_HEAD);
97 $pa = (split('/', git_unquote($pa), 2))[1];
98 $pb = (split('/', git_unquote($pb), 2))[1];
100 Q => "?b=".uri_escape_utf8($pb, UNSAFE),
104 "&a=".uri_escape_utf8($pa, UNSAFE);
107 $$dst .= to_html($linkify, $s);
108 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
109 $$dst .= $1 . oid($dctx, $spfx, $2);
111 $$dst .= to_html($linkify, $s) ;
112 } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
113 $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
115 $$dst .= to_html($linkify, $s);
116 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
119 $$dst .= to_html($linkify, $s);
120 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
121 $$dst .= '</span>' if $state2class[$state];
122 $$dst .= qq(<span\nclass="hunk">);
123 $$dst .= diff_hunk($dctx, $spfx, $1, $2);
126 $$dst .= to_html($linkify, $s);
127 } elsif ($s =~ m!^--- $PATH_A! ||
128 $s =~ m!^\+{3} $PATH_B!) {
129 # color only (no oid link)
130 $state == DSTATE_INIT and
131 to_state($dst, $state, DSTATE_HEAD);
132 $$dst .= to_html($linkify, $s);
133 } elsif ($s =~ /^\+/) {
134 if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
135 to_state($dst, $state, DSTATE_ADD);
137 $$dst .= to_html($linkify, $s);
138 } elsif ($s =~ /^-/) {
139 if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
140 to_state($dst, $state, DSTATE_DEL);
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);
151 $state == DSTATE_INIT or
152 to_state($dst, $state, DSTATE_INIT);
153 $$dst .= to_html($linkify, $s);
157 $$dst .= '</span>' if $state2class[$state];