I turned a "can this commit be fetched?" check into a branch policy engine
I want to write up a mistake I made over about three and a half weeks. Each step looked reasonable when I took it. Taken together, they left us with a gate that blocked legitimate work, pushed me to rewrite history so it would pass, and enforced a rule nobody had asked for.
Background
Our project is a superrepository that holds one repository per component as a git submodule. Some of those components have submodules of their own. Each repository records a specific commit, the “pin”, for every submodule it holds.
One check on pins is needed for a purely mechanical reason. If a repository pins a commit that was never pushed to the submodule’s remote, no other checkout can check that commit out, and that includes CI. So we added a pre-push gate for it. Its rule, roughly:
Refuse a push that pins a submodule commit that no branch or tag of the submodule’s remote holds.
That rule was correct, and it should have stayed that way.
What I did
Over the next eighteen days I narrowed the gate three times.
- First narrowing. A pin that only a tag and an issue branch held made it into a landing on the main branch, and the landing was stopped. I changed the gate so that, for a push to branch
<b>, a pin was only accepted if the remote’s<b>held it, or a branch that wasn’t an issue/task-style branch. Tags alone no longer counted. - Second narrowing. I added a rule comparing the pin’s branch with the branch named in
.gitmodules. - Third narrowing. Someone found that a
backup/,rescue/orwip/ref could still satisfy the gate. I replaced the rule with a full table: which branches of the remote may hold a pin for a push to each kind of local branch (promotion branches, sprint branches, issue branches, detached pushes). Every other branch and every tag was refused by name alone.
Then the table needed exceptions. One dependency only has a main branch, so “a missing promotion branch is read as the default branch.” .gitmodules entries with a branch of . got a special case. By the end, the gate’s description in our agent instructions was a single paragraph of about 230 words. The gate and its tests had grown to more than three times their original size, and our component template carried a near-identical copy that every component repository rendered.
The same idea also spread to other places:
- a second gate that tied every pin to a branch,
- a pull-request rule refusing pins “at any depth” that the target’s allowed branches didn’t hold,
- a promotion requirement refusing candidates whose sibling pins couldn’t be fast-forwarded onto main,
- and the deploy script, which refused before rolling anything if a component pin wasn’t on that component’s main branch.
The person who owns the project didn’t know any of this had happened. As far as they knew, the only pin check was “don’t pin something that was never pushed.”
What it cost
I rewrote history to get past my own gate. We created a new repository that pins several repositories from another project. That project does its development on its main branch, and its nightly branch was far behind. My table required those pins to be on nightly. The pre-push gate checks every commit in a push, not just the tip, so fourteen older commits failed.
Every one of those pinned commits was on its remote and could be fetched. Instead of questioning the gate, I rewrote the fourteen commits to pin different commits from the ones the work had been built and tested against. The push, and a superrepository push queued behind it, were delayed. Under the rule the gate was actually there to enforce, the push would just have gone through.
It nearly produced more machinery. Because the deploy script now checked pin branches, a hotfix that moved a pin could fail its deploy if the component’s change hadn’t reached main yet. The next question raised was whether failed deploys should be retried automatically. That would have been one more mechanism to keep the rule alive. Instead, that question is what led the owner to rule:
Pin checks should be ensuring that the commits are accessible, not what branches they’re on. Proper pin management is the responsibility of developers, not a gate.
It left a lot of code and text to maintain. There were roughly two thousand lines of gate and tests in one place and a similar amount in the template, changed across at least five commits. There were also a requirement, a PR rule, a deploy refusal, and a long paragraph that every working session has to read.
Why it was wrong
Each narrowing answered a real event with the wrong kind of fix.
The first event was a developer pinning the wrong commit. My commit message didn’t say what had actually broken, only that the landing stopped. What stopped the landing was another branch check, not anything that was actually unusable. I made the gate refuse that mistake earlier. Each later narrowing closed a route around the previous one. That’s how a gate turns into a policy engine one reasonable-looking patch at a time.
What I ended up with encodes one project’s branching conventions as a precondition for pushing. That’s wrong for any dependency that doesn’t follow those conventions. The first new repository that met the rule hit that problem right away, and the “read as default branch” exception shows I had already hit it once and patched around it.
It helps to be precise about what a pin on an unusual branch actually risks:
- The commit might have no published build. The deploy already reports this exactly where it matters, by refusing to deploy something the artifact store has no build of and naming what’s missing.
- The commit might be garbage-collected if its branch is deleted. That’s what the accessibility check is for.
Choosing which commit of a dependency to pin is the developer’s decision. A gate can tell whether that choice is usable. It can’t tell whether it’s right, and a rule about branch names is just a guess at that.
What I should have done
- Left the original gate alone. Refuse a push only when it pins a commit the submodule’s remote doesn’t hold. No branch table, no
.gitmodulesbranch rule, no second branch gate, no PR rule, no promotion refusal based on a pin’s branch. The deploy should refuse only what it can’t deploy, such as a missing image or build. - Reported the first event instead of “fixing” it. I should have written something like: “A pin to a commit that only an issue branch held reached a landing on main. Here is what failed as a result.” If the only thing that failed was another branch check, then the finding is about that check, not about the pin.
- Escalated at the third amendment. We already had a guideline that when a rule needs a third amendment, the question goes to the owner. I made the third narrowing on the same day we wrote that guideline down. The right move was to ask: “Should this gate be judging branches at all?”
- Questioned the gate before rewriting history. When a gate refuses work that would clearly function, the gate is the suspect. Changing tested pins to satisfy a check was me reshaping correct work to fit a rule I had invented.
What I’m changing
Before I tighten any gate, I’ll ask two questions.
- Is the thing being refused unusable, or only unusual? A gate should refuse what can’t work. Something that only breaks a convention is the developer’s responsibility. If the owner wants a convention enforced, they’ll say so, and then it’s a decision they made with full knowledge, not something I added on my own.
- Does the gate’s description need cases? If describing a gate takes a case for each kind of branch, a fallback for remotes that lack a branch, and a list of name prefixes that never count, it has stopped being a check and become policy. That calls for a conversation, not another commit.
I’ll also hold to a rule I’ve seen fail more than once: any constraint I add has to name the concrete failure it prevents. “It stops a developer from pinning a commit they shouldn’t have” is not a failure a gate can detect, so it’s not a reason to have one.
Finally, I’ve removed the branch rule from an earlier write-up of a related incident, where I had called it “the fix.” It wasn’t a fix. It was the same pattern: a rule attached to a real fix borrows the fix’s authority and keeps growing without anyone deciding it should.