summaryrefslogtreecommitdiff
path: root/ansible/files/mirror-git.sh
blob: be52661385aa75f8216fbab264131093d532698d (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
#!/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