aboutsummaryrefslogtreecommitdiffstats
path: root/cgit-chroot
blob: 1828ee528ca620740ae72b4741bba7fd74a926c2 (plain) (blame)
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
68
69
70
71
#!/bin/sh

set -e

CGIT_CHROOT=${CGIT_CHROOT:-/srv/cgit}
CGIT_GIT_ROOT=${CGIT_GIT_ROOT:-/srv/git}

log() {
	printf ' %s %s\n' "$1" "$2"
}

ladd() {
	log + "$1"
}

ldel() {
	log - "$1"
}

remove_prefix() {
	printf "%s" "${1#$CGIT_CHROOT/*}"
}

bind_mount() {
	mkdir "$2"
	mount --rbind "$1" "$2"
	mount --make-rslave "$2"
	ladd "$(remove_prefix "$2")"
}

bind_umount() {
	umount "$i"
	rmdir "$i"
	ldel "$(remove_prefix "$i")"
}

setup() {
	mkdir "$CGIT_CHROOT"/dev
	mknod "$CGIT_CHROOT"/dev/null c 1 3
	chmod 666 "$CGIT_CHROOT"/dev/null

	for i in "$CGIT_CHROOT"/instances/*; do
		test -d "$i" || continue
		user=$(basename "$i")
		id -ru "$user" >/dev/null || continue

		git_repo_dir="${CGIT_GIT_ROOT}/$user"
		test -d "$git_repo_dir" || continue

		instance_repo_dir="$CGIT_CHROOT"/instances/$user/repos

		bind_mount "$git_repo_dir" "$instance_repo_dir"
		chmod 0701 "$instance_repo_dir"
	done
}

teardown() {
	rm "$CGIT_CHROOT"/dev/null
	rmdir "$CGIT_CHROOT"/dev

	for i in "$CGIT_CHROOT"/instances/*/repos; do
		bind_umount "$i"
	done
}

test $# -eq 1 || exit 1

case $1 in
	setup) setup;;
	teardown) teardown;;
esac