Skip to content

Commit e8a49e9

Browse files
committed
fix: use regex to strip git describe suffix, fixing tags with dashes (#31)
Replace naive dash-splitting with a regex that matches the exact -<N>-g<hex> git describe suffix pattern. The old approach broke when tag names themselves contained dashes (e.g. v1.0.0-beta, v1.0.0-1-ios-beta).
1 parent 8791179 commit e8a49e9

2 files changed

Lines changed: 99 additions & 6 deletions

File tree

lib/fastlane/plugin/semantic_release/actions/analyze_commits.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ def self.get_beginning_of_next_sprint(params)
5353
}
5454
end
5555

56-
# Tag format is v2.3.4-5-g7685948 (see git describe man page)
57-
# Strip the git describe suffix (-<count>-g<hash>) to get the tag name
58-
tag_name = tag
59-
if tag.split('-').length >= 3
60-
tag_name = tag.split('-')[0...-2].join('-').strip
61-
end
56+
# git describe appends -<N>-g<hash> when HEAD is ahead of a tag.
57+
# Use a regex to strip this specific suffix rather than splitting on
58+
# dashes, which breaks when the tag itself contains dashes (e.g. v1.0.0-beta).
59+
tag_name = tag.strip
60+
tag_name = tag_name.sub(/-\d+-g[0-9a-f]+$/, '')
6261
parsed_version = tag_name.match(params[:tag_version_match])
6362

6463
if parsed_version.nil?

spec/analyze_commits_spec.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,100 @@ def execute_lane_test(params)
240240
expect(execute_lane_test(match: 'v*')).to eq(true)
241241
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("1.0.9")
242242
end
243+
244+
it "should handle tag without git describe suffix" do
245+
commits = [
246+
"fix: ...|"
247+
]
248+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v5.7.0')
249+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
250+
251+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v5.7.0', debug: false)
252+
expect(execute_lane_test(match: 'v*')).to eq(true)
253+
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("5.7.1")
254+
end
255+
256+
it "should handle hyphenated tag without git describe suffix" do
257+
commits = [
258+
"fix: ...|"
259+
]
260+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v1.0.0-1-ios-beta')
261+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
262+
263+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v1.0.0-1-ios-beta', debug: false)
264+
expect(execute_lane_test(match: 'v*')).to eq(true)
265+
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("1.0.1")
266+
end
267+
268+
it "should handle hyphenated tag with git describe suffix" do
269+
commits = [
270+
"fix: ...|"
271+
]
272+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v1.0.0-1-ios-beta-3-gabc1234')
273+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
274+
275+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v1.0.0-1-ios-beta', debug: false)
276+
expect(execute_lane_test(match: 'v*')).to eq(true)
277+
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("1.0.1")
278+
end
279+
280+
it "should handle prerelease tag (v1.0.0-beta) without git describe suffix" do
281+
commits = [
282+
"fix: ...|"
283+
]
284+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v1.0.0-beta')
285+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
286+
287+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v1.0.0-beta', debug: false)
288+
expect(execute_lane_test(match: 'v*')).to eq(true)
289+
end
290+
291+
it "should handle prerelease tag with git describe suffix" do
292+
commits = [
293+
"fix: ...|"
294+
]
295+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v1.0.0-beta-3-gabc1234')
296+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
297+
298+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v1.0.0-beta', debug: false)
299+
expect(execute_lane_test(match: 'v*')).to eq(true)
300+
end
301+
302+
it "should handle rc tag (v2.0.0-rc.1) with git describe suffix" do
303+
commits = [
304+
"fix: ...|"
305+
]
306+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v2.0.0-rc.1-5-g1234abc')
307+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
308+
309+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v2.0.0-rc.1', debug: false)
310+
expect(execute_lane_test(match: 'v*')).to eq(true)
311+
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("2.0.1")
312+
end
313+
314+
it "should handle release- prefix tag with git describe suffix" do
315+
commits = [
316+
"fix: ...|"
317+
]
318+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('release-1.2.3-7-gdeadbeef')
319+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
320+
321+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'release-1.2.3', debug: false)
322+
expect(execute_lane_test(match: 'release-*')).to eq(true)
323+
expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::RELEASE_NEXT_VERSION]).to eq("1.2.4")
324+
end
325+
326+
it "should not strip suffix-like parts from tag name" do
327+
commits = [
328+
"fix: ...|"
329+
]
330+
# Tag v1.0.0-3-beta ends in -3-beta which looks like a suffix but isn't (beta is not g-prefixed hex)
331+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag).and_return('v1.0.0-3-beta')
332+
allow(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_commits_from_hash).and_return(commits)
333+
334+
expect(Fastlane::Actions::AnalyzeCommitsAction).to receive(:get_last_tag_hash).with(tag_name: 'v1.0.0-3-beta', debug: false)
335+
expect(execute_lane_test(match: 'v*')).to eq(true)
336+
end
243337
end
244338

245339
it "should provide codepush last version" do

0 commit comments

Comments
 (0)