source: trunk/UTIL/hooks/pre-push @ 3575

Last change on this file since 3575 was 3575, checked in by afalco, 17 hours ago

git-svn: added hooks on other operations such as merge and checkout, to synchronize git and svn more often (not just git push).

Unfortunately, "git pull" will still not call the hook if there are no changes on git remote master.
AF

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