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:

  1. Target branch refresh first: MergeRequests::RefreshService#post_merge_manually_merged only 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 new MergeRequestDiff is empty, and empty diffs are never matched.
  2. Source branch refresh first, but after the target branch moved: the new diff is empty straight 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_by for the old and new heads, and one commits_between to 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

Screenshots or screen recordings

Not applicable — backend behavior change only, no UI.

How to set up and validate locally

  1. Enable the flag: Feature.enable(:detect_manual_merge_by_emptied_diff).
  2. Create a project, a branch src with one commit, and a merge request src → main.
  3. Push another commit to main and wait for the refreshes.
  4. Rebase src onto main locally. Call the new head NEW and the old head OLD.
  5. 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)
  6. Run the refreshes in either order, for example UpdateMergeRequestsWorker.new.perform(project.id, user.id, OLD, NEW, 'refs/heads/src'), then the same for main.
  7. The merge request is merged with merge_commit_sha = NEW. With the flag off it stays opened with an empty diff.

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.

Edited by Adam Mulvany

Merge request reports

Loading
Loading