浏览代码

Optimize pre-commit by OpenCode

jerryliao 2 天之前
父节点
当前提交
a651c8ddcd
共有 2 个文件被更改,包括 10 次插入8 次删除
  1. 6 5
      .githooks/pre-commit
  2. 4 3
      README.md

+ 6 - 5
.githooks/pre-commit

@@ -1,20 +1,21 @@
 #!/bin/sh
 
-# Pre-commit hook: ensures formatting and tests pass before committing
+# Pre-commit hook: auto-formats staged files and ensures tests pass before committing
 
 echo "Running pre-commit checks..."
 
-# 1. Check formatting on staged files
+# 1. Auto-format staged files
 STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx|json|md)$')
 
 if [ -n "$STAGED_FILES" ]; then
-  echo "Checking format..."
-  echo "$STAGED_FILES" | xargs deno fmt --check
+  echo "Auto-formatting..."
+  echo "$STAGED_FILES" | xargs deno fmt
   if [ $? -ne 0 ]; then
     echo ""
-    echo "Format check failed. Run 'deno fmt' to fix, then stage the changes."
+    echo "Auto-format failed. Fix the issues manually and try again."
     exit 1
   fi
+  echo "$STAGED_FILES" | xargs git add
 fi
 
 # 2. Run all tests

+ 4 - 3
README.md

@@ -40,6 +40,7 @@ deno task dev
 
 ### Git hooks
 
-A pre-commit hook is configured via `.githooks/` to automatically check
-formatting and run tests before each commit. The `deno task setup` command
-enables this by setting `core.hooksPath` to `.githooks/`.
+A pre-commit hook is configured via `.githooks/` to automatically format staged
+files and run tests before each commit. If auto-formatting fails, the commit is
+aborted. The `deno task setup` command enables this by setting `core.hooksPath`
+to `.githooks/`.