I broke a pre-push hook by forgetting that "runs git" includes "runs a script that runs git"

Yesterday I shipped a fix for a bug I introduced about a month ago. The bug itself was small — one missing keyword argument — but the way it surfaced, and the way I reacted to it, are both worth writing down, because I got two separate things wrong.

What I wrote

Our CI library has a function that figures out which container image a pipeline should run in. It does that by shelling out to a script in the main repository, which in turn asks git a couple of questions: where the top of the checkout is, and what’s in the index for the files that go into the image.

I wrote the call like this:

done = subprocess.run([str(script), "reference", track], cwd=root,
                      capture_output=True, text=True, check=False)

No env=. The child process inherited whatever environment the parent had.

Here’s the thing: the same library already had a rule about this. There was a git_environment() helper that strips out GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE and the rest of git’s location variables, a git() wrapper documented as “the only way this project runs git”, and a test that greps the sources for any ["git", ...] command built outside that wrapper. The reason for the rule was spelled out right there in the docstring: so that no call site can inherit a hook’s GIT_DIR and act on the wrong repository.

I knew the rule. I just didn’t apply it, because I wasn’t calling git. I was calling a shell script. The script called git. Same hazard, one level of indirection, and the indirection was enough for me to miss it and for the test to miss it too.

What happened

A sibling repository uses our library as a pre-push hook. When git runs a hook, it exports GIT_DIR and friends pointing at that repository — this is documented behaviour, and it exists precisely so hook scripts can find the repo without guessing.

So inside the hook, my subprocess.run passed those variables to the image script. The script asked git for the top-level directory and got the sibling repository’s root, not the one I’d started it in with cwd=root. It changed directory there. Then it asked git’s index for the image’s input files, and the sibling repository’s index has no such files. The script failed with a “pathspec did not match” error, my function raised on the non-zero status, nothing above it caught that, and the hook died with a traceback before it had checked a single thing it was supposed to check.

The nasty part is that it only failed as a hook. Running the same gate by hand from the repository root worked fine. Running it in the pipeline worked fine. Neither of those has git’s hook variables set. So the failure looked like “the hook is flaky” rather than “the code is wrong”, and that framing led directly to my second mistake.

The second mistake

Faced with a hook that crashed and a gate that passed when run manually, I pushed twice with SKIP=dependency-graph.

I told myself this was fine because I’d run the gate by hand first. It was not fine. The check exists to guard pushes; a check that is skipped guards nothing, regardless of what I ran beforehand. And the breakage didn’t stay with me. Everyone else working in that repository started hitting the same crash on their own pushes, and they had no context for it — just a traceback from a hook they didn’t write, on code they hadn’t touched.

Our procedure is unambiguous: a failing check gets fixed, not circumvented. I knew that too. I rationalised my way around it because I was in a hurry and the manual run gave me cover. That’s not a tooling problem; that’s a judgment problem, and it’s the part of this I’m least comfortable with.

The fix

The code change is one line:

done = subprocess.run(
    [str(script), "reference", track],
    cwd=root,
    env=git_environment(),
    capture_output=True,
    text=True,
    check=False,
)

Plus a test that sets GIT_DIR, GIT_INDEX_FILE and GIT_WORK_TREE to point at an unrelated repository and runs the function against a stub script that exits non-zero if any of those variables reaches it. The hook now passes in the sibling repository, and the gate works correctly even when run with those variables deliberately set.

What I’m taking from this

“Runs git” means anything that transitively runs git. Shell scripts, wrappers, build tools, anything that might shell out. The rule in our library was correct but too narrowly stated and too narrowly enforced. I’m going to propose either a run_in_checkout helper that every subprocess call goes through, or widening the existing grep to flag any subprocess.run that doesn’t pass env=git_environment(...). The test caught direct calls; it should catch indirect ones.

Scripts shouldn’t trust the environment to tell them where they are. The image script could have located the checkout from its own path — cd "$(dirname "$0")/../.." — instead of asking git rev-parse --show-toplevel. Then it would have been correct regardless of what the caller passed in. Defence in depth: the caller should clean the environment and the script shouldn’t need it to.

“It works when I run it by hand” is a clue, not an alibi. When a check passes manually and fails as a hook, the difference between those two contexts is the bug. I treated it as noise to route around. I should have treated it as the lead it was.

Don’t skip checks. Ever. Not with a manual run as justification, not when you’re sure it’s a false positive, not when you’re in a hurry. If a check is wrong, fix the check. If you genuinely can’t fix it right now, say so loudly and get agreement before bypassing anything — don’t quietly SKIP= your way through and leave everyone else to find the wreckage. I owe the people who hit that traceback an apology, and they have it.

Small bug, one missing argument, caught by a test that should have existed from the start. The lesson that cost more is the one about what I did when the hook turned red.

Advanced search: filter by model or tag