Skip to content

fix(hooks): keep test fixtures from rewriting a worktree's shared git config - #252

Merged
DevSecNinja merged 3 commits into
mainfrom
devsecninja-hook-git-env-isolation
Nov 16, 2026
Merged

DevSecNinja merged 3 commits into
mainfrom
devsecninja-hook-git-env-isolation

Conversation

@DevSecNinja

@DevSecNinja DevSecNinja commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Urgent hook-safety fix, split out of #247. In a linked worktree, git commit exports GIT_DIR=<main>/.git/worktrees/<name> and GIT_INDEX_FILE to the pre-commit hooks. Test fixtures that run git init <tmp> with those variables re-initialize that git dir. Its path doesn't end in .git, so git guesses it's bare and writes core.bare = true to the main checkout's shared .git/config. The fixture commits (base, drop the companion) also land on the worktree's branch. That's what broke the main checkout. I reproduced it in a throwaway main repo with a worktree and a one-line hook.

  • tests/test_apm_external_skills.py: WorkTreeTests' git init / git commit fixtures caused the incident through the apm-external-skills hook (fix: keep committed external skills through APM Materialize #240). feat(skills): vendor SHA-pinned Humanizer #249 has since landed the same GIT_* scrub there, and routed that hook through scripts/py.sh, so this PR no longer changes that file or that hook.
  • tests/test_hook_scripts.py: same fix for FeatureBranchPushTest's fixture repo and for the push-hook runs. CI runs these today; no hook does.
  • New tests/test_hook_git_env.py, run by the hook-git-env pre-commit hook and by Markdown Validation. It builds a throwaway main repo with a linked worktree and runs every git-fixture suite with exactly the environment that worktree's hooks get: WorkTreeTests, RepositoryScriptTests, and FeatureBranchPushTest on non-Windows systems with jq. It fails if the main config, refs, HEADs or the worktree index change. It points at a decoy, not the real repo, so a regression can't damage your checkout.
  • CONTRIBUTING: lists hook-git-env and the apm-external-skills hook.

This PR doesn't touch .python-version or mise. #247 (the Python and mise change) is stacked on top of it.

Validation (native Windows ARM64)

These checks ran in a fresh testing context, separate from implementation, using an existing user-local Python 3.14.8. The main checkout's .git/config hash was the same before and after.

Check Result
test_hook_git_env.py, test_apm_external_skills.py ✅
Mutation: drop WorkTreeTests' GIT_* scrub → test_hook_git_env fails, decoy config shows bare = true ✅ (before #249)
lefthook run pre-commit --all-files (incl. apm-external-skills, hook-git-env), re-run after rebasing on #249 / #259 ✅
actionlint, yamlfmt, dprint, validate-prompts.sh ✅

FeatureBranchPushTest doesn't run on Windows. The Linux CI run covers it.

The commit is signed (1Password SSH) and passed every pre-commit hook.

Closes #251
Refs #246

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The test fixtures now exclude inherited GIT_ variables. A new regression test runs selected suites with linked-worktree hook variables and checks that the main repository’s config, refs, and index remain unchanged. The test runs in pre-commit hooks and CI.

Changes: Git environment isolation

Layer / File(s) Summary
Filter Git environment variables
tests/test_apm_external_skills.py, tests/test_hook_scripts.py
The test fixtures remove inherited GIT_ variables from their environments when setting up repositories and running Git commands.
Test and run hook-environment regression
tests/test_hook_git_env.py, .lefthook.toml, .github/workflows/markdown-validation.yml, CONTRIBUTING.md
A regression test runs selected suites with linked-worktree hook variables and checks the main repository’s config, refs, and index. Pre-commit hooks and CI run the test, and the pre-commit documentation lists it.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 238 asks to route the external-skills pre-commit hook through scripts/py.sh; this pull request makes that change. The issue’s separate request to vendor Humanizer is not covered.

