Skip to content

Commit 050ab78

Browse files
committed
fix(hooks): replace echo with printf for consistent output
Replaced all echo statements with printf in git hooks for better cross-platform compatibility and consistent output formatting. Changes affect: - .husky/pre-commit - .husky/security-checks.sh - .git-hooks/commit-msg - .git-hooks/pre-commit - .git-hooks/pre-push Benefits: - printf is more portable and reliable across shells - Better handling of escape sequences and formatting - Consistent with CLAUDE.md standards for shell scripts - Avoids potential issues with echo -e flag inconsistencies Note: echo statements in pipes (e.g., echo "$VAR" | grep) and command substitutions were intentionally preserved as they serve different purposes.
1 parent 2d95c19 commit 050ab78

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

.git-hooks/commit-msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if [ -n "$COMMITTED_FILES" ]; then
2424
# Check for Socket API keys (except allowed).
2525
if grep -E 'sktsec_[a-zA-Z0-9_-]+' "$file" 2>/dev/null | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | grep -v '\.example' | grep -q .; then
2626
printf "${RED}✗ SECURITY: Potential API key detected in commit!${NC}\n"
27-
echo "File: $file"
27+
printf "File: %s\n" "$file"
2828
ERRORS=$((ERRORS + 1))
2929
fi
3030

.git-hooks/pre-commit

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,32 @@ fi
2626
ERRORS=0
2727

2828
# Check for .DS_Store files.
29-
echo "Checking for .DS_Store files..."
29+
printf "Checking for .DS_Store files...\n"
3030
if echo "$STAGED_FILES" | grep -q '\.DS_Store'; then
3131
printf "${RED}✗ ERROR: .DS_Store file detected!${NC}\n"
3232
echo "$STAGED_FILES" | grep '\.DS_Store'
3333
ERRORS=$((ERRORS + 1))
3434
fi
3535

3636
# Check for log files.
37-
echo "Checking for log files..."
37+
printf "Checking for log files...\n"
3838
if echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'; then
3939
printf "${RED}✗ ERROR: Log file detected!${NC}\n"
4040
echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'
4141
ERRORS=$((ERRORS + 1))
4242
fi
4343

4444
# Check for .env files.
45-
echo "Checking for .env files..."
45+
printf "Checking for .env files...\n"
4646
if echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'; then
4747
printf "${RED}✗ ERROR: .env or .env.local file detected!${NC}\n"
4848
echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'
49-
echo "These files should never be committed. Use .env.example instead."
49+
printf "These files should never be committed. Use .env.example instead.\n"
5050
ERRORS=$((ERRORS + 1))
5151
fi
5252

5353
# Check for hardcoded user paths (generic detection).
54-
echo "Checking for hardcoded personal paths..."
54+
printf "Checking for hardcoded personal paths...\n"
5555
for file in $STAGED_FILES; do
5656
if [ -f "$file" ]; then
5757
# Skip test files and hook scripts.
@@ -63,26 +63,26 @@ for file in $STAGED_FILES; do
6363
if grep -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" 2>/dev/null | grep -q .; then
6464
printf "${RED}✗ ERROR: Hardcoded personal path found in: $file${NC}\n"
6565
grep -n -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" | head -3
66-
echo "Replace with relative paths or environment variables."
66+
printf "Replace with relative paths or environment variables.\n"
6767
ERRORS=$((ERRORS + 1))
6868
fi
6969
fi
7070
done
7171

7272
# Check for Socket API keys.
73-
echo "Checking for API keys..."
73+
printf "Checking for API keys...\n"
7474
for file in $STAGED_FILES; do
7575
if [ -f "$file" ]; then
7676
if grep -E 'sktsec_[a-zA-Z0-9_-]+' "$file" 2>/dev/null | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'SOCKET_SECURITY_API_KEY=' | grep -v 'fake-token' | grep -v 'test-token' | grep -q .; then
7777
printf "${YELLOW}⚠ WARNING: Potential API key found in: $file${NC}\n"
7878
grep -n 'sktsec_' "$file" | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | head -3
79-
echo "If this is a real API key, DO NOT COMMIT IT."
79+
printf "If this is a real API key, DO NOT COMMIT IT.\n"
8080
fi
8181
fi
8282
done
8383

8484
# Check for common secret patterns.
85-
echo "Checking for potential secrets..."
85+
printf "Checking for potential secrets...\n"
8686
for file in $STAGED_FILES; do
8787
if [ -f "$file" ]; then
8888
# Skip test files, example files, and hook scripts.
@@ -113,9 +113,9 @@ for file in $STAGED_FILES; do
113113
done
114114

115115
if [ $ERRORS -gt 0 ]; then
116-
echo ""
116+
printf "\n"
117117
printf "${RED}✗ Security check failed with $ERRORS error(s).${NC}\n"
118-
echo "Fix the issues above and try again."
118+
printf "Fix the issues above and try again.\n"
119119
exit 1
120120
fi
121121

.git-hooks/pre-push

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ while read local_ref local_sha remote_ref remote_sha; do
3838
# ============================================================================
3939
# CHECK 1: Scan commit messages for AI attribution
4040
# ============================================================================
41-
echo "Checking commit messages for AI attribution..."
41+
printf "Checking commit messages for AI attribution...\n"
4242

4343
# Check each commit in the range for AI patterns.
4444
while IFS= read -r commit_sha; do
@@ -47,28 +47,28 @@ while read local_ref local_sha remote_ref remote_sha; do
4747
if echo "$full_msg" | grep -qiE "(Generated with|Co-Authored-By: Claude|Co-Authored-By: AI|🤖 Generated|AI generated|Claude Code|@anthropic|Assistant:|Generated by Claude|Machine generated)"; then
4848
if [ $ERRORS -eq 0 ]; then
4949
printf "${RED}✗ BLOCKED: AI attribution found in commit messages!${NC}\n"
50-
echo "Commits with AI attribution:"
50+
printf "Commits with AI attribution:\n"
5151
fi
52-
echo " - $(git log -1 --oneline "$commit_sha")"
52+
printf " - %s\n" "$(git log -1 --oneline "$commit_sha")"
5353
ERRORS=$((ERRORS + 1))
5454
fi
5555
done < <(git rev-list "$range")
5656

5757
if [ $ERRORS -gt 0 ]; then
58-
echo ""
59-
echo "These commits were likely created with --no-verify, bypassing the"
60-
echo "commit-msg hook that strips AI attribution."
61-
echo ""
62-
echo "To fix:"
63-
echo " git rebase -i $remote_sha"
64-
echo " Mark commits as 'reword', remove AI attribution, save"
65-
echo " git push"
58+
printf "\n"
59+
printf "These commits were likely created with --no-verify, bypassing the\n"
60+
printf "commit-msg hook that strips AI attribution.\n"
61+
printf "\n"
62+
printf "To fix:\n"
63+
printf " git rebase -i %s\n" "$remote_sha"
64+
printf " Mark commits as 'reword', remove AI attribution, save\n"
65+
printf " git push\n"
6666
fi
6767

6868
# ============================================================================
6969
# CHECK 2: File content security checks
7070
# ============================================================================
71-
echo "Checking files for security issues..."
71+
printf "Checking files for security issues...\n"
7272

7373
# Get all files changed in these commits.
7474
CHANGED_FILES=$(git diff --name-only "$range" 2>/dev/null || echo "")
@@ -77,21 +77,21 @@ while read local_ref local_sha remote_ref remote_sha; do
7777
# Check for sensitive files.
7878
if echo "$CHANGED_FILES" | grep -qE '^\.env(\.local)?$'; then
7979
printf "${RED}✗ BLOCKED: Attempting to push .env file!${NC}\n"
80-
echo "Files: $(echo "$CHANGED_FILES" | grep -E '^\.env(\.local)?$')"
80+
printf "Files: %s\n" "$(echo "$CHANGED_FILES" | grep -E '^\.env(\.local)?$')"
8181
ERRORS=$((ERRORS + 1))
8282
fi
8383

8484
# Check for .DS_Store.
8585
if echo "$CHANGED_FILES" | grep -q '\.DS_Store'; then
8686
printf "${RED}✗ BLOCKED: .DS_Store file in push!${NC}\n"
87-
echo "Files: $(echo "$CHANGED_FILES" | grep '\.DS_Store')"
87+
printf "Files: %s\n" "$(echo "$CHANGED_FILES" | grep '\.DS_Store')"
8888
ERRORS=$((ERRORS + 1))
8989
fi
9090

9191
# Check for log files.
9292
if echo "$CHANGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log' | grep -q .; then
9393
printf "${RED}✗ BLOCKED: Log file in push!${NC}\n"
94-
echo "Files: $(echo "$CHANGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log')"
94+
printf "Files: %s\n" "$(echo "$CHANGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log')"
9595
ERRORS=$((ERRORS + 1))
9696
fi
9797

@@ -144,9 +144,9 @@ while read local_ref local_sha remote_ref remote_sha; do
144144
done
145145

