Skip to content

Instantly share code, notes, and snippets.

@karuppasamy
Created July 25, 2016 04:15
Show Gist options
  • Save karuppasamy/4e2f8d4df92b56ca07d9a96ffc9e5776 to your computer and use it in GitHub Desktop.
Save karuppasamy/4e2f8d4df92b56ca07d9a96ffc9e5776 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/usr/bin/env sh
# This hook has a focus on portability.
# This hook will attempt to setup your environment before running checks.
#
# If you would like `pre-commit` to get out of your way and you are comfortable
# setting up your own environment, you can install the manual hook using:
#
# pre-commit install --manual
#
# This is a work-around to get GitHub for Mac to be able to run `node` commands
# https://stackoverflow.com/questions/12881975/git-pre-commit-hook-failing-in-github-for-mac-works-on-command-line
# PATH=$PATH:/usr/local/bin:/usr/local/sbin
protected_branch='development'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch == $current_branch ]
then
echo "***** You are not suppose to push $current_branch *****"
# read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
# echo
# if echo $REPLY | grep -E '^[Yy]$' > /dev/null
# then
# exit 0 # push will execute
# fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
cmd=`git config pre-commit.ruby 2>/dev/null`
if test -n "${cmd}"
then true
elif which rvm >/dev/null 2>/dev/null
then cmd="rvm default do ruby"
elif which rbenv >/dev/null 2>/dev/null
then cmd="rbenv exec ruby"
else cmd="ruby"
fi
export rvm_silence_path_mismatch_check_flag=1
${cmd} -rrubygems -e '
begin
require "pre-commit"
true
rescue LoadError => e
$stderr.puts <<-MESSAGE
pre-commit: WARNING: Skipping checks because: #{e}
pre-commit: Did you set your Ruby version?
MESSAGE
false
end and PreCommit.run
'
# npm test
# EXIT_CODE=$?
# if [[ ${EXIT_CODE} -ne 0 ]]; then
# echo "[ERRROR] code = " ${EXIT_CODE}
# echo "Unit Tests failed."
# echo "Push aborted."
# exit 1
# else
# echo "Unit Tests completed successfully\n"
# exit 1
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment