Skip to content

feat: added option to skip prerelease part of the version generated #103

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 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ You can configure the action with various inputs, a list of which has been provi
| tool-version | The version of the tool to run | latest |
| release-branch | The name of the master/main branch | master |
| dev-branch | The name of the development branch | dev |
| skip-prerelease | Skip prerelease part of the version. When true, release-branch and dev-branch are effectively ignored | false |
| minor-identifier | The string used to identify a minor release (wrap with '/' to match using a regular expression) | feature: |
| major-identifier | The string used to identify a major release (wrap with '/' to match using a regular expression) | breaking: |
| prefix | The prefix used for the version name | |
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
description: 'The name of the dev branch'
required: true
default: dev
skip-prerelease:
description: "Skip prerelease part of the version. When true, release-branch and dev-branch are effectively ignored"
required: true
default: "false"
minor-identifier:
description: 'The string or regex to identify a minor release commit'
required: true
Expand Down Expand Up @@ -61,6 +65,7 @@ runs:
--previous-version \
--release-branch "${{ inputs.release-branch }}" \
--dev-branch "${{ inputs.dev-branch }}" \
${{ inputs.skip-prerelease == 'true' && '--skip-prerelease' || '' }} \
--minor-identifier="${{ inputs.minor-identifier }}" \
--major-identifier="${{ inputs.major-identifier }}" \
--version-prefix "${{ inputs.prefix }}")
Expand All @@ -75,6 +80,8 @@ runs:
export VERSION=$(git-version \
--release-branch "${{ inputs.release-branch }}" \
--dev-branch "${{ inputs.dev-branch }}" \
${{ inputs.skip-prerelease == 'true' && '--skip-prerelease' || '' }} \
--skip-prerelease "${{ inputs.skip-prerelease }}" \
--minor-identifier="${{ inputs.minor-identifier }}" \
--major-identifier="${{ inputs.major-identifier }}" \
--version-prefix "${{ inputs.prefix }}")
Expand Down
22 changes: 22 additions & 0 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ describe GitVersion do
end
end

it "should skip prerelease component in the version number when configured" do
tmp = InTmp.new

begin
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 checkout -b my-fancy.branch)
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")

git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "", "", true)

version = git.get_new_version

version.should eq("1.0.1")
ensure
tmp.cleanup
end
end

it "should properly bump the version" do
tmp = InTmp.new

Expand Down
53 changes: 28 additions & 25 deletions src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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 = "",
@skip_prerelease : Bool = false)
@major_id_is_regex = false
@minor_id_is_regex = false
if match = /\/(.*)\//.match(@major_identifier)
Expand Down Expand Up @@ -187,31 +188,33 @@ module GitVersion
end
end

cb = current_branch_or_tag
if !@skip_prerelease
cb = current_branch_or_tag

if cb == @release_branch
#
elsif cb == @dev_branch
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
else
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
if cb == @release_branch
#
elsif cb == @dev_branch
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
else
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
end
end

return add_prefix(previous_version.to_s)
Expand Down
4 changes: 3 additions & 1 deletion src/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require "./git-version"
previous_version = false
dev_branch = "dev"
release_branch = "master"
skip_prerelease = false
minor_identifier = "feature:"
major_identifier = "breaking:"
prefix = ""
Expand All @@ -19,6 +20,7 @@ OptionParser.parse do |parser|
parser.on("-f FOLDER", "--folder=FOLDER", "Execute the command in the defined folder") { |f| folder = f }
parser.on("-b BRANCH", "--dev-branch=BRANCH", "Specifies the development branch") { |branch| dev_branch = branch }
parser.on("-r BRANCH", "--release-branch=BRANCH", "Specifies the release branch") { |branch| release_branch = branch }
parser.on("--skip-prerelease", "Skip the prerelase part of the version") { skip_prerelease=true }
parser.on("--minor-identifier=IDENTIFIER",
"Specifies the string or regex to identify a minor release commit with") { |identifier| minor_identifier = identifier }
parser.on("--major-identifier=IDENTIFIER",
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, skip_prerelease)

if previous_version
puts "#{git.get_previous_version}"
Expand Down