Created
June 10, 2020 00:24
-
-
Save luchiago/12be1df6dac3fe688040e0bb88a59037 to your computer and use it in GitHub Desktop.
Hooks for Git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
GIT_DIR=$(git rev-parse --git-dir) | |
echo "Installing hooks..." | |
# this command creates symlink to our pre-commit script | |
ln -s ~/Scripts/ruby/pre-commit.bash $GIT_DIR/hooks/pre-commit | |
ln -s ~/Scripts/ruby/pre-push.bash $GIT_DIR/hooks/pre-push | |
echo "Done!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
echo "Running pre-commit hook" | |
fileList_rb=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(rb)$') | |
if [ ${#fileList_rb} -lt 1 ] && [ ${#fileList_erb} -lt 1 ]; then | |
echo -e "You have no staged .rb to test\n" | |
exit 1 | |
fi | |
if [ ${#fileList_rb} -gt 1 ]; then | |
bundle exec rubocop -c .rubocop.yml --force-exclusion ${fileList_rb[*]} "$@" | |
if [ $? -ne 0 ]; then | |
echo -e "\nPlease fix the above rubocop issues before committing.\n" | |
exit 1 | |
fi | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
echo "Running pre-push hook" | |
echo "Running tests" | |
bundle exec rspec | |
if [ $? -ne 0 ]; then | |
echo "Tests must pass before pushing!" | |
exit 1 | |
fi |
Suggestion: I created a function to run this script with git clone
It looks like this:
~/.zshrc
function gcli {
chmod +x ~/Scripts/ruby/*.bash
git clone $1
FOLDER=$(echo ${1} | grep -oP "/(.+)\.git" | sed 's/\.git//g')
cd ${FOLDER:1}/
GEMFILE=$(find ./ -type f -name "Gemfile")
if echo ${GEMFILE} -gt 1 ;then
echo 'RUBY PROJECT'
bash ~/Scripts/ruby/install-hooks.bash
else
echo 'NOT A RUBY PROJECT'
fi
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place them at
~/Scripts/ruby/
and everything will be fine