+mirrorspeed()
+{
+ # first result is to be ignored, but we use it to check status
+ git ls-remote "$1" refs/heads/master >/dev/null 2>&1 || return 1
+ { time -p git ls-remote "$1" refs/heads/master; } 2>&1 >/dev/null | head -n 1 | cut -d ' ' -f 2 | tr -d . | sed 's,^0*,,'
+ # unit: clock ticks (depends on what "time" returns
+}
+
+bestmirror()
+{
+ pre=$1; shift
+ suf=$1; shift
+
+ if ! { time -p true; } >/dev/null 2>&1; then
+ return 1
+ fi
+
+ bestin=
+ bestt=
+ for in in "$@"; do
+ m=$pre$in$suf
+ if t=`mirrorspeed "$m"`; then
+ msg "$m -> $t ticks"
+ if [ -n "$t" ]; then
+ if [ -z "$bestt" ] || [ "$t" -lt "$bestt" ]; then
+ bestin=$in
+ bestt=$t
+ fi
+ fi
+ else
+ msg "$m -> FAIL"
+ fi
+ done
+ if [ -n "$bestin" ]; then
+ msg "Best mirror seems to be $pre$bestin$suf"
+ echo "$bestin"
+ else
+ return 1
+ fi
+}
+