Skip to content

Instantly share code, notes, and snippets.

@maxijonson
Created March 16, 2019 20:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxijonson/a1470f10d0af78effcf74842780b095b to your computer and use it in GitHub Desktop.
Save maxijonson/a1470f10d0af78effcf74842780b095b to your computer and use it in GitHub Desktop.
git post-commit hook to work with Heroku, Github and sensitive files
#!/bin/sh
# After each commit on master (Heroku), it will switch to the Github branch and cherry-pick that commit into it. Then, switch back to master.
# This makes the committing flow seamless so all you have to do is push (which is safer done manually than with a hook)
# IMPORTANT: ANY changes in sensitive files should be commited alone (without public changes) or the sensitive files will be cherry picked in the public branch
# To skip this cherry-picking procedure (when you add a sensitive commit), start the commit message with "NCP" e.g.: "NCP Add some secret stuff"
ERR='\e[31m'
SUCCESS='\e[32m'
WARN='\e[33m'
space="\n\n\n"
branch=`git branch | grep \* | cut -d ' ' -f2`
heroku="master" # private branch
github="Github" # public branch
if [ "$branch" = $heroku ]
then
echo -e $space
message=`git log -1 --pretty=%B`
if [[ $message = NCP* ]]; then
echo -e "${WARN}NOT cherry-picking (commit message starts with 'NCP') $space"
exit 0;
fi
echo -e "${WARN}Executing post-commit script (cherry-pick)..."
commitID=`git rev-parse HEAD`
if git checkout $github; then
if git cherry-pick $commitID; then
echo -e "${SUCCESS}Cherry picked $commitID into $github branch $space"
if ! git checkout $heroku; then
echo -e "${ERR}Couldn't checout to $heroku $space"
exit 1
fi
else
echo -e "${ERR}Failed to cherry pick $commitID into $github branch. Do it manually $space"
exit 1
fi
else
echo -e "${ERR}Couldn't checkout to $github $space"
exit 1
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment