#!/bin/sh _version() { echo "@VERSION@" exit 0 } _usage() { echo "Usage: $0 [-v] [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 :v opt; do case ${opt} in v) _version;; ?) _usage;; esac done shift $(( ${OPTIND} - 1 )) list="${1:-repositories.mirror}" test -f "${list}" || exit 1 _interpret <"${list}" | _mirror