Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions spec/git_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently that option has been around since 2020. I think we'll be fine.

`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' }

Expand Down