Created
May 22, 2016 09:28
-
-
Save sixertoy/c5ff2e6f46938004aa9e87c20771b631 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Add or remove keywords here | |
| FORBIDDEN="console\.(debug|info|log|warn)\(|alert\(|debugger" | |
| # Add extensions to check here | |
| FILES_PATTERN="\.(js|coffee)(\..+)?$" | |
| LINE="" | |
| VERBOSE=false | |
| ERRORS_BUFFER="" | |
| TEXT_BOLD="\\0033[1m" | |
| TEXT_INFO="\\033[1;32m" | |
| TEXT_ERROR="\\033[1;31m" | |
| TEXT_DEFAULT="\\033[0;39m" | |
| TEXT_UNDERLINE="\\0033[4m" | |
| FILES=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD) | |
| echo -e "\\033[1;33mKeywords checker - pre-commit hook$TEXT_DEFAULT" | |
| echo | |
| for FILE in $FILES; do | |
| if [[ $FILE =~ $FILES_PATTERN ]]; then | |
| ERRORS="" | |
| while IFS=: read -ra RESULT; do | |
| # line number ${RESULT[1]} | |
| ERRORS="$ERRORS\n\tline $TEXT_BOLD${RESULT[1]}$TEXT_DEFAULT: " | |
| ERRORS="$ERRORS"$(sed -n ${RESULT[1]}p $FILE | sed '/^$/d;s/[[:blank:]]//g') | |
| if [ "$ERRORS_BUFFER" != "" ]; then | |
| ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS" | |
| else | |
| ERRORS_BUFFER="$ERRORS" | |
| fi | |
| done < <(grep -sEnH $FORBIDDEN $FILE) | |
| if [ "$ERRORS" != "" ]; then | |
| ERRORS="$TEXT_ERROR Errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT$ERRORS" | |
| echo -e "$ERRORS" | |
| echo | |
| fi | |
| fi | |
| done | |
| if [ "$ERRORS_BUFFER" != "" ]; then | |
| echo -e "$TEXT_ERROR There were errors or warnings, commit aborted.$TEXT_DEFAULT" | |
| # echo -e "$TEXT_INFO" "If you are sure you want to commit those files, use --no-verify option" "$TEXT_DEFAULT" | |
| exit 1 | |
| else | |
| echo -e "$TEXT_INFO All files are clean. $TEXT_DEFAULT" | |
| exit 0 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment