Search code examples
gitstdoutstderrpre-commit-hook

Git - pre-commit hook error color


My pre-commit hook compresses/compiles css/js files. When an error occurs, I simply use echo to output the error and then 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?


Solution

  • Best way to deal with this is to colorize your hook output instead of the PS1 prompt, like so:

    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. (http://ss64.com/bash/echo.html)