Browse Source

feat(git-hooks): enhance pre-commit hook with whitelist support for console statements

Add whitelist functionality to debug checker to allow intentional console statements in specific files:
- Add WHITELIST_FILES configuration for platform services and utilities
- Update pre-commit hook to skip console pattern checks for whitelisted files
- Support regex patterns in whitelist for flexible file matching
- Maintain security while allowing legitimate debug code in platform services

This resolves the issue where the hook was blocking commits due to intentional console statements in whitelisted files like WebPlatformService and CapacitorPlatformService.
build-web-serve-test
Matthew Raymer 3 days ago
parent
commit
e733089bad
  1. 16
      scripts/git-hooks/debug-checker.config
  2. 39
      scripts/git-hooks/pre-commit

16
scripts/git-hooks/debug-checker.config

@ -61,6 +61,22 @@ SKIP_PATTERNS=(
"\.yaml$" # YAML config files "\.yaml$" # YAML config files
) )
# Files that are whitelisted for console statements
# These files may contain intentional console.log statements that are
# properly whitelisted with eslint-disable-next-line no-console comments
WHITELIST_FILES=(
"src/services/platforms/WebPlatformService.ts" # Worker context logging
"src/services/platforms/CapacitorPlatformService.ts" # Platform-specific logging
"src/services/platforms/ElectronPlatformService.ts" # Electron-specific logging
"src/services/QRScanner/.*" # QR Scanner services
"src/utils/logger.ts" # Logger utility itself
"src/utils/LogCollector.ts" # Log collection utilities
"scripts/.*" # Build and utility scripts
"test-.*/.*" # Test directories
".*\.test\..*" # Test files
".*\.spec\..*" # Spec files
)
# Logging level (debug, info, warn, error) # Logging level (debug, info, warn, error)
LOG_LEVEL="info" LOG_LEVEL="info"

39
scripts/git-hooks/pre-commit

@ -18,6 +18,11 @@ DEFAULT_DEBUG_PATTERNS=(
"<!-- debug" "<!-- debug"
"debug.*=" "debug.*="
) )
DEFAULT_WHITELIST_FILES=(
"src/services/platforms/WebPlatformService.ts"
"src/services/platforms/CapacitorPlatformService.ts"
"src/services/platforms/ElectronPlatformService.ts"
)
# Load configuration from file if it exists # Load configuration from file if it exists
load_config() { load_config() {
@ -27,6 +32,7 @@ load_config() {
PROTECTED_BRANCHES=() PROTECTED_BRANCHES=()
DEBUG_PATTERNS=() DEBUG_PATTERNS=()
SKIP_PATTERNS=() SKIP_PATTERNS=()
WHITELIST_FILES=()
# Read protected branches # Read protected branches
while IFS= read -r line; do while IFS= read -r line; do
@ -70,6 +76,20 @@ load_config() {
done done
fi fi
done < "$CONFIG_FILE" done < "$CONFIG_FILE"
# Read whitelist files
while IFS= read -r line; do
if [[ "$line" =~ ^WHITELIST_FILES=\( ]]; then
while IFS= read -r line; do
if [[ "$line" =~ ^\)$ ]]; then
break
fi
if [[ "$line" =~ \"([^\"]+)\" ]]; then
WHITELIST_FILES+=("${BASH_REMATCH[1]}")
fi
done
fi
done < "$CONFIG_FILE"
fi fi
# Use defaults if config loading failed # Use defaults if config loading failed
@ -85,6 +105,9 @@ load_config() {
SKIP_PATTERNS=("${DEFAULT_SKIP_PATTERNS[@]}") SKIP_PATTERNS=("${DEFAULT_SKIP_PATTERNS[@]}")
fi fi
if [[ ${#WHITELIST_FILES[@]} -eq 0 ]]; then
WHITELIST_FILES=("${DEFAULT_WHITELIST_FILES[@]}")
fi
} }
@ -110,6 +133,17 @@ should_skip_file() {
return 1 return 1
} }
# Check if file is whitelisted for console statements
is_whitelisted_file() {
local file="$1"
for whitelisted in "${WHITELIST_FILES[@]}"; do
if [[ "$file" =~ $whitelisted ]]; then
return 0
fi
done
return 1
}
# Main execution # Main execution
main() { main() {
# Load configuration # Load configuration
@ -159,6 +193,11 @@ main() {
# Check for debug patterns in the file # Check for debug patterns in the file
for pattern in "${DEBUG_PATTERNS[@]}"; do for pattern in "${DEBUG_PATTERNS[@]}"; do
# Skip console pattern checks for whitelisted files
if [[ "$pattern" == "console\." ]] && is_whitelisted_file "$file"; then
continue
fi
# For new files, check the file content directly # For new files, check the file content directly
# For modified files, check the staged diff # For modified files, check the staged diff
if [[ -f "$file" ]]; then if [[ -f "$file" ]]; then

Loading…
Cancel
Save