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 package PublicInbox::ViewDiff;
9 our @EXPORT_OK = qw(flush_diff);
11 use PublicInbox::Hval qw(ascii_html);
12 use PublicInbox::Git qw(git_unquote);
14 sub DSTATE_INIT () { 0 }
15 sub DSTATE_STAT () { 1 } # TODO
16 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
17 sub DSTATE_HUNK () { 3 } # /^@@ /
18 sub DSTATE_CTX () { 4 } # /^ /
19 sub DSTATE_ADD () { 5 } # /^\+/
20 sub DSTATE_DEL () { 6 } # /^\-/
22 my $OID_NULL = '0{7,40}';
23 my $OID_BLOB = '[a-f0-9]{7,40}';
24 my $PATH_A = '"?a/.+|/dev/null';
25 my $PATH_B = '"?b/.+|/dev/null';
28 $_[0]->linkify_1($_[1]);
29 $_[0]->linkify_2(ascii_html($_[1]));
32 # link to line numbers in blobs
33 sub diff_hunk ($$$$) {
34 my ($dctx, $spfx, $ca, $cb) = @_;
35 my $oid_a = $dctx->{oid_a};
36 my $oid_b = $dctx->{oid_b};
38 (defined($oid_a) && defined($oid_b)) or return "@@ $ca $cb @@";
40 my ($n) = ($ca =~ /^-(\d+)/);
41 $n = defined($n) ? do { ++$n; "#n$n" } : '';
43 my $rv = qq(@@ <a\nhref=$spfx$oid_a/s$n>$ca</a>);
45 ($n) = ($cb =~ /^\+(\d+)/);
46 $n = defined($n) ? do { ++$n; "#n$n" } : '';
48 $rv .= qq( <a\nhref=$spfx$oid_b/s$n>$cb</a> @@);
51 sub flush_diff ($$$$) {
52 my ($dst, $spfx, $linkify, $diff) = @_;
53 my $state = DSTATE_INIT;
54 my $dctx; # {}, keys: oid_a, oid_b, path_a, path_b
56 foreach my $s (@$diff) {
58 if ($state == DSTATE_HUNK || $state == DSTATE_ADD ||
59 $state == DSTATE_DEL || $state == DSTATE_HEAD) {
60 $$dst .= "</span><span\nclass=ctx>";
63 $$dst .= to_html($linkify, $s);
64 } elsif ($s =~ /^-- $/) { # email signature begins
65 if ($state != DSTATE_INIT) {
70 } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!x) {
71 if ($state != DSTATE_HEAD) {
72 my ($pa, $pb) = ($1, $2);
73 $$dst .= '</span>' if $state != DSTATE_INIT;
74 $$dst .= "<span\nclass=head>";
76 $pa = (split('/', git_unquote($pa), 2))[1];
77 $pb = (split('/', git_unquote($pb), 2))[1];
78 $dctx = { path_a => $pa, path_b => $pb };
80 $$dst .= to_html($linkify, $s);
81 } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
82 $$dst .= qq($1<a\nhref=$spfx$2/s>$2</a>);
83 $$dst .= to_html($linkify, $s) ;
84 } elsif ($s =~ s/^index ($OID_NULL)(\.\.$OID_BLOB)\b//o) {
86 $$dst .= qq(<a\nhref=$spfx$1/s>$1</a>$2);
87 $$dst .= to_html($linkify, $s);
88 } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
91 $$dst .= to_html($linkify, $s);
92 } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
93 my ($ca, $cb) = ($1, $2);
94 if ($state == DSTATE_HEAD || $state == DSTATE_CTX ||
95 $state == DSTATE_ADD || $state == DSTATE_DEL) {
96 $$dst .= "</span><span\nclass=hunk>";
98 $$dst .= diff_hunk($dctx, $spfx, $ca, $cb);
100 $$dst .= to_html($linkify, "@@ $ca $cb @@");
102 $$dst .= to_html($linkify, $s);
103 } elsif ($s =~ m!^--- $PATH_A!) {
104 if ($state == DSTATE_INIT) { # color only (no oid link)
105 $state = DSTATE_HEAD;
106 $$dst .= "<span\nclass=head>";
108 $$dst .= to_html($linkify, $s);
109 } elsif ($s =~ m!^\+{3} $PATH_B!) {
110 if ($state == DSTATE_INIT) { # color only (no oid link)
111 $state = DSTATE_HEAD;
112 $$dst .= "<span\nclass=head>";
114 $$dst .= to_html($linkify, $s);
115 } elsif ($s =~ /^\+/) {
116 if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
117 $$dst .= "</span><span\nclass=add>";
120 $$dst .= to_html($linkify, $s);
121 } elsif ($s =~ /^-/) {
122 if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
123 $$dst .= "</span><span\nclass=del>";
126 $$dst .= to_html($linkify, $s);
127 # ignore the following lines in headers:
128 } elsif ($s =~ /^(?:dis)similarity index/ ||
129 $s =~ /^(?:old|new) mode/ ||
130 $s =~ /^(?:deleted|new) file mode/ ||
131 $s =~ /^(?:copy|rename) (?:from|to) / ||
132 $s =~ /^(?:dis)?similarity index /) {
133 $$dst .= to_html($linkify, $s);
135 if ($state != DSTATE_INIT) {
137 $state = DSTATE_INIT;
139 $$dst .= to_html($linkify, $s);
143 $$dst .= '</span>' if $state != DSTATE_INIT;