use strict;
use warnings;
use 5.014;
use Term::ANSIColor ':constants';
my $tty=-t STDOUT;
$ENV{ANSI_COLORS_DISABLED}=1 unless $tty;
sub parse_line {
my ($line) = @_;
my ($graph,$hash,$decor,$date,$name,$subj) = split /\0/,$line;
return $graph unless defined $hash;
$decor =~ s{^\s+\(|\)$}{}g;
my @decors = split /, /,$decor;
return $graph,$hash,\@decors,$date,$name,$subj;
}
sub class_decor {
my ($decor) = @_;
my $colour=BRIGHT_BLUE;
if ($decor eq 'HEAD') {
$colour=BRIGHT_CYAN;
}
elsif ($decor =~ s{^(?:tag: )?refs/tags/}{tag: }) {
$colour=BRIGHT_YELLOW;
}
elsif ($decor =~ s{^refs/remotes/}{}) {
$colour=BRIGHT_RED;
}
elsif ($decor =~ s{^refs/heads/}{}) {
$colour=BRIGHT_GREEN;
}
return $colour,$decor;
}
open my $fh,'-|','git','log','--graph','--decorate=full','--pretty=format:%x00%h%x00%d%x00%ar%x00%aN%x00%s',($tty ? '--color=always' : ()),@ARGV;
my $out;
if ($tty) {
my @pager = $ENV{PAGER};
@pager = (qw(less -SR)) unless @pager && $pager[0];
open $out,'|-',@pager;
$SIG{PIPE}=sub{exit 0};
}
else {
open $out,'>&',\*STDOUT;
}
while (my $line = <$fh>) {
my ($graph,$hash,$decors,$date,$name,$subj) = parse_line($line);
if (not defined $hash) {
print $out $graph;
next;
}
my $ret = $graph.YELLOW."$hash ".RESET;
if (@$decors) {
$ret .= '('. join(', ',map {
my ($col,$str) = class_decor($_);
BOLD.$col.$str.RESET
}
@$decors).') '
}
$ret .= RED."[$date]".RESET." ".GREEN."$name".RESET." $subj";
print $out $ret;
}