当指定了 --no-color 时,不要使用 git log --pretty 对 refs 进行着色

Don't colorize the refs with git log --pretty when --no-color is specified

我正在使用 Git,使用 Windows 7、PowerShell 和 Posh-Git。我有以下别名设置:

ls = log --pretty=tformat:"%C(yellow)%h\ %C(green)[%ad]%C(cyan)\ <%cn>\ %C(reset)%s%C(auto)%d"

问题是,当我将输出通过管道传输到 clip.exe 以将其复制到剪贴板时,它还会复制一些颜色字符。稍微阅读一下,我发现您可以添加 "auto," 以便在指定 --no-color 选项时使用默认颜色。所以现在我有:

ls = log --pretty=tformat:"%C(auto,yellow)%h\ %C(auto,green)[%ad]%C(auto,cyan)\ <%cn>\ %C(auto,reset)%s%C(auto)%d"

我现在遇到的问题是显示引用的输出的最后一位。我将颜色设置为自动,这样 git 将为分支和标签应用默认颜色,但无法弄清楚如何让它尊重 --no-color 选项。如果我将它设置为 %C(auto),颜色会一直显示。我尝试了 %C(auto,auto) 并且它与 --no-color 一起工作,但没有它 git 抱怨:

error: invalid color value: auto
fatal: unable to parse --pretty format

can't figure out how to get it to respect the --no-color option.
If I set it to %C(auto), the colors show all the time.

这将在 git 2.9.x+(2016 年第 3 季度)

中得到解决

参见 commit b15a3e0 (27 May 2016) by Edward Thomson (ethomson)
(由 Junio C Hamano -- gitster -- in commit 1b3d14c 合并,2016 年 6 月 20 日)

format_commit_message: honor color=auto for %C(auto)

git-log(1) documents that when specifying the %C(auto) format placeholder will "turn on auto coloring on the next %placeholders until the color is switched again."

However, when %C(auto) is used, the present implementation will turn colors on unconditionally (even if the color configuration is turned off for the current context - for example, --no-color was specified or the color is auto and the output is not a tty).

Update format_commit_one to examine the current context when a format string of %C(auto) is specified, which ensures that we will not unconditionally write colors.
This brings that behavior in line with the behavior of %C(auto,<colorname>), and allows the user the ability to specify that color should be displayed only when the output is a tty.

这是你在 git-for-windows 2.9.0

中看到的

但是对于更新的 2.9.x+ git 版本,--no-color 确实有效: