2

I am trying to show different color for date and time in the git log output.

Currently i am using git config --global alias.l 'log --pretty=format:"%C(#d33682)%h %C(#b58900)%cd %C(#6c71c4)%ce %C(#2aa198)%s %C(#cb4b16)" --date=format:"%d-%m-%Y %I-%M-%S"'

So, i tried git config --global alias.l 'log --pretty=format:"%C(#d33682)%h %cd %C(#6c71c4)%ce %C(#2aa198)%s %C(#cb4b16)" --date=format:"%C(#b58900)%d-%m-%Y %C(#859900)%I-%M-%S"'

Color inside --date=format: is not working for me.

Any suggestions?

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87

2 Answers2

1

Here you have two options. First one is using %cs which return commit date in format %Y-%m-%d and formats time according to format passed in --date:

git log --pretty=format:"%C(#d33682)%h %Cred%cs %C(#b58900)%cd %C(#6c71c4)%ce %C(#2aa198)%s %C(#cb4b16)" --date=format:"%I-%M-%S"

Another option wound be to truncate output twice, one time from right, another from left. This one would add those .. indicating that the column was truncated…

git log --pretty=format:"%C(#d33682)%h %Cred%<(12,trunc)%cd %C(#b58900)%<(10,ltrunc)%cd %C(#6c71c4)%ce %C(#2aa198)%s %C(#cb4b16)" --date=format:"%d-%m-%Y %I-%M-%S"

Other option, in which you could use any datetime format you wish is to left handling colors for other tool ;)

git log --pretty=format:"%h %cd %ce %s" --date=format:"%d-%m-%Y %I-%M-%S" | awk '{printf "\033[1;31m" $1 "\033[0m \033[1;32m" $2 "\033[0m \033[1;33m" $3 "\033[0m \033[1;34m" $4 "\033[0m \033[1;35m"; $1=$2=$3=$4=""; print substr($0,5)}'
Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
0

OP Here. Thanks to @bk2204, there is another solution.

Run the following command in the terminal once.

git config --global alias.l '!f() {
    git log --format="%ct" | (
    n=0;
    while IFS= read -r line
    do
        date=$(date +"%d-%m-%Y" -d @$line)
        time=$(date +"%H:%M" -d @$line)
        git --no-pager log -1 --skip=$n --decorate\
            --pretty=format:"%C(#d33682)%h %C(#859900)$date %C(#b58900)$time %C(#6c71c4)%ce %C(#2aa198)%s%C(#d30102)%d%n"
        n=$((n + 1))
    done)
}; f'

Now every time you use the command git l, you will get log according to the requirement of this question.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87