From 54520a36d0efb5b6a59e07c7f25916cb59072fc3 Mon Sep 17 00:00:00 2001 From: Ian Maia Date: Wed, 12 Aug 2026 18:35:10 +0200 Subject: [PATCH] Fix remote branch deletion detection --- CHANGELOG.md | 2 +- .../wpmreleasetoolkit/helper/git_helper.rb | 8 +++--- spec/git_helper_spec.rb | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 126d82e45..f8f3205d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ _None_ ### Bug Fixes -_None_ +- `GitHelper.delete_remote_branch_if_exists!` now detects branches directly on the remote instead of requiring a local remote-tracking ref. [#769] ### Internal Changes diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb index f96a649a8..ca2d799ac 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb @@ -267,13 +267,13 @@ def self.delete_local_branch_if_exists!(branch_name) # # @param [String] branch_name The name of the remote branch to delete. # @param [String] remote_name The name of the remote to delete the branch from. Defaults to 'origin' - # @return [Boolean] true if the branch was deleted, false if not (e.g. no such local branch existed in the first place) + # @return [Boolean] true if the branch was deleted, false if it did not exist on the remote # def self.delete_remote_branch_if_exists!(branch_name, remote_name: 'origin') - git_repo = Git.open(Dir.pwd) - return false unless git_repo.branches.any? { |b| b.remote&.name == remote_name && b.name == branch_name } + return false unless branch_exists_on_remote?(branch_name: branch_name, remote_name: remote_name) - git_repo.push(remote_name, branch_name, delete: true) + Git.open(Dir.pwd).push(remote_name, branch_name, delete: true) + true end # Checks whether a given path is ignored by Git, relying on Git's `check-ignore` under the hood. diff --git a/spec/git_helper_spec.rb b/spec/git_helper_spec.rb index b1bdcb430..2f8ebb238 100644 --- a/spec/git_helper_spec.rb +++ b/spec/git_helper_spec.rb @@ -186,6 +186,33 @@ end end + describe 'delete_remote_branch_if_exists!' do + let(:branch_name) { 'automation/update' } + let(:remote_name) { 'upstream' } + + before do + init_git_repo + add_file_and_commit(file: 'file.txt', message: 'Initial commit') + + remote_path = File.join(@path, 'remote.git') + `git init --bare --initial-branch main #{remote_path}` + `git remote add #{remote_name} #{remote_path}` + end + + it 'deletes a branch that exists on the remote without requiring a local tracking ref' do + `git push #{remote_name} HEAD:refs/heads/#{branch_name}` + `git update-ref -d refs/remotes/#{remote_name}/#{branch_name}` + + expect(`git branch --remotes --list #{remote_name}/#{branch_name}`).to be_empty + expect(described_class.delete_remote_branch_if_exists!(branch_name, remote_name: remote_name)).to be true + expect(described_class.branch_exists_on_remote?(branch_name: branch_name, remote_name: remote_name)).to be false + end + + it 'does nothing when the branch does not exist on the remote' do + expect(described_class.delete_remote_branch_if_exists!(branch_name, remote_name: remote_name)).to be false + end + end + describe '#is_ignored?' do let(:path) { 'dummy.txt' }