summaryrefslogtreecommitdiff
path: root/ansible/files/mirror-git.sh
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-05-13 17:16:34 +0300
committerLars Wirzenius <liw@liw.fi>2021-05-13 17:16:34 +0300
commit9acbe58646ea92cf63070df4bd9a7aa3f0cdfb13 (patch)
treeeeda6d4f8cdcb130d743273d62266418ff2c9629 /ansible/files/mirror-git.sh
parente479b2862228b2faf68896da48571307c9211779 (diff)
downloadansibleness-9acbe58646ea92cf63070df4bd9a7aa3f0cdfb13.tar.gz
move mirror-git from Hetzner to local, and automatically clone repos
Diffstat (limited to 'ansible/files/mirror-git.sh')
-rw-r--r--ansible/files/mirror-git.sh67
1 files changed, 67 insertions, 0 deletions
diff --git a/ansible/files/mirror-git.sh b/ansible/files/mirror-git.sh
new file mode 100644
index 0000000..be52661
--- /dev/null
+++ b/ansible/files/mirror-git.sh
@@ -0,0 +1,67 @@
+#!/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