我正在尝试开发一个格式字符串以传递给 git log --pretty
这样每个日志条目都以完整的提交消息结束,但每个日志条目由 分隔。正好一个空行。问题是一些完整的提交消息以换行符结尾,而有些则没有。
例如,假设我有两个提交,abc1234
和 def5678
,但只有 abc1234
在完整提交消息的末尾包含一个换行符。在命令行上输出原始提交内容将如下所示:
[prompt]$ git cat-file commit abc1234
(...)
Title FOO
Full commit message FOO
[prompt]$ git cat-file commit def5678
(...)
Title BAR
Full commit message BAR[prompt]$
注意新的 shell 提示是如何出现在最后一行输出的末尾的,这表明提交
def5678
不是 在完整提交消息的末尾包含一个换行符。假设
def5678
是 abc1234
的父级我想输出一个简单的日志,其中每个条目只包含简短的提交哈希、标题行和完整的提交消息。我可能会尝试这样的事情:[prompt]$ git log --graph --pretty='commit %h%n%B' abc1234
* commit abc1234
| Title FOO
|
| Full commit message FOO
|
* commit def5678
| Title BAR
|
| Full commit message BAR
* commit <parent of def5678>
(...)
请注意日志条目之间的间距。
abc1234
的条目和 def5678
由空行分隔(图形字符除外),但 def5678
的条目而它的 parent 不是。如何构造格式字符串以使间距保持一致,即使完整提交消息的终止不一致?
medium
的内置漂亮格式, full
, fuller
, 和 email
已经这样做了,但我希望能够构造任意格式的字符串来做同样的事情。我已经尝试过
%+B
, %-B
和 % B
序列(以及它们的 %b
和 %n
等价物),但我似乎无法获得一致的间距。如果这有所不同,我将使用 Git 2.17.0。
最佳答案
正如@jthill 在评论中所提到的,并在 git-log(1)
中表示:
If you add a
-
(minus sign) after%
of a placeholder, all consecutive line-feeds immediately preceding the expansion are deleted if and only if the placeholder expands to an empty string.
因此,如果我们能找到一个格式序列
%<token>
那将 总是 展开为空字符串,我们可以使用 %-<token>%n
用单个换行符替换零个或多个连续的换行符。事实证明,有这样一个格式序列:%C()
,空颜色选择器。 (通常,括号包含一个非空字符串,指定要在日志输出中使用的颜色。有关更多详细信息,请参阅 git-log(1)
和 git-config(1)
。)事实
%C()
评估为空字符串而不是导致错误似乎是一个快乐的意外,而不是让人感到高兴的事情,但至少对于 Git 2.17,它可以解决问题。在这一点上,我有足够的信息来回答我自己的问题。保持
git log --pretty=<tformat>
输出的日志条目之间的一致分离,其中 <tformat>
可能会或可能不会评估为以换行符结尾的字符串,附加 %-C()%n
至 <tformat>
. 例如:[prompt]$ git log --graph --pretty='commit %h%n%B%-C()%n' abc1234
* commit abc1234
| Title FOO
|
| Full commit message FOO
|
* commit def5678
| Title BAR
|
| Full commit message BAR
|
* commit <parent of def5678>
(...)
关于git - `git log --pretty` 输出的条目之间一致分隔的格式字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58016135/