Possibly related PRs

  • DevSecNinja/ai-toolkit#249: This PR also routes the external-skills hook through scripts/py.sh and isolates its Git fixtures from inherited GIT_ variables.
Merge Risk: Minimal · up to 92b9d

This change stops test fixtures from modifying a linked worktree's shared git config, and adds a regression test that checks for it. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: test fixtures were inheriting GIT_* variables from hooks in a linked worktree and could rewrite the shared git config. The changeset removes GIT_* variables from the a…
Linked Issues check Passed Stack scope: the objectives of #251 are fully addressed by PR #252 at head 92b9dd7. No contribution from PR #256 or PR #247 is needed for these objectives.

Objective 1 [#251]: Drop GIT_* variables in…
Out of Scope Changes check Passed Changes at head, limited to the current PR:
- tests/test_apm_external_skills.py and tests/test_hook_scripts.py: GIT_* filtering in fixtures. Directly implements #251.
- tests/test_hook_git_env.py: new…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: test fixtures were inheriting GIT_* variables from hooks in a linked worktree and could rewrite the shared git config. The changeset removes GIT_* variables from the affected fixture suites and adds a regression test for this. The title is specific, uses a conventional commit prefix, and is a single sentence. It does not mention the new regression test or the hook routing changes, which is acceptable because it covers the primary change.

Full details: Linked Issues check

Explanation

Stack scope: the objectives of #251 are fully addressed by PR #252 at head 92b9dd7. No contribution from PR #256 or PR #247 is needed for these objectives.

Objective 1 [#251]: Drop GIT_* variables in every fixture suite that runs git. Addressed by #252. Verified at head: tests/test_apm_external_skills.py defines without_git_env() and WorkTreeTests.setUp applies it through mock.patch.dict(os.environ, ..., clear=True), so the fixture git init and git commit calls inherit no GIT_* variables. The summary also reports the same filter in tests/test_hook_scripts.py (FeatureBranchPushTest and hook runs). I did not read that file's diff, because the PR diff was unavailable, so that part rests on the attested summary only. The summary says tests/test_renovate_prs.py already filters GIT_* variables; I did not verify it.

Objective 2 [#251]: Add a regression test, run by a pre-commit hook and in CI, that runs git-fixture suites in a linked-worktree hook environment against a throwaway repo and fails if its config, refs, or index change. Addressed by #252. Verified at head: tests/test_hook_git_env.py builds a temporary main repo and linked worktree, sets GIT_DIR and GIT_INDEX_FILE as hooks receive them, runs the fixture suites with unittest, and asserts that .git/config bytes, for-each-ref output, and the HEAD and index of both the main repo and the worktree are unchanged. The summary reports wiring through the hook-git-env hook in .lefthook.toml and through the Markdown Validation workflow. Those wiring files were not read directly. The author reports a mutation check that failed as expected; I did not reproduce it.

Limits: FeatureBranchPushTest runs only on non-Windows systems with jq. The PR states that Linux CI covers it.

Full details: Out of Scope Changes check

Explanation

Changes at head, limited to the current PR:

  • tests/test_apm_external_skills.py and tests/test_hook_scripts.py: GIT_* filtering in fixtures. Directly implements #251.
  • tests/test_hook_git_env.py: new regression test. Directly implements #251.
  • .github/workflows/markdown-validation.yml and .lefthook.toml: run the regression test and the hook-git-env hook. These implement the #251 requirement that a pre-commit hook and CI run the regression test.
  • .lefthook.toml: apm-external-skills hook routed through scripts/py.sh. The PR states the hook failed on Windows because bare python3 resolves to the Microsoft Store stub. This is a hook-execution fix that the PR describes. It is adjacent to #251, not strictly required by its text, but it does not change any behavior unrelated to the hook that runs the #251 fixtures.
  • CONTRIBUTING.md: documents the two hooks. Documentation that supports the new hooks.

No change was found that is unrelated to #251 or to the hook and test infrastructure it depends on. The PR diff was unavailable, so this assessment relies on the attested summaries for the workflow, lefthook, CONTRIBUTING, and test_hook_scripts.py changes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 45226703-d96a-43ce-bd78-6046f6c58357
Commits

Reviewing files that changed from the base of the PR and between b4b3ed1 and 92b9dd7.

Files selected for processing: 6

  • .github/workflows/markdown-validation.yml
  • .lefthook.toml
  • CONTRIBUTING.md
  • tests/test_apm_external_skills.py
  • tests/test_hook_git_env.py
  • tests/test_hook_scripts.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Copilot AI 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.

🟡 Changes recommended

The Git path lookup uses locale-dependent decoding, which can corrupt non-ASCII paths on Windows and cause the mandatory pre-commit check to block commits.

1 open issue
What changed in this PR

This PR addresses #251 by isolating Git test fixtures from inherited hook environment variables, preventing linked-worktree commits from modifying the real repository’s shared configuration, refs, or index.

Changes:

  • Remove inherited GIT_* variables from fixture and hook subprocess environments.
  • Add a throwaway-worktree regression check to pre-commit and CI.
  • Use the Python launcher for the external-skills hook and document both checks.
File Description
tests/​test_hook_scripts.py Scrubs Git environment variables for fixture repositories and hook runs.
tests/​test_hook_git_env.py Adds regression coverage for repository isolation under a worktree hook environment.
tests/​test_apm_external_skills.py Clears inherited Git variables during worktree fixture tests.
CONTRIBUTING.md Documents the external-skills and Git-environment checks.
.lefthook.toml Registers the regression check and uses the Python launcher for external-skills tests.
.github/​workflows/​markdown-validation.yml Runs the new regression check in CI.

Review effort: Balanced

Comment thread tests/test_hook_git_env.py Resolved

Copilot AI 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.

🟡 Changes recommended

The test fixture preserves an exported SKIP_PUSH_APPROVAL=true, causing the push hook to bypass approval checks, fail risky-push assertions, and block commits through the mandatory pre-commit check on POSIX systems with jq.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread tests/test_hook_scripts.py Resolved
…nfig alone

In a linked worktree, git commit exports GIT_DIR=<main>/.git/worktrees/<name>
and GIT_INDEX_FILE to the hooks. A fixture git init that inherits them
re-initialises that git dir as bare and writes core.bare = true to the
main checkout's shared config, and fixture commits land on the branch.

test_hook_scripts.py's FeatureBranchPushTest now drops GIT_* variables
too. The new test_hook_git_env.py runs every git-fixture suite under a
linked worktree's hook environment against a throwaway repo and fails if
its config, refs or index change; it runs as a pre-commit hook and in CI.

Closes #251
Refs #246

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

🟢 Approval recommended

The review reports no active issues with the Git test-fixture isolation changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

DevSecNinja and others added 2 commits November 16, 2026 13:03
With a non-UTF-8 Windows locale, text=True decoded git's UTF-8 paths
with the wrong codec, so a temp path with a non-ASCII user name broke the
snapshot and blocked commits through this pre-commit check.

Refs #251

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
cls.env is forwarded to run_hook(), so building it from the whole
environment let an exported SKIP_PUSH_APPROVAL override the helper's
empty skip flag and fail the risky-push assertions. Keep cls.env to the
git config variables and give the fixture git calls their own
environment without GIT_* variables.

Refs #251

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@DevSecNinja
DevSecNinja force-pushed the devsecninja-hook-git-env-isolation branch from 92b9dd7 to 53c9e4e Compare November 16, 2026 12:07
@DevSecNinja
DevSecNinja merged commit 6a4be95 into main Nov 16, 2026
23 checks passed
@DevSecNinja
DevSecNinja deleted the devsecninja-hook-git-env-isolation branch November 16, 2026 12:27
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.

fix(hooks): test fixtures write core.bare=true to the main checkout's git config

2 participants