Skip to content

feat: add 3 example hooks (destructive, secret, branch guard)#7

Open
yurukusa wants to merge 1 commit intoGowayLee:mainfrom
yurukusa:feat/add-examples
Open

feat: add 3 example hooks (destructive, secret, branch guard)#7
yurukusa wants to merge 1 commit intoGowayLee:mainfrom
yurukusa:feat/add-examples

Conversation

@yurukusa
Copy link
Copy Markdown

Summary

Adds an examples/ directory with 3 practical hooks demonstrating the cchooks SDK:

Example What it blocks Issue
destructive_guard.py Destructive commands (filesystem deletion, git reset) #36339
secret_guard.py Secret file commits (.env, credentials, keys) #16561
branch_guard.py Push to main/master, force-push

Each example uses the create_context() + output.block() pattern from the SDK.

Why

The README shows great API design but has no runnable examples users can copy. These fill that gap with real-world safety hooks born from actual GitHub Issues.

Test plan

  • All 3 files are valid Python syntax
  • Uses only cchooks public API
  • Each has docstring with install instructions and Issue link

- destructive_guard.py — blocks rm -rf, git reset --hard (#36339)
- secret_guard.py — blocks git add .env, credential files (#16561)
- branch_guard.py — blocks push to main, force-push
Each example demonstrates the create_context() + output.block() pattern.
All born from real GitHub Issues with links.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a44a02b06d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

cmd = ctx.tool_input.get("command", "")

if re.search(r'\bgit\s+push\b.*\b(main|master)\b', cmd, re.I):
ctx.output.block(reason="Direct push to main/master branch")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace unsupported PreToolUse output.block calls

PreToolUseOutput has no block() method, so ctx.output.block(...) raises AttributeError whenever a rule matches. That turns an intended deny into a hook crash (exit code 1/non-blocking), so commands like git push origin main are not actually denied. The same pattern appears across the three new examples and should use a supported PreToolUse API such as deny(...) or exit_block(...).

Useful? React with 👍 / 👎.

Install: Add to PreToolUse hooks matching "Bash" in settings.json.

Born from: https://github.com/anthropics/claude-code/issues/36339
(User lost entire C:\Users directory to rm -rf via NTFS junctions)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Escape backslashes in destructive guard docstring

The docstring includes C:\Users as C:\Users text with a single backslash in source (C:\Users intended), and Python interprets \U as a Unicode escape starter, causing SyntaxError: truncated \UXXXXXXXX escape during parsing. Because this fails at import time, the example script cannot run at all until that path text is escaped (e.g., C:\\Users) or made raw.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant