Skip to content

Instantly share code, notes, and snippets.

@randombrein
Last active December 31, 2015 10:29
Show Gist options
  • Save randombrein/069405e6097a04dd1707 to your computer and use it in GitHub Desktop.
Save randombrein/069405e6097a04dd1707 to your computer and use it in GitHub Desktop.
'pre-push' script that prompt the push to user andthen call another script which exist at %rel_path/%script_name(mostly lays at $SRCROOT)
#!/bin/bash
############################
# 'pre-push' script that prompt the push to user and
# then call another script which exist at %rel_path/%script_name
# (mostly lays at $SRCROOT)
#set your script
script_name="another.sh"
#relative path
rel_path="../.."
#####
# call_another: calls another script specified with path
# @param1: path of the script
# @param2: name of the script
#
function call_another {
current_dir=$(pwd)
script_dir=$(dirname $0)
if [ $script_dir = '.' ]
then
script_dir="$current_dir"
fi
cd $script_dir
cd $1
call_path="$PWD/$2"
echo calling script: $script_name
sh $call_path
}
####
# http://blog.ittybittyapps.com/blog/2013/09/03/git-pre-push/
# have_commits: returns '0' if current branch have commits
#
function have_commits {
commits=`git log @{upstream}..`
if [ -z "$commits" ]
then
echo "No commits available!"
return 1
else
return 0
fi
}
####
# http://blog.ittybittyapps.com/blog/2013/09/03/git-pre-push/
# can_push: returns '0' if current branch is pushable
#
function can_push {
master_branch='master'
dev_branch='develop'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [[ $current_branch = $master_branch || $current_branch = $dev_branch ]]
then
echo "Pushing on branch: $current_branch"
return 0
else
echo "Can't push on branch: $current_branch"
return 1
fi
}
echo "#################"
echo "%pre-push script%"
msg="You're about to push? [Yy]:"
#prompt
read -p "$msg" -n 1 -r < /dev/tty
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
can_push
if [[ $? = 0 ]]
then
have_commits
if [[ $? = 0 ]]
then
### do stuffs
call_another $rel_path $script_name
echo "*** push executed ***"
exit 0 # push will execute
fi
fi
fi
echo "*** push failed ***"
exit 1 # push will not execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment