Skip to content

Instantly share code, notes, and snippets.

@pcofilada
Last active March 11, 2021 11:37
Show Gist options
  • Save pcofilada/5f1bdb4a764fdd5d9df099a536280bb1 to your computer and use it in GitHub Desktop.
Save pcofilada/5f1bdb4a764fdd5d9df099a536280bb1 to your computer and use it in GitHub Desktop.
Pre commit hooks fo rails development
#!/bin/sh
HOOKS="pre-commit-eslint pre-commit-rspec pre-commit-rubocop pre-commit-prevent-master"
for hook in $HOOKS; do
if [ -f "$PWD/.git/hooks/$hook" ]; then
"$PWD/.git/hooks/$hook"
if [ $? != 0 ]; then
exit 1
fi
else
echo "Error: file $hook not found."
exit 1
fi
done
#!/bin/bash
echo -e "[ESLINT] --> init (wait a second)"
files=$(git diff --cached --name-only | grep '\.js\?$')
if [[ $files = "" ]] ; then
exit 0
fi
failed=0
for file in ${files}; do
git show :$file | eslint $file
if [[ $? != 0 ]] ; then
failed=1
fi
done;
if [[ $failed == 0 ]] ; then
echo -e "[ESLINT] --> 👍 approved."
exit 0
else
echo -e "[ESLINT] --> ✋ You've offenses!!!"
exit 1
fi
#!/bin/bash
echo -e "[PREVENT] --> init (wait a second)"
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ $BRANCH == master ]; then
echo -e "[PREVENT] --> Non shall commit to master!"
exit 1
else
echo -e "[PREVENT] --> Prevent commit approved"
exit 0
fi
#!/bin/bash
echo -e "[RSPEC] --> init (wait a second)"
FAILS=`bundle exec rspec --format progress | grep -E '(\d*) failure(s?)' -o | awk '{print $1}'`
if [ $FAILS -ne 0 ]; then
echo -e "[RSPEC] --> ✋ Can't commit! You've broken $FAILS tests!!!"
exit 1
else
echo -e "[RSPEC] --> 👍 Commit approved."
exit 0
fi
#!/bin/bash
echo -e "[RUBOCOP] --> Init (wait a second)"
FAILS=`bundle exec rubocop | grep 'no offenses detected' -o | awk '{print $1}'`
if [ "$FAILS" == "no" ]; then
echo -e "[RUBOCOP] --> 👍 approved."
exit 0
else
echo -e "[RUBOCOP] --> ✋ You've $FAILS offenses!!!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment