summaryrefslogtreecommitdiff
path: root/mirror.sh
blob: 49a3057a33b110b3c0d3851524489e33635ddfee (plain)
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
#!/bin/sh
_version() {
	echo "@VERSION@"
	exit 0
}


_usage() {
	echo "Usage: $0 [-v] [list]"
	exit 1
} >&2


_path() {
	local path="$(url %h%p $1)"
	case "${path}" in
		*.git) echo "${path}";;
		*) echo "${path}.git";;
	esac
}


_update() {
	local repo="$1"
	local path="$(_path ${repo})"
	test -d "${path}" || git clone -q --mirror "${repo}" "${path}" || return 1  # TODO: Check if remotes are the same?
	if git -C "${path}" fetch -q; then
		echo "Updated ${path}"
	else
		echo "Failed to update ${path}" >&2
		return 1
	fi
}


while getopts :v opt; do
	case ${opt} in
		v) _version;;
		?) _usage;;
	esac
done
shift $(( ${OPTIND} - 1 ))
list="${1:-repositories.list}"
test -f "${list}" || exit 1
pids=
while read repo; do
	_update "${repo}" &
	pids="${pids} $!"
done <"${list}"
failed=0
for pid in ${pids}; do
	if ! wait ${pid}; then
		failed=1
	fi
done
exit ${failed}