Skip to content

feat: support for custom version suffix #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
114 changes: 114 additions & 0 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 12 additions & 12 deletions src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ release_branch = "master"
minor_identifier = "feature:"
major_identifier = "breaking:"
prefix = ""
suffix = ""
log_paths = ""

folder = FileUtils.pwd
Expand All @@ -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 }
Expand All @@ -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}"
Expand Down