Skip to content

Instantly share code, notes, and snippets.

@mkllnk
Last active April 1, 2016 04:49
Show Gist options
  • Save mkllnk/3ee2886439c893fd57fe to your computer and use it in GitHub Desktop.
Save mkllnk/3ee2886439c893fd57fe to your computer and use it in GitHub Desktop.
Git pre-commit hook for Rubocop offenses
#!/bin/sh
# This script only checks the total number of offenses in modified files.
# If you removed old offenses, it may not warn you about new ones.
# Other scripts check all modified files for offenses.
# They don't ignore old offenses.
NUMBER_BEFORE=`git diff --cached --diff-filter=CMRTUXB --name-only -- '*.rb' | sed 's/^\(.*\)$/HEAD:\1/' | xargs git show | rubocop -s head -f o | tail -n 2 | head -n 1 | cut -d ' ' -f 1`
NUMBER_NOW=`git diff --cached --diff-filter=ACMRTUXB --name-only -- '*.rb' | xargs cat | rubocop -s now -f o | tail -n 2 | head -n 1 | cut -d ' ' -f 1`
if [ "$NUMBER_BEFORE" -lt "$NUMBER_NOW" ]; then
echo "You have $NUMBER_NOW offenses now. There were $NUMBER_BEFORE before."
echo "Display all offenses of modified files by running:"
echo " git diff --diff-filter=ACMRTUXB --name-only | xargs rubocop"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment