Featured image of post The forge said yes and did something else

The forge said yes and did something else

I asked GitLab to merge without squashing, and it answered 200 and squashed. Then I went and asked the other three forges what they'd do.

I asked GitLab’s API to merge a branch with squash=false, keep my commits as they are, in other words. It answered 200 OK and squashed the lot into one.

To be fair to GitLab, it was doing what the project’s settings told it to, and I suspect I’d have done much the same in its shoes. It just didn’t think to mention it… A 200 and a squash commit I hadn’t asked for is a bit like a courier texting “delivered” with a photo of somebody else’s doorstep.

Why I cared which commit merged

I don’t squash, ever. A branch’s commits are the history, and I’d much sooner keep the logical ones than have the forge flatten them into a single blob on the way in (it’s in the standing instructions my coding agents work from, in bold, near the top, so I’m fairly committed to it).

Those agents are the other half of this. Most of my code these days is written by agent sessions working on branches, and nothing lands until I’ve reviewed the merge request. But a review is a promise about the commits I read. A session can carry on pushing after I’ve looked, and if the merge then takes whatever’s newest, I’ve approved something I didn’t see.

So when I built hud, the dashboard I use to keep an eye on those sessions (it’s private, but that post explains what it does), I gave it one rule above the rest for merging: it merges the commit I approved. If a session pushes something after I’ve said yes, my yes doesn’t cover it.

hud talks to GitLab through go/forge, my abstraction over GitLab, GitHub, Gitea and Bitbucket, so one tool can do release and merge-request work on whichever forge a project lives on. And go/forge had no way to say “merge this, but only if the branch still ends at the commit I checked”. The latest commit on a branch is its head, and handing over the one you expect is what I’ll call a pin. go/forge could merge all day long, it just couldn’t pin.

GitLab’s API does take a sha on a merge for this very job, and so does GitHub’s. The obvious move was to add a field, pass it through and call it done. Something about that felt too easy, so before writing a line of it I went and asked the forges what they’d actually do.

Four forges, one question

The spike, a throwaway experiment to settle the question before designing anything, was short. I’d allowed ninety minutes and the first round took twenty-six (the squash questions got two more short rounds later that morning), in scratch repositories created for the purpose on gitlab.com, github.com, Bitbucket Cloud and a Gitea 28.2 container, all deleted afterwards. Each pull request (GitLab calls them merge requests, the other three pull requests) had its head moved on by one commit, and then each forge was asked to merge it pinned to the old one.

Three of them said no, which is what you want. GitLab answered 409 SHA does not match HEAD of source branch, GitHub 409 Head branch was modified, Gitea 409 head out of date, and nothing merged anywhere.

Bitbucket said yes. Its merge call has no obvious place for a pin, so I threw everything at it in one request: ten plausible names for one, seven in the body (sha, commit, source_commit.hash and friends) and three in the query string, all set to the stale commit. It answered 200 and merged the moved head anyway! Its published schema for a merge has no such field either, so I’m fairly sure that one’s settled, on paper and on the wire.

The abbreviations were a smaller surprise, and kinda nasty with it. Send GitLab or Gitea the first twelve characters of the current head and they say the head has moved… and they’ll keep saying it forever, because a short SHA doesn’t match a full one. GitHub at least refuses anything that isn’t exactly forty lowercase hex characters, with a 422 that tells you so.

Delivered, allegedly

Then the squash round. Each forge lets a project set a policy on squashing (anywhere from “not allowed” through to “always”), and a merge can ask for a squash or not on top of that. I wanted the pinned merge to be able to ask either way, so the question was what each forge does when the request and the project’s policy disagree.

GitHub and Gitea refuse. Ask GitHub for a squash on a repository that doesn’t allow them and you get 405 Squash merges are not allowed on this repository, and the pull request is still open afterwards. Gitea refuses too, with slightly mangled grammar.

GitLab merges, and does what the project says:

  • under squash_option: never, a merge sent with squash=true answered 200 and fast-forwarded, no squash;
  • under always, a merge sent with squash=false answered 200 and landed a squash commit.

And you can’t read the outcome off the merge request’s own squash field either. Set it to false on a project that’s set to always and it happily reads false, while squash_on_merge, sitting right beside it, says the merge will squash. That’s the field that tells the truth. Of the merges that didn’t ask for a squash, it read true in exactly the ones that squashed. So the “delivered” text was there in both directions, and only one field on the receipt says which doorstep the parcel’s really on.

Scheduled merges were the other gap, the button that says “merge this once the pipeline goes green”. GitLab cancels a pinned one the moment someone pushes. GitHub and Gitea check the pin once, when you schedule it, and then merge whatever head turns green. Which, for a pin, kinda misses the point.

Checking the doorstep

What came out of it is spec 0028, released in go/forge v0.33.0. go/forge itself is mostly interfaces, and each forge gets its own adapter module (forge-gitlab, forge-github and so on), released on its own schedule. That shaped the part I’m surest of: the pin is a capability of its own, something an adapter has to implement, and not a field on the merge options. The doc comment spells out why:

// It is a capability rather than a field on [MergeOptions] because an adapter
// built before the field existed would ignore it and merge unpinned, telling
// a caller its precondition held when nothing checked it. Bitbucket does not
// implement it: its merge takes no expected commit.

An adapter that hasn’t heard of a field won’t refuse it, it’ll just drop it on the floor and carry on. That’s the Bitbucket behaviour all over again, only this time it’d be my own code doing it. A missing capability, on the other hand, is something a caller can check for before it relies on it.

The GitLab adapter (from forge-gitlab v0.26.0) checks before it sends. Asked not to squash, it reads squash_on_merge and refuses if it’s set. Asked to squash, it reads the project and refuses if the option is never. Either way it refuses with an error and merges nothing, rather than letting GitLab decide.

That still leaves a gap, kinda. Someone could change the project’s settings between the check and the merge. So once GitLab answers, the adapter looks at what actually landed: a squash_commit_sha when none was asked for, or none when one was, comes back as ErrMergedDifferently. That error carries a warning in its doc comment:

// Do not retry it. The pull request has merged at the pinned head, so a second
// call answers nil and the signal is lost.

The merge has happened, the parcel’s on the step, and asking again would just get a cheerful “yep, delivered”. Only GitLab can return it, since GitHub and Gitea enforce their own policy at merge time and there’s no window to fall through.

The rest follows from the spike. A pin has to be a commit ID in full (40 characters, or 64 on the newer SHA-256 repositories), checked before any request goes out, so a bad value gets one refusal whichever forge it was meant for. It started out with no scheduled form, because two of the three forges would only check the pin once. GitLab got one later, in spec 0033. The GitHub and Gitea adapters got pinned merges in their own v0.27.0. Bitbucket gets a flat no in the docs’ table of what each forge can do, which felt harsh until I remembered the ten fields.

What a 200 is worth

Most of what I’ve learnt this year about believing a “done” came from my agents (they’ll tell you a test passed with all the cheer of a courier). Finding the forges at it too was a bit of a surprise, and with HTTP status codes, which I’d always taken more or less on trust.

hud already refused to touch a project set to squash always, as it happens, so my own repositories were safe enough. But go/forge is a library other people can pick up, and the next person might not have a rule in bold. Now if a forge says yes and does something else, they’ll hear about it, which is more than the courier ever managed for me.

Built with Hugo · Theme Stack designed by Jimmy