source: trunk/UTIL/pre-push @ 3521

Last change on this file since 3521 was 3449, checked in by afalco, 7 weeks ago

git-svn: don't enter the hook if on local branch and not pushing on master.
Also pull from remote master using rebase to keep linear history.
AF

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1RED='\033[0;31m'
2NC='\033[0m' # No Color
3
4# check if on master
5local_branch=$(git branch --show-current)
6
7echo Branch: $local_branch
8
9echo done
10if [ ${local_branch} != "master" ]; then
11    # exit 0 # if on other branch than master, do not sync with svn
12    use_hook=false
13    while read local_ref local_oid remote_ref remote_oid; do
14        # echo $local_ref $local_oid $remote_ref $remote_oid
15        if test $local_ref = "refs/heads/master"; then
16            use_hook=true
17        fi
18    done
19    if test $use_hook = "false" ;then
20        echo "Sync with svn not necessary, no changes in master to be pushed."
21        echo "Continuing with push on $local_branch."
22        exit 0 # push only on local branch
23    fi
24    git checkout master
25    git pull --rebase origin master
26fi
27
28# git update-ref refs/remotes/git-svn refs/remotes/origin/master # what's this supposed to do?
29
30### check if git svn exists
31git svn info>/dev/null 2>&1
32GITSVN=$?
33if [ $GITSVN != 0 ]
34then
35    printf "${RED}You need to install git svn to use this script.${NC}\n"
36    git checkout ${local_branch}
37    exit $GITSVN
38fi
39
40HEAD=$(git rev-list master|head -n 1)
41CUR=$(git rev-list master | wc -l)
42echo "Before rebase, master had $CUR commits"
43
44### get changes from svn
45git svn rebase
46REBASED=$?
47if [ $REBASED != 0 ] # check if rebase worked
48then
49    echo "${RED}git svn rebase failed.${NC}\n"
50    git checkout ${local_branch}
51    exit $REBASED
52fi
53POST=$(git rev-list master | wc -l)
54echo "After rebase, master has $POST commits"
55echo "Object counts were $CUR::$POST"
56# if [ ! $CUR = $POST ] # check if there were changes from svn
57# then
58#     # do not push yet, check if everything is good
59#     echo "Got changes from SVN, please ensure the history is clean."
60#     echo "Once satisfied, you can push again!"
61#     exit 1
62# fi
63echo "git svn rebase worked."
64
65### commit changes to svn
66git svn dcommit < /dev/tty
67COMMITED=$?
68if [ $COMMITED != 0 ] # check if rebase worked
69then
70    printf "${RED}git svn dcommit failed.${NC}\n"
71    git checkout ${local_branch}
72    exit $COMMITED
73fi
74echo "git svn dcommit worked"
75
76### check if svn was up to date with local changes before push (git svn dcommit changes hashes)
77GITSVN=$(git rev-parse git-svn)
78echo "local is at $HEAD"
79echo "svn   is at $GITSVN"
80if [ "$HEAD" != "$GITSVN" ] # check if dcommit changed hashes
81then
82    echo "svn and local git not at same revision."
83    printf "${RED}Please check the status of all branches${NC}\n"
84    printf "${RED}with git log for example.${NC}\n"
85    printf "${RED}And synchronize svn and git.${NC}\n"
86    printf "${RED}You will need to push again after.${NC}\n"
87    git checkout ${local_branch}
88    exit 1
89fi
90
91echo "No changes between svn and local changes. Pushing..."
92
93# push only if svn was already up to date with local master
94git checkout ${local_branch}
95exit 0
Note: See TracBrowser for help on using the repository browser.