Skip to content

Commit 774aef2

Browse files
committed
feat: support for custom version suffix
1 parent 6c1123c commit 774aef2

File tree

4 files changed

+134
-14
lines changed

4 files changed

+134
-14
lines changed

action.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ inputs:
2828
prefix:
2929
description: 'The prefix to use in the version'
3030
required: false
31+
suffix:
32+
description: "The suffix to use in the version"
33+
required: false
3134
log-paths:
3235
description: 'The paths to be used to calculate changes (comma-separated)'
3336
required: false
@@ -63,7 +66,8 @@ runs:
6366
--dev-branch "${{ inputs.dev-branch }}" \
6467
--minor-identifier="${{ inputs.minor-identifier }}" \
6568
--major-identifier="${{ inputs.major-identifier }}" \
66-
--version-prefix "${{ inputs.prefix }}")
69+
--version-prefix "${{ inputs.prefix }}") \
70+
--version-suffix "${{ inputs.suffix }}")
6771
6872
echo "previous-version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
6973
echo "Previous Version: $PREVIOUS_VERSION"
@@ -77,7 +81,8 @@ runs:
7781
--dev-branch "${{ inputs.dev-branch }}" \
7882
--minor-identifier="${{ inputs.minor-identifier }}" \
7983
--major-identifier="${{ inputs.major-identifier }}" \
80-
--version-prefix "${{ inputs.prefix }}")
84+
--version-prefix "${{ inputs.prefix }}") \
85+
--version-suffix "${{ inputs.suffix }}")
8186
8287
echo "version=$VERSION" >> $GITHUB_OUTPUT
8388
echo "New Version: $VERSION"

spec/git-version-spec.cr

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,120 @@ describe GitVersion do
547547
end
548548
end
549549

550+
it "should properly manage suffixes" do
551+
tmp = InTmp.new
552+
553+
begin
554+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend")
555+
556+
tmp.exec %(git init)
557+
tmp.exec %(git checkout -b master)
558+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1")
559+
tmp.exec %(git tag "1.1.0-backend")
560+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
561+
562+
version = git.get_new_version
563+
version.should eq("1.1.1-backend")
564+
ensure
565+
tmp.cleanup
566+
end
567+
end
568+
569+
it "non-suffixed tags should be ignored if suffix is enabled" do
570+
tmp = InTmp.new
571+
572+
begin
573+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend")
574+
575+
tmp.exec %(git init)
576+
tmp.exec %(git checkout -b master)
577+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
578+
tmp.exec %(git tag "1.0.0")
579+
580+
version = git.get_new_version
581+
version.should eq("0.0.1-backend")
582+
ensure
583+
tmp.cleanup
584+
end
585+
end
586+
587+
it "should properly manage a tag with only suffix" do
588+
tmp = InTmp.new
589+
590+
begin
591+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend")
592+
593+
tmp.exec %(git init)
594+
tmp.exec %(git checkout -b master)
595+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
596+
tmp.exec %(git tag "-backend")
597+
598+
version = git.get_new_version
599+
version.should eq("0.0.1-backend")
600+
ensure
601+
tmp.cleanup
602+
end
603+
end
604+
605+
it "should properly manage combination of prefixes and suffixes" do
606+
tmp = InTmp.new
607+
608+
begin
609+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend")
610+
611+
tmp.exec %(git init)
612+
tmp.exec %(git checkout -b master)
613+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1")
614+
tmp.exec %(git tag "v1.1.0-backend")
615+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
616+
617+
version = git.get_new_version
618+
version.should eq("v1.1.1-backend")
619+
ensure
620+
tmp.cleanup
621+
end
622+
end
623+
624+
it "non-prefixed or suffixed tags should be ignored if prefix and suffix are enabled" do
625+
tmp = InTmp.new
626+
627+
begin
628+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend")
629+
630+
tmp.exec %(git init)
631+
tmp.exec %(git checkout -b master)
632+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
633+
tmp.exec %(git tag "1.0.0")
634+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
635+
tmp.exec %(git tag "v1.0.0")
636+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
637+
tmp.exec %(git tag "1.0.0-backend")
638+
639+
version = git.get_new_version
640+
version.should eq("v0.0.1-backend")
641+
ensure
642+
tmp.cleanup
643+
end
644+
end
645+
646+
it "should properly manage a tag with only prefix and suffix" do
647+
tmp = InTmp.new
648+
649+
begin
650+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend")
651+
652+
tmp.exec %(git init)
653+
tmp.exec %(git checkout -b master)
654+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
655+
tmp.exec %(git tag "v-backend")
656+
657+
version = git.get_new_version
658+
version.should eq("v0.0.1-backend")
659+
ensure
660+
tmp.cleanup
661+
end
662+
end
663+
550664
it "should count the commits distance" do
551665
tmp = InTmp.new
552666

