feat(git): refuse git push to main with a Claude Code hook #282

Merged
Defame1297 merged 4 commits from feat/178-refuse-push-to-main into main 2026-12-05 20:33:16 +00:00
Owner

Closes #178

The git plugin now ships a PreToolUse Bash hook that refuses a git push whose destination is main. Pushes to other branches and tags go through.

Behaviour

  • Refused: explicit destinations (main, HEAD:main, feat:main, refs/heads/main), deletes, bulk pushes (--all, --mirror, :, wildcard refspecs), and a bare push, HEAD or @ while on main. Force variants, rtk git push and git -C <dir> push are covered too.
  • Claude Code only: apm also deploys the hook to Copilot and Codex, where the script exits 0 without checking anything.
  • If the hook fails, the push goes through. This is a guardrail; server-side branch protection is the real control.
  • Opt-out: set GIT_ALLOW_PUSH_TO_MAIN=1 under env in the Claude Code settings. Writing it inline in the command has no effect.

delivery depends on git, so its consumers get the hook too. ADR-0032 has a short amendment recording that git ships its own guardrail hook.

Known gaps

The plugin README lists the limits. The last review found these, which it does not list yet:

  • A directory change after git checkout main can let a bare push to main through, for example git checkout main && git pull && cd sub && git push.
  • Less common ways of leaving main in the same command, such as a git alias or git stash branch, can cause a wrong refusal. Pushing the branch by name avoids it.
  • Nested double quotes inside "$(...)" can be misread and cause a wrong refusal.
  • nice git push origin main is let through.

Verification

  • tests/test-git-push-main-hook.sh: 180 passed
  • tests/run-tests.sh --strict: 26 suites passed
  • All pre-commit and pre-push hooks passed. This repo has no forge CI.

Not tested: the hook in a live Claude Code session, which needs the merge first, and behaviour in Copilot or Codex sessions.

After merge

  • Commit the refreshed apm.lock.yaml and deployed hook files after the next session start on main.
  • Cut the git-v1.4.0 release tag, with the maintainer's approval.

🤖 Generated with Claude Code

https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi

Closes #178 The `git` plugin now ships a `PreToolUse` Bash hook that refuses a `git push` whose destination is `main`. Pushes to other branches and tags go through. ## Behaviour - **Refused:** explicit destinations (`main`, `HEAD:main`, `feat:main`, `refs/heads/main`), deletes, bulk pushes (`--all`, `--mirror`, `:`, wildcard refspecs), and a bare push, `HEAD` or `@` while on `main`. Force variants, `rtk git push` and `git -C <dir> push` are covered too. - **Claude Code only:** apm also deploys the hook to Copilot and Codex, where the script exits 0 without checking anything. - **If the hook fails,** the push goes through. This is a guardrail; server-side branch protection is the real control. - **Opt-out:** set `GIT_ALLOW_PUSH_TO_MAIN=1` under `env` in the Claude Code settings. Writing it inline in the command has no effect. `delivery` depends on `git`, so its consumers get the hook too. ADR-0032 has a short amendment recording that `git` ships its own guardrail hook. ## Known gaps The plugin README lists the limits. The last review found these, which it does not list yet: - A directory change after `git checkout main` can let a bare push to `main` through, for example `git checkout main && git pull && cd sub && git push`. - Less common ways of leaving `main` in the same command, such as a git alias or `git stash branch`, can cause a wrong refusal. Pushing the branch by name avoids it. - Nested double quotes inside `"$(...)"` can be misread and cause a wrong refusal. - `nice git push origin main` is let through. ## Verification - `tests/test-git-push-main-hook.sh`: 180 passed - `tests/run-tests.sh --strict`: 26 suites passed - All pre-commit and pre-push hooks passed. This repo has no forge CI. Not tested: the hook in a live Claude Code session, which needs the merge first, and behaviour in Copilot or Codex sessions. ## After merge - Commit the refreshed `apm.lock.yaml` and deployed hook files after the next session start on `main`. - Cut the `git-v1.4.0` release tag, with the maintainer's approval. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Claude added 4 commits 2026-12-04 23:39:11 +00:00
An agent on main can push straight to it when the server does not protect the branch. The git
plugin now ships a Claude Code hook that refuses such a push before it runs. It fails open, does
nothing on other runtimes, and has an opt-out read only from the hook's own environment.

Closes: #178

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Refs: #178

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Review found pushes refused after `gh pr checkout` or `git branch -m`, and pushes to main allowed
after `git switch main` or `cd <repo>`. The hook now follows literal branch and directory changes
and treats any other as unknown. Heredoc stripping is linear and handles several per line.

Refs: #178

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
`git rebase <upstream> <branch>` checks out <branch>, so a later bare push was refused as if main
were still checked out. Pushes through xargs get refspecs the hook cannot see, so only an explicit
main is refused there.

Refs: #178

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Defame1297 merged commit 78cfbe2f32 into main 2026-12-05 20:33:16 +00:00
Defame1297 deleted branch feat/178-refuse-push-to-main 2026-12-05 20:33:16 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Holocron#282