146146
if [ $TOTAL_ERRORS -gt 0 ]; then
147-
echo ""
147+
printf "\n"
148148
printf "${RED}✗ Push blocked by mandatory validation!${NC}\n"
149-
echo "Fix the issues above before pushing."
149+
printf "Fix the issues above before pushing.\n"
150150
exit 1
151151
fi
152152

.husky/pre-commit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
if [ -z "${DISABLE_PRECOMMIT_LINT}" ]; then
55
pnpm lint --staged
66
else
7-
echo "Skipping lint due to DISABLE_PRECOMMIT_LINT env var"
7+
printf "Skipping lint due to DISABLE_PRECOMMIT_LINT env var\n"
88
fi
99

1010
if [ -z "${DISABLE_PRECOMMIT_TEST}" ]; then
1111
NODE_COMPILE_CACHE="./.cache" PRE_COMMIT=1 pnpm test --staged
1212
else
13-
echo "Skipping testing due to DISABLE_PRECOMMIT_TEST env var"
13+
printf "Skipping testing due to DISABLE_PRECOMMIT_TEST env var\n"
1414
fi

.husky/security-checks.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,32 @@ fi
2828
ERRORS=0
2929

3030
# Check for .DS_Store files.
31-
echo "Checking for .DS_Store files..."
31+
printf "Checking for .DS_Store files...\n"
3232
if echo "$STAGED_FILES" | grep -q '\.DS_Store'; then
3333
printf "${RED}✗ ERROR: .DS_Store file detected!${NC}\n"
3434
echo "$STAGED_FILES" | grep '\.DS_Store'
3535
ERRORS=$((ERRORS + 1))
3636
fi
3737

3838
# Check for log files.
39-
echo "Checking for log files..."
39+
printf "Checking for log files...\n"
4040
if echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'; then
4141
printf "${RED}✗ ERROR: Log file detected!${NC}\n"
4242
echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'
4343
ERRORS=$((ERRORS + 1))
4444
fi
4545

4646
# Check for .env files.
47-
echo "Checking for .env files..."
47+
printf "Checking for .env files...\n"
4848
if echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'; then
4949
printf "${RED}✗ ERROR: .env or .env.local file detected!${NC}\n"
5050
echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'
51-
echo "These files should never be committed. Use .env.example instead."
51+
printf "These files should never be committed. Use .env.example instead.\n"
5252
ERRORS=$((ERRORS + 1))
5353
fi
5454

5555
# Check for hardcoded user paths (generic detection).
56-
echo "Checking for hardcoded personal paths..."
56+
printf "Checking for hardcoded personal paths...\n"
5757
for file in $STAGED_FILES; do
5858
if [ -f "$file" ]; then
5959
# Skip test files and hook scripts.
@@ -65,26 +65,26 @@ for file in $STAGED_FILES; do
6565
if grep -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" 2>/dev/null | grep -q .; then
6666
printf "${RED}✗ ERROR: Hardcoded personal path found in: $file${NC}\n"
6767
grep -n -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" | head -3
68-
echo "Replace with relative paths or environment variables."
68+
printf "Replace with relative paths or environment variables.\n"
6969
ERRORS=$((ERRORS + 1))
7070
fi
7171
fi
7272
done
7373

7474
# Check for Socket API keys.
75-
echo "Checking for API keys..."
75+
printf "Checking for API keys...\n"
7676
for file in $STAGED_FILES; do
7777
if [ -f "$file" ]; then
7878
if grep -E 'sktsec_[a-zA-Z0-9_-]+' "$file" 2>/dev/null | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'SOCKET_SECURITY_API_KEY=' | grep -v 'fake-token' | grep -v 'test-token' | grep -q .; then
7979
printf "${YELLOW}⚠ WARNING: Potential API key found in: $file${NC}\n"
8080
grep -n 'sktsec_' "$file" | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | head -3
81-
echo "If this is a real API key, DO NOT COMMIT IT."
81+
printf "If this is a real API key, DO NOT COMMIT IT.\n"
8282
fi
8383
fi
8484
done
8585

8686
# Check for common secret patterns.
87-
echo "Checking for potential secrets..."
87+
printf "Checking for potential secrets...\n"
8888
for file in $STAGED_FILES; do
8989
if [ -f "$file" ]; then
9090
# Skip test files, example files, and hook scripts.
@@ -115,9 +115,9 @@ for file in $STAGED_FILES; do
115115
done
116116

117117
if [ $ERRORS -gt 0 ]; then
118-
echo ""
118+
printf "\n"
119119
printf "${RED}✗ Security check failed with $ERRORS error(s).${NC}\n"
120-
echo "Fix the issues above and try again."
120+
printf "Fix the issues above and try again.\n"
121121
exit 1
122122
fi
123123

0 commit comments

Comments
 (0)