Git:格式化为正确自动着色 %d

标签 git colors git-log

我阅读 git-log manpage 的方式:

%C(…​): color specification, as described in color.branch.* config option; adding auto, at the beginning will emit color only when colors are enabled for log output (by color.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal). auto alone (i.e. %C(auto)) will turn on auto coloring on the next placeholders until the color is switched again.

那么以下两种格式之一应该是为 % 有条件地请求颜色的正确方法(就像您需要在脚本、别名或命名漂亮格式中做的那样) d%D:

--pretty='%C(auto)%d'
--pretty='%C(auto,auto)%d'

但是,第一种格式 (%C(auto)%d) 似乎不起作用,用这三个命令进行测试——首先,在没有分页器的情况下生成颜色输出;其次,尝试通过管道传输到 cat 来调用自动颜色抑制;最后,显式抑制颜色(注意,您需要检查一个分支和/或标记以查看颜色效果,否则 %d 不输出任何内容):

git --no-pager log -5 --pretty='%C(auto)%d %C(auto,blue)%s'
# correctly shows %d dynamically colored and %s blue

git --no-pager log -5 --pretty='%C(auto)%d %C(auto,blue)%s' | cat
# still colored %d, but %s uncolored

git --no-pager log --color=never -5 --pretty='%C(auto) %d %C(auto,blue)%s'
# same: still colored %d, but %s uncolored

尝试第二种格式 (%C(auto,auto)%d) 也不起作用:

git --no-pager log -5 --pretty='%C(auto,auto) %d %C(auto,blue)%s'
error: invalid color value: auto
fatal: unable to parse --pretty format

git --no-pager log -5 --pretty='%C(auto,auto) %d %C(auto,blue)%s' | cat
# correct, no colors

git --no-pager log --color=never -5 --pretty='%C(auto,auto) %d %C(auto,blue)%s'
# correct, no colors

我不确定我做错了什么。当且仅当输出通常是彩色的时,打印彩色 %d%D 的正确格式顺序是什么?

最佳答案

您的第一种格式在彩色/非彩色输出方面的工作方式与预期一致(至少对于 v2.11.0)。

长话短说

然而,您错过了重置颜色规范,如果在下一个项目的开头(如果没有设置颜色),这会导致错误的颜色。

更正的颜色规范:

%h %C(auto)%d %C(auto,blue)%s%C(auto,reset)

测试:

git config pretty.test "%h %C(auto)%d %C(auto,blue)%s%C(auto,reset)"

echo; git --no-pager log -5 --pretty=test
echo; git --no-pager log -5 --pretty=test | cat
echo; git --no-pager log -5 --pretty=test --color=never

关于Git:格式化为正确自动着色 %d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554406/

相关文章:

windows - 在 Windows 上使用单个命令通过 git log 获取第一行

git - 使用 "git format-patch origin/master --stdout"创建的空补丁

git - 安全的 git rebase

git - 为什么 git checkout 文件的行为类似于重置暂存文件,然后 check out 未暂存文件?

android - 如何在自定义 View 中找到像素颜色?

ios - UINavigationBar 色调颜色不更新

bash - 每次提交的提交中的所有文件的列表

git - Git 提交范围中的双点 ".."和三点 "..."有什么区别?

Github远程: Invalid username or password.致命:身份验证失败

xcode - 在 xcode 颜色 Assets 中按十六进制代码搜索颜色