-
Notifications
You must be signed in to change notification settings - Fork 45
Ruby applications with git pre commit hook
Abraham edited this page Aug 4, 2017
·
1 revision
The intention of this guide is to add a git hook in order to run rubocop
and reek
on any ruby application you want to force yourself to follow the rules.
First you need to run the following commands, under the ruby/rails project directory:
$ mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
$ chmod +x .git/hooks/pre-commit
This will give the hook execution permissions, don't worry we have not done anything yet.
Next add the following code into your .git/hooks/pre-commit
file:
#!/bin/sh
pass=true
RED='\033[1;31m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "Running Linters:"
# Run rubocop and get the output and return code
rubocop=$(docker-compose run --rm web rubocop)
return_code=$?
if [ $return_code != 0 ]; then
echo "$rubocop\n"
printf "\n${RED}Rubocop failed, not commit was made"
pass=false
else
printf "${GREEN}Rubocop passed.${NC}\n"
fi
# Run reek and get the output and return code
reek=$(docker-compose run --rm web reek)
return_code=$?
if [ $return_code != 0 ]; then
echo "$reek\n"
printf "\n${RED}Reek failed, not commit was made"
pass=false
else
printf "${GREEN}Reek passed.${NC}\n"
fi
# If you reach this point, everything was cool and means you are a good player
if $pass; then
exit 0
fi
exit 1
And that is it, next time you try to commit something reek
& rubocop
will execute and if either of those break, you will not be able to commit.
The code above, was based on https://github.com/angular/material2/wiki/Pre-commit-hook-for-linters