blob: 3accca8d1196b9f7fd6cbdab8e308938aa3c2de0 (
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
|
#!/bin/sh
# create-repo - create a bare repository the policy can talk about.
#
# Nothing is installed here, which is the point: every repository reaches the
# shared hook through the global core.hooksPath in /etc/gitconfig, so a bare
# repository you make by hand is enforced exactly like one made here.
#
# The policy is a read-only mount in a deployment, so this cannot grant
# anything. It prints the line to add to your policy source; until a rule
# reaches the new repository, 'git-authz validate' says so and every push to it
# is refused.
set -eu
GIT_REPO_ROOT="${GIT_REPO_ROOT:-/var/lib/git}"
GIT_USER="${GIT_USER:-git}"
GIT_GROUP="${GIT_GROUP:-git}"
DEFAULT_BRANCH="${GIT_HOST_DEFAULT_BRANCH:-main}"
usage() {
cat >&2 <<USAGE
usage: create-repo <name> [description]
Creates $GIT_REPO_ROOT/<name>.git - bare, owned by $GIT_USER, HEAD at
refs/heads/$DEFAULT_BRANCH - and prints the policy line to add.
USAGE
exit 64
}
[ "$#" -ge 1 ] || usage
name="$1"
description="${2:-Unnamed repository}"
case "${name}" in
*/* | .* | *" "* | *".."* )
printf 'invalid repository name: %s\n' "${name}" >&2
exit 64
;;
esac
case "${name}" in
*.git) repo_name="${name}" ;;
*) repo_name="${name}.git" ;;
esac
repo_path="${GIT_REPO_ROOT}/${repo_name}"
if [ -e "${repo_path}" ]; then
printf 'repository already exists: %s\n' "${repo_path}" >&2
exit 1
fi
install -d -o "${GIT_USER}" -g "${GIT_GROUP}" "${repo_path}"
git init --bare -b "${DEFAULT_BRANCH}" "${repo_path}" >/dev/null
printf '%s\n' "${description}" >"${repo_path}/description"
chown -R "${GIT_USER}:${GIT_GROUP}" "${repo_path}"
printf 'created %s\n' "${repo_path}"
printf 'policy line to add: <user> %s write refs/heads/*\n' "${name%.git}"
printf ' (edit the policy source and roll the ConfigMap; nothing here writes it)'
|