如果消息正文非空,如何让 `git log` 在提交主题行后附加省略号?

问题描述 投票:0回答:1

例如假设这就是我的 git 历史记录:

$ git log --oneline
f9b86d3 (HEAD -> main) blah the baz
966a640 foobar the frob
ac27e4d Revert "do something special"
338ff0f do something special
...

但是假设

966a640
ac27e4d
在其提交消息正文中还有其他行。 (
f9b86d3
338ff0f
只是主题行/有空正文。)

如何制作 git log 格式说明符,仅当正文中有其他文本时才在主题行末尾附加省略号?

例如我希望输出如下所示:

$ $ git log --abbrev-commit --pretty='%h%d %s ???'
f9b86d3 (HEAD -> main) blah the baz
966a640 foobar the frob ...
ac27e4d Revert "do something special" ...
338ff0f do something special
...

我找到了

%<([n],trunc)
并且我可以使用 %<(1,trunc)% b
几乎
到达那里(其中
%b
是提交消息正文,中间的空格如果非空则在前面添加一个空格),但是虽然 是否正确添加了两个字符的省略号,不幸的是,它似乎保留了(仅)正文中的换行符:

$ git log --abbrev-commit --pretty='%h%d %s%<(1,trunc)% b' f9b86d3 (HEAD -> main) blah the baz 966a640 foobar the frob .. ac27e4d Revert "do something special" .. 338ff0f do something special
    
git git-log
1个回答
0
投票
你们非常接近!要从

%<(1,trunc)% b

 中删除多余的换行符,您可以使用 
%-
 后跟保证为空的占位符。

来自

https://git-scm.com/docs/git-log

如果在占位符的

-

 之后添加 
%
(减号),当且仅当占位符扩展为空字符串时,紧邻扩展之前的所有连续换行都会被删除。

您可以通过请求使用不存在的密钥的提交“预告片”来做到这一点,即

%-(trailers:key=x-always-empty)

大家一起:

$ git log --abbrev-commit --pretty='%h%d %s%<(1,trunc)% b%-(trailers:key=x-always-empty)' f9b86d3 (HEAD -> main) blah the baz 966a640 foobar the frob .. ac27e4d Revert "do something special" .. 338ff0f do something special
    
© www.soinside.com 2019 - 2024. All rights reserved.