问题描述
我使用 git for-each-ref
作为git别名来显示分支名称和该分支上最后一次提交的主题。也就是说,很难判断分支名称的结束位置和提交消息主题的开始位置,因此我试图将分支名称进行着色以更容易地分辨两者之间的差异。下面是没有颜色的工作别名:
[别名]
logbranch = for-each-ref --sort = -committerdate refs / heads / --format ='%(refname:short)%(subject)'
添加颜色,我尝试使用shell颜色转义(我使用bash),如下所示:
[别名]
logbranch = for-each-ref --sort = -committerdate refs / heads / --format ='[\033 [0; 31m]%(refname:short)[\e [m]%(subject) '
这给我一个git配置错误。
想法?
我在 for-each-ref
手册页中看不到任何内容,这表明它支持反斜杠转义序列 \033
。如果用文字转义字符替换 \033
(和 \e
) ,它似乎工作得很好。
Cupcake说 for-each-ref
也支持<$ c $
[别名]
logbranch =for-each-ref --sort = -committerdate refs / heads / --format ='[%1B [0; 31m]%(refname:short)[%1B [m]%(subject)'
这也适用于我的系统。
I'm using git for-each-ref
as a git alias to display a branch name and the subject of the last commit on that branch. That said, it's hard to tell where the branch name ends and the commit message subject starts, so I'm trying to colourize the branch name to more easily tell the difference between the two. Below is the working alias without colour:
[alias]
logbranch = for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(subject)'
To add colour, I tried using shell colour escapes (I'm using bash) like so:
[alias]
logbranch = for-each-ref --sort=-committerdate refs/heads/ --format='[\033[0;31m]%(refname:short)[\e[m] %(subject)'
which gives me a git config error. I also tried replacing the single quotes with double quotes, and escaping the square brackets, but no dice.
Ideas?
I don't see anything in the for-each-ref
man page that suggests it supports backslash-escape sequences like \033
. If you replace \033
(and \e
) with a literal escape character, it seems to work just fine.
Cupcake says that for-each-ref
also supports %xx
hex escape sequences, which would look like:
[alias]
logbranch = "for-each-ref --sort=-committerdate refs/heads/ --format='[%1B[0;31m]%(refname:short)[%1B[m] %(subject)' "
This also works fine on my system.
这篇关于git for-each-ref使用彩色格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!