#!/bin/bash LIST=/etc/mirror-git.list # LIST=mirror-list set -euo pipefail die() { echo "$@" 1>&2 exit 1 } setup_clone() { local dir="$1" local fromurl="$2" local tourl="$3" if [ ! -e "$dir" ]; then git clone -q "$fromurl" "$dir" cd "$dir" git remote rename origin gitlab git remote add origin "$tourl" fi } current_branch() { git rev-parse --abbrev-ref HEAD } uncommitted() { git status --short | grep '^ M ' >/dev/null } ssh-keyscan gitlab.com git.liw.fi >"$HOME/.ssh/known_hosts" grep '^[^#]' "$LIST" | while read -r fromurl tourl; do dir="$HOME/mirrors/$tourl" fromurl="https://gitlab.com/$fromurl.git" tourl="ssh://git@git.liw.fi/$tourl" echo echo "==============================================" echo "from: $fromurl" echo "via : $dir" echo "to : $tourl" setup_clone "$dir" "$fromurl" "$tourl" cd "$dir" case "$(current_branch)" in master | main) if uncommitted; then die "there are uncommitted changes" fi git remote update --prune git pull -q --rebase gitlab HEAD git push -q origin HEAD ;; *) die "not in main or master branch" ;; esac done echo echo all mirror OK