aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/git-mkbare
blob: 973ebaeda29a8abd8b521d4254c7f7de13dbf579 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env sh

readonly CGIT_INDEX="$HOME/.local/srv/cgit"
readonly GIT_INDEX="$HOME/.local/git"

main()
{
	# Handle opts
	opts "$@"
	shift "$OPTS"
	unset OPTS

	# Show help
	[ "$OPT_HELP_ONLY" ] && usage && exit 0

	cd -- "$GIT_INDEX"

	location=$(readlink -f "${1:-$PWD}")

	# Prompt for location
	printf "%s: [%s] " "Path" "$location"
	read -r input
	[ -z "$input" ] || location=$(readlink -f "$input")
	unset input

	# Prompt for description
	printf "%s: " "Description"
	read -r description

	# Prompt for linking into cgit index
	if [ -d "$CGIT_INDEX" ]
	then
		while :
		do
			printf "%s: [%s] " "Link in cgit?" "Y/n"
			read -r input

			case "$input" in
				Y|y|'') cgit_link=1; break;;
				N|n) break;;
			esac
		done

		unset input
	fi

	# Create the bare git repository
	mkdir -p "$location"
	cd -- "$location"
	git --bare init

	# Update the description, if given
	printf "%s\n" "$description" > "$location/description"

	# Link into cgit index
	if [ -n "$cgit_link" ]
	then
		mkdir -p "$CGIT_INDEX/$(dirname "${location#$GIT_INDEX}")"
		ln -s "$location" "$CGIT_INDEX/${location#$GIT_INDEX}"
	fi
}

opts()
{
	OPTS=0

	while getopts ":h" opt
	do
		case "$opt" in
			h) OPT_HELP_ONLY=1 ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	unset opt
}

usage()
{
	cat <<EOF
Usage:
	$(basename "$0") -h

Create a bare git repository at a target location.

Options:
	-h  Show this help text and exit.
EOF
}

main "$@"