# Colorized git log

6 messages from 2005-04-21 to 2005-04-22. Participants: Sean, Petr Baudis, Daniel Serpell, Steven Cole.
Thread: https://gitlist.dev/t/231

## Sean, 2005-04-21 21:12

Subject: Colorized git log
Message-ID: <3536.10.10.10.24.1114117965.squirrel@linux1>
URL: https://gitlist.dev/e/3536.10.10.10.24.1114117965.squirrel%40linux1

```

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



```

## Petr Baudis, 2005-04-21 22:42

Subject: Re: Colorized git log
Message-ID: <20050421224229.GR7443@pasky.ji.cz>
URL: https://gitlist.dev/e/20050421224229.GR7443%40pasky.ji.cz
In-Reply-To: <3536.10.10.10.24.1114117965.squirrel@linux1>

```
Dear diary, on Thu, Apr 21, 2005 at 11:12:45PM CEST, I got a letter
where Sean <seanlkml@sympatico.ca> told me that...
> 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.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```

## Daniel Serpell, 2005-04-22 00:46

Subject: [PATCH] Colorized git log
Message-ID: <f0796bb7050421174647943f0c@mail.gmail.com>
URL: https://gitlist.dev/e/f0796bb7050421174647943f0c%40mail.gmail.com
In-Reply-To: <20050421224229.GR7443@pasky.ji.cz>

```
Hi!

On 4/21/05, Petr Baudis <pasky@ucw.cz> wrote:
> 
> 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
--- a/gitlog.sh
+++ 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
--- a/gitlog.sh
+++ b/gitlog.sh
@@ -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=

```

## Petr Baudis, 2005-04-22 00:54

Subject: Re: [PATCH] Colorized git log
Message-ID: <20050422005452.GZ7443@pasky.ji.cz>
URL: https://gitlist.dev/e/20050422005452.GZ7443%40pasky.ji.cz
In-Reply-To: <f0796bb7050421174647943f0c@mail.gmail.com>

```
Dear diary, on Fri, Apr 22, 2005 at 02:46:19AM CEST, I got a letter
where Daniel Serpell <daniel.serpell@gmail.com> told me that...
> Hi!

Hi,

> On 4/21/05, Petr Baudis <pasky@ucw.cz> wrote:
> > 
> > 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?

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

Oh, good catch, thanks.

> * 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. :/

> gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07
> --- a/gitlog.sh
> +++ b/gitlog.sh
> @@ -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=

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).

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```

## Daniel Serpell, 2005-04-22 01:04

Subject: Re: [PATCH] Colorized git log
Message-ID: <f0796bb7050421180433eeeb8@mail.gmail.com>
URL: https://gitlist.dev/e/f0796bb7050421180433eeeb8%40mail.gmail.com
In-Reply-To: <20050422005452.GZ7443@pasky.ji.cz>

```
Hi!

On 4/21/05, Petr Baudis <pasky@ucw.cz> wrote:
> Dear diary, on Fri, Apr 22, 2005 at 02:46:19AM CEST, I got a letter
> where Daniel Serpell <daniel.serpell@gmail.com> told me that...
> >
> > 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.

Signed-off-by:  Daniel Serpell <daniel.serpell@gmail.com>

+++ 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.

Signed-off-by:  Daniel Serpell <daniel.serpell@gmail.com>

gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07
--- a/gitlog.sh
+++ b/gitlog.sh
@@ -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=

```

## Steven Cole, 2005-04-22 01:04

Subject: Re: [PATCH] Colorized git log
Message-ID: <200504211904.18283.elenstev@mesatop.com>
URL: https://gitlist.dev/e/200504211904.18283.elenstev%40mesatop.com
In-Reply-To: <20050422005452.GZ7443@pasky.ji.cz>

```
On Thursday 21 April 2005 06:54 pm, Petr Baudis wrote:

> Duh. And they say "Where possible terminfo is consulted to find the
> string to use." in their manual page. :/
> 
> > gitlog.sh: 6d24d857fb6c2f7e810954adaca1990599906f07
> > --- a/gitlog.sh
> > +++ b/gitlog.sh
> > @@ -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=
> 
> 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

```
