No exit here
[jprojects-scripts.git] / fix-project-remotes.sh
1 #!/bin/bash
2
3 . ./.jprojects.sh || exit 255
4
5 UNWANTED_NAME="stick"
6 WANTED_NAME1="upstream"
7 WANTED_NAME2="daedalus"
8 WANTED_NAME3="transport"
9
10 GIT_USER="quix0r"
11 GIT_SERVER="git.mxchange.org"
12 GIT_PATH="/var/cache/git/repos"
13 GIT_BRANCH="master"
14 GIT_ORIGIN="origin"
15
16 MEDIA_BASE="/media"
17 MEDIA_NAME1="INTENSO"
18 MEDIA_NAME2="Transport"
19 MEDIA_PATH="Java Project"
20
21 for project in ${LIST}; do
22         echo "$0: project='${project}'"
23         SKIP_TEST="${project:0:9}"
24
25         if [ "${SKIP_TEST}" = "jratecalc" ]
26         then
27                 echo "$0: Not fixing URLs for jratecalc project '${project}'."
28                 continue
29         fi
30
31         GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
32         GIT_URL2="${GIT_PATH}/${project}.git"
33         GIT_URL3="${MEDIA_BASE}/${USER}/${MEDIA_NAME2}/${MEDIA_PATH}/${project}.git"
34         echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}',GIT_URL3='${GIT_URL3}'"
35
36         cd "${project}"
37
38         if [ "$1" = "l" ]
39         then
40                 echo "$0: Listing remotes for '${project}' ..."
41                 git remote -v
42                 cd -
43                 continue;
44         fi
45
46         REMOTES=`git remote | wc -l`
47         echo "$0: REMOTES='${REMOTES}'"
48
49         if [ "${REMOTES}" = "1" ]
50         then
51                 echo "$0: Only one remote found for '${project}'"
52
53                 REMOTE=`git remote`
54                 REMOTE_PUSH=`git remote -v | grep "push"`
55                 echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
56
57                 MEDIA1=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/"`
58                 echo "$0: MEDIA1='${MEDIA1}'"
59
60                 if [ -n "${MEDIA1}" ]
61                 then
62                         echo "$0: Found '${MEDIA_BASE}/${USER}/${MEDIA_NAME1}' remote for '${project}'."
63                         if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
64                         then
65                                 echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
66                         else
67                                 echo "$0: Renaming '${REMOTE}' to '${GIT_ORIGIN}' ..."
68                                 git remote rename "${REMOTE}" "${GIT_ORIGIN}" || exit 255
69                         fi
70                 else
71                         echo "$0: Not a media remote."
72                         MXCHANGE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL1}"`
73                         echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}'"
74
75                         if [ -n "${MXCHANGE_REMOTE}" ]
76                         then
77                                 echo "$0: Found a '${GIT_SERVER}' remote."
78
79                                 if [ "${REMOTE}" != "${WANTED_NAME1}" ]
80                                 then
81                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
82                                 else
83                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
84                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
85                                 fi
86
87                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
88                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
89                         else
90                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
91                                 exit 1
92                         fi
93                 fi
94         fi
95
96         UNWANTED_REMOTE=`git remote | grep "${UNWANTED_NAME}"`
97         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
98
99         if [ -n "${UNWANTED_REMOTE}" ]
100         then
101                 echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
102
103                 UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
104                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
105
106                 if [ -n "${UPSTREAM_REMOTE}" ]
107                 then
108                         echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
109                         git remote rm "${WANTED_NAME1}" || exit 255
110                 fi
111
112                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
113                 git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
114         else
115                 echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
116                 UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
117                 UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
118                 UPSTREAM_REMOTE3=`git remote | grep "${WANTED_NAME3}"`
119                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
120
121                 if [ -n "${UPSTREAM_REMOTE1}" ]
122                 then
123                         echo "$0: Remote '${WANTED_NAME1}' found in '${project}'."
124                 else
125                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
126                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
127                 fi
128
129                 if [ -n "${UPSTREAM_REMOTE2}" ]
130                 then
131                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
132                 elif [ ! -d "${GIT_URL2}" ]
133                 then
134                         echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
135                         exit 1
136                 else
137                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
138                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
139                 fi
140
141                 if [ -n "${UPSTREAM_REMOTE3}" ]
142                 then
143                         echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
144                 elif [ ! -d "${GIT_URL3}" ]
145                 then
146                         echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
147                         exit 1
148                 else
149                         echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
150                         git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
151                 fi
152         fi
153
154         cd -
155 done