Skip to content

Instantly share code, notes, and snippets.

@hovissimo
Created May 1, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovissimo/68508c57f07211a9110704e30cf8778f to your computer and use it in GitHub Desktop.
Save hovissimo/68508c57f07211a9110704e30cf8778f to your computer and use it in GitHub Desktop.
#!/bin/zsh
# Redirect output to stderr.
exec 1>&2
# join_by from https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
# pattern is just all of these strings smushed into a big regex pattern
booboos=(
# ruby debug tools
'byebug'
'binding.pry'
'pry-rescue/minitest'
# frontend debug tools
'debugger'
'console\.' # console.log, console.warn, console.error
# frontend test limiters
'.only('
'.skip('
# special comments
'DO NOT COMMIT'
'TODO'
# merge tokens
'[<>|=]\{7\}'
)
pattern=$(join_by '\|' ${booboos[@]})
# Test for boo-boos among lines added in this commit
if test $(git diff --cached | grep ^+\[^+] | grep -e $pattern | wc -l) != 0
then
for file in $(git diff --cached --name-only); do
# This next line shows the offending tokens, but it shows them from the working directory and not from the staging area.
# TODO: Make this show problems in the staging area instead, because that's what you're trying to commit.
# (If the problem is fixed in the working directory but not in the staged files, this tool will puke but not show you the error)
grep --color -Hne $pattern $file # output matches
done
echo "\nFound a boo-boo in your cached files."
echo "Enter \"I'm naughty\" to continue, anything else to exit."
vared -ce response
if [[ ! $response == "I'm naughty" ]]
then
exit 1
fi
fi
# Run Rubocop on added and modified files. Stop commit if there are any offenses.
files=$(git status -s | grep -E 'A|M' | awk '{print $2}')
files="$files $(git status -s | grep -E 'R' | awk '{print $4}')"
echo $files | xargs rubocop --display-cop-names --extra-details --parallel --force-exclusion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment