Last active
December 19, 2015 08:18
-
-
Save kcollasarundell/5924305 to your computer and use it in GitHub Desktop.
stop commits: a simple pre-commit hook to prevent commits to the central production repo
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
#!/bin/bash | |
USER=`whoami` | |
DATE=`date --iso-8601=minute` | |
REPONAME= | |
PREFIX="$REPONAME_files_attempt" | |
PATCHFILE="${PREFIX}-${DATE}.patch" | |
REPODIR="/opt/$REPONAME" | |
MAINREPO="SOURCEGOESHERE" | |
EMAIL= | |
if [ `whoami` = 'ops' ]; | |
then | |
echo 'You should not be attempting to commit as the ops user' | |
exit 1 | |
fi | |
function diff_all { | |
git --no-pager diff $REPODIR | |
git ls-files --others --exclude-standard | | |
while read -r i; do git --no-pager diff -- /dev/null "$i"; done | |
} | |
function repo { | |
if [ ! -d "$HOME/$REPONAME" ]; then | |
git clone -q $MAINREPO $HOME/$REPONAME 2>/dev/null | |
elif [ -d "$HOME/$REPONAME/.git" ]; then | |
GIT_WORK_TREE="$HOME/$REPONAME" \ | |
GIT_DIR="$HOME/$REPONAME/.git" \ | |
git pull -q $MAINREPO 2>/dev/null | |
fi | |
} | |
function unstage { | |
git reset HEAD | |
} | |
echo "Do not commit to this repository." | |
echo "" | |
echo "This repository has changes deployed automatically." | |
echo "" | |
echo "a clone of this repository will be created" | |
echo "or updated in your HOME Directory" | |
repo | |
echo "" | |
echo "Your changes will be unstaged" | |
unstage | |
echo "and a patch will be created in your home directory" | |
diff_all > $HOME/$PATCHFILE | |
echo "you can apply the patchfile with the following command" | |
git apply ~/$PATCHFILE --directory=$HOME/$REPONAME/ | |
echo "cd ~/$REPONAME && git-apply ~/$PATCHFILE" | |
echo "but this script has already done that for you" | |
echo "now simply add them, commit and push to the primary repo" | |
echo "" | |
echo "Please clean up after yourself with a git reset" | |
echo "to ensure that this repo is consistant" | |
diff_all |mail -s "$USER just tried to commit" $EMAIL | |
echo "git reset HEAD --hard" | |
echo "note that this command should be used sparingly" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment