summaryrefslogtreecommitdiffstats
path: root/git-svn-helper
blob: 505691de8e954607b0277bd94a6babf1a2f6bbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /bin/bash

GIT_SVN_CONFIG=${1:-".git_svn_config"}

RED="\033[0;31m"
RESET="\033[0m"

function error () {
    echo -e "   => ${RED}FAILURE${RESET}"  && exit 1
}

[ -e $GIT_SVN_CONFIG ] || error "$GIT_SVN_CONFIG does not exists!"
source ./$GIT_SVN_CONFIG

[ ! -z "$GIT_BASE" ] || error "GIT_BASE variable is not defined"
[ ! -z "$PRJ" ] || error "PRJ variable is not defined"
[ ! -z "$SVN_URL" ] || error "SVN_URL variable is not defined"
[ ! -z "$BRANCHES_TO_TRACK" ] || error "BRANCHES_TO_TRACK variable is not defined"

LOCAL_REPO=${LOCAL_REPO:-"local"}
LOCAL_DIR=${LOCAL_DIR:-"${GIT_BASE}/${PRJ}"}
SVN_CLONE_REPO=${SVN_CLONE_REPO:-"svn_clone"}
SVN_CLONE_DIR=${SVN_CLONE_DIR:-"${GIT_BASE}/${PRJ}_svn"}

URL_COLOR="\033[0;34m"
LRC="\033[0;33m"    # LOCAL REPO COLOR
LBC="\033[0;36m"    # LOCAL BRANCH COLOR
SRC="\033[0;35m"    # SVN CLONE REPO COLOR
SBC="\033[0;32m"    # SVN CLONE BRANCH COLOR
DSBC="\033[0;37m"   # DISTANT SVN BRANCH COLOR

function ask_ok () {
    echo -n -e "$1${RESET}[${RED}Y${RESET}n] : "
    read  OK
    if [ "$OK" = "" -o "$OK" = "y" -o "$OK" = "Y" ]; then
        return 0
    fi
    echo -e "${RED}abort${RESET}" && return 1
}

function create_svn_clone () {
    echo -e "** clone from ${URL_COLOR}${SVN_URL}${RESET}" && cd $GIT_BASE && git svn clone ${SVN_URL} -T trunk -b branches -t tags ${PRJ}_svn || error
    cd ${SVN_CLONE_DIR}
    for branch in $BRANCHES_TO_TRACK; do
        echo -e "** checkout ${DSBC}${branch}${RESET} in ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${branch}_svn${RESET}" && git checkout $branch -b ${branch}_svn  || error
    done
    echo -e "** add ${LRC}${LOCAL_DIR}${RESET} as repo ${LRC}${LOCAL_REPO}${RESET}" && git remote add ${LOCAL_REPO} ${LOCAL_DIR} || error
}

function create_local_repo () {
    echo -e "** git init ${LRC}${LOCAL_DIR}${RESET}" && git init ${LOCAL_DIR}|| error
    echo -e "** cd ${LRC}${LOCAL_DIR}${RESET}" && cd ${LOCAL_DIR} || error
    echo -e "** add ${SRC}${SVN_CLONE_DIR}${RESET} as repo ${SRC}${SVN_CLONE_REPO}${RESET}" && git remote add ${SVN_CLONE_REPO} ${SVN_CLONE_DIR} || error
    echo -e "** fetch ${SRC}${SVN_CLONE_REPO}${RESET}" && git fetch ${SVN_CLONE_REPO} || error
    for branch in $BRANCHES_TO_TRACK; do
        echo -e "** checkout ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${branch}_svn${RESET} to ${SBC}${branch}${RESET}" && git checkout ${SVN_CLONE_REPO}/${branch}_svn -b ${branch}  || error
    done
}

function fetch_svn () {
    echo -e "** cd ${SRC}${SVN_CLONE_DIR}${RESET}" && cd ${SVN_CLONE_DIR} || error
    echo -e "** fetch from ${URL_COLOR}${SVN_URL}${RESET}" && git svn fetch || error
    BRANCHES=$(git branch | sed -e s/*//)
    for branch in $BRANCHES; do
        echo -e "** rebase ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${branch}${RESET}" && git checkout $branch && git svn rebase || error
    done
    echo -e "** cd ${SRC}${LOCAL_DIR}${RESET}" && cd ${LOCAL_DIR} || error
    echo -e "** fetch ${SRC}${SVN_CLONE_REPO}${RESET}" && git fetch ${SVN_CLONE_REPO} || error
}

function pull_from_clone () {
    #echo -e "** cd ${SRC}${LOCAL_DIR}${RESET}" && cd ${LOCAL_DIR} || error
    #echo -e "** fetch ${SRC}${SVN_CLONE_REPO}${RESET}" && git fetch ${SVN_CLONE_REPO} || error
    #echo -e "** merge ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET} within ${LRC}${LOCAL_REPO}${RESET}/${LBC}${LOCAL_B}${RESET}" && git merge ${SVN_CLONE_REPO}/${SVN_CLONE_B} || error
    echo -e "To do so :"
    echo -e "  ${LRC}- save your current work      ${SRC}$ git stash save NAME${RESET}"
    echo -e "  ${LRC}- leave your current branch   ${SRC}$ git checkout ANOTHER_BRANCH${RESET}"
    echo -e "  ${LRC}- delete your branch          ${SRC}$ git branch -D ${LOCAL_B}${RESET}"
    echo -e "  ${LRC}- checkout your branch        ${SRC}$ git checkout ${SVN_CLONE_REPO}/${LOCAL_B}_svn -b ${LOCAL_B}${RESET}"
    echo -e "  ${LRC}- apply your current work     ${SRC}$ git stash pop/apply${RESET}"
}

function push_to_clone () {
    echo -e "** cd ${SRC}${SVN_CLONE_DIR}${RESET}" && cd ${SVN_CLONE_DIR} || error
    echo -e "** checkout ${SBC}${SVN_CLONE_B}${RESET}" && git checkout ${SVN_CLONE_B} || error
    echo -e "** fetch ${LRC}${LOCAL_REPO}${RESET}" && git fetch ${LOCAL_REPO} || error
    echo -e "** rebase ${LRC}${LOCAL_REPO}${RESET}/${LBC}${LOCAL_B}${RESET} within ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET}" && git rebase ${LOCAL_REPO}/${LOCAL_B} || error
    #git log --format="%H %an %s" $SVN_HEAD..$LOCAL_HEAD
    echo -e "** cd ${SRC}${LOCAL_DIR}${RESET}" && cd ${LOCAL_DIR} || error
    echo -e "** fetch ${SRC}${SVN_CLONE_REPO}${RESET}" && git fetch ${SVN_CLONE_REPO} || error
}

function dcommit () {
    echo -e "** cd ${SRC}${SVN_CLONE_DIR}${RESET}" && cd ${SVN_CLONE_DIR} || error
    echo -e "** checkout ${SBC}${SVN_CLONE_B}${RESET}" && git checkout ${SVN_CLONE_B} || error
    echo -e "** rebase ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET}" && git svn rebase || error
    echo -e "** dcommit ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET} to ${URL_COLOR}${SVN_URL}${RESET}" && git svn dcommit
}

if [ ! -d ${SVN_CLONE_DIR} ]; then
    ask_ok "clone svn repository ${URL_COLOR}${SVN_URL}${RESET} to ${SRC}${SVN_CLONE_DIR}${RESET} " && create_svn_clone
fi
if [ ! -d ${LOCAL_DIR} ]; then
    ask_ok "create local repository within ${LRC}${LOCAL_DIR}${RESET} " && create_local_repo
fi

cd ${LOCAL_DIR}
LOCAL_B=$(git branch |grep '*' | gawk '{print $2;}') || error
LOCAL_HEAD=$(git show --format="%H" HEAD | head -n 1 | gawk '{print $1 }') || error
echo -e "** ${LRC}${LOCAL_REPO}${RESET} current branch is ${LBC}${LOCAL_B}${RESET} head ${RED}${LOCAL_HEAD}${RESET}"
cd ${SVN_CLONE_DIR}
SVN_CLONE_B=${LOCAL_B}_svn
SVN_HEAD=$(git show --format="%H" HEAD | head -n 1 | gawk '{print $1 }') || error
echo -e "** ${LRC}${SVN_CLONE_REPO}${RESET} corresponding branch is ${LBC}${SVN_CLONE_B}${RESET} head ${RED}${SVN_HEAD}${RESET}"
cd ${LOCAL_DIR}

while [ 1 ]; do
    echo -e "\nOptions :"
    echo -e " # 1) ${RED}fetch svn${RESET} and update branches within ${SRC}${SVN_CLONE_DIR}${RESET}"
    echo -e " # 2) ${RED}push${RESET} ${LRC}${LOCAL_REPO}${RESET}/${LBC}${LOCAL_B}${RESET} within ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET}"
    echo -e " # 3) ${RED}push${RESET} ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET} to ${URL_COLOR}${SVN_URL}${RESET}"
    echo -e " # 4) ${RED}pull${RESET} ${SRC}${SVN_CLONE_REPO}${RESET}/${SBC}${SVN_CLONE_B}${RESET} within ${LRC}${LOCAL_REPO}${RESET}/${LBC}${LOCAL_B}${RESET}"
    echo -e " # q) ${RED}QUIT${RESET}"
    echo -n -e "\nyour choice : "
    read CHOICE
    echo ""
    case $CHOICE in
        q)
            exit 0
            ;;
        1)
            fetch_svn
            ;;
        2)
            push_to_clone
            ;;
        3)
            dcommit
            ;;
        4)
            pull_from_clone
            ;;
    esac
done