简体   繁体   English

git 分支漂亮格式 '%C(always/auto)'

[英]git branch pretty format '%C(always/auto)'

I'm trying to create an alias for a particular view of my git branches, with the usual verbose data plus some extra.我正在尝试为我的 git 分支的特定视图创建一个别名,其中包含通常的详细数据以及一些额外的数据。 (Datetime; I'm also sorting by date.) This is possible with a custom format string, but it loses the default coloring from git branch -v ; (日期时间;我也按日期排序。)这可以使用自定义格式字符串,但它会丢失git branch -v的默认颜色; the active branch doesn't get highlighted in green and other coloring like upstreams doesn't show up either.活动分支不会以绿色突出显示,并且上游等其他颜色也不会显示。 It looks to me from git docs like this should be fixed by adding %C(always) as a prefix to the format string, but this doesn't seem to work:git 文档看起来我应该通过添加%C(always)作为格式字符串的前缀来修复,但这似乎不起作用:

git branch --format=:%C(always) %(committerdate:short) %(refname:short) %(contents:lines=1)"

just displays只显示

%C(always) 2022-08-23 foo/bar testing Token
%C(always) 2022-08-26 seethefnords fix validation

with no color.没有颜色。 What is wrong here?这里有什么问题?

(Also, just incidentally, is it possible to get the smart alignment back in a custom format string?) (另外,顺便说一句,是否可以将智能 alignment 恢复为自定义格式字符串?)

just displays只显示

%C(always) 2022-08-23 foo/bar testing Token

Then the first thing to check is your Git version.那么首先要检查的是您的 Git 版本。

%C(always) was introduced with Git commit 4261775 , Git 2.22 (Q2 2019). %C(always)是在Git 提交 4261775 、Git 2.22(2019 年第二季度)中引入的。
Which means you might be using an older Git version.这意味着您可能使用的是较旧的 Git 版本。

git version

TL;DR: for git log , you may want %C(always,red) for instance, or you may just want to use git log --color=always directly here. TL;DR:对于git log ,您可能需要%C(always,red)例如,或者您可能只想在此处直接使用git log --color=always Meanwhile git branch uses git for-each-ref formats, which simply don't support the same options (but git branch and git for-each-ref do have --color=always ). Meanwhile git branch uses git for-each-ref formats, which simply don't support the same options (but git branch and git for-each-ref do have --color=always ).

Longer更长

The %C(stuff,goes,here) formatting directive is indeed a color-changing directive, but it does not work the way you think here. %C(stuff,goes,here)格式化指令确实是一个变色指令,但它并不像你在这里想象的那样工作。

Color, in Git, is handled in a sort of complicated way. Git 中的颜色以一种复杂的方式处理。 First, Git assumes an ANSI-style terminal emulator that allows ESC [ <digits and semicolons sequence> m "escape sequences" that control attributes like color, boldface, underline, blinking, italics (if available), and so on.首先,Git 采用 ANSI 样式的终端仿真器,它允许ESC [ <数字和分号序列> m “转义序列”来控制颜色、粗体、下划线、闪烁、斜体(如果可用)等属性。 A set of eight standard colors are available with nummeric values 0, 1, 2, 3, 4, 5, 6, and 7: adding 30 (to get, eg, 33) sets a foreground color, so that:一组八个标准 colors 可提供数值 0、1、2、3、4、5、6 和 7:加上 30(例如,得到 33)设置前景色,因此:

printf "\033[31mred \033[34mblue\033[m\n"

prints the words red blue in red and blue respectively (the \033[m at the end cancels the coloring).分别以红色和蓝色打印单词red blue (末尾的\033[m取消着色)。 Adding 40 instead of 30 obtains a background color instead of a foreground color.添加 40 而不是 30 获得背景颜色而不是前景色。 These can be combined:这些可以组合:

printf "\033[37;41mred \033[44mblue\033[m\n"

prints the words red blue again, but this time the text is white and the background of each word is first red, then blue (the blank in between is also red).再次打印单词red blue ,但这一次文本是白色的,每个单词的背景首先是红色,然后是蓝色(中间的空白也是红色)。

If you like, you can specify these colors with these names: this is very portable these days.如果您愿意,您可以使用这些名称指定这些 colors:这些天非常便携。 Or, you can use raw color values, if your terminal emulator allows them.或者,如果您的终端模拟器允许,您可以使用原始颜色值。 The details for the terminal emulation are covered pretty well here , with some of the Git-side details for selecting such colors covered in How to use ANSI 256-color in.gitconfig .终端仿真的细节在这里很好地介绍了,在如何使用 ANSI 256-color in.gitconfig中介绍了一些用于选择colors 的 Git 端细节。

Now, the thing about color—whether specified by a directive or not—is that Git will only emit color-changing ANSI escape sequences when this is enabled .现在,关于颜色的问题——无论是否由指令指定——是 Git 将仅在启用发出变色的 ANSI 转义序列。 It is enabled when:它在以下情况下启用:

  • the output is going to a terminal and the enablement is auto and there's a color, or output 将进入终端并且启用是auto的并且有颜色,或者
  • it's forced on它是被迫的

and it's disabled whenever it's forced off or there's no color available for that particular mode or output is piped .并且每当它被强制关闭或没有可用于该特定模式的颜色或 output 是管道时,它就会被禁用。

The %C directive in git log can take a color name, perhaps even parenthesized, so %C(red)red%C(reset) puts the word red in red-foreground (with ESC [ 31 m) and then resets color mode (with ESC [ m), but only if color is enabled . git log中的%C指令可以采用颜色名称,甚至可以用括号括起来,因此%C(red)red%C(reset)将单词red置于红色前景(使用 ESC [ 31 m),然后重置颜色模式(使用 ESC [ m),但仅在启用颜色的情况下

The %C(always) modifier applies only to the color also in the parentheses . %C(always)修饰符仅适用于括号中的颜色。 That is, you'd write %C(always,red)red%C(always,reset) to override the lack of --color=always as a command line option.也就是说,您将编写%C(always,red)red%C(always,reset)来覆盖缺少--color=always作为命令行选项。

Adding --color=always is a lot easier and is the way to go if you have that option.添加--color=always要容易得多,如果您有该选项,它是 go 的方法。

(Also, just incidentally, is it possible to get the smart alignment back in a custom format string?) (另外,顺便说一句,是否可以将智能 alignment 恢复为自定义格式字符串?)

I'm not entirely sure what you mean by "smart alignment" here, but aside from %w , %< , and %> directives, there's not a lot of control here.我不完全确定您在这里所说的“智能对齐”是什么意思,但除了%w%<%>指令之外,这里没有太多控制。 Some of the built in formats cannot (currently) be expressed as percent-escapes.某些内置格式(当前)不能表示为百分比转义。 This has very gradually improved over time: Git 1.5 couldn't do very many, Git 2.30 can do a fair number of them.随着时间的推移,这种情况逐渐得到改善:Git 1.5 做不了很多,Git 2.30 可以做很多。

The git for-each-ref plumbing command, and its git branch and git tag porcelain variants, don't have as many formatting directives. git for-each-ref管道命令及其git branchgit tag瓷器变体没有那么多的格式化指令。 In particular there are no color ones at all (though there probably should be).特别是根本没有彩色的(尽管可能应该有)。

Here's a short answer: git branch does not obey the pretty format directives you've linked.这是一个简短的答案: git branch不遵守您链接的漂亮格式指令。 Those are for git log only.这些仅适用于git log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM