aboutsummaryrefslogtreecommitdiffstats
path: root/roles/obsd_git/templates/git-shell-commands/create
blob: dcce4639487f11ac09b502d1bc45601139a99f83 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh
set -e

REPO=""
DESC=""
PROJ=""
VISIBILITY="public"

main() {
	parse_args "$@"

	repo_name=$(basename "$REPO" .git)
	if [ -n "$PROJ" ]; then
		proj_dir="{{ obsd_git_home }}/$VISIBILITY/$PROJ"
		if [ ! -d "$proj_dir" ]; then
			mkdir "$proj_dir"
		fi
		repo_path="$proj_dir/$repo_name.git"
		repo_url_path="$PROJ/$repo_name"
	else
		repo_path="{{ obsd_git_home }}/$VISIBILITY/$repo_name.git"
		repo_url_path="$repo_name"
	fi

	if [ ! -d "$repo_path" ]; then
		echo "Creating bare repository: $repo_name.git"
		git init --bare --initial-branch=main "$repo_path"
		if [ -n "$DESC" ]; then
			echo "$DESC" >>"$repo_path/description"
		fi
		cgit_config_extra_head_content_set_go_import "$repo_path" "$repo_url_path"
		chown -R git:git "$repo_path"
	fi
}

parse_args() {
	while [ $# -gt 0 ]; do
		case "$1" in
		--visibility)
			VISIBILITY="$2"
			case "$VISIBILITY" in public | private) ;; *) usage ;; esac
			shift 2
			;;
		--description)
			DESC="$2"
			shift 2
			;;
		--project)
			PROJ="$2"
			shift 2
			;;
		-*)
			echo "Unknown option: $1" >&2
			exit 1
			;;
		*)
			if [ -n "$REPO" ]; then
				echo "Unexpected argument: $1" >&2
				usage
			fi
			REPO="$1"
			shift
			;;
		esac
	done
	[ -n "$REPO" ] || usage
}

usage() {
	echo "Usage: $0 [--visibility public|private] [--description DESC] [--project PROJ] <repo>" >&2
	exit 1
}

# Ensure cgit advertises a go-import meta tag so the repo can be used as a Go
# module at {{ obsd_git_domain }}/<path>.
#
# Requires enable-git-config=1 in cgitrc.
cgit_config_extra_head_content_set_go_import() {
	module="{{ obsd_git_domain }}/$2"
	git -C "$1" config cgit.extra-head-content \
		"<meta name=\"go-import\" content=\"$module git https://$module\">"
}

main "$@"