如何上色git错误,警告和致命的消息? [英] How to colorify git errors, warnings and fatal messages?
问题描述
在排除用户的git问题时,我一直遇到人们不注意到来自git的错误/警告消息,然后燃烧他们的手指。有没有什么办法可以使错误和警告变成git输出?
When troubleshooting git issues of users, I keep running into people not noticing error/warning messages from git, and then burning their fingers. Is there any way to colorify errors and warnings git outputs?
推荐答案
,我的解决方案是当git返回一个错误代码(!= 0)时添加一个额外的警告。
Since I didn't find a suitable way to color error messages, my solution is to add an additional warning when git returns an error code (!=0).
要做到这一点,将它添加到 〜/ .bashrc
或〜/ .bash_profile
To do it, add this to your ~/.bashrc
or ~/.bash_profile
# Wrap git. On errors, print an additional line in red.
git(){
command git "$@"
local exitCode=$?
if [ $exitCode -ne 0 ]; then
printf "\033[0;31mERROR: git exited with code $exitCode\033[0m\n"
return $exitCode
fi
}
结果如下:
Here is the result:
请注意,使用红色着色stderr不能很好地工作,因为git会在stderr中记录很多事情,而不仅仅是错误。标准输出中会输出一些错误。
Note that coloring stderr in red does not work very well because git logs many things in stderr, not only errors. And some errors are output in standard output.
这篇关于如何上色git错误,警告和致命的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!