summaryrefslogtreecommitdiff
path: root/mirror.sh
blob: 118d2a45bac3ae5dccf700a02a9249f8a4c0c71c (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh
_version() {
	echo "@VERSION@"
	exit 0
}


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


_interpret() {
	while read type_ source_; do
		case "${type_}" in
			gitolite) _gitolite "${source_}" ;;
			repo) echo "${source_}" ;;
			'#'*) : ;;
			*) { echo -n "${type_}"; [ -n "${source_}" ] && echo -n " ${source_}"; echo; } ;;
		esac
	done
}


_gitolite() {
	{ ssh -n "$1" info -p || return 1; } | awk -F'\t' '/^\s*R/{ print "'"$1"'/"$2 }'
}


_mirror() {
	local failed=0
	while read repo; do
		if ! _update "${repo}"; then
			failed=1
		fi
	done
	return ${failed}
}


_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 :vn opt; do
	case ${opt} in
		v) _version;;
		n) _update() { echo "Would update: $(_path $1)"; };;
		?) _usage;;
	esac
done
shift $(( ${OPTIND} - 1 ))
list="${1:-repositories.mirror}"
test -f "${list}" || exit 1
_interpret <"${list}" | _mirror