Skip to content

Commit 8c481e1

Browse files
committed
fix: Handle prefixes correctly
1 parent 4ff9875 commit 8c481e1

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

spec/git-version-spec.cr

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ describe GitVersion do
443443
tmp.exec %(git init)
444444
tmp.exec %(git checkout -b master)
445445
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
446-
tmp.exec %(git tag "0.1.0-asd")
446+
tmp.exec %(git tag "0.1.0")
447+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 2")
448+
tmp.exec %(git tag "0.2.0-asd")
447449
tmp.exec %(git commit --no-gpg-sign --allow-empty -m ":breaking: 2")
448450

449451
version = git.get_version
@@ -484,7 +486,7 @@ describe GitVersion do
484486

485487
version = git.get_version
486488
hash = git.current_commit_hash
487-
version.should eq("1.0.0-v1.0.#{hash}")
489+
version.should eq("1.0.0-v1.1.#{hash}")
488490
ensure
489491
tmp.cleanup
490492
end
@@ -498,11 +500,12 @@ describe GitVersion do
498500

499501
tmp.exec %(git init)
500502
tmp.exec %(git checkout -b master)
501-
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "1")
502-
tmp.exec %(git tag "v1.0.0")
503+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 1")
504+
tmp.exec %(git tag "v1.1.0")
505+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
503506

504507
version = git.get_version
505-
version.should eq("v1.0.1")
508+
version.should eq("v1.1.1")
506509
ensure
507510
tmp.cleanup
508511
end
@@ -559,7 +562,7 @@ describe GitVersion do
559562

560563
version = git.get_version
561564
hash = git.current_commit_hash
562-
version.should eq("1.0.0-v1.0.#{hash}")
565+
version.should eq("1.0.0-v1.3.#{hash}")
563566
ensure
564567
tmp.cleanup
565568
end
@@ -703,6 +706,7 @@ describe GitVersion do
703706

704707
tmp.exec %(git init)
705708
tmp.exec %(git checkout -b master)
709+
706710
# Create dir1 and tag dir1-1.0.0
707711
base_dir = "dir1"
708712
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
@@ -718,16 +722,17 @@ describe GitVersion do
718722
tmp.exec %(git tag "dir2-1.0.0")
719723

720724
tmp.exec %(git checkout -b dev)
725+
721726
# Create dir2 and commit breaking
722727
base_dir = "dir2"
723-
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
728+
tmp.exec %(mkdir -p #{base_dir} && touch #{base_dir}/dummy_file_2)
724729
tmp.exec %(git add #{base_dir}/)
725-
tmp.exec %(git commit --no-gpg-sign -m "breaking: 2")
730+
tmp.exec %(git commit --no-gpg-sign -m "breaking: 3")
726731

727732
# git-version should accept the breaking tag on commit with dir2
728733
version = git.get_version
729734
hash = git.current_commit_hash
730-
version.should eq("dir2-2.0.0-SNAPSHOT.0.#{hash}")
735+
version.should eq("dir2-2.0.0-SNAPSHOT.1.#{hash}")
731736
ensure
732737
tmp.cleanup
733738
end

src/git-version.cr

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@ module GitVersion
7070
return (exec cmd)[0].rjust(7, '0')
7171
end
7272

73-
def commits_distance(latest_tagged_version)
74-
return (exec "git rev-list --count HEAD ^#{latest_tagged_version}")[0]
73+
def commits_distance(tag : String | Nil)
74+
if tag.nil?
75+
return (exec "git rev-list --count HEAD")[0]
76+
else
77+
return (exec "git rev-list --count HEAD ^#{tag}")[0]
78+
end
7579
rescue
7680
return 0
7781
end
7882

79-
def get_bumps(latest)
80-
latest_exists = (exec "git tag -l #{latest}")
81-
if latest_exists.any?
83+
def get_bumps(latest : String | Nil)
84+
if !latest.nil? && (exec "git tag -l #{latest}").any?
8285
last_commit = (exec "git show-ref -s #{latest}")[0]
8386
return (exec "git log --pretty=%B #{last_commit}..HEAD #{log_paths_filter}")
8487
else
@@ -94,26 +97,26 @@ module GitVersion
9497
branch_tags = tags_by_branch(cb)
9598

9699
latest_version = BASE_VERSION
100+
latest_tag = nil
97101

98102
branch_tags.each do |tag|
99103
begin
100104
tag_without_prefix = strip_prefix(tag)
101105
if tag_without_prefix.nil?
102106
next
103107
end
104-
current_tag = SemanticVersion.parse(tag_without_prefix)
105-
if !current_tag.prerelease.identifiers.empty?
108+
current_version = SemanticVersion.parse(tag_without_prefix)
109+
if !current_version.prerelease.identifiers.empty?
106110
next
107-
elsif (latest_version < current_tag)
108-
latest_version = current_tag
111+
elsif (latest_version < current_version)
112+
latest_version = current_version
113+
latest_tag = tag
109114
end
110115
rescue
111116
#
112117
end
113118
end
114119

115-
latest_tagged_version = latest_version
116-
117120
latest_version =
118121
SemanticVersion.new(
119122
latest_version.major,
@@ -124,7 +127,7 @@ module GitVersion
124127
)
125128

126129
major = false
127-
get_bumps(latest_tagged_version).each do |bump|
130+
get_bumps(latest_tag).each do |bump|
128131
commit = bump.downcase
129132
if commit.includes?(MAJOR_BUMP_COMMENT)
130133
latest_version =
@@ -141,7 +144,7 @@ module GitVersion
141144
end
142145

143146
if !major
144-
get_bumps(latest_tagged_version).each do |bump|
147+
get_bumps(latest_tag).each do |bump|
145148
commit = bump.downcase
146149
if commit.includes?(MINOR_BUMP_COMMENT)
147150
latest_version =
@@ -160,7 +163,7 @@ module GitVersion
160163
if cb == @release_branch
161164
#
162165
elsif cb == @dev_branch
163-
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(latest_tagged_version), current_commit_hash()] of String | Int32
166+
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(latest_tag), current_commit_hash()] of String | Int32
164167
latest_version =
165168
SemanticVersion.new(
166169
latest_version.major,
@@ -171,7 +174,7 @@ module GitVersion
171174
)
172175
else
173176
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")
174-
prerelease = [branch_sanitized_name, commits_distance(latest_tagged_version), current_commit_hash()] of String | Int32
177+
prerelease = [branch_sanitized_name, commits_distance(latest_tag), current_commit_hash()] of String | Int32
175178
latest_version =
176179
SemanticVersion.new(
177180
latest_version.major,

0 commit comments

Comments
 (0)