oclaw/openclaw/git-hooks/pre-commit
oliver dbbe3add6a 重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。

Made-with: Cursor
2026-04-26 08:34:33 +08:00

80 lines
2.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
RUN_NODE_TOOL="$ROOT_DIR/scripts/pre-commit/run-node-tool.sh"
FILTER_FILES="$ROOT_DIR/scripts/pre-commit/filter-staged-files.mjs"
if [[ ! -x "$RUN_NODE_TOOL" ]]; then
echo "Missing helper: $RUN_NODE_TOOL" >&2
exit 1
fi
if [[ ! -f "$FILTER_FILES" ]]; then
echo "Missing helper: $FILTER_FILES" >&2
exit 1
fi
# Security: avoid option-injection from malicious file names (e.g. "--all", "--force").
# Robustness: NUL-delimited file list handles spaces/newlines safely.
# Compatibility: use read loops instead of `mapfile` so this runs on macOS Bash 3.x.
files=()
while IFS= read -r -d '' file; do
files+=("$file")
done < <(git diff --cached --name-only --diff-filter=ACMR -z)
if [ "${#files[@]}" -eq 0 ]; then
exit 0
fi
lint_files=()
while IFS= read -r -d '' file; do
lint_files+=("$file")
done < <(node "$FILTER_FILES" lint -- "${files[@]}")
format_files=()
while IFS= read -r -d '' file; do
format_files+=("$file")
done < <(node "$FILTER_FILES" format -- "${files[@]}")
docs_only=true
for file in "${files[@]}"; do
case "$file" in
docs/*|*.md|*.mdx)
;;
*)
docs_only=false
break
;;
esac
done
if [ "${#lint_files[@]}" -gt 0 ]; then
"$RUN_NODE_TOOL" oxlint --type-aware --fix -- "${lint_files[@]}"
fi
if [ "${#format_files[@]}" -gt 0 ]; then
"$RUN_NODE_TOOL" oxfmt --write --no-error-on-unmatched-pattern "${format_files[@]}"
fi
git add -- "${files[@]}"
# This hook is also exercised from lightweight temp repos in tests, where the
# staged-file safety behavior matters but the full OpenClaw workspace does not
# exist. Only run the repo-wide validation gate inside a real checkout.
if [[ -f "$ROOT_DIR/package.json" ]] && [[ -f "$ROOT_DIR/pnpm-lock.yaml" ]]; then
cd "$ROOT_DIR"
case "${FAST_COMMIT:-}" in
1|true|TRUE|yes|YES|on|ON)
echo "FAST_COMMIT enabled: skipping pnpm check in pre-commit hook."
;;
*)
if [[ "$docs_only" == true ]]; then
echo "Docs-only staged changes detected: skipping pnpm check in pre-commit hook."
else
pnpm check
fi
;;
esac
fi