diff --git a/spec/git-version-spec.cr b/spec/git-version-spec.cr index 3ec3e7f..fe7a351 100644 --- a/spec/git-version-spec.cr +++ b/spec/git-version-spec.cr @@ -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 diff --git a/src/git-version.cr b/src/git-version.cr index d49959b..121512d 100644 --- a/src/git-version.cr +++ b/src/git-version.cr @@ -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 @@ -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) @@ -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