-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-hook
More file actions
executable file
·86 lines (73 loc) · 2.74 KB
/
Copy pathpre-commit-hook
File metadata and controls
executable file
·86 lines (73 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# Git Pre-Commit Hook: Citation Verification
#
# Checks staged files for citations and verifies them against the database.
# Blocks commits containing unverified or hallucinated citations.
#
# Installation:
# cp pre-commit-hook .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
set -e
# Get the repository root
REPO_ROOT="$(git rev-parse --show-toplevel)"
CITATION_CHECKER="$REPO_ROOT/citationChecker.py"
# Colors for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
echo ""
echo "📚 Checking citations in staged files..."
# Check if citation checker exists
if [ ! -f "$CITATION_CHECKER" ]; then
echo -e "${YELLOW}⚠️ Citation checker not found. Skipping verification.${NC}"
echo "Install: Copy citationChecker.py to repository root"
exit 0
fi
# Get staged files (exclude binary files)
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | \
grep -E '\.(md|txt|py|ts|tsx|js|jsx)$' || true)
if [ -z "$STAGED_FILES" ]; then
echo -e "${GREEN}✅ No text files staged${NC}"
echo ""
exit 0
fi
# Check each staged file
UNVERIFIED_FILES=""
TOTAL_UNVERIFIED=0
for file in $STAGED_FILES; do
# Get the staged content (not working directory)
CONTENT=$(git show ":$file" 2>/dev/null || true)
if [ -z "$CONTENT" ]; then
continue
fi
# Run citation checker in quiet mode
if ! echo "$CONTENT" | python3 "$CITATION_CHECKER" --stdin --quiet 2>/dev/null; then
UNVERIFIED_FILES="$UNVERIFIED_FILES\n - $file"
((TOTAL_UNVERIFIED++)) || true
fi
done
# Report results
if [ $TOTAL_UNVERIFIED -eq 0 ]; then
echo -e "${GREEN}✅ All citations verified${NC}"
echo ""
exit 0
else
echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${RED}❌ COMMIT BLOCKED: Unverified citations found${NC}"
echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "Files with unverified citations:"
echo -e "$UNVERIFIED_FILES"
echo ""
echo "Run citation checker manually for details:"
echo " python3 citationChecker.py --file <filename>"
echo ""
echo "To commit anyway (not recommended):"
echo " git commit --no-verify"
echo ""
echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
# Block commit (change to 'exit 0' to allow with warning)
exit 1
fi