Added docs and wildfly paths
[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
9 GIT_USER="quix0r"
10 GIT_SERVER="git.mxchange.org"
11 GIT_PATH="/var/cache/git/repos"
12 GIT_BRANCH="master"
13 GIT_ORIGIN="origin"
14
15 MEDIA_NAME="INTENSO"
16 MEDIA_PATH="Java Project"
17 MEDIA_BASE="/media"
18
19 for project in ${LIST}; do
20         GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
21         GIT_URL2="${GIT_PATH}/${project}.git"
22         echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}'"
23
24         cd "${project}"
25
26         if [ "$1" = "l" ]
27         then
28                 echo "$0: Listing remotes for '${project}' ..."
29                 git remote -v
30                 cd -
31                 continue;
32         fi
33
34         REMOTES=`git remote | wc -l`
35         echo "$0: REMOTES='${REMOTES}'"
36
37         if [ "${REMOTES}" = "1" ]
38         then
39                 echo "$0: Only one remote found for '${project}'"
40
41                 REMOTE=`git remote`
42                 REMOTE_PUSH=`git remote -v | grep "push"`
43                 echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
44
45                 MEDIA=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/"`
46                 echo "$0: MEDIA='${MEDIA}'"
47
48                 if [ -n "${MEDIA}" ]
49                 then
50                         echo "$0: Found ${MEDIA_BASE}/ remote for '${project}'."
51                         if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
52                         then
53                                 echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
54                         else
55                                 echo "$0: Renaming '${REMOTE}' to '${GIT_ORIGIN}' ..."
56                                 git remote rename "${REMOTE}" "${GIT_ORIGIN}" || exit 255
57                         fi
58                 else
59                         echo "$0: Not a media remote."
60                         MXCHANGE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL1}"`
61                         echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}'"
62
63                         if [ -n "${MXCHANGE_REMOTE}" ]
64                         then
65                                 echo "$0: Found a '${GIT_SERVER}' remote."
66
67                                 if [ "${REMOTE}" != "${WANTED_NAME1}" ]
68                                 then
69                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
70                                 else
71                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
72                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
73                                 fi
74
75                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
76                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
77                         else
78                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
79                                 exit 1
80                         fi
81                 fi
82         fi
83
84         UNWANTED_REMOTE=`git remote | grep "${UNWANTED_NAME}"`
85         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
86
87         if [ -n "${UNWANTED_REMOTE}" ]
88         then
89                 echo "$0: Found remote '${UNWANTED_NAME1}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME1}'->'${GIT_ORIGIN}' ..."
90
91                 UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
92                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
93
94                 if [ -n "${UPSTREAM_REMOTE}" ]
95                 then
96                         echo "$0: Remotes '${UNWANTED_NAME1}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
97                         git remote rm "${WANTED_NAME1}" || exit 255
98                 fi
99
100                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
101                 git remote rename "${UNWANTED_NAME1}" "${GIT_ORIGIN}" || exit 255
102         else
103                 echo "$0: No unwanted '${UNWANTED_NAME1}' remote in '${project}' ..."
104                 UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
105                 UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
106                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}'"
107
108                 if [ -n "${UPSTREAM_REMOTE1}" ]
109                 then
110                         echo "$0: Remote '${WANTED_NAME1}' found in '${project}'."
111                 else
112                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
113                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
114                 fi
115
116                 if [ -n "${UPSTREAM_REMOTE2}" ]
117                 then
118                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
119                 else
120                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
121                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
122                 fi
123         fi
124
125         cd -
126 done