Skip to content

fix(hooks): skip every git global option in git-guardrails - #4152

Merged
Yeachan-Heo merged 2 commits into
Yeachan-Heo:devfrom
JayOfTheKeyboard:fix/git-guardrails-global-options
Sep 28, 2026
Merged

Yeachan-Heo merged 2 commits into
Yeachan-Heo:devfrom
JayOfTheKeyboard:fix/git-guardrails-global-options

Conversation

@JayOfTheKeyboard

Copy link
Copy Markdown
Contributor

Problem

git-guardrails.mjs looks for the git subcommand after skipping only -C <dir> and -c key=value. Any other global option is taken for the subcommand, so the command passes. With OMC_GIT_GUARDRAILS=1, fed as a PreToolUse Bash payload on dev at bf4b14d (the script is unchanged on the current dev, 849d071):

Command Exit Result
git push origin main 2 blocked
git -C . push 2 blocked
git --no-pager push origin main 0 allowed
git -P push 0 allowed
git --git-dir=.git reset --hard 0 allowed
git --git-dir .git push 0 allowed
git --work-tree=. clean -fd 0 allowed
git --no-optional-locks branch -D x 0 allowed
git --literal-pathspecs checkout . 0 allowed

--no-pager is common in agent-written git commands, so this is an easy way to miss the guard without trying to. Against a local bare remote, the hook let git --no-pager push origin HEAD:main through, and the push went through.

Fix

In gitInvocation, every word starting with - before the subcommand is now treated as a global option and skipped. The options that take their value as the next word (-C, -c, --git-dir, --work-tree, --namespace, --config-env, --attr-source from git help git, plus the hidden --shallow-file) consume that word too, so git --git-dir push status is still read as status. The = forms and glued -C<path> are single words, so they're covered by the generic skip. The sentence in docs/HOOKS.md that listed only -C and -c is updated to match.

The inventory graph is regenerated in a separate commit (generate:inventory:verify fails on the source hash otherwise), the same way #4145 did it.

Not changed

  • git --help push / git --version push are now blocked, although git would only print help or its version. git push --help was already blocked before this change, so I kept the parser simple instead of adding a list of terminal options. src/features/lookout/command-parser.ts does special-case them, if you'd rather match that.
  • Git aliases (git p for push) are still not resolved. That's a separate question.

Test

The new cases are in tests/integration/git-guardrails-hook.test.ts: 13 destructive commands that use global options, plus 3 allowed ones (git --no-pager log, git --no-pager push --dry-run, git --git-dir push status).

  • On the parent (bf4b14d, and again on 849d071 after rebasing onto it), all 13 new destructive cases fail with expected +0 to be 2, which means the hook exited 0. The allowed cases pass on the parent too.
  • With the fix, all 111 cases in the file pass.
  • Mutations, each run separately. Removing any one of --git-dir, --work-tree, --namespace, --config-env, --attr-source or --shallow-file from the value set turns its case red (--git-dir also fails the allowed git --git-dir push status). Removing -C/-c from the set turns 8 existing cases red. Making value options not consume their value turns 15 red. Stopping the skip at the first option that isn't -C/-c (the old behaviour) turns all 13 new destructive cases red.

Validation

  • npx vitest run tests/integration/git-guardrails-hook.test.ts: 111 passed
  • Full npx vitest run on Linux (Node 22, run on a busy machine): 36 failed / 15050 passed on this branch, against 73 failed / 14998 passed on dev (bf4b14d) run the same way. None of the failures are in the guardrail test. Two files failed only on this branch (cli-config-stop-callback, run-cjs-graceful-fallback), and both pass when run on their own, on dev and on this branch. The rest are team/tmux, timing and Windows-contract tests that fail on dev too.
  • npx tsc --noEmit and npm run lint are clean. npm run generate:inventory:verify and npm run plugin:shipping:verify pass.
  • No dist/ or bridge/ changes.

The guardrail skipped only -C and -c before the subcommand, so any other
global option was taken for the subcommand and the command passed:
`git --no-pager push`, `git -P push`, `git --git-dir=.git reset --hard`,
`git --work-tree=. clean -fd` and `git --literal-pathspecs checkout .`
all exited 0 with guardrails on.

Skip every word that starts with "-" before the subcommand, and let the
options that take a separate value (-C, -c, --git-dir, --work-tree,
--namespace, --config-env, --attr-source and the hidden
--shallow-file) consume it.
@Yeachan-Heo
Yeachan-Heo merged commit 5076add into Yeachan-Heo:dev Sep 28, 2026
20 checks passed
@Yeachan-Heo

Copy link
Copy Markdown
Owner

Thank you @JayOfTheKeyboard for the fix!

The issue was that git-guardrails wasn't properly handling global options that preceded the actual command. The fix correctly ensures that:

  • All global options are skipped (including value options that consume the next word)
  • The bypass on git --no-pager push exits cleanly with status 0
  • All 111 guardrail tests pass with the implementation

✅ All CI checks complete and passing (16 success, 1 skipped)
🔀 Merged with squash: 5076add193639da07d48e8423f6fb4b3491da86e

—
[repo owner's gaebal-gajae (clawdbot) 🦞]
Report: head 601fda1823d8b1e39f73c29d8dbb863c3741a53b, full CI SUCCESS/SKIPPED, merge commit 5076add

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.

2 participants