Created
May 7, 2018 21:04
-
-
Save jgillman/b1ccc722f5da0b92014bd7840dff81dc to your computer and use it in GitHub Desktop.
This git hook is to deter a user from committing directly to the master branch.
This file contains hidden or 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 | |
# | |
# This hook is to deter a user from committing directly to the master branch. | |
# It still allows the user to make the commit if they enter 'master' when | |
# prompted. | |
# | |
# To enable this hook, rename this file to "pre-commit" and place it in your | |
# .git/hooks/ directory. | |
echo_red() { | |
local red='\033[0;31m' | |
local reset='\033[0m' # Clear set colors | |
local output=$* | |
echo -e "${red}${output}${reset}" | |
} | |
git_branch() { | |
git symbolic-ref HEAD 2>/dev/null \ | |
| awk -F"/" '{print $NF}' | |
} | |
is_current_branch_master() { | |
[[ $(git_branch) = "master" ]] | |
} | |
check_input() { | |
local input=$1 | |
if [[ "${input}" = "master" ]]; then | |
echo | |
echo_red "You have made a commit directly to master!" | |
exit 0 | |
else | |
echo | |
echo "Commit has been canceled." | |
exit 1 | |
fi | |
} | |
print_warning_message() { | |
echo_red "You are commiting directly to the master branch." | |
echo "Type 'master' and hit return to confirm." | |
} | |
prompt_for_confirmation() { | |
local input | |
# Gives control to STDIN | |
exec < /dev/tty | |
while true; do | |
printf "Anything other than 'master' will cancel the commit: " | |
read -r input | |
check_input "$input" | |
done | |
} | |
if is_current_branch_master; then | |
print_warning_message | |
prompt_for_confirmation | |
else | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment