summaryrefslogtreecommitdiff
path: root/scripts/pre-commit.sh
blob: afcd09c061251031948e72fa7a3da566b92a26e6 (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
#!/bin/bash

set -e

_uuid() {
	if which uuid >/dev/null 2>&1 ; then
		uuid
	else
		uuidgen
	fi
}

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	base=HEAD
else
	# Initial commit: diff against an empty tree object
	base=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

error=0

for i in $(git diff --cached --name-only --diff-filter=AM $base -- "*yaml" | grep -vF .schema.yaml)
do
	# Generate unique key if missing
	if ! grep -q ^key: "$i"
	then
		key=$(_uuid)
		while grep -qrw "$key" .
		do
			key=$(_uuid)
		done
		echo "Generated key $key for $i"
		sed -i "/^team:/akey: $key" "$i"
		git add "$i"
	fi

	# Validate new/modified YAML project files
	echo -n "Validating $i: "
	if ! scripts/validator "$i"
	then
		echo "ERROR: $i failed to validate"
		error=1
	fi
done

exit $error