]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - tig.pl
d7272a3183981ad8040d2c963f9f6fe1a4e03bf5
[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 = '%x00'         # separator from --graph
38            . '%h'  . '%x00' # abbreviated commit hash
39            . '%at' . '%x00' # author date
40            . '%an' . '%x00' # author name
41            . '%s'  . '%x00' # subject
42            . '%d';          # ref names
43 my @cmd = ('git', 'log', '--graph', "--format=$format",
44            # use either given arguments or --all to list all commits
45            (scalar @ARGV) ? @ARGV : '--all');
46 open my $fh, '-|', @cmd or die $!;
47
48 my $pager = $ENV{PAGER};
49 # Try to find an usable pager without searching $PATH.
50 if (not defined $pager) {
51     foreach my $path (qw(/usr/bin/less /bin/less /usr/bin/more /bin/more)) {
52         next if not -x $path;
53
54         $pager = $path;
55         last;
56     }
57 }
58 # Use a pager if STDOUT is a terminal.
59 if (-t STDOUT and defined $pager) {
60     open STDOUT, '|-', $pager or die $!;
61 }
62
63 while (my $line = <$fh>) {
64     # History graph line.
65     if ($line =~ m{^([|/\\_ ]+)$}) {
66         print colored($line, $color_graph);
67         next;
68     }
69
70     # Commit line.
71     $line =~ /^([ *|.\\-]+)\x00(.+)\x00(.+)\x00(.+)\x00(.*)\x00(.*)$/
72         or die $line;
73     my $prefix  = $1;
74     my $hash    = colored($2, $color_hash);
75     my $date    = POSIX::strftime('%Y-%m-%d', localtime($3));
76     my $author  = colored($4, $color_author);
77     my $message = $5;
78     my $refs    = $6;
79
80     # Strip trailing whitespace.
81     $prefix =~ s/\s+$//;
82     # Color "graph".
83     $prefix =~ s/\|/colored($&, $color_graph)/ge;
84
85     # Strip leading whitespace and braces.
86     $refs =~ s/^\s+//;
87     $refs =~ tr/()//d;
88
89     # Color refs.
90     $refs = join colored(', ', $color_ref_sep), map {
91         my $color;
92         if ($_ eq 'HEAD') {
93             $color = $color_ref_head;
94         } elsif (m{/}) {
95             $color = $color_ref_reference;
96         } else {
97             $color = $color_ref_branch;
98         }
99         colored($_, $color);
100     } split /, /, $refs;
101
102     if ($refs ne '') {
103         $refs = ' '
104               . colored('(', $color_ref_sep)
105               . $refs
106               . colored(')', $color_ref_sep);
107     }
108
109     printf "%s %s %s %s%s %s\n",
110            $prefix, $hash, $date, $author, $refs, $message;
111 }
112
113 close $fh or die $!;
114
115 # Necessary for the redirection to a pager or the pager terminates after our
116 # script finishes without displaying all data.
117 if (defined $pager) {
118     close STDOUT or die $!;
119 }