Install pre-commit hook via make hooks; resolve shared hooks dir

The hooks target uses git rev-parse --git-common-dir so it works from
both the main checkout and linked worktrees.
This commit is contained in:
2026-07-23 06:45:02 +07:00
parent 375233fff4
commit 5b20171dbd

View File

@@ -33,12 +33,16 @@ check: test lint fmt-check
docker: docker:
docker build -t $(BINARY) . docker build -t $(BINARY) .
# Hooks are shared between the main checkout and all worktrees, so
# resolve the common git dir instead of assuming .git is a directory.
HOOKS_DIR := $(shell git rev-parse --git-common-dir)/hooks
hooks: hooks:
@printf '#!/bin/sh\nset -e\n' > .git/hooks/pre-commit @printf '#!/bin/sh\nset -e\n' > $(HOOKS_DIR)/pre-commit
@printf 'go mod tidy\ngo fmt ./...\n' >> .git/hooks/pre-commit @printf 'go mod tidy\ngo fmt ./...\n' >> $(HOOKS_DIR)/pre-commit
@printf 'git diff --exit-code -- go.mod go.sum || { echo "go mod tidy changed files; please stage and retry"; exit 1; }\n' >> .git/hooks/pre-commit @printf 'git diff --exit-code -- go.mod go.sum || { echo "go mod tidy changed files; please stage and retry"; exit 1; }\n' >> $(HOOKS_DIR)/pre-commit
@printf 'make check\n' >> .git/hooks/pre-commit @printf 'make check\n' >> $(HOOKS_DIR)/pre-commit
@chmod +x .git/hooks/pre-commit @chmod +x $(HOOKS_DIR)/pre-commit
clean: clean:
rm -f $(BINARY) files.dat rm -f $(BINARY) files.dat