f4bd38bf05d3c4f746037cd20965f97ed7338228
[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         if [ ! -d "${JPROJECTS_HOME}/${project}" ]
23         then
24                 continue
25         fi
26
27         echo "$0: project='${project}'"
28
29         GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
30         GIT_URL2="${GIT_PATH}/${project}.git"
31         GIT_URL3="${MEDIA_BASE}/${USER}/${MEDIA_NAME2}/${MEDIA_PATH}/${project}.git"
32         GIT_URL4="file:///E:/Java%20Project/${project}.git"
33         GIT_URL5="E:/Java Project/${project}.git"
34         GIT_URL6="${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git"
35         echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}',GIT_URL3='${GIT_URL3}',GIT_URL4='${GIT_URL4}',GIT_URL5='${GIT_URL5}',GIT_URL6='${GIT_URL6}'"
36
37         cd "${JPROJECTS_HOME}/${project}" || exit 255
38
39         if [ "$1" = "l" ]
40         then
41                 echo "$0: Listing remotes for '${project}' ..."
42                 git remote -v
43                 continue
44         fi
45
46         REMOTES=`git remote | wc -l`
47         echo "$0: REMOTES='${REMOTES}'"
48
49         if [ "${REMOTES}" = "0" ]
50         then
51                 if [ -n "${SHELL}" -a -f "/proc/version" ]
52                 then
53                         echo "$0: Found no repositories in '${project}'. Creating all (Linux) ..."
54                         git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}"
55                         git remote add "${WANTED_NAME1}" "${GIT_URL1}"
56                         git remote add "${WANTED_NAME2}" "${GIT_URL2}"
57                         git remote add "${WANTED_NAME3}" "${GIT_URL3}"
58                 else
59                         echo "$0: Don't know how to create repositories, maybe unsupported."
60                         exit 1
61                 fi
62         elif [ "${REMOTES}" = "1" ]
63         then
64                 echo "$0: Only one remote found for '${project}'"
65
66                 REMOTE=`git remote`
67                 REMOTE_PUSH=`git remote -v | grep "push"`
68                 echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
69
70                 MEDIA1=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/"`
71                 echo "$0: MEDIA1='${MEDIA1}'"
72
73                 if [ -n "${MEDIA1}" ]
74                 then
75                         echo "$0: Found '${MEDIA_BASE}/${USER}/${MEDIA_NAME1}' remote for '${project}'."
76                         if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
77                         then
78                                 echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
79                         else
80                                 echo "$0: Renaming '${REMOTE}' to '${GIT_ORIGIN}' ..."
81                                 git remote rename "${REMOTE}" "${GIT_ORIGIN}" || exit 255
82                         fi
83                 else
84                         echo "$0: Not a media remote."
85                         MXCHANGE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL1}"`
86                         LINUX_MEDIA_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL3}"`
87                         WINDOWS_FILE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL4}"`
88                         WINDOWS_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL5}"`
89                         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}'"
90
91                         if [ -n "${MXCHANGE_REMOTE}" ]
92                         then
93                                 echo "$0: Found a '${GIT_SERVER}' remote."
94
95                                 if [ -n "${WANTED_NAME1}" -a "${REMOTE}" != "${WANTED_NAME1}" ]
96                                 then
97                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
98                                 else
99                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
100                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
101                                 fi
102
103                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
104                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
105                         elif [ -n "${WINDOWS_FILE_REMOTE}" -o -n "${WINDOWS_PATH_REMOTE}" ]
106                         then
107                                 echo "$0: Found a Windows (USB stick) remote."
108
109                                 if [ -n "${WANTED_NAME3}" -a "${REMOTE}" == "${WANTED_NAME3}" ]
110                                 then
111                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME3}'."
112                                 else
113                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME3}' ..."
114                                         git remote rename "${REMOTE}" "${WANTED_NAME3}" || exit 255
115                                 fi
116
117                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
118                                 git remote add "${GIT_ORIGIN}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
119                         elif [ -n "${LINUX_MEDIA_PATH_REMOTE}" ]
120                         then
121                                 echo "$0: Found a Linux (USB stick) remote."
122
123                                 if [ -n "${GIT_ORIGIN}" -a "${REMOTE}" = "${GIT_ORIGIN}" ]
124                                 then
125                                         echo "$0: Need to fix remote repositories ..."
126                                         git remote rename origin transport || exit 255
127                                         git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}" || exit 255
128                                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
129                                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
130                                 else
131                                         echo "$0: Don't know how to fix '${REMOTE}' (${REMOTE_PUSH})."
132                                         exit 1
133                                 fi
134                         else
135                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
136                                 exit 1
137                         fi
138                 fi
139         fi
140
141         UNWANTED_REMOTE=`git remote | grep "${UNWANTED_NAME}"`
142         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
143
144         if [ -n "${UNWANTED_REMOTE}" ]
145         then
146                 echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
147
148                 UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
149                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
150
151                 if [ -n "${UPSTREAM_REMOTE}" ]
152                 then
153                         echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
154                         git remote rm "${WANTED_NAME1}" || exit 255
155                 fi
156
157                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
158                 git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
159         else
160                 echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
161                 UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
162                 UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
163                 UPSTREAM_REMOTE3=`git remote | grep "${WANTED_NAME3}"`
164                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
165
166                 if [ -n "${UPSTREAM_REMOTE1}" ]
167                 then
168                         echo "$0: Remote '${WANTED_NAME1}' found in '${project}'."
169                 else
170                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
171                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
172                 fi
173
174                 if [ -n "${UPSTREAM_REMOTE2}" ]
175                 then
176                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
177                 elif [ ! -d "${GIT_URL2}" ]
178                 then
179                         echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
180                 elif [ -n "${WANTED_NAME2}" ]
181                 then
182                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
183                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
184                 fi
185
186                 if [ -n "${UPSTREAM_REMOTE3}" ]
187                 then
188                         echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
189                 elif [ ! -d "${GIT_URL3}" ]
190                 then
191                         echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
192                 else
193                         echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
194                         git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
195                 fi
196         fi
197 done
198
199 echo "$0: All done."
200 exit 0