Skip to content

Instantly share code, notes, and snippets.

@HelioCampos
Forked from textarcana/pre-commit
Created January 10, 2016 11:17
Show Gist options
  • Save HelioCampos/df1b9209f4a776e14ee0 to your computer and use it in GitHub Desktop.
Save HelioCampos/df1b9209f4a776e14ee0 to your computer and use it in GitHub Desktop.
Pre-commit hook that prevents debugging code and merge artifacts from being committed.
#!/bin/bash
# Pre-commit hook that prevents debugging code and merge artifacts from being committed.
FILES_PATTERN='\.(php|ctp|ctpm|css|rb|erb|haml|js|coffee)(\..+)?$'
FORBIDDEN=( "binding\.pry" "save_and_open_page" "debugger" "it\.only" "describe\.only" ">>>>>>" "<<<<<<" "======" )
# the exit code from `grep -E $FILES_PATTERN` gets swallowed unless the pipefail option is set
set -o pipefail
FORBIDDEN_FOUND=false
# grep for forbidden patterns
for i in "${FORBIDDEN[@]}"
do
git diff --cached --diff-filter=ACMR --name-only | grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $i \
&& echo -e 'WARNING: Found' $i 'references. Commit with \033[0;32m--no-verify \033[0mto bypass this check.' \
&& FORBIDDEN_FOUND=true
done
$FORBIDDEN_FOUND && exit 1 || exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment