-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdateRepos.sh
executable file
·186 lines (171 loc) · 4.46 KB
/
updateRepos.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo -e "$text"
}
declare -a updatedRepos=()
addUpdatedRepo(){
updatedRepos=("${updatedRepos[@]}" "$1")
}
printUpdatedRepos(){
if [ ${#updatedRepos[@]} -gt 0 ]; then
cecho p "# ${#updatedRepos[@]} Updated repositories:"
cecho p "# --------------------------------------"
for repo in "${updatedRepos[@]}";
do
cecho p "# > ${repo}"
done
cecho p "# --------------------------------------"
else
cecho r "# ------------------"
cecho r "# | NOTHING UPDATED! |"
cecho r "# ------------------"
fi
}
declare -a failedRepos=()
addFailedRepo(){
failedRepos=("${failedRepos[@]}" "$1")
}
printFailedRepos(){
if [ ${#failedRepos[@]} -gt 0 ]; then
cecho r "# ${#failedRepos[@]} Failed repositories:"
cecho r "# --------------------------------------"
for repo in "${failedRepos[@]}";
do
cecho r "# > ${repo}"
done
cecho r "# --------------------------------------"
else
cecho g "# ------------------"
cecho g "# | NOTHING FAILED! |"
cecho g "# ------------------"
fi
}
update(){
clear
dir=$1;
cd $dir ;
if [ -d .git ]; then
cecho b "# UPDATING => $dir ";
cecho c "$(git fetch)";
git rev-parse --verify develop
if [ "$?" == "0" ]; then
cecho c "$(git checkout 'develop')";
cecho c "# Branch develop will be updated"
else
git rev-parse --verify develop
# git rev-parse --verify master
if [ "$?" == "0" ]; then
cecho c "$(git checkout 'master')";
cecho c "# Branch master will be updated"
fi
fi
output=$(git pull)
status=$?
cecho c $output;
if [[ $status != 0 ]]; then cecho r "${dir} failed :("; addFailedRepo ${dir}; fi
addUpdatedRepo ${dir}
cecho b "\n"
else
cecho y "#### $PWD does not contains a git repository"
fi
cd .. ;
}
finish(){
clear
cecho g "########################################";
cecho g "# DONE! ";
printUpdatedRepos ${updatedRepos}
printFailedRepos ${failedRepos}
cecho g "########################################";
cecho g "\nSEE YOU LATER BRO..."
}
clear
cecho g "\t\t---- GIT UPDATER ----"
if [ "$1" == "-a" ] || [ "$1" == "all" ]; then
cecho p "\tALL GIT REPOS WILL BE UPDATED\n"
for d in */ ; do
update $d;
done
finish
elif [ "$1" == "-e" ] || [ "$1" == "each" ]; then
for d in */ ; do
cd $d
isGit="0"
if [ -d .git ]; then
isGit="1"
fi
cd ..
if [ "$isGit" == "1" ]; then
cecho y "Do you want to update => $d (yes/no)"
read -p ">>> " res
while [ "$res" != "yes" ] && [ "$res" != "no" ]; do
cecho r "please write 'yes' or 'no'"
read -p ">>> " res
done
if [ "$res" == "yes" ]; then
update $d;
else
cecho r "$d Was NOT updated"
fi
fi
done
finish
else
cecho y "Do you want to update your repos in folder ${PWD} ? (yes/no)";
read -p ">>> " resp
while [ "$resp" != "yes" ] && [ "$resp" != "no" ]; do
cecho r "please write 'yes' or 'no'"
read -p ">>> " resp
done
if [ "$resp" == "yes" ]; then
clear
cecho y "Do you want to update all repositories or want to decide per folder?"
cecho y "1) update all"
cecho y "2) ask me for each one"
read -p ">>> " op;
opone="1";
optwo="2"
while [ "$op" != "$opone" ] && [ "$op" != "$optwo" ]; do
cecho r "please choose a valid option"
read -p ">>> " op;
done
if [ "$op" == "$opone" ]; then
for d in */ ; do
update $d;
done
finish;
elif [ "$op" == "$optwo" ];then
for d in */ ; do
cecho y "Do you want to update => $d (yes/no)"
read -p ">>> " res
while [ "$res" != "yes" ] && [ "$res" != "no" ]; do
cecho r "please write 'yes' or 'no'"
read -p ">>> " res
done
if [ "$res" == "yes" ]; then
update $d;
else
cecho y "$d Was not updated"
fi
done
finish
fi
elif [ "$resp" == "no" ]; then
cecho p "WELL.. see you later bro!!!"
else
cecho r "please write 'yes' or 'no'";
fi
fi