From 27f1f6f5ccebd4e1078db65745ca68afafbf4b8e Mon Sep 17 00:00:00 2001 From: Stephen Adamson Date: Sun, 5 Jul 2026 22:37:08 +0100 Subject: [PATCH] chore: add pre-commit hook with auto-format and lint checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-commit hook runs prettier --write on staged frontend files, re-stages them, then checks eslint and go vet. Blocks the commit if eslint or vet fails. Stored in .githooks/ (version controlled) — enable with: git config core.hooksPath .githooks Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .githooks/pre-commit | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..c683d0b --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,52 @@ +#!/bin/sh +# Pre-commit hook: run CI-equivalent checks on staged files. +# Fails the commit if any check fails — matches CI pipeline in .gitea/workflows/ci.yaml. +# +# To skip: git commit --no-verify + +set -e + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Colour + +FAILED=0 + +# ---------- Frontend (prettier —write, then eslint) ---------- +FRONTEND_FILES=$(git diff --cached --name-only -- 'frontend/' | head -1) +if [ -n "$FRONTEND_FILES" ]; then + printf "${YELLOW}Auto-formatting staged frontend files with prettier...${NC}\n" + cd frontend && npx prettier --write . 2>&1 + # Re-stage any files prettier modified so formatting is included in the commit + cd .. + git diff --name-only -- 'frontend/' | xargs -r git add + + printf "${YELLOW}Checking eslint...${NC}\n" + if ! cd frontend && npx eslint . 2>&1; then + printf "${RED}✖ eslint failed${NC}\n" + FAILED=1 + fi + cd .. +fi + +# ---------- Backend (go vet) ---------- +BACKEND_FILES=$(git diff --cached --name-only -- 'backend/' | head -1) +if [ -n "$BACKEND_FILES" ]; then + printf "${YELLOW}Checking go vet...${NC}\n" + if ! cd backend && go vet ./... 2>&1; then + printf "${RED}✖ go vet failed${NC}\n" + FAILED=1 + fi + cd .. +fi + +# ---------- Result ---------- +if [ "$FAILED" -ne 0 ]; then + printf "\n${RED}Commit blocked — fix the issues above, then commit again.${NC}\n" + printf "${YELLOW}To skip checks: git commit --no-verify${NC}\n" + exit 1 +fi + +printf "${GREEN}✓ All pre-commit checks passed${NC}\n" +exit 0