RED='\033[0;31m'
NC='\033[0m' # No Color

# check if on master
local_branch=$(git branch --show-current)

echo Branch: $local_branch

echo done
if [ ${local_branch} != "master" ]; then
    # exit 0 # if on other branch than master, do not sync with svn
    use_hook=false
    while read local_ref local_oid remote_ref remote_oid; do
        # echo $local_ref $local_oid $remote_ref $remote_oid
        if test $local_ref = "refs/heads/master"; then
            use_hook=true
        fi
    done
    if test $use_hook = "false" ;then
        echo "Sync with svn not necessary, no changes in master to be pushed."
        echo "Continuing with push on $local_branch."
        exit 0 # push only on local branch
    fi
    git checkout master
    git pull --rebase origin master
fi

# git update-ref refs/remotes/git-svn refs/remotes/origin/master # what's this supposed to do?

### check if git svn exists
git svn info>/dev/null 2>&1
GITSVN=$?
if [ $GITSVN != 0 ]
then
    printf "${RED}You need to install git svn to use this script.${NC}\n"
    git checkout ${local_branch}
    exit $GITSVN
fi

HEAD=$(git rev-list master|head -n 1)
CUR=$(git rev-list master | wc -l)
echo "Before rebase, master had $CUR commits"

### get changes from svn
git svn rebase
REBASED=$?
if [ $REBASED != 0 ] # check if rebase worked
then
    echo "${RED}git svn rebase failed.${NC}\n"
    git checkout ${local_branch}
    exit $REBASED
fi
POST=$(git rev-list master | wc -l)
echo "After rebase, master has $POST commits"
echo "Object counts were $CUR::$POST"
# if [ ! $CUR = $POST ] # check if there were changes from svn
# then
#     # do not push yet, check if everything is good
#     echo "Got changes from SVN, please ensure the history is clean."
#     echo "Once satisfied, you can push again!"
#     exit 1
# fi
echo "git svn rebase worked."

### commit changes to svn
git svn dcommit < /dev/tty
COMMITED=$?
if [ $COMMITED != 0 ] # check if rebase worked
then
    printf "${RED}git svn dcommit failed.${NC}\n"
    printf "${RED}It might just be 'normal' git-svn sync.${NC}\n"
    printf "${RED}You may try to push a second time.${NC}\n"
    printf "${RED}Be sure to check the status of all branches otherwise.${NC}\n"
    git checkout ${local_branch}
    exit $COMMITED
fi
echo "git svn dcommit worked"

### check if svn was up to date with local changes before push (git svn dcommit changes hashes)
GITSVN=$(git rev-parse git-svn)
echo "local is at $HEAD"
echo "svn   is at $GITSVN"
if [ "$HEAD" != "$GITSVN" ] # check if dcommit changed hashes
then
    echo "svn and local git not at same revision."
    printf "${RED}Please check the status of all branches${NC}\n"
    printf "${RED}with git log for example.${NC}\n"
    printf "${RED}And synchronize svn and git.${NC}\n"
    printf "${RED}You will need to push again after.${NC}\n"
    git checkout ${local_branch}
    exit 1
fi

echo "No changes between svn and local changes. Pushing..."

# push only if svn was already up to date with local master
git checkout ${local_branch}
exit 0