在git日志输出中,如何将颜色与条件换行相结合?

9
当我执行以下命令时:
git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%-b"

我得到的输出如下所示:

enter image description here

我希望提交信息能够显示为灰色,因此我尝试在我的格式字符串的末尾插入指令%C(dim)。然而,似乎没有插入位置能够达到我的目标。
  • Inserting %C(dim) after the newlines and before the (conditional newline-chomping) %-b command correctly applies the dimming effect, but breaks conditional newline-chomping:

    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%C(dim)%-b"
    

    enter image description here

  • Inserting %C(dim) before both the newlines and the (conditional newline-chomping) %-b command correctly retains conditional newline-chomping, but fails to apply the dimming effect (i.e. no change from original output):

    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(dim)%n%n%-b"
    

    enter image description here

此外,我不能将“chomp”操作符 - 移动到颜色命令中,因为它总是“计算”为非空字符串(因此,换行符不会被去除)。

有没有办法实现我的目标?

2个回答

3

没有任何已知的怪异行为,最终解决方案在本答案底部

解决方法是在格式字符串中放置一个自定义占位符来表示换行(而不使用%-b魔法),然后使用sed进行输出后处理。在下面的示例中,占位符是CHOMPABLENEWLINES字符串(当然,您可以将其替换为任何您选择的文本,但确保它不会出现在您的提交消息中):

git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(dim)CHOMPABLENEWLINES%b"|sed -e 's/CHOMPABLENEWLINES$//; s/CHOMPABLENEWLINES/\n\n/; $ s/$/\n/'

使用sed后处理的git log带颜色输出

请注意,这种方法还解决了一个问题,即%C指令的效果仅延伸到当前行的末尾(至少在git 2.7.4中是这样)。因此,在多行正文的情况下,颜色仅应用于其第一行。比较如下:

# Only the first line of a multiline message body is colored
git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%Cred%b"

Colored output of git log without sed postprocessing

# Entire multiline message body is colored (though a byproduct of this
# is that the color setting persists beyond the current
# command - note the red prompt following the output)
git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%CredCHOMPABLENEWLINES%b"|sed -e 's/CHOMPABLENEWLINES$//; s/CHOMPABLENEWLINES/\n\n/; $ s/$/\n/'

使用sed后处理的git log彩色输出

如果持续颜色设置产生不良的副作用,可以通过在格式字符串结尾添加%C(reset)指令来抵消这种影响。然后我们还需要一个自定义标记来结束%b占位符,因为从sed的角度来看,视觉上的行末不再是实际的行末了。在下面的示例中,字符串ENDOFBODY被用作这样的标记(当然你必须选择它,以使其不出现在你预期的输出中)。

# This version works with GNU sed. For a portable version (including BSD
# and MacOS X systems) scroll down a little more
git log --format=tformat:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%CredCHOMPABLENEWLINES%bENDOFBODY%C(reset)"|sed -e 's/CHOMPABLENEWLINESENDOFBODY//; s/CHOMPABLENEWLINES/\n\n/; s/ENDOFBODY//'

最终版本的输出

有些版本或设置的git会在输出不直接传输到终端时禁用彩色输出。在这种情况下,您还必须为git log提供--color=always选项。


最终解决方案只使用sed的可移植特性,如下所示:

git log --color=always --format=tformat:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(red)CHOMPABLENEWLINES%bENDOFBODY%C(reset)"|sed -e 's/CHOMPABLENEWLINESENDOFBODY//; s/CHOMPABLENEWLINES/\'$'\n''\'$'\n/; s/ENDOFBODY//'

1
@stkent --format=tformat:类似于--format=format:,但它在输出的最后一行后添加了一个换行符,这有助于稍微缩短sed脚本。 - Leon
是的,所有输出都是白色的。我使用的是macOS 10.13操作系统。 - stkent
添加 --color=always 可以恢复颜色!还有一个问题我无法解决;换行符打印为字面上的 n。我尝试了双重转义,但在这种情况下它们打印为字面上的 \n... :S - stkent
1
@stkent 换行符打印为字面上的 n 这就是我在承诺提供 macOS 正确的 sed 命令时所想到的。我更新了答案,提供了一个可移植版本,在 Linux 和 BSD 系统(包括 macOS)下完全相同。 - Leon
@Leon 另外,我认为可以使用 sed 来解决这个问题?https://dev59.com/fpPea4cB1Zd3GeqP-heJ - angrydust
显示剩余7条评论

2
我认为我找到了一种不依赖于像sed这样的外部工具来完成此操作的方法。
我期望它能够正常工作,只需添加换行控制和颜色格式化程序,两者都可以正常工作,例如:
things things %C(yellow)%+d%C(reset)

但黄色被忽略了。然而,像这样将yellow更改为auto
things things %C(auto)%+d%C(reset)

应用一些颜色,但这是一些默认颜色。不过你可以在gitconfig中更改这些颜色!因此,漂亮的格式不会100%可移植(比如你不能将其嵌入到git命令中以便给同事),但它在我的终端中可行。

为了找到要更改的配置值,我必须进行一些尝试。运行git help -c | grep color查看颜色配置值列表。下面是那些着色%d(给定提交上的所有分支名称)的配置值:

color.decorate.HEAD
color.decorate.branch
color.decorate.grafted
color.decorate.remoteBranch
color.decorate.stash
color.decorate.tag

可能还有另一个类似的配置值可以像楼主想要的那样使提交信息变暗。然后您可以运行git config --global color.decorate.HEAD yellow将其添加到您的gitconfig文件中:

[color "decorate"]
    HEAD = yellow

其他配置值也是如此。

因此,最终我的配置文件会像这样:

[format]
    pretty = format:%C(bold cyan)%h%Creset %C(cyan)<%an>%Creset %Cgreen(%cr)%Creset%  %s%C(auto)%+d%C(reset)
[color "decorate"]
    HEAD = yellow
    branch = yellow
    grafted = yellow
    remoteBranch = yellow
    stash = yellow
    tag = yellow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接