Skip to content

fix: add sha prefix to versioning #105

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 3 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
27 changes: 27 additions & 0 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ require "../src/git-version"

include Utils
describe GitVersion do
it "should match hash with hash without prefix" do
tmp = InTmp.new
begin
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir)

tmp.exec %(git init)
tmp.exec %(git checkout -b #{git.release_branch})
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("1.0.1")

tmp.exec %(git checkout -b dev)
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")

tag_on_master = git.tags_by_branch("#{git.release_branch}")

tag_on_master.should eq(["1.0.0"])

hash = git.current_commit_hash
hashWithoutPrefix = git.current_commit_hash_without_prefix

hash.should eq("sha#{hashWithoutPrefix}")
end
end
it "should get the correct version in master and dev branch" do
tmp = InTmp.new

Expand Down
10 changes: 8 additions & 2 deletions src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module GitVersion
end

def current_commit_hash : String
cmd = "git rev-parse --verify HEAD --short"
sha = (exec cmd)[0].rjust(7, '0')
return "sha" + sha
end

def current_commit_hash_without_prefix : String
cmd = "git rev-parse --verify HEAD --short"
return (exec cmd)[0].rjust(7, '0')
end
Expand All @@ -98,7 +104,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 Down Expand Up @@ -126,7 +132,7 @@ 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)
end
Expand Down