Skip to content

Commit 52af579

Browse files
author
OpenClaw Subagent
committed
chore: add pre-commit hook to prevent committing sensitive files
1 parent 04d04d8 commit 52af579

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

HOOKS.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Git Hooks
2+
3+
## Pre-commit Hook
4+
5+
This repository uses a pre-commit hook to prevent committing sensitive files like `.env` files, secrets, and config files with sensitive data.
6+
7+
### Installing the Hook
8+
9+
Run the following command to install the hook:
10+
11+
```bash
12+
cp scripts/pre-commit-hook.sh .git/hooks/pre-commit
13+
chmod +x .git/hooks/pre-commit
14+
```
15+
16+
Or use the setup script:
17+
18+
```bash
19+
./scripts/setup-hooks.sh
20+
```
21+
22+
### What It Blocks
23+
24+
The pre-commit hook prevents commits containing files matching these patterns:
25+
- `.env*` (any file starting with .env)
26+
- `*.env` (any file ending with .env)
27+
- `env.*` (any file starting with env.)
28+
- `secrets.*` (any file starting with secrets.)
29+
- `config.*` (any file starting with config.)
30+
31+
### Best Practices
32+
33+
- Use `.env.example` for non-sensitive default values
34+
- Add sensitive files to `.gitignore` before creating them
35+
- Use environment-specific config files that are gitignored
36+
- Never commit actual API keys, passwords, or tokens

scripts/pre-commit-hook.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
# .env File Gatekeeper - Because developers are sneaky little hobbitses
3+
4+
# Colors for our dramatic warnings
5+
RED='\033[0;31m'
6+
GREEN='\033[0;32m'
7+
YELLOW='\033[1;33m'
8+
NC='\033[0m' # No Color
9+
10+
# The forbidden patterns - we know your tricks!
11+
FORBIDDEN_PATTERNS=(".env*" "*.env" "env.*" "secrets.*" "config.*")
12+
13+
# Check if we're in a git repo (because why else would you run this?)
14+
if ! git rev-parse --git-dir > /dev/null 2>&1; then
15+
echo -e "${RED}Not in a git repository. Are you lost?${NC}"
16+
exit 1
17+
fi
18+
19+
# Look for sneaky files
20+
FOUND_FILES=()
21+
for pattern in "${FORBIDDEN_PATTERNS[@]}"; do
22+
while IFS= read -r -d '' file; do
23+
# Skip .gitignore because we're not monsters
24+
if [[ "$file" != *".gitignore" ]]; then
25+
FOUND_FILES+=("$file")
26+
fi
27+
done < <(find . -name "$pattern" -type f -print0 2>/dev/null)
28+
done
29+
30+
# The moment of truth
31+
if [ ${#FOUND_FILES[@]} -eq 0 ]; then
32+
echo -e "${GREEN}✓ No forbidden files found. You're a good developer!${NC}"
33+
exit 0
34+
fi
35+
36+
# Oh dear...
37+
echo -e "${RED}⚠ DANGER WILL ROBINSON! Found forbidden files:${NC}"
38+
for file in "${FOUND_FILES[@]}"; do
39+
echo -e "${YELLOW}$file${NC}"
40+
# Check if it's in .gitignore (common rookie mistake)
41+
if grep -q "$(basename "$file")" .gitignore 2>/dev/null; then
42+
echo -e " ${RED}But wait... it's in .gitignore! Classic misdirection.${NC}"
43+
fi
44+
# Check if it's already tracked (oh no)
45+
if git ls-files --error-unmatch "$file" > /dev/null 2>&1; then
46+
echo -e " ${RED}AND IT'S ALREADY TRACKED! PANIC!${NC}"
47+
fi
48+
done
49+
50+
echo -e " ${RED}🚫 These files should NOT be committed. Add them to .gitignore!${NC}"
51+
echo -e "${YELLOW}💡 Pro tip: Use .env.example for non-sensitive defaults${NC}"
52+
exit 1

0 commit comments

Comments
 (0)