简体   繁体   English

Git-预提交挂钩错误颜色

[英]Git - pre-commit hook error color

My pre-commit hook compresses/compiles css/js files. 我的预提交挂钩可压缩/编译css / js文件。 When an error occurs, I simply use echo to output the error and then exit 1 . 发生错误时,我仅使用echo输出错误,然后exit 1 However, the text that's written to the console is WHITE so it's not easy to see when an error occurs. 但是,写入控制台的文本为白色,因此不容易看到何时发生错误。

Is there another way to write to the console (errOut?) that will make the text RED ? 还有另一种写入控制台的方法(errOut?),它将使文本变为红色

Best way to deal with this is to colorize your hook output instead of the PS1 prompt, like so: 解决此问题的最佳方法是为钩子输出着色,而不是PS1提示符,如下所示:

red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
no_color='\033[0m'

echo -e "\n${yellow}Executing pre-commit hook${no_color}\n"

... do your hook stuff ...

if [[ something bad happens ]]; then
    >&2 echo -e "\n${red}ERROR - Something BAD happened!\n${no_color}"
    exit 1
fi

echo -e "${green}Git hook was SUCCESSFUL!${no_color}\n"

Note: Using -e with echo is required - it specifies to interpret special characters, like colors and new lines. 注意:需要将-eecho使用-它指定解释特殊字符,例如颜色和换行符。 ( http://ss64.com/bash/echo.html ) http://ss64.com/bash/echo.html

It might be a good idea to customize you bash, like this: 像这样自定义bash可能是一个好主意:

0 ;) $ cat ~/.bashrc
PS1="\[\033[01;37m\]\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\];)\"; else echo \"\[\033[01;31m\];(\"; fi) $(if [[ ${EUID} == 0 ]]; then echo
 '\[\033[01;31m\]\h'; else echo '\[\033[01;32m\]\u@\h'; fi)\[\033[01;34m\] \w \$\[\033[00m\] "

It displays a green happy face ;) if the last command finished with no errors, and a red sad face ;( if it failed, for example: 如果最后一个命令没有错误完成,它将显示绿色的笑脸;),如果失败则显示红色的悲伤脸;(例如,):

0 ;) $ cat 1.sh
#!/bin/bash
exit 1
0 ;) $ ./1.sh
1 ;( $ 
0 ;( $ cat 1.sh
#!/bin/bash
exit 0
0 ;) $ ./1.sh
0 ;) $

You can customize the output however you want. 您可以根据需要自定义输出。

The example was taken from here 这个例子是从这里拿来的

In action: 实际上: 在此处输入图片说明

Update 更新资料

For Git 2.5 for windows it should be 对于Windows的Git 2.5

if ! \$?; then
  PS1="\[\e[1;32m\]Nice Work!\n\[\e[0;32m\]\u@\h \[\e[0;33m\]\w\[\e[0;37m\]\n\$ " 
else
  PS1="\[\e[1;31m\]Something is wrong!\n\[\e[0;32m\]\u@\h \[\e[0;33m\]\w\[\e[0;37m\]\n\$ "
fi 

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

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