Skip to content

Instantly share code, notes, and snippets.

@Synkevych
Last active April 8, 2021 12:46
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 Synkevych/15426b53361c5795bc2d456a01a187c2 to your computer and use it in GitHub Desktop.
Save Synkevych/15426b53361c5795bc2d456a01a187c2 to your computer and use it in GitHub Desktop.
Run RSpec tests on pre-push - git hook for Rails projects
#!/bin/sh
changed_files=`git status --short`
if [ "$changed_files" ]; then
echo "There some changes:"
echo "$changed_files"
echo "please commit or reset them, before push."
exit 1
else
echo "There no changes, move on to the next task"
fi
echo "Running RSpec"
bundle exec rspec spec
spec=$?
if [ $spec -eq 0 ]; then
echo -en "\\033[32mTests are green, pushing...\\033[0;39m\n"
exit
else
echo -en "\\033[1;31mCannot push, tests are failing. Use --no-verify to force push.\\033[0;39m\n"
exit 1
fi
echo "looks for console.log or puts inside project files"
FILES_PATTERN='\.(js|coffee|rb)(\..+)?$'
FORBIDDEN='console.info\|console.log\|alert\|puts|
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo 'COMMIT REJECTED Found "$FORBIDDEN" references. Please remove them before commiting' && exit 1
changed_files=`git log ..origin/develop`
if [ "$changed_files" ]; then
echo "There are some changes in origin/develop, please pull them before pushing"
exit 1
else
echo "There are no changes in origin/develop, pushing to the remote server"
fi
@Synkevych
Copy link
Author

Synkevych commented Jan 30, 2021

To setup this hook:

cd your-project-name
cd .git/hooks;
curl https://gist.githubusercontent.com/Synkevych/15426b53361c5795bc2d456a01a187c2/raw/6ab5bc9335d538e0097b7d32718644f598e3c4bb/pre-push > pre-push;
chmod +x pre-push;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment