aboutsummaryrefslogtreecommitdiff
path: root/.githooks/pre-commit
blob: 9e919205d4faf8229f118bcfc8fbabcb72326489 (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
#!/bin/sh

# This hook formats every Go file before committing them.
# It helps to enforce a consistent style guide for those who forget to format their code properly.

STAGED="$(git diff --cached --name-only -- '*.go')"

if [ -n "$STAGED" ]; then
	for file in $STAGED; do
		if [ ! -e "$file" ]; then
			# file doesn't exist, skip
			continue
		fi

		# format the file
		go fmt "$file"

		# run goimports if it's there
		# it organizes imports
		if command -v goimports >/dev/null 2>&1; then
			goimports -w "$file"
		fi

		git add "$file"
	done
fi