Continued:
[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="Backup"
18 MEDIA_NAME2="Transport"
19 MEDIA_PATH="Java Project"
20
21 for project in ${LIST}; do
22         if [ "${project}" = "jprojects-scripts" ]
23         then
24                 echo "$0: Skipping jprojects-scripts ..."
25                 continue
26         elif [ ! -d "${JPROJECTS_HOME}/${project}" ]
27         then
28                 echo "$0: Project '${project}' does not exist."
29                 continue
30         fi
31
32         echo "$0: project='${project}'"
33         cd "${JPROJECTS_HOME}/${project}" || exit 255
34
35         GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
36         GIT_URL2="${GIT_PATH}/${project}.git"
37         GIT_URL3="${MEDIA_BASE}/${USER}/${MEDIA_NAME2}/${MEDIA_PATH}/${project}.git"
38         GIT_URL4="file:///E:/Java%20Project/${project}.git"
39         GIT_URL5="E:/Java Project/${project}.git"
40         GIT_URL6="${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git"
41         echo "$0: GIT_URL1='${GIT_URL1}'"
42         echo "$0: GIT_URL2='${GIT_URL2}'"
43         echo "$0: GIT_URL3='${GIT_URL3}'"
44         echo "$0: GIT_URL4='${GIT_URL4}'"
45         echo "$0: GIT_URL5='${GIT_URL5}'"
46         echo "$0: GIT_URL6='${GIT_URL6}'"
47
48         if [ "$1" = "l" ]
49         then
50                 echo "$0: Listing remotes for '${project}' ..."
51                 git remote -v
52                 continue
53         fi
54
55         REMOTES=$(git remote | wc -l)
56         echo "$0: REMOTES='${REMOTES}'"
57
58         if [ "${REMOTES}" = "0" ]
59         then
60                 if [ -n "${SHELL}" -a -f "/proc/version" ]
61                 then
62                         echo "$0: Found no repositories in '${project}'. Creating all (Linux) ..."
63                         git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}"
64                         git remote add "${WANTED_NAME1}" "${GIT_URL1}"
65                         git remote add "${WANTED_NAME2}" "${GIT_URL2}"
66                         git remote add "${WANTED_NAME3}" "${GIT_URL3}"
67                 else
68                         echo "$0: Don't know how to create repositories, maybe unsupported."
69                         exit 1
70                 fi
71         elif [ "${REMOTES}" = "1" ]
72         then
73                 echo "$0: Only one remote found for '${project}'"
74
75                 REMOTE=$(git remote)
76                 REMOTE_PUSH=$(git remote -v | grep "push")
77                 echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
78
79                 MEDIA1=$(echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/")
80                 echo "$0: MEDIA1='${MEDIA1}'"
81
82                 if [ -n "${MEDIA1}" ]
83                 then
84                         echo "$0: Found '${MEDIA_BASE}/${USER}/${MEDIA_NAME1}' remote for '${project}'."
85                         if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
86                         then
87                                 echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
88                         else
89                                 echo "$0: Renaming '${REMOTE}' to '${GIT_ORIGIN}' ..."
90                                 git remote rename "${REMOTE}" "${GIT_ORIGIN}" || exit 255
91                         fi
92                 else
93                         echo "$0: Not a media remote."
94                         MXCHANGE_REMOTE=$(echo "${REMOTE_PUSH}" | grep "${GIT_URL1}")
95                         LINUX_MEDIA_PATH_REMOTE=$(echo "${REMOTE_PUSH}" | grep "${GIT_URL3}")
96                         WINDOWS_FILE_REMOTE=$(echo "${REMOTE_PUSH}" | grep "${GIT_URL4}")
97                         WINDOWS_PATH_REMOTE=$(echo "${REMOTE_PUSH}" | grep "${GIT_URL5}")
98                         echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}',WINDOWS_FILE_REMOTE='${WINDOWS_FILE_REMOTE}',WINDOWS_PATH_REMOTE='${WINDOWS_PATH_REMOTE}',LINUX_MEDIA_PATH_REMOTE='${LINUX_MEDIA_PATH_REMOTE}'"
99
100                         if [ -n "${MXCHANGE_REMOTE}" ]
101                         then
102                                 echo "$0: Found a '${GIT_SERVER}' remote."
103
104                                 if [ -n "${WANTED_NAME1}" -a "${REMOTE}" != "${WANTED_NAME1}" ]
105                                 then
106                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
107                                 else
108                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
109                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
110                                 fi
111
112                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
113                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
114                         elif [ -n "${WINDOWS_FILE_REMOTE}" -o -n "${WINDOWS_PATH_REMOTE}" ]
115                         then
116                                 echo "$0: Found a Windows (USB stick) remote."
117
118                                 if [ -n "${WANTED_NAME3}" -a "${REMOTE}" == "${WANTED_NAME3}" ]
119                                 then
120                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME3}'."
121                                 else
122                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME3}' ..."
123                                         git remote rename "${REMOTE}" "${WANTED_NAME3}" || exit 255
124                                 fi
125
126                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
127                                 git remote add "${GIT_ORIGIN}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
128                         elif [ -n "${LINUX_MEDIA_PATH_REMOTE}" ]
129                         then
130                                 echo "$0: Found a Linux (USB stick) remote."
131
132                                 if [ -n "${GIT_ORIGIN}" -a "${REMOTE}" = "${GIT_ORIGIN}" ]
133                                 then
134                                         echo "$0: Need to fix remote repositories ..."
135                                         git remote rename origin transport || exit 255
136                                         git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}" || exit 255
137                                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
138                                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
139                                 else
140                                         echo "$0: Don't know how to fix '${REMOTE}' (${REMOTE_PUSH})."
141                                         exit 1
142                                 fi
143                         else
144                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
145                                 exit 1
146                         fi
147                 fi
148         fi
149
150         UNWANTED_REMOTE=$(git remote | grep "${UNWANTED_NAME}")
151         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
152
153         if [ -n "${UNWANTED_REMOTE}" ]
154         then
155                 echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
156
157                 UPSTREAM_REMOTE=$(git remote | grep "${WANTED_NAME1}")
158                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
159
160                 if [ -n "${UPSTREAM_REMOTE}" ]
161                 then
162                         echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
163                         git remote rm "${WANTED_NAME1}" || exit 255
164                 fi
165
166                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
167                 git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
168         else
169                 echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
170                 UPSTREAM_REMOTE1=$(git remote | grep "${WANTED_NAME1}")
171                 UPSTREAM_REMOTE2=$(git remote | grep "${WANTED_NAME2}")
172                 UPSTREAM_REMOTE3=$(git remote | grep "${WANTED_NAME3}")
173                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
174
175                 if [ -n "${UPSTREAM_REMOTE1}" ]
176                 then
177                         echo "$0: Wanted remote '${WANTED_NAME1}' already found in '${project}'."
178                 else
179                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
180                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
181                 fi
182
183                 if [ -n "${UPSTREAM_REMOTE2}" ]
184                 then
185                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
186                 elif [ ! -d "${GIT_URL2}" ]
187                 then
188                         echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
189                 elif [ -n "${WANTED_NAME2}" ]
190                 then
191                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
192                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
193                 fi
194
195                 if [ -n "${UPSTREAM_REMOTE3}" ]
196                 then
197                         echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
198                 elif [ ! -d "${GIT_URL3}" ]
199                 then
200                         echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
201                 else
202                         echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
203                         git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
204                 fi
205         fi
206
207         # Is not an application by default
208         IS_APP=""
209
210         # Now "walk" through all base application names
211         for APP_NAME in ${BASE_APPLICATION_NAMES}; do
212                 echo "$0: project='${project}',APP_NAME='${APP_NAME}'"
213
214                 # Is it part of the project's name?
215                 PART=$(echo "${project}" | grep "${APP_NAME}")
216
217                 # Check it
218                 if [ -z "${PART}" ]
219                 then
220                         # Skip this entire loop
221                         continue
222                 fi
223
224                 # Found one that maybe needs fixing
225                 IS_APP="${APP_NAME}"
226
227                 # Skip further search
228                 break
229         done
230
231         echo "$0: IS_APP='${IS_APP}'"
232
233         # Is the current project an application (or part of it)?
234         if [ -z "${IS_APP}" ]
235         then
236                 # No, then skip it
237                 echo "$0: Project '${project}' is no application."
238                 continue
239         fi
240
241         # Generate all sub-project names
242         EAR_APP_NAME="${IS_APP}"
243         CORE_APP_NAME="${IS_APP}-core"
244         EJB_APP_NAME="${IS_APP}-ejb"
245         LIB_APP_NAME="${IS_APP}-lib"
246         MAILER_EJB_APP_NAME="${IS_APP}-mailer-ejb"
247         MAILER_LIB_APP_NAME="${IS_APP}-mailer-lib"
248         SWING_APP_NAME="${IS_APP}-swing"
249         WAR_APP_NAME="${IS_APP}-war"
250
251         # It is an application, then run through all projects again
252         for APP_NAME in ${BASE_APPLICATION_NAMES}; do
253                 echo "$0: project='${project}',APP_NAME='${APP_NAME}'"
254
255                 # What project do we have?
256                 if [ "${project}" = "${EAR_APP_NAME}" ]
257                 then
258                         # EAR (triangle symbol) project
259                         PROJECT_APP_NAME="${APP_NAME}"
260                         OWN_APP_NAME="${EAR_APP_NAME}"
261                 elif [ "${project}" = "${CORE_APP_NAME}" ]
262                 then
263                         # Model library project
264                         PROJECT_APP_NAME="${APP_NAME}-core"
265                         OWN_APP_NAME="${CORE_APP_NAME}"
266                 elif [ "${project}" = "${EJB_APP_NAME}" ]
267                 then
268                         # General EJB (bean symbol) project
269                         PROJECT_APP_NAME="${APP_NAME}-ejb"
270                         OWN_APP_NAME="${EJB_APP_NAME}"
271                 elif [ "${project}" = "${LIB_APP_NAME}" ]
272                 then
273                         # General remote-interface project
274                         PROJECT_APP_NAME="${APP_NAME}-lib"
275                         OWN_APP_NAME="${LIB_APP_NAME}"
276                 elif [ "${project}" = "${MAILER_EJB_APP_NAME}" ]
277                 then
278                         # Mailer EJB (bean symbol) project
279                         PROJECT_APP_NAME="${APP_NAME}-mailer-ejb"
280                         OWN_APP_NAME="${MAILER_EJB_APP_NAME}"
281                 elif [ "${project}" = "${MAILER_LIB_APP_NAME}" ]
282                 then
283                         # Mailer remote-interface project
284                         PROJECT_APP_NAME="${APP_NAME}-mailer-lib"
285                         OWN_APP_NAME="${MAILER_LIB_APP_NAME}"
286                 elif [ "${project}" = "${SWING_APP_NAME}" ]
287                 then
288                         # Swing project
289                         PROJECT_APP_NAME="${APP_NAME}-swing"
290                         OWN_APP_NAME="${SWING_APP_NAME}"
291                 elif [ "${project}" = "${WAR_APP_NAME}" ]
292                 then
293                         # WAR (globe symbol) project
294                         PROJECT_APP_NAME="${APP_NAME}-war"
295                         OWN_APP_NAME="${WAR_APP_NAME}"
296                 else
297                         # Unknown, skip this
298                         echo "$0: Project '${project}' is not supported. Please consider adding it."
299                         break
300                 fi
301
302                 # Generic ...
303                 LOCAL_APP_PATH="${JPROJECTS_HOME}/${PROJECT_APP_NAME}"
304
305                 echo "$0: LOCAL_APP_PATH='${LOCAL_APP_PATH}',PROJECT_APP_NAME='${PROJECT_APP_NAME}'"
306                 echo "$0: project='${project}',OWN_APP_NAME='${OWN_APP_NAME}'"
307
308                 # Is same project name?
309                 if [ "${OWN_APP_NAME}" != "${project}" ]
310                 then
311                         echo "$0: Not the right project: '${project}'<>'${OWN_APP_NAME}', skipping ..."
312                         continue
313                 elif [ "${project}" = "${LOCAL_APP_PATH##*/}" ]
314                 then
315                         # Skip same project
316                         echo "$0: Not local-tracking same project '${project}'='${LOCAL_APP_PATH##*/}', skipping ..."
317                         continue;
318                 elif [ ! -d "${LOCAL_APP_PATH}" ]
319                 then
320                         # Other application's name does not exist
321                         echo "$0: Application '${APP_NAME}' not found, skipping ..."
322                         continue;
323                 fi
324
325                 # Local-tracking
326                 LOCAL_TRACKING="${APP_NAME}-local"
327                 echo "$0: LOCAL_TRACKING='${LOCAL_TRACKING}'"
328
329                 # Maybe a local-tracking needs to be setup?
330                 LOCAL_TRACKING_SETUP=$(git remote | grep "${LOCAL_TRACKING}")
331
332                 if [ -n "${LOCAL_TRACKING_SETUP}" ]
333                 then
334                         echo "$0: Local-tracking from '${project}' to '${LOCAL_APP_PATH##*/}' is already setup."
335                         continue
336                 fi
337
338                 # Setup it
339                 echo "$0: Setting up local-tracking (${LOCAL_TRACKING}) from '${project}' to '${LOCAL_APP_PATH##*/}' ..."
340                 git remote add "${LOCAL_TRACKING}" "${LOCAL_APP_PATH}" || exit 255
341         done
342 done
343
344 echo "$0: All done."
345 exit 0