use PublicInbox::Hval qw(ascii_html to_attr from_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 }
sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
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
my $PATH_A = '"?a/.+|/dev/null';
my $PATH_B = '"?b/.+|/dev/null';
+# cf. git diff.c :: get_compact_summary
+my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
+
sub to_html ($$) {
$_[0]->linkify_1($_[1]);
$_[0]->linkify_2(ascii_html($_[1]));
(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(@@ <a\nhref="$spfx$oid_a/s/$dctx->{Q}$n">$ca</a>);
- ($n) = ($cb =~ /^\+(\d+)/);
+ ($n) = ($cb =~ /^\+([0-9]+)/);
$n = defined($n) ? do { ++$n; "#n$n" } : '';
$rv .= qq( <a\nhref="$spfx$oid_b/s/$dctx->{Q}$n">$cb</a> @@);
# 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/ +\z//s;
+ $fn =~ s/(?: *$DIFFSTAT_COMMENT)? *\z//so;
$fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
$fn = git_unquote($fn);
if ($s =~ /^---$/) {
to_state($dst, $state, DSTATE_STAT);
$$dst .= $s;
- } elsif ($s =~ /^ /) {
+ } elsif ($s =~ /^ / || ($s =~ /^$/ && $state >= DSTATE_CTX)) {
# works for common cases, but not weird/long filenames
if ($state == DSTATE_STAT &&
$s =~ /^ (.+)( +\| .*\z)/s) {
$state == DSTATE_INIT or
to_state($dst, $state, DSTATE_INIT);
$$dst .= $s;
- } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!) {
+ } elsif ($s =~ m!^diff --git ($PATH_A) ($PATH_B)$!o) {
my ($pa, $pb) = ($1, $2);
if ($state != DSTATE_HEAD) {
to_state($dst, $state, DSTATE_HEAD);
$$dst .= '</span>';
$state = DSTATE_CTX;
$$dst .= to_html($linkify, $s);
- } elsif ($s =~ m!^--- $PATH_A! ||
- $s =~ m!^\+{3} $PATH_B!) {
+ } elsif ($s =~ m!^--- (?:$PATH_A)!o ||
+ $s =~ m!^\+{3} (?:$PATH_B)!o) {
# color only (no oid link) if missing dctx->{oid_*}
$state <= DSTATE_STAT and
to_state($dst, $state, DSTATE_HEAD);