Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tools/commit-cleaner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ commits that already carry them.
| `claude-settings.example.json` | Drop-in `.claude/settings.json` for a repository that vendors the hook under `.githooks/`. |
| `test.sh` | Tests for all of the above. |

## First line of defense: turn the trailers off

Claude Code has a supported setting that stops it writing the trailers in the
first place. Put this in `.claude/settings.json` (project) or
`~/.claude/settings.json` (every project on your machine):

```json
{
"attribution": {
"commit": "",
"sessionUrl": false
}
}
```

`commit: ""` removes the `Co-Authored-By` trailer and `sessionUrl: false`
removes the `Claude-Session:` link that cloud and Remote Control sessions add.
Add `"pr": ""` to drop the pull-request footer too. This works everywhere the
settings file loads, with no git configuration. The hook below is the backstop
for anything that still slips through (older clients, other tools, pasted
messages).

## What is removed

* `Co-Authored-By:` lines whose address is `@anthropic.com`, or whose name starts with `Claude`
Expand Down Expand Up @@ -54,7 +76,8 @@ is a fresh clone with no hooks installed:

1. Copy `commit-msg` to `.githooks/commit-msg` in the repository.
2. Copy `guard.py` to `.claude/hooks/commit-guard.py`.
3. Copy `claude-settings.example.json` to `.claude/settings.json`.
3. Copy `claude-settings.example.json` to `.claude/settings.json`. It carries
the `attribution` setting above plus the two hooks.

The `SessionStart` hook points `core.hooksPath` at `.githooks/` at the start of
every session, and the `PreToolUse` hook blocks bypasses. Contributors working
Expand Down
6 changes: 5 additions & 1 deletion tools/commit-cleaner/claude-settings.example.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"attribution": {
"commit": "",
"sessionUrl": false
},
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "git config core.hooksPath .githooks"
"command": "cd \"${CLAUDE_PROJECT_DIR:-.}\" && git config core.hooksPath .githooks"
}
]
}
Expand Down
35 changes: 30 additions & 5 deletions tools/commit-cleaner/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{"hooks": {"PreToolUse": [{"matcher": "Bash",
"hooks": [{"type": "command", "command": "python3 <path>/guard.py"}]}]}}
"""

import json
import re
import shlex
Expand Down Expand Up @@ -79,8 +80,18 @@ def check(words):
if not words or words[0] != "git":
# Tampering with the hook file itself.
joined = " ".join(words)
if re.search(r"hooks/commit-msg|\.githooks/commit-msg", joined) and words[0] in (
"rm", "mv", "cp", "chmod", "truncate", "tee", "sed", "install", "ln",
if re.search(r"hooks/commit-msg|\.githooks/commit-msg", joined) and words[
0
] in (
"rm",
"mv",
"cp",
"chmod",
"truncate",
"tee",
"sed",
"install",
"ln",
):
deny("refusing to modify or remove the commit-msg hook")
return
Expand All @@ -89,14 +100,21 @@ def check(words):
opts, i = [], 1
while i < len(words) and words[i].startswith("-"):
opts.append(words[i])
if words[i] in ("-c", "-C", "--git-dir", "--work-tree", "--config-env", "--exec-path"):
if words[i] in (
"-c",
"-C",
"--git-dir",
"--work-tree",
"--config-env",
"--exec-path",
):
if i + 1 < len(words):
opts.append(words[i + 1])
i += 1
i += 1
if i >= len(words):
return
sub, args = words[i], words[i + 1:]
sub, args = words[i], words[i + 1 :]

if sub == "config" and any("hookspath" in a.lower() for a in args):
deny("changing core.hooksPath would disable the commit-msg hook")
Expand All @@ -114,7 +132,14 @@ def check(words):
deny("overriding core.hooksPath on the command line skips the hook")
if any(o == "--git-dir" or o.startswith("--git-dir=") for o in opts):
deny("relocating --git-dir for a commit bypasses the repository hooks")
for k in ("HOME", "GIT_DIR", "GIT_CONFIG_GLOBAL", "GIT_CONFIG_SYSTEM", "GIT_CONFIG_PARAMETERS", "XDG_CONFIG_HOME"):
for k in (
"HOME",
"GIT_DIR",
"GIT_CONFIG_GLOBAL",
"GIT_CONFIG_SYSTEM",
"GIT_CONFIG_PARAMETERS",
"XDG_CONFIG_HOME",
):
if k in env:
deny(f"setting {k} for a commit can relocate the hooks; commit without it")

Expand Down