I was trying to write a function in to a bash script so it could check to make sure the files that get rsynced as part of the script are from an up to date master copy from git. I found this question which seemed to cover this situation. Maybe I have misunderstood what this should do but it doesn't seem to work as I hoped.
I have noticed that if I make a commit on a separate branch, and then merge in to master, when I change bask to master and forget to pull (which I almost always forget to do) the script doesn't notice that I am behind the master and allows the rsync. Can anyone advise why this function doesn't work as I hoped?
startup_check()
{
# Check to make sure we are on the master branch
CURRENTBRANCH=$(git status|awk 'NR==1{print $3}')
if [ ! "$CURRENTBRANCH" == "master" ]; then
echo -e "Not on master - cannot proceed, please change to master by using:\ngit checkout master"
exit 1
fi
# Check whether the current working branch is ahead, behind or diverged from remote master, and exit if we're not using the current remote master
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})
if [ "$LOCAL" == "$REMOTE" ]; then
echo "Working branch is up-to-date with remote master, proceeding...."
elif [ "$LOCAL" == "$BASE" ]; then
echo -e "Your working branch is behind the remote branch, you need to run:\ngit pull"
exit 1
elif [ "$REMOTE" == "$BASE" ]; then
echo -e "Your working branch is ahead of the remote branch, you need to run:\ngit push origin master"
exit 1
else
echo "Your working branch has diverged from the remote master, cannot continue"
exit 1
fi
}
I'm using git-2.6.2 and bash-4.2