aboutsummaryrefslogtreecommitdiff
path: root/.githooks/pre-commit
diff options
context:
space:
mode:
Diffstat (limited to '.githooks/pre-commit')
-rwxr-xr-x.githooks/pre-commit12
1 files changed, 10 insertions, 2 deletions
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
index 7fda0a3..9e91920 100755
--- a/.githooks/pre-commit
+++ b/.githooks/pre-commit
@@ -1,14 +1,22 @@
#!/bin/sh
-# This hook formats every Go file with `go fmt` before committing them.
-# It helps to enforce the Go style guide for those who forget to format their code properly.
+# 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