summaryrefslogtreecommitdiff
path: root/create-repo.sh
blob: 5bc213a89e6bfc868f86c3b10f9f07360cb7367f (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
#!/bin/sh
set -eu

GIT_REPO_ROOT="${GIT_REPO_ROOT:-/var/lib/git}"
GIT_USER="${GIT_USER:-git}"
GIT_GROUP="${GIT_GROUP:-git}"

usage() {
  echo "usage: create-repo <name> [description]" >&2
  echo "example: create-repo myrepo 'My application repository'" >&2
  exit 64
}

[ "$#" -ge 1 ] || usage

name="$1"
description="${2:-Unnamed repository}"

case "${name}" in
  */* | .* | *" "* | *".."* )
    echo "invalid repository name: ${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
  echo "repository already exists: ${repo_path}" >&2
  exit 1
fi

install -d -o "${GIT_USER}" -g "${GIT_GROUP}" "${repo_path}"
git init --bare "${repo_path}" >/dev/null
printf '%s\n' "${description}" > "${repo_path}/description"
chown -R "${GIT_USER}:${GIT_GROUP}" "${repo_path}"

echo "created ${repo_path}"