How to reproduce the format of git log --oneline --decorate?
Asked Answered
G

2

7

Where is the format of git log --oneline --decorate defined?

Using git Iog --format=format:'my format', I cannot reproduce the colours of the branches, tags and HEAD as shown by git log --oneline --decorate.

 git log --oneline --decorate    

shows HEAD in light blue, the branch names in green and the punctuations (,,) in brown.

The closest I have come to getting what I want is:

git log --graph --abbrev-commit --decorate --date=short --format=format:'%C(bold blue)%h%C(reset) %C(bold green)%ad%C(reset)%d %C(white)%s%C(reset)' -20  

with the only difference being that the branches/HEAD/tags are not coloured like with the previous command.

Gulgee answered 6/10, 2016 at 4:44 Comment(0)
G
9

It does not appear to be possible with git version < 1.8.3.

Since git 1.8.3, one could use the colour %C(auto) token:

   git log --graph --decorate --date=short --format='%C(bold blue)%h%C %C(bold green)%ad %C(auto)%d  %C(white)%s%C(reset)' -10   

The key element being:

%C(auto)%d ...  %C(reset) 

See also:

Color in git-log
Git pretty format colors

Gulgee answered 6/10, 2016 at 5:5 Comment(3)
Instead of %d you may want to use %D for some use cases but %d does match the --oneline --decorate rendering. Also note that %Creset does also work but %Cauto does not work and one must use %C(auto) for nice colors. Also, you may often want tformat instead of format.Arbitrage
@MikkoRantalainen, you are correct about tformat. There is a shorthand --format=xxx for --format=tformat:xxx, but I do not really know when it was introduced, and the answer as it stands is backward-compatible to 1.8.3, so I'm reluctant to edit it. --abbrev-commit seems superfluous, even probably for 1.8.3, however, because %h already stands for the abbreviated form. I'll leave both point to augustin to decide. The third one is that %C(reset) is excessive right before the next %C…. It's a very helpful answer, even in its current form.Sail
@kkm I edited the answer as per your suggestions. Thanks.Gulgee
S
3

TL;DR - I don't think it's possible for you to get the exact format that git log --oneline provides because the --format option doesn't allow any conditional statements, while git-log is using a function to generate that string on a per commit message.

The closest I got with the colors is this:

git log --format=format:'%C(auto)%h%C(reset) %C(auto)%s%C(reset)'

Looks like this on my machine:

terminal-screenshot


Long version:

The most documentation you can read on git-log is here: https://git-scm.com/docs/git-log

I delved into the source (available on github here) to see what exactly git does for the --oneline option, here's what I found:

  • The trail starts here in revision.c, where the command line options are parsed

  • The option is then parsed in pretty.c to match CMIT_FMT_ONELINE of the cmit_fmt enum (defined in commit.h)

  • Finally, the actual printing happens over in pretty.c at pp_commit_easy.

  • This is where it gets tricky and complicated. You can see references to CMIT_FMT_ONELINE in a handful of places in pretty.c. I believe that only pp_title_line is executed for each commit when the --oneline option is specified. You can see that there's a bunch of conditional formatting going on as the string buffer is constructed.

So yeah, I don't think there's a way to replicate the actual format with the tags and (HEAD -> master) meta information. You can probably write a shell script that does the same, but its performance may not match up to that of git's.

Sadducee answered 6/10, 2016 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.