简体   繁体   中英

Show merge parents in git log when using pretty format string

Git log documentation says:

If the commit is a merge, and if the pretty-format is not oneline, email or raw, an additional line is inserted before the Author: line. This line begins with "Merge: " and the sha1s of ancestral commits are printed, separated by spaces.

Buy I'm using format.pretty in my git global config, and I don't see the "Merge line".

I could emulate it with the %p (or %P) parameters shown in the git-log documentation :

%P: parent hashes

%p: abbreviated parent hashes

But that shows an empty "Merge:" line if the commit is not a merge commit.

Is there a way to emulate the standard log behavior about merge commits parents while using a pretty format string?

Unfortunately, no, there's no way to do this at the moment. The problem is that you must include %p or %P if and only if this is a merge commit, but there is no "test some condition, execute format directives based on result" format directive. (The closest we get are the %< , %> , etc directives.)

There is one way to work around this but it is slow: use git rev-list to get the commit IDs you want, then log them one at a time with git log -1 or git log --no-walk . This way you can run whatever code you like before invoking git log , such as testing "is this a merge commit". If it is a merge commit, add the desired directives to the format, and if not, leave them out. For instance:

git rev-list HEAD | while read hash; do
    if is-merge $hash; then fmt="... %p ..."; else fmt="... ..."; fi
    git log --no-walk --format="$fmt" $hash
done

where is-merge checks the parent count. (To do that more efficiently, use git rev-list --parents and alter the read command to read hash parents or read hash parent1 rest , after which you can test $parents or $rest . If you're writing in bash you can use arrays.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM