chore: better pre-hooks

This commit is contained in:
Slinetrac
2025-10-22 00:08:16 +08:00
Unverified
parent 4d2c1b4dc2
commit 95aee6ec81
2 changed files with 65 additions and 40 deletions

View File

@@ -1,24 +1,39 @@
#!/bin/bash
set -euo pipefail
echo "[pre-commit] Running lint-staged for JS/TS files..."
# Auto-fix staged JS/TS files, print warnings but don't fail commit
npx lint-staged || true
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"
# Check staged Rust files
RUST_FILES=$(git diff --cached --name-only | grep -E '^src-tauri/.*\.rs$' || true)
if [ -n "$RUST_FILES" ]; then
echo "[pre-commit] Running rustfmt and clippy on staged Rust files..."
cd src-tauri || exit
# Auto-format Rust code
cargo fmt
# Lint with clippy, print warnings but don't fail commit
cargo clippy-all || echo "⚠️ clippy found issues, but commit will continue."
cd ..
if ! command -v pnpm >/dev/null 2>&1; then
echo "❌ pnpm is required for pre-commit checks."
exit 1
fi
echo "[pre-commit] Checks completed. Some warnings may exist, please review."
exit 0
echo "[pre-commit] Running lint-staged for JS/TS files..."
pnpm exec lint-staged
RUST_FILES="$(git diff --cached --name-only --diff-filter=ACMR | grep -E '^src-tauri/.*\.rs$' || true)"
if [ -n "$RUST_FILES" ]; then
echo "[pre-commit] Formatting Rust changes with cargo fmt..."
(
cd src-tauri
cargo fmt
)
while IFS= read -r file; do
[ -n "$file" ] && git add "$file"
done <<< "$RUST_FILES"
echo "[pre-commit] Linting Rust changes with cargo clippy..."
(
cd src-tauri
cargo clippy-all
)
fi
TS_FILES="$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(ts|tsx)$' || true)"
if [ -n "$TS_FILES" ]; then
echo "[pre-commit] Running TypeScript type check..."
pnpm typecheck
fi
echo "[pre-commit] All checks completed successfully."

View File

@@ -1,32 +1,42 @@
#!/bin/bash
set -euo pipefail
remote_name="$1"
remote_name="${1:-origin}"
remote_url="${2:-unknown}"
# --- Rust clippy for staged files in src-tauri ---
if git diff --cached --name-only | grep -q '^src-tauri/'; then
echo "[pre-push] Running clippy on src-tauri..."
cargo clippy --manifest-path ./src-tauri/Cargo.toml -- -D warnings || {
echo "❌ Clippy found issues in src-tauri. Please fix them before pushing."
exit 1
}
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"
if ! command -v pnpm >/dev/null 2>&1; then
echo "❌ pnpm is required for pre-push checks."
exit 1
fi
# --- JS/TS format check only for main repo ---
if git remote get-url "$remote_name" >/dev/null 2>&1; then
remote_url=$(git remote get-url "$remote_name")
if [[ "$remote_url" =~ github\.com[:/]+clash-verge-rev/clash-verge-rev(\.git)?$ ]]; then
echo "[pre-push] Detected push to clash-verge-rev/clash-verge-rev ($remote_url)"
echo "[pre-push] Running pnpm format:check..."
if ! pnpm format:check; then
echo "❌ Code format check failed. Please fix formatting before pushing."
exit 1
fi
else
echo "[pre-push] Not pushing to target repo. Skipping format check."
fi
echo "[pre-push] Preparing to push to '$remote_name' ($remote_url). Running full validation..."
echo "[pre-push] Checking Prettier formatting..."
pnpm format:check
echo "[pre-push] Running ESLint..."
pnpm lint
echo "[pre-push] Running TypeScript type checking..."
pnpm typecheck
if command -v cargo >/dev/null 2>&1; then
echo "[pre-push] Verifying Rust formatting..."
(
cd src-tauri
cargo fmt --check
)
echo "[pre-push] Running cargo clippy..."
(
cd src-tauri
cargo clippy-all
)
else
echo "[pre-push] Remote '$remote_name' does not exist. Skipping format check."
echo "[pre-push] ⚠️ cargo not found; skipping Rust checks."
fi
echo "[pre-push] All checks passed."