Skip to content

Instantly share code, notes, and snippets.

@harunyardimci
Last active December 10, 2015 22:18
Show Gist options
  • Select an option

  • Save harunyardimci/4501603 to your computer and use it in GitHub Desktop.

Select an option

Save harunyardimci/4501603 to your computer and use it in GitHub Desktop.
pre-recevie git hook to check if the user has allowed to commit master branch.
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
FILE_PATH="$(pwd)/$DIR"
# full path of the allowed commiters file.
COMMITERS_FILE="$FILE_PATH/commiters.txt"
function isFileExits() {
if [ -f $1 ]; then
return 1
fi
return 0
}
# compare two email addresses
function checkMail() {
echo $1 $2
if [ "$1" == "$2" ];
then
return 1
fi
return 0
}
isFileExits "$COMMITERS_FILE"; FILE_EXISTS=$?
echo "-------------------------------------------------"
if [ $FILE_EXISTS -eq 0 ]; then
echo ""
echo "Allowed commiters file is not exists. So all commiters are allowed to push master branch."
echo ""
exit 0
fi
INVALID_CNT=0
while read old new name; do
commiter=$(git log -1 --pretty=format:%ae $new)
# check email
if [ $FILE_EXISTS -eq 1 ]; then
while read allowed; do
checkMail $commiter $allowed; VALID_MAIL=$?
if [ $VALID_MAIL -eq 0 ]; then
INVALID_CNT=1
fi
done < $COMMITERS_FILE
if [ $INVALID_CNT -eq 1 ]; then
echo "===================================================="
echo "YOU ARE NOT ALLOWED TO COMMIT TO THE MASTER BRANCH!!"
echo "===================================================="
exit 1
fi
fi
done
echo "YOUR CHANGES SUCCESSFULLY COMMITTED."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment