Skip to contentSkip to conversation

Colorized git log

PATCH In review Sean 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore

Sean

author
#0 lore raw

On the off chance that someone else might like it, here is a pager script for the git log that adds a splash of color:

#!/bin/sh
git log "$@" | sed -re '

     /.*signed-off-by.*/Is//\x1b[34m&\x1b[0m/
     /^--*$/Is//\x1b[31m&\x1b[0m/
     /^(commit|tree|parent) .*/Is//\x1b[32m&\x1b[0m/
     /^author .*/Is//\x1b[36m&\x1b[0m/
     /^committer .*/Is//\x1b[35m&\x1b[0m/'  | less -R

Sean

#1 lore raw

Dear diary, on Thu, Apr 21, 2005 at 11:12:45PM CEST, I got a letter where Sean <[email protected]> told me that...

Quoted 2 lines

On the off chance that someone else might like it, here is a pager script for the git log that adds a splash of color:

Actually, I would've never expected that I would like it, but we all sometimes surprise ourselves.

I made git log colorized if you pass it -c in current git-pasky. Thanks.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#2 lore raw

PATCH 1/1 Colorized git log

Hi!

Quoted 3 lines · Petr Baudis

I made git log colorized if you pass it -c in current git-pasky.

This has two problems, solved in two patches:
* A space is added in front of header lines when you use color.
* It does not work in my (Debian) xterm. This is because here "setterm" only works with TERM=linux.

-------------------------
Strip space in front of colorized header lines.

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07

gitlog.sh +4 -4
@@ -27,7 +27,7 @@ fi
 base=$(gitXnormid.sh -c $1) || exit 1
 
 rev-tree $base | sort -rn | while read time commit parents; do
-	echo $colheader commit ${commit%:*} $coldefault;
+	echo $colheader""commit ${commit%:*} $coldefault;
 	cat-file commit $commit | \
 		while read key rest; do
 			case "$key" in
@@ -43,10 +43,10 @@ rev-tree $base | sort -rn | while read t
 				dtz=${tz/+/+ }; dtz=${dtz/-/- }
 				pdate="$(date -Rud "1970-01-01 UTC + $sec sec $dtz" 2>/dev/null)"
 				if [ "$pdate" ]; then
-					echo -n $color $key $rest | sed "s/>.*/> ${pdate/+0000/$tz}/"
+					echo -n $color$key $rest | sed "s/>.*/> ${pdate/+0000/$tz}/"
 					echo $coldefault
 				else
-					echo $color $key $rest $coldefault
+					echo $color$key $rest $coldefault
 				fi
 				;;
 			"")
@@ -56,7 +56,7 @@ rev-tree $base | sort -rn | while read t
 				'
 				;;
 			*)
-				echo $colheader $key $rest $coldefault
+				echo $colheader$key $rest $coldefault
 				;;
 			esac
 

-------------------------
Uses tput instead of setterm to set colors, seems to work with more terminals.

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07

gitlog.sh +5 -5
@@ -11,11 +11,11 @@
 
 if [ "$1" = "-c" ]; then
 	shift
-	colheader=$(setterm -foreground green)
-	colauthor=$(setterm -foreground cyan)
-	colcommitter=$(setterm -foreground magenta)
-	colsignoff=$(setterm -foreground yellow)
-	coldefault=$(setterm -foreground default)
+	colheader="$(tput setaf 2)"
+	colauthor="$(tput setaf 6)"
+	colcommitter="$(tput setaf 5)"
+	colsignoff="$(tput setaf 3)"
+	coldefault="$(tput op)"
 else
 	colheader=
 	colauthor=
#3 lore raw

Dear diary, on Fri, Apr 22, 2005 at 02:46:19AM CEST, I got a letter where Daniel Serpell <[email protected]> told me that...

Quoted 1 line

Hi!

Hi,

Quoted 6 lines · Petr Baudis
Quoted 3 lines

I made git log colorized if you pass it -c in current git-pasky.

This has two problems, solved in two patches:

could you please sign them off?

Quoted 1 line

* A space is added in front of header lines when you use color.

Oh, good catch, thanks.

Quoted 2 lines

* It does not work in my (Debian) xterm. This is because here "setterm" only works with TERM=linux.

Duh. And they say "Where possible terminfo is consulted to find the string to use." in their manual page. :/

Quoted 10 lines

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07

gitlog.sh +0 -3
@@ -11,11 +11,11 @@
 
 if [ "$1" = "-c" ]; then
 	shift
-	colheader=$(setterm -foreground green)
-	colauthor=$(setterm -foreground cyan)
-	colcommitter=$(setterm -foreground magenta)

gitlog.sh line 21

Quoted 10 lines
gitlog.sh +5 -2
-	colsignoff=$(setterm -foreground yellow)
-	coldefault=$(setterm -foreground default)
+	colheader="$(tput setaf 2)"
+	colauthor="$(tput setaf 6)"
+	colcommitter="$(tput setaf 5)"
+	colsignoff="$(tput setaf 3)"
+	coldefault="$(tput op)"
 else
 	colheader=
 	colauthor=

Please at least stick the colors in comments after the assignment. Not everyone knows ANSI color codes off-hand (the last thing I've memorized were BIOS color codes in the distant DOS days).

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#4 lore raw
Quoted 19 lines · Petr Baudis

Duh. And they say "Where possible terminfo is consulted to find the string to use." in their manual page. :/

Quoted 16 lines

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07

gitlog.sh +4 -5
@@ -11,11 +11,11 @@
 
 if [ "$1" = "-c" ]; then
 	shift
-	colheader=$(setterm -foreground green)
-	colauthor=$(setterm -foreground cyan)
-	colcommitter=$(setterm -foreground magenta)
-	colsignoff=$(setterm -foreground yellow)
-	coldefault=$(setterm -foreground default)
+	colheader="$(tput setaf 2)"
+	colauthor="$(tput setaf 6)"
+	colcommitter="$(tput setaf 5)"
+	colsignoff="$(tput setaf 3)"
Quoted 9 lines

gitlog.sh line 21

Quoted 4 lines
gitlog.sh +1 -0
+	coldefault="$(tput op)"
 else
 	colheader=
 	colauthor=

Please at least stick the colors in comments after the assignment. Not everyone knows ANSI color codes off-hand (the last thing I've memorized were BIOS color codes in the distant DOS days).

I like the color idea, but since many people have their own idea of what colors are appropriate, etc (I use a dark background, and the magenta is painful), perhaps we could have a LOG_COLORS file, similar in concept (but more readable) to the /etc/DIR_COLORS file. Great work !

Steven

#5 lore raw
In reply to Petr Baudis

Hi!

Quoted 6 lines · Petr Baudis

Dear diary, on Fri, Apr 22, 2005 at 02:46:19AM CEST, I got a letter where Daniel Serpell <[email protected]> told me that...

Quoted 2 lines

This has two problems, solved in two patches:

could you please sign them off?

Ok, here are, resent (I suppose it's ok tho put all in one e-mail), and with the comments added.

--------------
Strip space in front of colorized header lines.

+++ b/gitlog.sh
@@ -27,7 +27,7 @@ fi
base=$(gitXnormid.sh -c $1) || exit 1

rev-tree $base | sort -rn | while read time commit parents; do
- echo $colheader commit ${commit%:*} $coldefault;
+ echo $colheader""commit ${commit%:*} $coldefault;
cat-file commit $commit | \
while read key rest; do
case "$key" in
@@ -43,10 +43,10 @@ rev-tree $base | sort -rn | while read t
dtz=${tz/+/+ }; dtz=${dtz/-/- }
pdate="$(date -Rud "1970-01-01 UTC + $sec sec $dtz" 2>/dev/null)" if [ "$pdate" ]; then
- echo -n $color $key $rest | sed "s/>.*/> ${pdate/+0000/$tz}/"
+ echo -n $color$key $rest | sed "s/>.*/> ${pdate/+0000/$tz}/"
echo $coldefault
else
- echo $color $key $rest $coldefault
+ echo $color$key $rest $coldefault
fi
;;
"")
@@ -56,7 +56,7 @@ rev-tree $base | sort -rn | while read t
'
;;
*)
- echo $colheader $key $rest $coldefault
+ echo $colheader$key $rest $coldefault
;;
esac

--------------
Uses tput instead of setterm to set colors, seems to work with more terminals.

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07

gitlog.sh +5 -5
@@ -11,11 +11,11 @@
 
 if [ "$1" = "-c" ]; then
 	shift
-	colheader=$(setterm -foreground green)
-	colauthor=$(setterm -foreground cyan)
-	colcommitter=$(setterm -foreground magenta)
-	colsignoff=$(setterm -foreground yellow)
-	coldefault=$(setterm -foreground default)
+	colheader="$(tput setaf 2)" # Green, see terminfo(5), "Color Handling"
+	colauthor="$(tput setaf 6)" # Cyan
+	colcommitter="$(tput setaf 5)" # Magenta
+	colsignoff="$(tput setaf 3)" # Yellow
+	coldefault="$(tput op)" # Restore default
 else
 	colheader=
 	colauthor=