Skip to content

Commit 5eb73fb

Browse files
committed
More generic implementation
1 parent 558d868 commit 5eb73fb

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,19 @@ def self.commit(message:, files: nil)
113113

114114
# Get the SHA of a given git ref. Typically useful to get the SHA of the current HEAD commit.
115115
#
116-
# @param [String] ref The git ref (commit, branch name, 'HEAD', …) to resolve as a SHA
116+
# @param ref [String]
117+
# The git ref (commit, branch name, 'HEAD', …) to resolve as a SHA
118+
# @param prepend_origin_if_needed [Boolean]
119+
# If true, will retry the rev-parse by prefixing `origin/` to the ref it it failed without it
117120
# @return [String] The commit SHA of the ref
118121
#
119-
def self.get_commit_sha(ref: 'HEAD')
120-
Git.open(Dir.pwd).revparse(ref)
122+
def self.get_commit_sha(ref: 'HEAD', prepend_origin_if_needed: false)
123+
repo = Git.open(Dir.pwd)
124+
repo.revparse(ref)
125+
rescue Git::FailedError
126+
raise unless prepend_origin_if_needed
127+
128+
repo.revparse("origin/#{ref}")
121129
end
122130

123131
# Creates a tag for the given version, and optionally push it to the remote.
@@ -170,20 +178,19 @@ def self.fetch_all_tags
170178
Action.sh('git', 'fetch', '--tags')
171179
end
172180

173-
# Use `git merge-base` to find as good common ancestors as possible for a merge
181+
# Use `git merge-base` to find as good a common ancestors as possible for a merge
174182
#
175183
# @param ref1 [String] The first git reference (sha1, ref name…)to find the common ancestor of
176184
# @param ref2 [String] The second git reference (sha1, ref name…)to find the common ancestor of
177185
# @return [String] The merge-base aka common ancestor for the 2 commits provided
186+
# @note If a reference (e.g. branch name) can't be found locally, it will try with the same ref prefixed with `origin/`
178187
#
179188
def self.find_merge_base(ref1, ref2)
180189
git_repo = Git.open(Dir.pwd)
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
190+
# Resolve to shas, mostly so that we can support cases with and without `origin/` explicit prefix on branch names
191+
ref1_sha, ref2_sha = [ref1, ref2].map { |ref| get_commit_sha(ref: ref, prepend_origin_if_needed: true) }
192+
193+
git_repo.merge_base(ref1_sha, ref2_sha)&.first&.sha
187194
end
188195

189196
# Checks if two git references point to the same commit.
@@ -194,16 +201,13 @@ def self.find_merge_base(ref1, ref2)
194201
# @return [Boolean] true if the two references point to the same commit, false otherwise.
195202
#
196203
def self.point_to_same_commit?(ref1, ref2)
197-
git_repo = Git.open(Dir.pwd)
198-
199204
begin
200-
ref1_commit = git_repo.gcommit(ref1)
201-
ref2_commit = git_repo.gcommit(ref2)
205+
ref1_sha = get_commit_sha(ref: ref1, prepend_origin_if_needed: true)
206+
ref2_sha = get_commit_sha(ref: ref2, prepend_origin_if_needed: true)
202207
rescue StandardError => e
203-
UI.error "Error fetching commits for #{ref1} and/or #{ref2}: #{e.message}"
204-
return false
208+
UI.user_error! "Error fetching commits for #{ref1} and/or #{ref2}: #{e.message}"
205209
end
206-
ref1_commit.sha == ref2_commit.sha
210+
ref1_sha == ref2_sha
207211
end
208212

209213
# Returns the current git branch, or "HEAD" if it's not checked out to any branch

0 commit comments

Comments
 (0)