Skip to content

Commit

Permalink
Move handling of origin to method point_to_same_commit?
Browse files Browse the repository at this point in the history
  • Loading branch information
iangmaia committed Sep 3, 2024
1 parent c6e2a12 commit 6491e89
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ def self.create_backmerge_pr(token:, repository:, title:, head_branch:, base_bra
# if there's a callback, make sure it didn't switch branches
other_action.ensure_git_branch(branch: "^#{intermediate_branch}/") unless intermediate_branch_created_callback.nil?

base_branch_ref = base_branch.start_with?('origin/') ? base_branch : "origin/#{base_branch}"
head_branch_ref = head_branch.start_with?('origin/') ? head_branch : "origin/#{head_branch}"

if Fastlane::Helper::GitHelper.point_to_same_commit?(base_branch_ref, head_branch_ref)
if Fastlane::Helper::GitHelper.point_to_same_commit?(base_branch, head_branch)
UI.error("No differences between #{head_branch} and #{base_branch}. Skipping PR creation.")
return nil
end
Expand Down
13 changes: 8 additions & 5 deletions lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,23 @@ def self.fetch_all_tags
#
# @param ref1 [String] the first git reference to check.
# @param ref2 [String] the second git reference to check.
# @param remote_name [String] the name of the remote repository to use (default is 'origin').
# If nil or empty, no remote prefix will be used.
#
# @return [Boolean] true if the two references point to the same commit, false otherwise.
#
def self.point_to_same_commit?(ref1, ref2)
def self.point_to_same_commit?(ref1, ref2, remote_name: 'origin')
git_repo = Git.open(Dir.pwd)

ref1_full = remote_name.to_s.empty? ? ref1 : "#{remote_name}/#{ref1}"
ref2_full = remote_name.to_s.empty? ? ref2 : "#{remote_name}/#{ref2}"
begin
ref1_commit = git_repo.gcommit(ref1)
ref2_commit = git_repo.gcommit(ref2)
ref1_commit = git_repo.gcommit(ref1_full)
ref2_commit = git_repo.gcommit(ref2_full)
rescue StandardError => e
puts "Error: #{e.message}"
UI.error "Error fetching commits for #{ref1_full} and #{ref2_full}: #{e.message}"
return false
end

ref1_commit.sha == ref2_commit.sha
end

Expand Down
14 changes: 7 additions & 7 deletions spec/git_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,37 +145,37 @@
end

it 'checks if a tag and a branch point to the same commit' do
same_commit = described_class.point_to_same_commit?('1.0', 'another-branch')
same_commit = described_class.point_to_same_commit?('1.0', 'another-branch', remote_name: nil)
expect(same_commit).to be false
end

it 'checks if a tag and a branch that had a merge point to the same commit' do
same_commit = described_class.point_to_same_commit?('1.0', 'main')
same_commit = described_class.point_to_same_commit?('1.0', 'main', remote_name: nil)
expect(same_commit).to be false
end

it 'checks if a tag and a commit hash point to the same commit' do
same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D'))
same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D'), remote_name: nil)
expect(same_commit).to be false
end

it 'checks if a commit hash and a branch point to the same commit' do
same_commit = described_class.point_to_same_commit?(commit_hash(commit_message: 'commit B'), 'another-branch')
same_commit = described_class.point_to_same_commit?(commit_hash(commit_message: 'commit B'), 'another-branch', remote_name: nil)
expect(same_commit).to be false
end

it 'checks if commits between the same branch point to the same commit' do
same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch')
same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch', remote_name: nil)
expect(same_commit).to be true
end

it 'checks if commits between branches that have no difference point to the same commit' do
same_commit = described_class.point_to_same_commit?('another-branch', 'new-branch')
same_commit = described_class.point_to_same_commit?('another-branch', 'new-branch', remote_name: nil)
expect(same_commit).to be true
end

it 'raises error for a non-existent base_ref' do
expect { described_class.point_to_same_commit?('non-existent', 'main') }.to raise_error(StandardError)
expect { described_class.point_to_same_commit?('non-existent', 'main', remote_name: nil) }.to raise_error(StandardError)
end
end

Expand Down

0 comments on commit 6491e89

Please sign in to comment.