summaryrefslogtreecommitdiff
path: root/git-logc
blob: de64a720dcc2c0aadd3f2e057dde7c3eb6af779f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env perl 
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 -S)unless @pager;
    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;
}