X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FViewDiff.pm;h=6a60d0dca63450926f9a3c57bd0c1a9aa991509d;hb=a27717d1717b6fdf69e9472e8e9f61b2bbaeb5e7;hp=ee450fa790d49559a1091f10ccd42a602858fa11;hpb=ca0a28d52d43609cd6cabf5af085260032e5afa9;p=public-inbox.git diff --git a/lib/PublicInbox/ViewDiff.pm b/lib/PublicInbox/ViewDiff.pm index ee450fa7..6a60d0dc 100644 --- a/lib/PublicInbox/ViewDiff.pm +++ b/lib/PublicInbox/ViewDiff.pm @@ -2,32 +2,46 @@ # License: AGPL-3.0+ # # used by PublicInbox::View +# This adds CSS spans for diff highlighting. +# It also generates links for ViewVCS + SolverGit to show +# (or reconstruct) blobs. + package PublicInbox::ViewDiff; use strict; use warnings; use base qw(Exporter); our @EXPORT_OK = qw(flush_diff); - -use PublicInbox::Hval qw(ascii_html); +use URI::Escape qw(uri_escape_utf8); +use PublicInbox::Hval qw(ascii_html to_attr); use PublicInbox::Git qw(git_unquote); +# keep track of state so we can avoid redundant HTML tags for +# identically-classed lines sub DSTATE_INIT () { 0 } -sub DSTATE_STAT () { 1 } # TODO +sub DSTATE_STAT () { 1 } sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ / -sub DSTATE_HUNK () { 3 } # /^@@ / -sub DSTATE_CTX () { 4 } # /^ / -sub DSTATE_ADD () { 5 } # /^\+/ -sub DSTATE_DEL () { 6 } # /^\-/ +sub DSTATE_CTX () { 3 } # /^ / +sub DSTATE_ADD () { 4 } # /^\+/ +sub DSTATE_DEL () { 5 } # /^\-/ + +# maps the DSTATE_* to CSS class names compatible with what cgit uses: +my @state2class = ( + '', # init + '', # stat + 'head', + '', # ctx + 'add', + 'del' +); + +sub UNSAFE () { "^A-Za-z0-9\-\._~/" } my $OID_NULL = '0{7,40}'; my $OID_BLOB = '[a-f0-9]{7,40}'; -my $PATH_A = '"?a/.+|/dev/null'; -my $PATH_B = '"?b/.+|/dev/null'; +my $PATH_X = '"?[^/]+/.+|/dev/null'; -sub to_html ($$) { - $_[0]->linkify_1($_[1]); - $_[0]->linkify_2(ascii_html($_[1])); -} +# cf. git diff.c :: get_compact_summary +my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/; # link to line numbers in blobs sub diff_hunk ($$$$) { @@ -35,112 +49,180 @@ sub diff_hunk ($$$$) { my $oid_a = $dctx->{oid_a}; my $oid_b = $dctx->{oid_b}; - (defined($oid_a) && defined($oid_b)) or return "@@ $ca $cb @@"; + (defined($spfx) && defined($oid_a) && defined($oid_b)) or + return "@@ $ca $cb @@"; - my ($n) = ($ca =~ /^-(\d+)/); + my ($n) = ($ca =~ /^-([0-9]+)/); $n = defined($n) ? do { ++$n; "#n$n" } : ''; - my $rv = qq(@@ $ca); + my $rv = qq(@@ {Q}$n">$ca); - ($n) = ($cb =~ /^\+(\d+)/); + ($n) = ($cb =~ /^\+([0-9]+)/); $n = defined($n) ? do { ++$n; "#n$n" } : ''; - $rv .= qq( $cb @@); + $rv .= qq( {Q}$n">$cb @@); +} + +sub oid ($$$) { + my ($dctx, $spfx, $oid) = @_; + defined($spfx) ? qq({Q}">$oid) : $oid; +} + +sub to_state ($$$) { + my ($dst, $state, $new_state) = @_; + $$dst .= '' if $state2class[$state]; + $_[1] = $new_state; + my $class = $state2class[$new_state] or return; + $$dst .= qq(); } -sub flush_diff ($$$$) { - my ($dst, $spfx, $linkify, $diff) = @_; +sub anchor0 ($$$$$) { + my ($dst, $ctx, $linkify, $fn, $rest) = @_; + + my $orig = $fn; + + # normal git diffstat output is impossible to parse reliably + # without --numstat, and that isn't the default for format-patch. + # So only do best-effort handling of renames for common cases; + # which works well in practice. If projects put "=>", or trailing + # spaces in filenames, oh well :P + $fn =~ s/(?: *$DIFFSTAT_COMMENT)? *\z//so; + $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/; + $fn = git_unquote($fn); + + # long filenames will require us to walk backwards in anchor1 + if ($fn =~ s!\A\.\.\./?!!) { + my $lp = $ctx->{-long_path} ||= {}; + $lp->{$fn} = qr/\Q$fn\E\z/s; + } + + if (my $attr = to_attr($ctx->{-apfx}.$fn)) { + $ctx->{-anchors}->{$attr} = 1; + my $spaces = ($orig =~ s/( +)\z//) ? $1 : ''; + $$dst .= " " . + ascii_html($orig) . '' . $spaces . + $linkify->to_html($rest); + return 1; + } + undef; +} + +sub anchor1 ($$$$$) { + my ($dst, $ctx, $linkify, $pb, $s) = @_; + my $attr = to_attr($ctx->{-apfx}.$pb) or return; + my $line = $linkify->to_html($s); + + my $ok = delete $ctx->{-anchors}->{$attr}; + + # unlikely, check the end of all long path names we captured: + unless ($ok) { + my $lp = $ctx->{-long_path} or return; + foreach my $fn (keys %$lp) { + $pb =~ $lp->{$fn} or next; + + delete $lp->{$fn}; + $attr = to_attr($ctx->{-apfx}.$fn) or return; + $ok = delete $ctx->{-anchors}->{$attr} or return; + last; + } + } + if ($ok && $line =~ s/^diff //) { + $$dst .= "diff ".$line; + return 1; + } + undef +} + +sub flush_diff ($$$) { + my ($dst, $ctx, $linkify) = @_; + my $diff = $ctx->{-diff}; + my $spfx = $ctx->{-spfx}; my $state = DSTATE_INIT; - my $dctx; # {}, keys: oid_a, oid_b, path_a, path_b + my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b foreach my $s (@$diff) { - if ($s =~ /^ /) { - if ($state == DSTATE_HUNK || $state == DSTATE_ADD || - $state == DSTATE_DEL || $state == DSTATE_HEAD) { - $$dst .= ""; - $state = DSTATE_CTX; + if ($s =~ /^---$/) { + to_state($dst, $state, DSTATE_STAT); + $$dst .= $s; + } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) { + # works for common cases, but not weird/long filenames + if ($state == DSTATE_STAT && + $s =~ /^ (.+)( +\| .*\z)/s) { + anchor0($dst, $ctx, $linkify, $1, $2) and next; + } elsif ($state2class[$state]) { + to_state($dst, $state, DSTATE_CTX); } - $$dst .= to_html($linkify, $s); + $$dst .= $linkify->to_html($s); } elsif ($s =~ /^-- $/) { # email signature begins - if ($state != DSTATE_INIT) { - $state = DSTATE_INIT; - $$dst .= ''; - } + $state == DSTATE_INIT or + to_state($dst, $state, DSTATE_INIT); $$dst .= $s; - } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!x) { + } elsif ($s =~ m!^diff --git ($PATH_X) ($PATH_X)$!o) { + my ($pa, $pb) = ($1, $2); if ($state != DSTATE_HEAD) { - my ($pa, $pb) = ($1, $2); - $$dst .= '' if $state != DSTATE_INIT; - $$dst .= ""; - $state = DSTATE_HEAD; - $pa = (split('/', git_unquote($pa), 2))[1]; - $pb = (split('/', git_unquote($pb), 2))[1]; - $dctx = { path_a => $pa, path_b => $pb }; + to_state($dst, $state, DSTATE_HEAD); + } + $pa = (split('/', git_unquote($pa), 2))[1]; + $pb = (split('/', git_unquote($pb), 2))[1]; + $dctx = { + Q => "?b=".uri_escape_utf8($pb, UNSAFE), + }; + if ($pa ne $pb) { + $dctx->{Q} .= '&a='. + uri_escape_utf8($pa, UNSAFE); } - $$dst .= to_html($linkify, $s); + anchor1($dst, $ctx, $linkify, $pb, $s) and next; + $$dst .= $linkify->to_html($s); } elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) { - $$dst .= qq($1$2); - $$dst .= to_html($linkify, $s) ; - } elsif ($s =~ s/^index ($OID_NULL)(\.\.$OID_BLOB)\b//o) { - $$dst .= 'index '; - $$dst .= qq($1$2); - $$dst .= to_html($linkify, $s); + $$dst .= $1 . oid($dctx, $spfx, $2); + $dctx = { Q => '' }; + $$dst .= $linkify->to_html($s) ; + } elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) { + $$dst .= 'index ' . oid($dctx, $spfx, $1) . $2; + $dctx = { Q => '' }; + $$dst .= $linkify->to_html($s); } elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) { $dctx->{oid_a} = $1; $dctx->{oid_b} = $2; - $$dst .= to_html($linkify, $s); + $$dst .= $linkify->to_html($s); } elsif ($s =~ s/^@@ (\S+) (\S+) @@//) { - my ($ca, $cb) = ($1, $2); - if ($state == DSTATE_HEAD || $state == DSTATE_CTX || - $state == DSTATE_ADD || $state == DSTATE_DEL) { - $$dst .= ""; - $state = DSTATE_HUNK; - $$dst .= diff_hunk($dctx, $spfx, $ca, $cb); - } else { - $$dst .= to_html($linkify, "@@ $ca $cb @@"); - } - $$dst .= to_html($linkify, $s); - } elsif ($s =~ m!^--- $PATH_A!) { - if ($state == DSTATE_INIT) { # color only (no oid link) - $state = DSTATE_HEAD; - $$dst .= ""; - } - $$dst .= to_html($linkify, $s); - } elsif ($s =~ m!^\+{3} $PATH_B!) { - if ($state == DSTATE_INIT) { # color only (no oid link) - $state = DSTATE_HEAD; - $$dst .= ""; - } - $$dst .= to_html($linkify, $s); + $$dst .= '' if $state2class[$state]; + $$dst .= qq(); + $$dst .= diff_hunk($dctx, $spfx, $1, $2); + $$dst .= ''; + $state = DSTATE_CTX; + $$dst .= $linkify->to_html($s); + } elsif ($s =~ m!^--- (?:$PATH_X)!o || + $s =~ m!^\+{3} (?:$PATH_X)!o) { + # color only (no oid link) if missing dctx->{oid_*} + $state <= DSTATE_STAT and + to_state($dst, $state, DSTATE_HEAD); + $$dst .= $linkify->to_html($s); } elsif ($s =~ /^\+/) { - if ($state != DSTATE_ADD && $state != DSTATE_INIT) { - $$dst .= ""; - $state = DSTATE_ADD; + if ($state != DSTATE_ADD && $state > DSTATE_STAT) { + to_state($dst, $state, DSTATE_ADD); } - $$dst .= to_html($linkify, $s); + $$dst .= $linkify->to_html($s); } elsif ($s =~ /^-/) { - if ($state != DSTATE_DEL && $state != DSTATE_INIT) { - $$dst .= ""; - $state = DSTATE_DEL; + if ($state != DSTATE_DEL && $state > DSTATE_STAT) { + to_state($dst, $state, DSTATE_DEL); } - $$dst .= to_html($linkify, $s); + $$dst .= $linkify->to_html($s); # ignore the following lines in headers: } elsif ($s =~ /^(?:dis)similarity index/ || $s =~ /^(?:old|new) mode/ || $s =~ /^(?:deleted|new) file mode/ || $s =~ /^(?:copy|rename) (?:from|to) / || $s =~ /^(?:dis)?similarity index /) { - $$dst .= to_html($linkify, $s); + $$dst .= $linkify->to_html($s); } else { - if ($state != DSTATE_INIT) { - $$dst .= ''; - $state = DSTATE_INIT; - } - $$dst .= to_html($linkify, $s); + $state <= DSTATE_STAT or + to_state($dst, $state, DSTATE_INIT); + $$dst .= $linkify->to_html($s); } } @$diff = (); - $$dst .= '' if $state != DSTATE_INIT; + $$dst .= '' if $state2class[$state]; undef; }