问题
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.
回答1:
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
回答2:
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:
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 thecmit_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 inpretty.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.
来源:https://stackoverflow.com/questions/39887690/how-to-reproduce-the-format-of-git-log-oneline-decorate