diff --git a/action.yml b/action.yml index 4080496..d22f82e 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,9 @@ inputs: prefix: description: 'The prefix to use in the version' required: false + suffix: + description: 'The suffix to use in the version' + required: false log-paths: description: 'The paths to be used to calculate changes (comma-separated)' required: false @@ -63,7 +66,8 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") + --version-prefix "${{ inputs.prefix }}" \ + --version-suffix "${{ inputs.suffix }}") echo "previous-version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT echo "Previous Version: $PREVIOUS_VERSION" @@ -77,7 +81,8 @@ runs: --dev-branch "${{ inputs.dev-branch }}" \ --minor-identifier="${{ inputs.minor-identifier }}" \ --major-identifier="${{ inputs.major-identifier }}" \ - --version-prefix "${{ inputs.prefix }}") + --version-prefix "${{ inputs.prefix }}" \ + --version-suffix "${{ inputs.suffix }}") echo "version=$VERSION" >> $GITHUB_OUTPUT echo "New Version: $VERSION" diff --git a/spec/git-version-spec.cr b/spec/git-version-spec.cr index 3ec3e7f..5a52d72 100644 --- a/spec/git-version-spec.cr +++ b/spec/git-version-spec.cr @@ -547,6 +547,120 @@ describe GitVersion do end end + it "should properly manage suffixes" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1") + tmp.exec %(git tag "1.1.0-backend") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2") + + version = git.get_new_version + version.should eq("1.1.1-backend") + ensure + tmp.cleanup + end + end + + it "non-suffixed tags should be ignored if suffix is enabled" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0") + + version = git.get_new_version + version.should eq("0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage a tag with only suffix" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "-backend") + + version = git.get_new_version + version.should eq("0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage combination of prefixes and suffixes" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1") + tmp.exec %(git tag "v1.1.0-backend") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2") + + version = git.get_new_version + version.should eq("v1.1.1-backend") + ensure + tmp.cleanup + end + end + + it "non-prefixed or suffixed tags should be ignored if prefix and suffix are enabled" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "v1.0.0") + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "1.0.0-backend") + + version = git.get_new_version + version.should eq("v0.0.1-backend") + ensure + tmp.cleanup + end + end + + it "should properly manage a tag with only prefix and suffix" do + tmp = InTmp.new + + begin + git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "v", "", "-backend") + + tmp.exec %(git init) + tmp.exec %(git checkout -b master) + tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1") + tmp.exec %(git tag "v-backend") + + version = git.get_new_version + version.should eq("v0.0.1-backend") + ensure + tmp.cleanup + end + end + it "should count the commits distance" do tmp = InTmp.new diff --git a/src/git-version.cr b/src/git-version.cr index d49959b..8829e44 100644 --- a/src/git-version.cr +++ b/src/git-version.cr @@ -12,7 +12,7 @@ module GitVersion class Git def initialize(@dev_branch : String, @release_branch : String, @minor_identifier : String, @major_identifier : String, - @folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "") + @folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "", @suffix : String = "") @major_id_is_regex = false @minor_id_is_regex = false if match = /\/(.*)\//.match(@major_identifier) @@ -26,12 +26,12 @@ module GitVersion # end - private def add_prefix(version : String) : String - return "#{@prefix}#{version}" + private def add_prefix_suffix(version : String) : String + return "#{@prefix}#{version}#{@suffix}" end - private def strip_prefix(version : String) : String | Nil - version.lchop?(@prefix) + private def strip_prefix_suffix(version : String) : String | Nil + version.lchop?(@prefix).try &.rchop?(@suffix) end private def exec(cmd) @@ -98,7 +98,7 @@ module GitVersion return [] of String end - def get_previous_tag_and_version: Tuple(String | Nil, SemanticVersion) + def get_previous_tag_and_version : Tuple(String | Nil, SemanticVersion) cb = current_branch_or_tag branch_tags = tags_by_branch(cb) @@ -108,11 +108,11 @@ module GitVersion branch_tags.each do |tag| begin - tag_without_prefix = strip_prefix(tag) - if tag_without_prefix.nil? + tag_without_prefix_suffix = strip_prefix_suffix(tag) + if tag_without_prefix_suffix.nil? next end - current_version = SemanticVersion.parse(tag_without_prefix) + current_version = SemanticVersion.parse(tag_without_prefix_suffix) if !current_version.prerelease.identifiers.empty? next elsif (previous_version < current_version) @@ -126,9 +126,9 @@ module GitVersion return {previous_tag, previous_version} end - def get_previous_version: String + def get_previous_version : String lt, lv = get_previous_tag_and_version - return lt ? lt : add_prefix(lv.to_s) + return lt ? lt : add_prefix_suffix(lv.to_s) end def get_new_version @@ -214,7 +214,7 @@ module GitVersion ) end - return add_prefix(previous_version.to_s) + return add_prefix_suffix(previous_version.to_s) end end end diff --git a/src/main.cr b/src/main.cr index 6de97d0..f2e9293 100644 --- a/src/main.cr +++ b/src/main.cr @@ -10,6 +10,7 @@ release_branch = "master" minor_identifier = "feature:" major_identifier = "breaking:" prefix = "" +suffix = "" log_paths = "" folder = FileUtils.pwd @@ -24,6 +25,7 @@ OptionParser.parse do |parser| parser.on("--major-identifier=IDENTIFIER", "Specifies the string or regex to identify a major release commit with") { |identifier| major_identifier = identifier } parser.on("-p PREFIX", "--version-prefix=PREFIX", "Specifies a version prefix") { |p| prefix = p } + parser.on("-s SUFFIX", "--version-suffix=SUFFIX", "Specifies a version suffix") { |s| suffix = s } parser.on("-l PATH", "--log-paths=PATH", "") { |path| log_paths = path } parser.on("--previous-version", "Returns the previous tag instead of calculating a new one") { previous_version=true } parser.on("-h", "--help", "Show this help") { puts parser } @@ -34,7 +36,7 @@ OptionParser.parse do |parser| end end -git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths) +git = GitVersion::Git.new(dev_branch, release_branch, minor_identifier, major_identifier, folder, prefix, log_paths, suffix) if previous_version puts "#{git.get_previous_version}"