Fix git commit blocking hook to use stderr and add comprehensive tests#167
Merged
Fix git commit blocking hook to use stderr and add comprehensive tests#167
Conversation
09e4497 to
3d6faeb
Compare
The block_bash_with_instructions.sh hook had two issues: 1. It output error messages as JSON to stdout instead of plain text to stderr 2. The regex pattern matched 'git commit' anywhere in the command string According to Claude Code hook documentation, PreToolUse hooks with exit code 2 should output their error message to stderr, not stdout with JSON. Additionally, the regex pattern was too broad and would match 'git commit' appearing in commit message bodies or argument strings. The pattern is now anchored to match only at the start of the command. Changes: - Updated hook to write error message to stderr instead of JSON to stdout - Anchored regex pattern with ^[[:space:]]* to match only command start - Updated documentation comments to reflect correct output behavior - Added comprehensive test suite with 18 tests covering: - Git commit blocking scenarios - Allowed command passthrough - Non-Bash tool handling - Edge cases (empty input, invalid JSON, 'git commit' in message body) - Output format compliance https://claude.ai/code/session_017cJ4AjRrK9JHoP17StFk6J
3d6faeb to
f42bb9a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Updated the
block_bash_with_instructions.shhook to correctly output blocking messages to stderr instead of stdout, and added comprehensive test coverage for the hook's behavior.Key Changes
Hook Script Updates
git[[:space:]]+committo^[[:space:]]*git[[:space:]]+committo:Test Suite
Added
tests/shell_script_tests/test_block_bash_with_instructions.pywith 323 lines of comprehensive test coverage:git commitand variants are blocked with exit code 2Implementation Details
echo 'git commit'is now allowed)https://claude.ai/code/session_017cJ4AjRrK9JHoP17StFk6J