如何上色git错误,警告和致命的消息? [英] How to colorify git errors, warnings and fatal messages?

查看:119
本文介绍了如何上色git错误,警告和致命的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在排除用户的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