From 7111049c38197e61c97166d1cb01507e1910c22f Mon Sep 17 00:00:00 2001 From: Noel Kwan Date: Wed, 22 Apr 2026 21:40:17 +0800 Subject: [PATCH] chore: add project pre-commit hook mirroring CI Adds a versioned `.githooks/pre-commit` that runs the same checks as `.github/workflows/ci.yml` (svelte-check, vitest, cargo fmt/clippy/test) so failures surface locally before push. A `prepare` script in package.json auto-sets `core.hooksPath=.githooks` after `pnpm install`, covering fresh clones and new worktrees without manual setup. Cargo steps are pinned to `+stable` to match CI's toolchain, cargo test runs single-threaded to avoid a pre-existing shared-env-var flake between scaffold and worktree tests, and the hook clears GIT_DIR and related env vars that git exports into hooks (which otherwise leak into test subprocesses and make them operate on the parent repo). Co-Authored-By: Claude Opus 4.7 (1M context) --- .githooks/pre-commit | 29 +++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..8b43f27 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Mirrors .github/workflows/ci.yml so local commits match CI. +# Runs from the worktree root regardless of where `git commit` was invoked. +cd "$(git rev-parse --show-toplevel)" + +# Git exports GIT_DIR/GIT_INDEX_FILE/etc. into hooks; subprocess `git` +# calls inside tests would otherwise target the parent repo instead of +# their tmp repos, breaking any test that shells out to git. +unset GIT_DIR GIT_INDEX_FILE GIT_WORK_TREE GIT_PREFIX GIT_EXEC_PATH \ + GIT_AUTHOR_DATE GIT_COMMITTER_DATE GIT_EDITOR GIT_REFLOG_ACTION + +echo "[pre-commit] pnpm check" +pnpm check + +echo "[pre-commit] pnpm test" +pnpm test + +echo "[pre-commit] cargo +stable fmt --check" +(cd src-tauri && cargo +stable fmt --check) + +echo "[pre-commit] cargo +stable clippy -- -D warnings" +(cd src-tauri && cargo +stable clippy -- -D warnings) + +echo "[pre-commit] cargo +stable test" +# --test-threads=1 works around a shared-env-var flake between scaffold +# tests and worktree tests; see followup issue. +(cd src-tauri && cargo +stable test -- --test-threads=1) diff --git a/package.json b/package.json index 4980b99..9815d18 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "test": "vitest run", "test:e2e": "npx playwright test --project=e2e", "demo": "npx playwright test --project=demo", - "test:watch": "vitest" + "test:watch": "vitest", + "prepare": "git config core.hooksPath .githooks" }, "license": "MIT", "dependencies": {