Skip to content

Commit 3106133

Browse files
added unit test and fixed warnings
1 parent e923bc6 commit 3106133

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

spec/git-version-spec.cr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ require "../src/git-version"
66

77
include Utils
88
describe GitVersion do
9+
it "should match hash with hash without prefix" do
10+
tmp = InTmp.new
11+
begin
12+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir)
13+
14+
tmp.exec %(git init)
15+
tmp.exec %(git checkout -b #{git.release_branch})
16+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
17+
tmp.exec %(git tag "1.0.0")
18+
19+
version = git.get_new_version
20+
21+
version.should eq("1.0.1")
22+
23+
tmp.exec %(git checkout -b dev)
24+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
25+
26+
tag_on_master = git.tags_by_branch("#{git.release_branch}")
27+
28+
tag_on_master.should eq(["1.0.0"])
29+
30+
current_branch = git.current_branch_or_tag
31+
32+
hash = git.current_commit_hash
33+
hashWithoutPrefix = git.current_commit_hash_without_prefix
34+
35+
hash.should eq("sha#{hashWithoutPrefix}")
36+
end
37+
end
938
it "should get the correct version in master and dev branch" do
1039
tmp = InTmp.new
1140

src/git-version.cr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ module GitVersion
7878
return "sha" + sha
7979
end
8080

81+
def current_commit_hash_without_prefix : String
82+
cmd = "git rev-parse --verify HEAD --short"
83+
return (exec cmd)[0].rjust(7, '0')
84+
end
85+
8186
def commits_distance(tag : String | Nil)
8287
if tag.nil?
8388
return (exec "git rev-list --count HEAD")[0]
@@ -99,7 +104,7 @@ module GitVersion
99104
return [] of String
100105
end
101106

102-
def get_previous_tag_and_version: Tuple(String | Nil, SemanticVersion)
107+
def get_previous_tag_and_version : Tuple(String | Nil, SemanticVersion)
103108
cb = current_branch_or_tag
104109

105110
branch_tags = tags_by_branch(cb)
@@ -127,7 +132,7 @@ module GitVersion
127132
return {previous_tag, previous_version}
128133
end
129134

130-
def get_previous_version: String
135+
def get_previous_version : String
131136
lt, lv = get_previous_tag_and_version
132137
return lt ? lt : add_prefix(lv.to_s)
133138
end

0 commit comments

Comments
 (0)