gitgit-loggit-reflog

How to color a relative date placeholder in git reflog?


I want to color just the date part of the identifier git reflog --date, prints so I need to run with the --pretty option. However, there is no placeholder for the date a command is issued, even though you can use the --date option.

For example, here is the timestamps when using git reflog --date=relative:

64d5e420 HEAD@{19 hours ago}: commit (amend): message 2
e3be3a1f HEAD@{19 hours ago}: commit (amend): message 1
edb1e49d HEAD@{19 hours ago}: rebase (finish): returning to refs/heads/main
edb1e49d HEAD@{19 hours ago}: commit (amend): message 1
aff94bd7 HEAD@{19 hours ago}: commit (amend): message 1
c8cc8718 HEAD@{19 hours ago}: rebase: fast-forward
d5224a9e HEAD@{19 hours ago}: rebase (start): checkout c8cc8718a70d02d00d9c26f6879563c629eb60b4~1
1a792993 HEAD@{19 hours ago}: commit (amend): message 2
302aa0ce HEAD@{19 hours ago}: rebase (abort): returning to refs/heads/main
8153cc7e HEAD@{19 hours ago}: commit: debug: exit rebase
b7694429 HEAD@{19 hours ago}: commit (amend): message 1
c8cc8718 HEAD@{19 hours ago}: rebase: fast-forward
d5224a9e HEAD@{19 hours ago}: rebase (start): checkout c8cc8718a70d02d00d9c26f6879563c629eb60b4~1
302aa0ce HEAD@{19 hours ago}: reset: moving to HEAD
302aa0ce HEAD@{19 hours ago}: commit: message 2

You see that all timestamps are 19 hours ago. But if I use the committer date instead (git reflog --pretty="%h %Cred%cr%Creset %gs"):

64d5e420 19 hours ago commit (amend): message 2
e3be3a1f 19 hours ago commit (amend): message 1
edb1e49d 19 hours ago rebase (finish): returning to refs/heads/main
edb1e49d 19 hours ago commit (amend): message 1
aff94bd7 19 hours ago commit (amend): message 1
c8cc8718 9 months ago rebase: fast-forward
d5224a9e 10 months ago rebase (start): checkout c8cc8718a70d02d00d9c26f6879563c629eb60b4~1
1a792993 19 hours ago commit (amend): message 2
302aa0ce 19 hours ago rebase (abort): returning to refs/heads/main
8153cc7e 19 hours ago commit: debug: exit rebase
b7694429 19 hours ago commit (amend): message 1
c8cc8718 9 months ago rebase: fast-forward
d5224a9e 10 months ago rebase (start): checkout c8cc8718a70d02d00d9c26f6879563c629eb60b4~1
302aa0ce 19 hours ago reset: moving to HEAD
302aa0ce 19 hours ago commit: message 2

Then some dates are 9 and 10 months ago. I'd like to not having them in the report.

Is this possible?


Solution

  • There's --date=format: that takes strftime formats, but that doesn't do relative dates, so I think you're going to have to sed to insert the colors yourself.

    red=`tput setaf 1`
    reset=`tput sgr0`
    git reflog --date=relative --color=always --pretty='%C(auto)%h%d %gd %gs' \
    | sed -E "s,([^{}]*)},$red\1$reset},"
    

    does the trick for me.