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