]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - tig.pl
gitconfig: Add alias for log with --color-words (lpw).
[config/dotfiles.git] / tig.pl
diff --git a/tig.pl b/tig.pl
index f753408bba21d20eb89af891c55b82e19578349f..c5648a81c321589dde508ad92586aebc8de2c7e0 100755 (executable)
--- a/tig.pl
+++ b/tig.pl
@@ -34,13 +34,39 @@ my $color_ref_reference = 'red bold';
 my $color_author        = 'magenta';
 
 
-my $format = '%h'  . '%x00' # abbreviated commit hash
+# Aliases in Git with "! ..." are always run in the top-level-directory.
+# GIT_PREFIX contains the relative path to the current subdirectory. Thanks to
+# dr_lepper in #git on Freenode (2013-04-03 23:17) for telling me about
+# GIT_PREFIX.
+if (defined $ENV{GIT_PREFIX} and $ENV{GIT_PREFIX} ne '') {
+    chdir $ENV{GIT_PREFIX} or die $!;
+}
+
+my $format = '%x00'         # separator from --graph
+           . '%h'  . '%x00' # abbreviated commit hash
            . '%at' . '%x00' # author date
            . '%an' . '%x00' # author name
            . '%s'  . '%x00' # subject
            . '%d';          # ref names
-my $cmd = "git log --all --graph --format='$format'";
-open my $fh, '-|', $cmd or die $!;
+my @cmd = ('git', 'log', '--graph', "--format=$format",
+           # use either given arguments or --all to list all commits
+           (scalar @ARGV) ? @ARGV : '--all');
+open my $fh, '-|', @cmd or die $!;
+
+my $pager = $ENV{PAGER};
+# Try to find an usable pager without searching $PATH.
+if (not defined $pager) {
+    foreach my $path (qw(/usr/bin/less /bin/less /usr/bin/more /bin/more)) {
+        next if not -x $path;
+
+        $pager = $path;
+        last;
+    }
+}
+# Use a pager if STDOUT is a terminal.
+if (-t STDOUT and defined $pager) {
+    open STDOUT, '|-', $pager or die $!;
+}
 
 while (my $line = <$fh>) {
     # History graph line.
@@ -50,7 +76,8 @@ while (my $line = <$fh>) {
     }
 
     # Commit line.
-    $line =~ /^([ *|]+) (.+)\x00(.+)\x00(.+)\x00(.+)\x00(.*)$/ or die;
+    $line =~ /^([ *|.\\-]+)\x00(.+)\x00(.+)\x00(.+)\x00(.*)\x00(.*)$/
+        or die $line;
     my $prefix  = $1;
     my $hash    = colored($2, $color_hash);
     my $date    = POSIX::strftime('%Y-%m-%d', localtime($3));
@@ -58,6 +85,8 @@ while (my $line = <$fh>) {
     my $message = $5;
     my $refs    = $6;
 
+    # Strip trailing whitespace.
+    $prefix =~ s/\s+$//;
     # Color "graph".
     $prefix =~ s/\|/colored($&, $color_graph)/ge;
 
@@ -90,3 +119,9 @@ while (my $line = <$fh>) {
 }
 
 close $fh or die $!;
+
+# Necessary for the redirection to a pager or the pager terminates after our
+# script finishes without displaying all data.
+if (defined $pager) {
+    close STDOUT or die $!;
+}