Skip to content

Commit 558d868

Browse files
committed
Fix implementation bug
1 parent bc8874d commit 558d868

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,29 @@ def self.fetch_all_tags
178178
#
179179
def self.find_merge_base(ref1, ref2)
180180
git_repo = Git.open(Dir.pwd)
181-
git_repo.merge_base(ref1, ref2)
181+
ref1_full, ref2_full = [ref1, ref2].map do |ref|
182+
# If the ref is the name of a branch and that branch exists remotely but not locally,
183+
# then prefix `origin/` to the ref name to fallback to use the remote reference
184+
git_repo.is_remote_branch?(ref) && !git_repo.is_local_branch?(ref) ? "origin/#{ref}" : ref
185+
end
186+
git_repo.merge_base(ref1_full, ref2_full)&.first&.sha
182187
end
183188

184189
# Checks if two git references point to the same commit.
185190
#
186191
# @param ref1 [String] the first git reference to check.
187192
# @param ref2 [String] the second git reference to check.
188-
# @param remote_name [String] the name of the remote repository to use (default is 'origin').
189-
# If nil or empty, no remote prefix will be used.
190193
#
191194
# @return [Boolean] true if the two references point to the same commit, false otherwise.
192195
#
193-
def self.point_to_same_commit?(ref1, ref2, remote_name: 'origin')
196+
def self.point_to_same_commit?(ref1, ref2)
194197
git_repo = Git.open(Dir.pwd)
195198

196-
ref1_full = remote_name.to_s.empty? ? ref1 : "#{remote_name}/#{ref1}"
197-
ref2_full = remote_name.to_s.empty? ? ref2 : "#{remote_name}/#{ref2}"
198199
begin
199-
ref1_commit = git_repo.gcommit(ref1_full)
200-
ref2_commit = git_repo.gcommit(ref2_full)
200+
ref1_commit = git_repo.gcommit(ref1)
201+
ref2_commit = git_repo.gcommit(ref2)
201202
rescue StandardError => e
202-
UI.error "Error fetching commits for #{ref1_full} and #{ref2_full}: #{e.message}"
203+
UI.error "Error fetching commits for #{ref1} and/or #{ref2}: #{e.message}"
203204
return false
204205
end
205206
ref1_commit.sha == ref2_commit.sha

spec/git_helper_spec.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@
111111
end
112112

113113
describe 'point_to_same_commit?(ref1, ref2)' do
114-
# We cannot test the happy path using a remote because the repo we use for the tests does not have a remote.
115-
let(:remote_name) { nil }
116-
117114
before do
118115
# Spec branching setup:
119116
#
@@ -148,37 +145,37 @@
148145
end
149146

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

155152
it 'checks if a tag and a branch that had a merge point to the same commit' do
156-
same_commit = described_class.point_to_same_commit?('1.0', 'main', remote_name: remote_name)
153+
same_commit = described_class.point_to_same_commit?('1.0', 'main')
157154
expect(same_commit).to be false
158155
end
159156

160157
it 'checks if a tag and a commit hash point to the same commit' do
161-
same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D'), remote_name: remote_name)
158+
same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D'))
162159
expect(same_commit).to be false
163160
end
164161

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

170167
it 'checks if commits between the same branch point to the same commit' do
171-
same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch', remote_name: remote_name)
168+
same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch')
172169
expect(same_commit).to be true
173170
end
174171

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

180177
it 'raises error for a non-existent base_ref' do
181-
expect { described_class.point_to_same_commit?('non-existent', 'main', remote_name: remote_name) }.to raise_error(StandardError)
178+
expect { described_class.point_to_same_commit?('non-existent', 'main') }.to raise_error(StandardError)
182179
end
183180
end
184181

0 commit comments

Comments
 (0)