summaryrefslogtreecommitdiff
path: root/mirror.sh
blob: d3bb652eb2f4dfcc58d6db07a706d7ebf85de592 (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
#!/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
failed=0
while read repo; do
	if ! _update "${repo}"; then  # TODO: Parallelize this without DDOSing the hosts
		failed=1
	fi
done <"${list}"
exit ${failed}