1 | RED='\033[0;31m' |
---|
2 | NC='\033[0m' # No Color |
---|
3 | |
---|
4 | # check if on master |
---|
5 | local_branch=$(git branch --show-current) |
---|
6 | |
---|
7 | echo Branch: $local_branch |
---|
8 | |
---|
9 | echo done |
---|
10 | if [ ${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 |
---|
26 | fi |
---|
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 |
---|
31 | git svn info>/dev/null 2>&1 |
---|
32 | GITSVN=$? |
---|
33 | if [ $GITSVN != 0 ] |
---|
34 | then |
---|
35 | printf "${RED}You need to install git svn to use this script.${NC}\n" |
---|
36 | git checkout ${local_branch} |
---|
37 | exit $GITSVN |
---|
38 | fi |
---|
39 | |
---|
40 | HEAD=$(git rev-list master|head -n 1) |
---|
41 | CUR=$(git rev-list master | wc -l) |
---|
42 | echo "Before rebase, master had $CUR commits" |
---|
43 | |
---|
44 | ### get changes from svn |
---|
45 | git svn rebase |
---|
46 | REBASED=$? |
---|
47 | if [ $REBASED != 0 ] # check if rebase worked |
---|
48 | then |
---|
49 | echo "${RED}git svn rebase failed.${NC}\n" |
---|
50 | git checkout ${local_branch} |
---|
51 | exit $REBASED |
---|
52 | fi |
---|
53 | POST=$(git rev-list master | wc -l) |
---|
54 | echo "After rebase, master has $POST commits" |
---|
55 | echo "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 |
---|
63 | echo "git svn rebase worked." |
---|
64 | |
---|
65 | ### commit changes to svn |
---|
66 | git svn dcommit < /dev/tty |
---|
67 | COMMITED=$? |
---|
68 | if [ $COMMITED != 0 ] # check if rebase worked |
---|
69 | then |
---|
70 | printf "${RED}git svn dcommit failed.${NC}\n" |
---|
71 | git checkout ${local_branch} |
---|
72 | exit $COMMITED |
---|
73 | fi |
---|
74 | echo "git svn dcommit worked" |
---|
75 | |
---|
76 | ### check if svn was up to date with local changes before push (git svn dcommit changes hashes) |
---|
77 | GITSVN=$(git rev-parse git-svn) |
---|
78 | echo "local is at $HEAD" |
---|
79 | echo "svn is at $GITSVN" |
---|
80 | if [ "$HEAD" != "$GITSVN" ] # check if dcommit changed hashes |
---|
81 | then |
---|
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 |
---|
89 | fi |
---|
90 | |
---|
91 | echo "No changes between svn and local changes. Pushing..." |
---|
92 | |
---|
93 | # push only if svn was already up to date with local master |
---|
94 | git checkout ${local_branch} |
---|
95 | exit 0 |
---|