Skip to content

Instantly share code, notes, and snippets.

@luchiago
Created June 10, 2020 00:24
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 luchiago/12be1df6dac3fe688040e0bb88a59037 to your computer and use it in GitHub Desktop.
Save luchiago/12be1df6dac3fe688040e0bb88a59037 to your computer and use it in GitHub Desktop.
Hooks for Git
#!/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!"
#!/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
#!/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
@luchiago
Copy link
Author

Place them at ~/Scripts/ruby/ and everything will be fine

@luchiago
Copy link
Author

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