src/git-version.cr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module GitVersion
1212

1313
class Git
1414
def initialize(@dev_branch : String, @release_branch : String, @minor_identifier : String, @major_identifier : String,
15-
@folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "")
15+
@folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "", @suffix : String = "")
1616
@major_id_is_regex = false
1717
@minor_id_is_regex = false
1818
if match = /\/(.*)\//.match(@major_identifier)
@@ -26,12 +26,12 @@ module GitVersion
2626
#
2727
end
2828

29-
private def add_prefix(version : String) : String
30-
return "#{@prefix}#{version}"
29+
private def add_prefix_suffix(version : String) : String
30+
return "#{@prefix}#{version}#{@suffix}"
3131
end
3232

33-
private def strip_prefix(version : String) : String | Nil
34-
version.lchop?(@prefix)
33+
private def strip_prefix_suffix(version : String) : String | Nil
34+
version.lchop?(@prefix).try &.rchop?(@suffix)
3535
end
3636

3737
private def exec(cmd)
@@ -98,7 +98,7 @@ module GitVersion
9898
return [] of String
9999
end
100100

101-
def get_previous_tag_and_version: Tuple(String | Nil, SemanticVersion)
101+
def get_previous_tag_and_version : Tuple(String | Nil, SemanticVersion)
102102
cb = current_branch_or_tag
103103

104104
branch_tags = tags_by_branch(cb)
@@ -108,11 +108,11 @@ module GitVersion
108108

109109
branch_tags.each do |tag|
110110
begin
111-
tag_without_prefix = strip_prefix(tag)
112-
if tag_without_prefix.nil?
111+
tag_without_prefix_suffix = strip_prefix_suffix(tag)
112+
if tag_without_prefix_suffix.nil?
113113
next
114114
end
115-
current_version = SemanticVersion.parse(tag_without_prefix)
115+
current_version = SemanticVersion.parse(tag_without_prefix_suffix)
116116
if !current_version.prerelease.identifiers.empty?
117117
next
118118
elsif (previous_version < current_version)
@@ -128,7 +128,7 @@ module GitVersion
128128

129129
def get_previous_version: String
130130
lt, lv = get_previous_tag_and_version
131-
return lt ? lt : add_prefix(lv.to_s)
131+
return lt ? lt : add_prefix_suffix(lv.to_s)
132132
end
133133

134134
def get_new_version
@@ -214,7 +214,7 @@ module GitVersion
214214
)
215215
end
216216

217-
return add_prefix(previous_version.to_s)
217+
return add_prefix_suffix(previous_version.to_s)
218218
end
219219
end
220220
end

src/main.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ OptionParser.parse do |parser|
2424
parser.on("--major-identifier=IDENTIFIER",
2525
"Specifies the string or regex to identify a major release commit with") { |identifier| major_identifier = identifier }
2626
parser.on("-p PREFIX", "--version-prefix=PREFIX", "Specifies a version prefix") { |p| prefix = p }
27+
parser.on("-s SUFFIX", "--version-suffix=SUFFIX", "Specifies a version suffix") { |s| suffix = s }
2728
parser.on("-l PATH", "--log-paths=PATH", "") { |path| log_paths = path }
2829
parser.on("--previous-version", "Returns the previous tag instead of calculating a new one") { previous_version=true }
2930
parser.on("-h", "--help", "Show this help") { puts parser }
@@ -34,7 +35,7 @@ OptionParser.parse do |parser|
3435
end
3536
end
3637

37-
git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths)
38+
git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths, suffix)
3839

3940
if previous_version
4041
puts "#{git.get_previous_version}"

0 commit comments

Comments
 (0)