No more abort + merging upstream added (work).
[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}" = "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                         LINUX_MEDIA_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL3}"`
74                         WINDOWS_FILE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL4}"`
75                         WINDOWS_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL5}"`
76                         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}'"
77
78                         if [ -n "${MXCHANGE_REMOTE}" ]
79                         then
80                                 echo "$0: Found a '${GIT_SERVER}' remote."
81
82                                 if [ -n "${WANTED_NAME1}" -a "${REMOTE}" != "${WANTED_NAME1}" ]
83                                 then
84                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
85                                 else
86                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
87                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
88                                 fi
89
90                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
91                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
92                         elif [ -n "${WINDOWS_FILE_REMOTE}" -o -n "${WINDOWS_PATH_REMOTE}" ]
93                         then
94                                 echo "$0: Found a Windows (USB stick) remote."
95
96                                 if [ -n "${WANTED_NAME3}" -a "${REMOTE}" == "${WANTED_NAME3}" ]
97                                 then
98                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME3}'."
99                                 else
100                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME3}' ..."
101                                         git remote rename "${REMOTE}" "${WANTED_NAME3}" || exit 255
102                                 fi
103
104                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
105                                 git remote add "${GIT_ORIGIN}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
106                         elif [ -n "${LINUX_MEDIA_PATH_REMOTE}" ]
107                         then
108                                 echo "$0: Found a Linux (USB stick) remote."
109
110                                 if [ -n "${GIT_ORIGIN}" -a "${REMOTE}" = "${GIT_ORIGIN}" ]
111                                 then
112                                         echo "$0: Need to fix remote repositories ..."
113                                         git remote rename origin transport || exit 255
114                                         git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}" || exit 255
115                                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
116                                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
117                                 else
118                                         echo "$0: Don't know how to fix '${REMOTE}' (${REMOTE_PUSH})."
119                                         exit 1
120                                 fi
121                         else
122                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
123                                 exit 1
124                         fi
125                 fi
126         fi
127
128         UNWANTED_REMOTE=`git remote | grep "${UNWANTED_NAME}"`
129         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
130
131         if [ -n "${UNWANTED_REMOTE}" ]
132         then
133                 echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
134
135                 UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
136                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
137
138                 if [ -n "${UPSTREAM_REMOTE}" ]
139                 then
140                         echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
141                         git remote rm "${WANTED_NAME1}" || exit 255
142                 fi
143
144                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
145                 git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
146         else
147                 echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
148                 UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
149                 UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
150                 UPSTREAM_REMOTE3=`git remote | grep "${WANTED_NAME3}"`
151                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
152
153                 if [ -n "${UPSTREAM_REMOTE1}" ]
154                 then
155                         echo "$0: Remote '${WANTED_NAME1}' found in '${project}'."
156                 else
157                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
158                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
159                 fi
160
161                 if [ -n "${UPSTREAM_REMOTE2}" ]
162                 then
163                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
164                 elif [ ! -d "${GIT_URL2}" ]
165                 then
166                         echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
167                 elif [ -n "${WANTED_NAME2}" ]
168                 then
169                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
170                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
171                 fi
172
173                 if [ -n "${UPSTREAM_REMOTE3}" ]
174                 then
175                         echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
176                 elif [ ! -d "${GIT_URL3}" ]
177                 then
178                         echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
179                 else
180                         echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
181                         git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
182                 fi
183         fi
184 done
185
186 echo "$0: All done."
187 exit 0