summaryrefslogtreecommitdiff
path: root/git.d/config
blob: e76a795042c62a1695ff7044c2da2a0c8ffb75a2 (about) (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
85
#!/bin/bash

check_repo(){
	if [ -z "$SELECTED_REPO" ]; then
		echo "$0: error: you must first use the select command to pick a repo to operate on" >&2
		exit 1
	else
		echo "Operating on: $SELECTED_REPO"
	fi
}
usage_and_exit(){
	exec >&2
	echo "$0: error: incorrect syntax"
	echo "Usage: $parentcmd $cmd [--global|--local] list"
	echo "       $parentcmd $cmd [--global|--local] <get|unset> <key>"
	echo "       $parentcmd $cmd [--global|--local] set <key> <value>"
	exit 1
}

cmd="$(basename "$0")"
is_global=""
mode=""
while [ -n "$1" ]; do
	if [ "$1" = "--global" ]; then
		is_global="--global"
	elif [ "$1" = "--local" ]; then
		is_global="--local"
	elif [ "$1" = "unset" ]; then
		mode="unset"
	elif [ "$1" = "get" ]; then
		mode="get"
	elif [ "$1" = "set" ]; then
		mode="set"
	elif [ "$1" = "list" ]; then
		mode="list"
	else
		break
	fi
	shift
done

if [ "$mode" = "set" ]; then
	if [ $# -ne 2 ]; then
		usage_and_exit
	fi
elif [ "$mode" = "list" ]; then
	if [ $# -ne 0 ]; then
		usage_and_exit
	fi
	git -C "$SELECTED_REPO" config list $is_global
	exit 0
else
	if [ $# -ne 1 ]; then
		usage_and_exit
	fi
fi

if [ "$is_global" != "--global" ]; then
	check_repo
fi

key="$1"
value="$2"

whitelist_keys=(
	hooks.*
	init.defaultObjectFormat
	init.defaultRefFormat
	cgit.{owner,section,desc,homepage,hide,defbranch,branch-sort,readme}
	cgit.{logo,logo-link,extra-head-content,enable-remote-branches}
)

for whitelist_key in "${whitelist_keys[@]}"; do
	if [[ $key == $whitelist_key ]]; then
		if [ "$mode" = "set" ]; then
			git -C "$SELECTED_REPO" config $is_global $mode -- "$key" "$value"
		else
			git -C "$SELECTED_REPO" config $is_global $mode -- "$key"
		fi
		exit
	fi
done

echo "Key not allowed: $key" >&2
exit 2