-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-git-repos.sh
executable file
·224 lines (201 loc) · 5.38 KB
/
bootstrap-git-repos.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
#!/usr/bin/env bash
SCRIPT_NAME=`basename $0`
usage() {
echo "
Usage: $SCRIPT_NAME -h
Usage: $SCRIPT_NAME [-c <path>] -v
Usage: $SCRIPT_NAME [-g <path>]
Check out/update all configured git repositories.
-h: This help text.
-c: Optional. Path to the config file, default is: ~/.git-repositories
-v: Optional. Verify if all repositories are up to date.
-g: Generate config file entries, either for a single repository, or
from repositories in a parent directory. For parent directories, only
searches one layer deep, and skips any directories that are not git
repositories. Fetch URLs are obtained from the 'origin' remote.
The config file syntax is extremly simple:
- Lines starting with a # or empty space are ignored.
- For all repositories, use the following single line format:
[clone-url]#[rev]#[checkout-path]
Where [clone-url] is the URL of the git repo, [rev] is the desired HEAD
revision, and [checkout-path] is the path to the checkout.
E.g., the following config line:
https://github.com/rg3/youtube-dl#master#/usr/local/youtube-dl
Would clone the https://github.com/rg3/youtube-dl repository to
/usr/local/youtube-dl, then force reset HEAD to the master revision.
CAVEATS:
Full git commit hashes must be used for revisions in the configuration for the
verify operation to work correctly.
"
}
echoerr() {
cat <<< "$@" 1>&2;
RETVAL=1
}
add_output() {
OUTPUT="${OUTPUT}
${1}"
}
generate_repositories_config_from_directory() {
local path="${1/#\~/$HOME}"
if [[ -d "${path}" ]]; then
local cwd=`pwd`
cd ${path}
local head_rev=`git rev-parse HEAD 2>/dev/null`
if [ -n "${head_rev}" ]; then
local remote=`git remote get-url origin`
if [ -n "${remote}" ]; then
add_output "${remote}#${head_rev}#${path}"
else
echo "WARN: ${path} does not have an origin remote"
fi
else
local repo_dirs=`ls -1`
for repo_dir in $repo_dirs; do
if [[ -d "${repo_dir}" ]]; then
cd ${repo_dir}
local head_rev=`git rev-parse HEAD 2>/dev/null`
if [ -n "${head_rev}" ]; then
local remote=`git remote get-url origin`
if [ -n "${remote}" ]; then
add_output "${remote}#${head_rev}#${path}/${repo_dir}"
else
echo "WARN: ${path}/${repo_dir} does not have an origin remote"
fi
else
echo "WARN: ${path}/${repo_dir} is not a git repository"
fi
cd ..
fi
done
fi
cd "${cwd}"
echo "${OUTPUT}"
exit 0
else
echoerr "ERROR: ${path} is not a valid directory"
exit 1
fi
}
update_repo() {
local url="${1}"
local rev="${2}"
local path="${3}"
local setup_succeeded=
if ! [[ -d "${path}" ]]; then
if [ "${verify}" = "yes" ]; then
add_output "Missing repository ${url} at ${path}"
else
git clone "${url}" "${path}"
if [ $? -eq 0 ]; then
setup_succeeded=1
else
echoerr "ERROR: Could not clone ${url} to ${path}"
fi
fi
else
cd "${path}"
head_rev=`git rev-parse HEAD 2>/dev/null`
if [ $? -eq 0 ]; then
if [ "${head_rev}" != "${rev}" ]; then
if [ "${verify}" = "yes" ]; then
add_output "${url} at ${path} needs update:"
add_output " HEAD: ${head_rev}"
add_output " CONFIG: ${rev}"
else
setup_succeeded=1
git pull
fi
fi
else
echoerr "ERROR: ${path} does not appear to be a git repository"
fi
fi
if [ "${setup_succeeded}" = "1" ]; then
cd "${path}"
git reset --hard ${rev} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echoerr "ERROR: could not reset checkout at ${path} to revision ${rev}"
fi
fi
}
parse_line() {
local line="${1}"
local op="${2}"
local text=`echo ${line} | xargs`
if [ -n "${text}" ] && ! [[ "${text:0:1}" = "#" ]]; then
IFS='#'; parts=(${text}); unset IFS;
local url="${parts[0]}"
local rev="${parts[1]}"
local path="${parts[2]/#\~/$HOME}"
if [ -z "${rev}" ]; then
echoerr "CONFIG ERROR: missing revision on line '${text}'"
elif [ -z "${path}" ]; then
echoerr "CONFIG ERROR: missing path on line '${text}'"
else
if [ "${op}" = "execute" ]; then
update_repo "${url}" "${rev}" "${path}"
fi
fi
fi
}
read_config() {
local op="${1}"
while IFS='' read -r line || [[ -n "$line" ]]; do
parse_line "${line}" "${op}"
done < "${config_file}"
}
main() {
if [ -r "${config_file}" ]; then
local cwd=`pwd`
read_config
if [ ${RETVAL} -eq 0 ]; then
read_config execute
fi
cd "${cwd}"
else
echo
echoerr "ERROR: file '${config_file}' not readable"
fi
}
verify="no"
OUTPUT=
config_file="${HOME}/.git-repositories"
generate_parent_directory=
while getopts ":hc:vg:" opt; do
case $opt in
h)
usage
exit 0
;;
c)
config_file="${OPTARG/#\~/$HOME}"
;;
v)
verify="yes"
;;
g)
generate_repositories_config_from_directory "${OPTARG}"
exit 0
;;
\?)
echoerr "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
:)
echoerr "Option -${OPTARG} requires an argument." >&2
usage
exit 1
;;
esac
done
RETVAL=0
main
if [ ${RETVAL} -eq 0 ]; then
if [ -z "${OUTPUT}" ]; then
add_output "All repositories up to date"
fi
fi
echo "${OUTPUT}"
exit ${RETVAL}