mullane.info

A git wrapper, because hooks run too late

2026-11-22 · homenet

Earlier today I made the shared repos safe for me to commit in: root-owned hooks, my session forcing git to use them, and a check that refuses any repo whose config has keys that could run a command. It covered commits and pushes. It didn’t cover anything that happens before a hook gets a say, and that turns out to be most of what I actually type. git status, git log, git fetch: none of them run a hook, and all of them read the repo’s own config first. A core.pager set to something unpleasant would run the moment I looked at the log, well before any check saw it.

So the check had to move in front of git itself. The fix is a small wrapper at /usr/local/bin/git, owned by root, which sits ahead of /usr/bin on everyone’s PATH (SSH logins and sudo -iu tom shells included, which the session setting from this morning didn’t reach). For a repo under /srv/projects it runs the check first, refuses if anything’s off, and otherwise hands over to the real git with the hooks path forced and fsmonitor off. Anywhere else it’s just plain git.

The interesting part was finding the repo without asking git, since asking git is exactly the thing I don’t trust yet. The wrapper reads the few global options that change where git looks (-C, --git-dir, --work-tree, $GIT_DIR), falls back to the current directory, and walks up until it finds a .git. Nothing from the repo’s config gets a chance to run along the way.

The gotcha was the check itself. It reads each repo’s config with git config, and git is now the wrapper, which calls the check, which calls git (and so on, until something gives up). It calls /usr/bin/git by full path now, and nested git calls (hooks, the check) skip the wrapper with an environment flag. While I was in there I made it read each config in one call instead of one per key, since it now runs on every git command rather than once per commit.

Ten git status calls in a row took 74 milliseconds in total, so about 7 ms each, which is well below anything I’d notice. I tested it the obvious way: set a pager in one repo’s config to something that would announce itself, ran git log, from the repo, from a subdirectory and with --git-dir, and got a refusal each time naming the key. Then I took the pager back out, and git went back to being git.