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_HUNK () { 3 } # /^@@ /
22 sub DSTATE_CTX () { 4 } # /^ /
23 sub DSTATE_ADD () { 5 } # /^\+/
24 sub DSTATE_DEL () { 6 } # /^\-/
25 sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
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';
33 $_[0]->linkify_1($_[1]);
34 $_[0]->linkify_2(ascii_html($_[1]));
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};
43 (defined($oid_a) && defined($oid_b)) or return "@@ $ca $cb @@";
45 my ($n) = ($ca =~ /^-(\d+)/);
46 $n = defined($n) ? do { ++$n; "#n$n" } : '';
48 my $rv = qq(@@ <a\nhref=$spfx$oid_a/s$dctx->{Q}$n>$ca</a>);
50 ($n) = ($cb =~ /^\+(\d+)/);
51 $n = defined($n) ? do { ++$n; "#n$n" } : '';
53 $rv .= qq( <a\nhref=$spfx$oid_b/s$dctx->{Q}$n>$cb</a> @@);
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
61 foreach my $s (@$diff) {
63 if ($state == DSTATE_HUNK || $state == DSTATE_ADD ||
64 $state == DSTATE_DEL || $state == DSTATE_HEAD) {
65 $$dst .= "</span><span\nclass=ctx>";
68 $$dst .= to_html($linkify, $s);
69 } elsif ($s =~ /^-- $/) { # email signature begins
70 if ($state != DSTATE_INIT) {
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>";
81 $pa = (split('/', git_unquote($pa), 2))[1];
82 $pb = (split('/', git_unquote($pb), 2))[1];
84 Q => "?b=".uri_escape_utf8($pb, UNSAFE),
88 "&a=".uri_escape_utf8($pa, UNSAFE);
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) {
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) {
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);
111 $$dst .= to_html($linkify, "@@ $ca $cb @@");
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>";
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>";
125 $$dst .= to_html($linkify, $s);
126 } elsif ($s =~ /^\+/) {
127 if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
128 $$dst .= "</span><span\nclass=add>";
131 $$dst .= to_html($linkify, $s);
132 } elsif ($s =~ /^-/) {
133 if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
134 $$dst .= "</span><span\nclass=del>";
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);
146 if ($state != DSTATE_INIT) {
148 $state = DSTATE_INIT;
150 $$dst .= to_html($linkify, $s);
154 $$dst .= '</span>' if $state != DSTATE_INIT;