From 1211b87f4e8aef3bd9f4cdb62efdcc689f5d4696 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 19 Aug 2025 05:51:05 +0000 Subject: [PATCH] feat(git): implement debug code prevention system with deliberate installation Implements comprehensive pre-commit hook system to prevent debug code from reaching protected branches while maintaining developer choice. - Hooks stored in scripts/git-hooks/ (not in .git tree) - Deliberate installation required - no forced behavior - Automated installation script for team members - Comprehensive testing - Branch-aware execution (protected vs feature branches) - Configurable patterns and protected branch list Philosophy: Each developer chooses whether to use the hook, ensuring team flexibility while providing powerful debug code prevention tools. --- doc/debug-hook-guide.md | 35 ++-- scripts/git-hooks/README.md | 103 ++++++++++++ scripts/git-hooks/debug-checker.config | 70 ++++++++ scripts/git-hooks/pre-commit | 213 +++++++++++++++++++++++++ scripts/install-debug-hook.sh | 171 ++++++++++++++++++++ scripts/test-debug-hook.sh | 117 ++++++++++++++ 6 files changed, 700 insertions(+), 9 deletions(-) create mode 100644 scripts/git-hooks/README.md create mode 100644 scripts/git-hooks/debug-checker.config create mode 100755 scripts/git-hooks/pre-commit create mode 100755 scripts/install-debug-hook.sh create mode 100755 scripts/test-debug-hook.sh diff --git a/doc/debug-hook-guide.md b/doc/debug-hook-guide.md index 2b029d33..956a21e7 100644 --- a/doc/debug-hook-guide.md +++ b/doc/debug-hook-guide.md @@ -18,15 +18,17 @@ A pre-commit hook that automatically detects and prevents debug code from reachi ./scripts/install-debug-hook.sh ``` -This automatically installs, updates, and verifies the hook in your current repository. +This automatically installs, updates, and verifies the hook in your current +repository. **Note**: Hooks are not automatically installed - you must run this +script deliberately to enable debug code checking. ## ๐Ÿ”ง Manual Installation **Copy files manually:** ```bash -cp .git/hooks/pre-commit /path/to/your/repo/.git/hooks/ -cp .git/hooks/debug-checker.config /path/to/your/repo/.git/hooks/ +cp scripts/git-hooks/pre-commit /path/to/your/repo/.git/hooks/ +cp scripts/git-hooks/debug-checker.config /path/to/your/repo/.git/hooks/ chmod +x /path/to/your/repo/.git/hooks/pre-commit ``` @@ -36,12 +38,27 @@ chmod +x /path/to/your/repo/.git/hooks/pre-commit - **`debug-checker.config`** - Configuration file - **`README.md`** - Documentation and troubleshooting +**Note**: Hooks are stored in `scripts/git-hooks/` and must be deliberately +installed by each developer. They are not automatically active. + ## ๐ŸŽฏ How It Works -1. **Branch Detection**: Only runs on protected branches -2. **File Filtering**: Automatically skips tests, scripts, and documentation -3. **Pattern Matching**: Detects debug code using regex patterns -4. **Commit Prevention**: Blocks commits containing debug code +1. **Deliberate Installation**: Hooks must be explicitly installed by each + developer +2. **Branch Detection**: Only runs on protected branches +3. **File Filtering**: Automatically skips tests, scripts, and documentation +4. **Pattern Matching**: Detects debug code using regex patterns +5. **Commit Prevention**: Blocks commits containing debug code + +## ๐Ÿ”’ Installation Philosophy + +**Why deliberate installation?** + +- **Developer choice**: Each developer decides whether to use the hook +- **No forced behavior**: Hooks don't interfere with existing workflows +- **Local control**: Hooks are installed locally, not globally +- **Easy removal**: Can be uninstalled at any time +- **Team flexibility**: Some developers may prefer different tools ## ๐ŸŒฟ Branch Behavior @@ -145,8 +162,8 @@ A test script is available at `scripts/test-debug-hook.sh` to verify the hook wo ## ๐Ÿ“š Additional Resources -- **Hook documentation**: `.git/hooks/README.md` -- **Configuration**: `.git/hooks/debug-checker.config` +- **Hook documentation**: `scripts/git-hooks/README.md` +- **Configuration**: `scripts/git-hooks/debug-checker.config` - **Test script**: `scripts/test-debug-hook.sh` - **Installation script**: `scripts/install-debug-hook.sh` diff --git a/scripts/git-hooks/README.md b/scripts/git-hooks/README.md new file mode 100644 index 00000000..d335b3b2 --- /dev/null +++ b/scripts/git-hooks/README.md @@ -0,0 +1,103 @@ +# TimeSafari Git Hooks + +This directory contains custom Git hooks for the TimeSafari project. + +## Debug Code Checker Hook + +### Overview +The `pre-commit` hook automatically checks for debug code when committing to protected branches (master, main, production, release). This prevents debug statements from accidentally reaching production code. + +### How It Works +1. **Branch Detection**: Only runs on protected branches (configurable) +2. **File Filtering**: Automatically skips test files, scripts, and documentation +3. **Pattern Matching**: Detects common debug patterns using regex +4. **Commit Prevention**: Blocks commits containing debug code + +### Protected Branches (Default) +- `master` +- `main` +- `production` +- `release` +- `stable` + +### Debug Patterns Detected +- **Console statements**: `console.log`, `console.debug`, `console.error` +- **Template debug**: `Debug:`, `debug:` in Vue templates +- **Debug constants**: `DEBUG_`, `debug_` variables +- **HTML debug**: `" 1 + +echo -e "\n${BLUE}Test Case 7: Debug attribute (should fail)${NC}" +run_test "Debug attribute" "
content
" 1 + +echo -e "\n${BLUE}Test Case 8: Test file (should be skipped)${NC}" +run_test "Test file" "console.log('this should be skipped')" 0 + +# Test branch detection +echo -e "\n${BLUE}Testing branch detection...${NC}" +cd "$TEST_DIR" +git init > /dev/null 2>&1 +git checkout -b feature-branch > /dev/null 2>&1 +echo "console.log('debug')" > test.vue +git add test.vue > /dev/null 2>&1 + +if bash ../../.git/hooks/pre-commit > hook_output.txt 2>&1; then + echo -e " ${GREEN}โœ… PASS${NC} - Hook skipped on feature branch" +else + echo -e " ${RED}โŒ FAIL${NC} - Hook should have been skipped on feature branch" + echo -e " ${YELLOW}Hook output:${NC}" + cat hook_output.txt +fi + +rm -rf .git +rm -f hook_output.txt + +echo -e "\n${GREEN}๐ŸŽ‰ All tests completed!${NC}" +echo -e "\n${BLUE}To test manually:${NC}" +echo "1. Make changes to a file with debug code" +echo "2. Stage the file: git add " +echo "3. Try to commit: git commit -m 'test'" +echo "4. The hook should prevent the commit if debug code is found"