Skip to content

Check the repo gh pr create runs in, in the pre-PR hook - #1531

Closed
sergiopaniego wants to merge 5 commits into
openenv:mainfrom
sergiopaniego:fix-pre-pr-check-hook
Closed

sergiopaniego wants to merge 5 commits into
openenv:mainfrom
sergiopaniego:fix-pre-pr-check-hook

Conversation

@sergiopaniego

@sergiopaniego sergiopaniego commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

.claude/hooks/pre-pr-check.sh always checks the repository the Claude Code session runs in, so it blocks gh pr create for other repositories with the freshness of the OpenEnv checkout. Opening a TRL PR from a TRL worktree (cd ~/trl/... && gh pr create -R huggingface/trl ...) was blocked as "755 commit(s) behind main", which was the state of the OpenEnv branch, not of the TRL one. This PR makes the hook check the repo the command actually runs in, and fixes two smaller issues:

  • Repo: uses the target of a leading cd, else the session cwd from the hook input, and runs every git command with git -C.
  • --base parsing: used grep -P, which macOS grep does not support (grep: invalid option -- P on every gh pr create), so --base was never read. Now sed -E.
  • --head: the freshness and "not from main" checks used the checked-out branch even when the PR is opened for another one with --head. They now use the --head branch when given.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • New environment
  • Refactoring

Alignment Checklist

Before submitting, verify:

  • I have read .claude/docs/PRINCIPLES.md and this PR aligns with our principles
  • I have checked .claude/docs/INVARIANTS.md and no invariants are violated
  • I have run /pre-submit-pr (or bash .claude/hooks/lint.sh and tests) and addressed all issues

RFC Status

  • Not required (bug fix, docs, minor refactoring)
  • RFC exists: #___
  • RFC needed (will create before merge)

Test Plan

Fed the hook the same JSON Claude Code sends, on macOS:

  • cd <trl worktree> && gh pr create -R huggingface/trl --base main --head async-kernels-install-docs ... with cwd = the OpenEnv checkout: the hook now checks the TRL worktree and reports the TRL branch's real state (checked in .../trl/.claude/worktrees/kernels-docs), instead of the OpenEnv branch.
  • gh pr create --base main ... from an OpenEnv branch 755 commits behind main: still blocked, as before.
  • No grep: invalid option -- P anymore.

Claude Code Review

N/A


Note

Low Risk
Changes only affect a local Claude Code bash hook for PR creation; no runtime, auth, or data-path impact.

Overview
Fixes .claude/hooks/pre-pr-check.sh so stale-branch and “no PR from main” rules apply to the repo and branches gh pr create actually uses, not always the Claude session checkout.

The hook now resolves REPO_DIR from the last cd before gh pr create (relative paths against session cwd) and runs git -C "$REPO_DIR" for fetch and rev-list. Freshness compares $HEAD_BRANCH..origin/$BASE instead of HEAD, and block messages name the head branch and directory checked.

Command parsing is tightened: backslash continuations are merged, quoted --title/--body and heredocs are stripped before reading flags, and only lines that execute gh pr create (not strings that merely mention it) are gated. --base/--head are read with grep -oE / sed -E instead of grep -P so --base works on macOS; --head (including owner:branch) drives head-branch checks when set. If parsed base/head look like options (**-*), the hook skips the check rather than passing bad args to git.

Reviewed by Cursor Bugbot for commit 58a0d0e. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread .claude/hooks/pre-pr-check.sh Resolved
Comment thread .claude/hooks/pre-pr-check.sh Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread .claude/hooks/pre-pr-check.sh Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a137690. Configure here.

# Only a command that runs gh pr create, not one that mentions it (a commit message, a grep)
RUNS_PR_CREATE='(^|[;&|(])[[:space:]]*gh[[:space:]]+pr[[:space:]]+create'
if ! [[ "$FLAGS" =~ $RUNS_PR_CREATE ]]; then
exit 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Heredoc erases later create command

Medium Severity

Flattening the full command and then cutting FLAGS at << also deletes a later gh pr create. RUNS_PR_CREATE then fails and the hook exits 0, so a heredoc-then-create sequence never gets a freshness check.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a137690. Configure here.

# Only a command that runs gh pr create, not one that mentions it (a commit message, a grep)
RUNS_PR_CREATE='(^|[;&|(])[[:space:]]*gh[[:space:]]+pr[[:space:]]+create'
if ! [[ "$FLAGS" =~ $RUNS_PR_CREATE ]]; then
exit 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Create-command regex misses valid invocations

Medium Severity

RUNS_PR_CREATE only accepts gh at the start of FLAGS or after [;&|(]. An env assignment, a full path to gh, or a newline-separated command (flattened to spaces) leaves gh after other tokens, so the hook skips the check.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a137690. Configure here.

@sergiopaniego

Copy link
Copy Markdown
Collaborator Author

Closing this: handling every way a command can be written turned the hook into a shell parser, which is more complexity than a freshness warning is worth.

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