]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - tig.pl
tig.pl: Add, tig-like log view.
[config/dotfiles.git] / tig.pl
1 #!/usr/bin/perl
2
3 # tig-like git log output. Used when tig is not available or broken.
4
5 # Copyright (C) 2013  Simon Ruderich
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 use strict;
22 use warnings;
23
24 use POSIX ();
25 use Term::ANSIColor qw(colored);
26
27
28 my $color_graph         = 'yellow';
29 my $color_hash          = 'cyan';
30 my $color_ref_sep       = 'cyan';
31 my $color_ref_head      = 'cyan bold';
32 my $color_ref_branch    = 'green bold';
33 my $color_ref_reference = 'red bold';
34 my $color_author        = 'magenta';
35
36
37 my $format = '%h'  . '%x00' # abbreviated commit hash
38            . '%at' . '%x00' # author date
39            . '%an' . '%x00' # author name
40            . '%s'  . '%x00' # subject
41            . '%d';          # ref names
42 my $cmd = "git log --all --graph --format='$format'";
43 open my $fh, '-|', $cmd or die $!;
44
45 while (my $line = <$fh>) {
46     # History graph line.
47     if ($line =~ m{^([|/\\ ]+)$}) {
48         print colored($line, $color_graph);
49         next;
50     }
51
52     # Commit line.
53     $line =~ /^([ *|]+) (.+)\x00(.+)\x00(.+)\x00(.+)\x00(.*)$/ or die;
54     my $prefix  = $1;
55     my $hash    = colored($2, $color_hash);
56     my $date    = POSIX::strftime('%Y-%m-%d', localtime($3));
57     my $author  = colored($4, $color_author);
58     my $message = $5;
59     my $refs    = $6;
60
61     # Color "graph".
62     $prefix =~ s/\|/colored($&, $color_graph)/ge;
63
64     # Strip leading whitespace.
65     $refs =~ s/^\s+//;
66
67     printf "%s %s %s %s %s %s\n",
68            $prefix, $hash, $date, $author, $message, $refs;
69 }
70
71 close $fh or die $!;