简体   繁体   English

使用漂亮的格式字符串时在git日志中显示合并父级

[英]Show merge parents in git log when using pretty format string

Git log documentation says: Git日志文档说:

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. 如果提交是合并,并且如果pretty-format格式不是oneline,email或raw,则在Author:行之前插入另一行。 This line begins with "Merge: " and the sha1s of ancestral commits are printed, separated by spaces. 该行以“ Merge:”开头,并打印祖先提交的sha1,以空格分隔。

Buy I'm using format.pretty in my git global config, and I don't see the "Merge line". 购买我在git全局配置中使用format.pretty,但没有看到“合并行”。

I could emulate it with the %p (or %P) parameters shown in the git-log documentation : 我可以用git-log文档中显示的%p(或%P)参数来模拟它:

%P: parent hashes %P:父哈希

%p: abbreviated parent hashes %p:缩写的父哈希

But that shows an empty "Merge:" line if the commit is not a merge commit. 但是,如果提交不是合并提交,则会显示一个空的“ Merge:”行。

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. 问题是,当且仅当这是一个合并提交时,您才必须包括%p%P ,但是没有“测试某些条件,根据结果执行格式指令”格式指令。 (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 . 有一种解决方法,但是很慢:使用git rev-list获取所需的提交ID,然后使用git log -1git log --no-walk一次git log -1一次。 This way you can run whatever code you like before invoking git log , such as testing "is this a merge commit". 这样,您可以在调用git log之前运行所需的任何代码,例如测试“这是合并提交”。 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. is-merge在哪里检查父级计数。 (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.) (要更有效地执行此操作,请使用git rev-list --parents并更改read命令以read hash parentsread hash parent1 rest ,之后可以测试$parents$rest 。如果您使用bash编写,则可以使用数组。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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