Detect manually merged MRs when a diff reload empties the diff
What does this MR do and why?
A merge request merged outside GitLab by rebasing the source branch, pushing it, then fast-forwarding the target branch can stay opened until its source branch is deleted, and then ends up closed instead of merged. Two independent UpdateMergeRequestsWorker jobs decide it, and nothing orders them:
- Target branch refresh first:
MergeRequests::RefreshService#post_merge_manually_mergedonly matches a merge request whose stored diff head is among the pushed commits. The stored head is still the pre-rebase SHA, so it doesn't match. When the source branch refresh runs later, the target already contains the head, so the newMergeRequestDiffisempty, andemptydiffs are never matched. - Source branch refresh first, but after the target branch moved: the new diff is
emptystraight away, so the target branch refresh skips it.
This MR detects the merge where both orderings meet: when a refresh reloads a merge request's diff and a non-empty diff becomes empty, its head is already on the target branch, so the merge request was merged. Gitlab::MergeRequests::EmptiedDiffMergeDetector makes that call after each diff reload in RefreshService (source branch, force push and stacked target branch reloads), and RefreshService marks the merge request merged with the existing post-merge code.
- Cost: nothing extra on normal pushes. The non-empty-to-empty check uses the diffs the refresh already loaded. Gitaly is called only when a diff actually empties: one
commits_byfor the old and new heads, and onecommits_betweento find the merge commit, capped at 1,000 commits (the merge request stays open past that). - False positives: an empty diff alone isn't proof, so:
- The previous diff must have been non-empty and stored. A merge request created from an issue, whose branch starts at the target, stays open (the case gitlab-foss MR gitlab-foss!12711 (merged) fixed).
- The new head must have the previous head's author email and authored date, as a rebase keeps them. A source branch reset onto the target branch stays open.
- Same-project merge requests only.
- Flag: behind
detect_manual_merge_by_emptied_diff(gitlab_com_derisk, off by default, rollout in #636913), because a wrong match is terminal: a merged merge request can't be reopened and can close the issues it references.
The stored diff head detection moves out of post_merge_manually_merged into Gitlab::MergeRequests::ManualMergeDetector (shape from !266542 (closed)). That part isn't flagged and behaves like master.
An earlier revision of this MR looked up the source branch tips with one Gitaly ListRefs call per push to a branch that open merge requests target. That's replaced, because it cost every default-branch push and missed ordering 2 (!264935 (comment 4190388082)).
Not covered: merging a rebased branch without pushing the source branch (no ref says it landed; the merge request stays open, as on master), the source branch being deleted before either refresh runs (#348961 (closed)), and a history rewrite that changes the head's author or authored date. A cherry-pick of only the head commit onto the target, with the source branch reset to it, is marked merged; that's tracked in #637876 and needs to land before the flag is enabled by default.
References
- Resolves #636014
- Related: #348961 (closed) (same race, closed as stale)
- Follow-up: #637876 (head-only cherry-pick is marked merged)
Screenshots or screen recordings
Not applicable — backend behavior change only, no UI.
How to set up and validate locally
- Enable the flag:
Feature.enable(:detect_manual_merge_by_emptied_diff). - Create a project, a branch
srcwith one commit, and a merge requestsrc→main. - Push another commit to
mainand wait for the refreshes. - Rebase
srcontomainlocally. Call the new headNEWand the old headOLD. - Move both branches before either refresh runs:
project.repository.raw_repository.write_ref('refs/heads/src', NEW) project.repository.raw_repository.write_ref('refs/heads/main', NEW) - Run the refreshes in either order, for example
UpdateMergeRequestsWorker.new.perform(project.id, user.id, OLD, NEW, 'refs/heads/src'), then the same formain. - The merge request is
mergedwithmerge_commit_sha = NEW. With the flag off it staysopenedwith anemptydiff.
spec/lib/gitlab/merge_requests/emptied_diff_merge_detector_spec.rb covers the detector: fast-forward and merge-commit merges, each half of the authorship check, a source branch reset onto the target, a previous diff that was empty or never stored, the commit limit, forks, and no Gitaly calls when the flag is off or the diff isn't emptied. The refresh_service_spec.rb context when a rebased source branch reaches the target branch before its refresh covers both orderings end to end, the stacked target branch reload, and the flag being off.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.