Fix incorrect “branch hasn’t been merged” warning when deleting identical branches

What does this MR do and why?

Fixes: #419332 (closed)

GitLab incorrectly displayed a “branch hasn’t been merged” warning when trying to delete a branch that is identical to the default branch (same commit SHA).

Previously, even if two branches had the same commit history GitLab still showed the warning, making it confusing for users. This MR updates the logic to correctly detect and mark identical branches as merged ensuring that users can delete them without unnecessary warnings.

Screenshot

Before After
image.png image.png

How to set up and validate locally

  1. Visit a project (for example, http://gdk.test:3000/flightjs/Flight)
  2. Create a branch "test-branch" from "master" branch
  3. Try to delete the branch via UI
  4. The warning message doesn't mention that the branch hasn't been merged into master.
header header
Screenshot_2025-07-16_at_14.09.01 Screenshot_2025-07-16_at_14.08.04
Previous reproduction steps

To reproduce the issue, follow these steps:

1. Clone the repository and start GitLab locally

git clone http://127.0.0.1:3000/flightjs/Flight.git

cd Flight

cd ~/gdk

gdk start

2. Open the Rails console

cd ~/gdk/gitlab

bundle exec rails c

3. Fetch the project and repository and list available branches and check if a branch is considered merged

project = Project.find_by_full_path("flightjs/Flight")

repo = project.repository

repo.merged_branch_names(["test-branch1"], include_identical: true)

Expected (Correct) Behavior:

If test-branch1 is identical to the default branch it should be detected as merged and appear in the list.

4. Expire the cache and re-check

repo.expire_branches_cache

repo.merged_branch_names(["test-branch1"], include_identical: true)


5. After applying the fix repeat the steps above and check if the issue is resolved:

repo.merged_branch_names(["test-branch1"], include_identical: true)

One of the problems we faced was that the branch merge status was cached causing inconsistent results.

Screenshot & Screen recording

Fixed the delete branch message in GitLab so it warns only when a branch is truly not merged. Before it shows the warning even for branches that were identical to the default branch (master/main). Below updated the logic to correctly detect merged branches. To test I created some branches (test-merged-1 & test-merged-2) that were updated to master and some that weren’t (test-unmerged-1). Now GitLab only warns for unmerged branches and allows deleting identical ones without confusion. 

Screen Recording 2025-03-04 at 17.11.22.mov

image.png

image.png

image.png

image.png

Edited by Vasilii Iakliushin

Merge request reports

Loading