55 lines
1.3 KiB
Bash
55 lines
1.3 KiB
Bash
#!/bin/bash
|
|
#
|
|
# NAC Lint Pre-commit Hook
|
|
# 在Git提交前自动检查代码是否符合NAC原则
|
|
#
|
|
# 安装方法:
|
|
# cp memory/tools/pre-commit .git/hooks/pre-commit
|
|
# chmod +x .git/hooks/pre-commit
|
|
|
|
set -e
|
|
|
|
echo "🔍 Running NAC Lint Check..."
|
|
|
|
# 获取staged文件
|
|
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(rs|toml|json|md)$' || true)
|
|
|
|
if [ -z "$STAGED_FILES" ]; then
|
|
echo "✅ No relevant files to check."
|
|
exit 0
|
|
fi
|
|
|
|
# 运行NAC Lint检查
|
|
MEMORY_DIR="$(git rev-parse --show-toplevel)/memory"
|
|
|
|
if [ ! -f "$MEMORY_DIR/tools/nac_lint.py" ]; then
|
|
echo "⚠️ NAC Lint tool not found. Skipping check."
|
|
exit 0
|
|
fi
|
|
|
|
# 检查每个staged文件
|
|
HAS_VIOLATIONS=0
|
|
|
|
for FILE in $STAGED_FILES; do
|
|
if [ -f "$FILE" ]; then
|
|
python3 "$MEMORY_DIR/tools/nac_lint.py" check --path "$FILE" --output console > /tmp/nac_lint_output.txt 2>&1
|
|
|
|
if [ $? -ne 0 ]; then
|
|
cat /tmp/nac_lint_output.txt
|
|
HAS_VIOLATIONS=1
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [ $HAS_VIOLATIONS -eq 1 ]; then
|
|
echo ""
|
|
echo "❌ NAC Lint check failed. Commit blocked."
|
|
echo "Please fix the violations before committing."
|
|
echo ""
|
|
echo "Tip: Run 'python3 memory/tools/nac_lint.py check --path <file>' to check specific files."
|
|
exit 1
|
|
fi
|
|
|
|
echo "✅ NAC Lint check passed."
|
|
exit 0
|