-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_CheckoutRepos.sh
executable file
·236 lines (203 loc) · 8.34 KB
/
run_CheckoutRepos.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
#*******************************************************************************
# brief:
# clone or update repos to given checkout dir
#
# for usage, please refer to runUsage
#
# date: 2016.09.23
#*******************************************************************************
runUsage()
{
echo -e "\033[32m ******************************************************************************* \033[0m"
echo -e "\033[32m usage: \033[0m"
echo -e "\033[32m ./run_CheckoutRepos.sh \${GitRepositoryAddr} \${Branch} \033[0m"
echo -e "\033[32m \${CheckoutDir} \${ReposUpdateOption} \033[0m"
echo -e "\033[32m \033[0m"
echo -e "\033[32m ReposUpdateOption: fast or clone \033[0m"
echo -e "\033[32m if no value, will set to fast by default \033[0m"
echo -e "\033[32m ----fast, will only update via git pull command based on current repos, \033[0m"
echo -e "\033[32m no need to clone a new one \033[0m"
echo -e "\033[32m ----clone, will delete entire repos, and clone a new one \033[0m"
echo -e "\033[32m \033[0m"
echo -e "\033[32m example: \033[0m"
echo -e "\033[32m ./run_CheckoutRepos.sh https://github.com/cisco/openh264 master Source fast \033[0m"
echo -e "\033[32m ******************************************************************************* \033[0m"
}
runCheckParameter()
{
[ -z ${ReposUpdateOption} ] && ReposUpdateOption="fast"
if [ "${ReposUpdateOption}" = "fast" ]
then
if [ ! -d ${CheckoutDir} ]
then
echo "Source folder ${CheckoutDir} does not exist!"
echo "now change ReposUpdateOption to clone, which will clone a new repos to ${CheckoutDir} "
ReposUpdateOption="clone"
mkdir ${CheckoutDir}
return 0
fi
cd ${CheckoutDir}
#check if there is a repos
git remote -v
if [ ! $? -eq 0 ]
then
echo "git remote -v error, seems there is no repos"
echo "now change ReposUpdateOption to clone, which will clone a new repos to ${CheckoutDir} "
ReposUpdateOption="clone"
cd ${CurrentDir}
return 0
fi
git checkout ${Branch}
if [ ! $? -eq 0 ]
then
echo "git checkout ${Branch} error, seems there is no repos"
echo "now change ReposUpdateOption to clone, which will clone a new repos to ${CheckoutDir} "
ReposUpdateOption="clone"
cd ${CurrentDir}
return 0
fi
#check if the current repos git address is the same with input
CurrentReposAddr=` git remote -v | grep origin | head -1 | awk '{print $2}'`
if [ ! "${CurrentReposAddr}" = "${GitRepositoryAddr}" ]
then
echo "CurrentReposAddr is: ${CurrentReposAddr}"
echo "input GitRepositoryAddr is: ${GitRepositoryAddr}"
echo "current repos address is not the same with input"
echo "now change ReposUpdateOption to clone, which will clone a new repos to ${CheckoutDir} "
ReposUpdateOption="clone"
cd ${CurrentDir}
return 0
fi
cd ${CurrentDir}
fi
return 0
}
runGetCodecInfo()
{
git remote -v
git branch
git log -4
}
runUpdateRepos()
{
cd ${CheckoutDir}
#clean up repos
git clean -fdx
git status | grep "modified:" | awk '{print $2}' | xargs git checkout
#checkout to input branch
git checkout ${Branch}
[ ! $? -eq 0 ] && echo "checkout to branch ${Branch} failed!" && cd ${CurrentDir} && return 1
#clean up repos
git clean -fdx
git status | grep "modified:" | awk '{print $2}' | xargs git checkout
#pull latest commit
git pull origin ${Branch}
[ ! $? -eq 0 ] && echo "pull to latest commit failed!" && cd ${CurrentDir} && return 1
runGetCodecInfo>${CodecReposInfo}
cp -f ${CodecReposInfo} ${CurrentDir}
cd ${CurrentDir}
return 0
}
runCloneRepos()
{
#remove and clone a new repos to CheckoutDir
[ -d ${CheckoutDir} ] && echo "remove CheckoutDir ${CheckoutDir}" && ./Scripts/run_SafeDelete.sh ${CheckoutDir}
mkdir -p ${CheckoutDir}
git clone ${GitRepositoryAddr} ${CheckoutDir}
[ ! $? -eq 0 ] && echo "clone repos ${GitRepositoryAddr} failed!" && return 1
#checkout to input branch
cd ${CheckoutDir}
git checkout ${Branch}
[ ! $? -eq 0 ] && echo "checkout to branch ${Branch} failed!" && cd ${CurrentDir} && return 1
runGetCodecInfo>${CodecReposInfo}
cp -f ${CodecReposInfo} ${CurrentDir}
cd ${CurrentDir}
return 0
}
runOutputReposUpdateInfo()
{
echo -e "\033[32m*********************************************** \033[0m"
echo -e "\033[34m ReposUpdateOption is: ${ReposUpdateOption} \033[0m"
echo -e "\033[32m repos addr is: ${GitRepositoryAddr} \033[0m"
echo -e "\033[32m branch is: ${Branch} \033[0m"
echo -e "\033[32m update src dir is: ${CheckoutDir} \033[0m"
echo -e "\033[32m*********************************************** \033[0m"
}
runOutputReposCommitInfo()
{
echo -e "\033[33m*********************************************** \033[0m"
echo -e "\033[33m repos basic info \033[0m"
echo -e "\033[33m*********************************************** \033[0m"
cd ${CheckoutDir}
git branch
git remote -v
echo -e "\033[33m*********************************************** \033[0m"
git log -2
cd ${CurrentDir}
echo -e "\033[33m*********************************************** \033[0m"
echo -e "\033[32m repos update succeed! \033[0m"
echo -e "\033[33m*********************************************** \033[0m"
}
runMain()
{
CurrentDir=`pwd`
CodecReposInfo="CodecReposInfo.log"
runCheckParameter
runOutputReposUpdateInfo
if [ "${ReposUpdateOption}" = "fast" ]
then
echo -e "\033[33m*********************************************** \033[0m"
echo -e "\033[33m update repos only \033[0m"
echo -e "\033[33m*********************************************** \033[0m"
runUpdateRepos >ReposCheckout.log 2>&1
[ ! $? -eq 0 ] && echo -e "\033[31m\n update repos failed!\n\033[0m" && exit 1
elif [ "${ReposUpdateOption}" = "clone" ]
then
echo -e "\033[33m*********************************************** \033[0m"
echo -e "\033[33m clone a new repos \033[0m"
echo -e "\033[33m*********************************************** \033[0m"
runCloneRepos >ReposCheckout.log 2>&1
[ ! $? -eq 0 ] && echo -e "\033[31m\n update repos failed!\n\033[0m" && exit 1
else
echo -e "\033[31m\n ReposUpdateOption should be fast or clone \n\033[0m"
runUsage
exit 1
fi
runOutputReposCommitInfo
return 0
}
runExampleTest()
{
GitRepositoryAddr="https://github.com/cisco/openh264"
Branch="master"
CheckoutDir="Source"
ReposUpdateOption="fast"
runMain
}
#******************************************************************************************************************
#example:
#runExampleTest
#EnableWhenUsingExampleFunction()
#{
#******************************************************************************************************************
echo ""
echo "*********************************************************"
echo " call bash file is $0"
echo " input parameters is:"
echo " $0 $@"
echo "*********************************************************"
echo ""
#******************************************************************************************************************
if [ $# -lt 2 ]
then
runUsage
exit 1
fi
GitRepositoryAddr=$1
Branch=$2
CheckoutDir=$3
ReposUpdateOption=$4
runMain
#******************************************************************************************************************
#}