Skip to content

Commit 8024111

Browse files
committed
fix: fixed bumping version when there is no matching commit
1 parent 6c1123c commit 8024111

File tree

2 files changed

+93
-35
lines changed

2 files changed

+93
-35
lines changed

spec/git-version-spec.cr

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ describe GitVersion do
1919

2020
version = git.get_new_version
2121

22-
version.should eq("1.0.1")
22+
# no commit since latest tag, expect no version bump
23+
version.should eq("1.0.0")
2324

2425
tmp.exec %(git checkout -b dev)
2526
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "2")
@@ -132,7 +133,8 @@ describe GitVersion do
132133

133134
version = git.get_new_version
134135

135-
version.should eq("1.0.1")
136+
# no new commit in master, expect no version bump
137+
version.should eq("1.0.0")
136138

137139
tmp.exec %(git merge my-fancy.branch)
138140

@@ -148,7 +150,8 @@ describe GitVersion do
148150

149151
version = git.get_new_version
150152

151-
version.should eq("2.0.1")
153+
# no new commit in master, expect no version bump
154+
version.should eq("2.0.0")
152155

153156
tmp.exec %(git merge --ff-only my-fancy.branch2)
154157

@@ -164,7 +167,8 @@ describe GitVersion do
164167

165168
version = git.get_new_version
166169

167-
version.should eq("3.0.1")
170+
# no new commit in master, expect no version bump
171+
version.should eq("3.0.0")
168172

169173
tmp.exec %(git merge --no-gpg-sign --no-ff my-fancy.branch3)
170174

@@ -737,6 +741,44 @@ describe GitVersion do
737741
tmp.cleanup
738742
end
739743
end
744+
745+
it "should not bump version if no commit matches log-path filter" do
746+
tmp = InTmp.new
747+
748+
begin
749+
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "dir1-", "dir1/")
750+
751+
tmp.exec %(git init)
752+
tmp.exec %(git checkout -b master)
753+
754+
# Create dir1 and tag dir1-1.0.0
755+
base_dir = "dir1"
756+
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
757+
tmp.exec %(git add #{base_dir}/)
758+
tmp.exec %(git commit --no-gpg-sign -m "feature: 1")
759+
tmp.exec %(git tag "dir1-1.0.0")
760+
761+
# Create dir2 and tag dir2-1.0.0
762+
base_dir = "dir2"
763+
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
764+
tmp.exec %(git add #{base_dir}/)
765+
tmp.exec %(git commit --no-gpg-sign -m "feature: 2")
766+
tmp.exec %(git tag "dir2-1.0.0")
767+
768+
# Commit feature in dir2
769+
base_dir = "dir2"
770+
tmp.exec %(mkdir -p #{base_dir} && touch #{base_dir}/dummy_file_2)
771+
tmp.exec %(git add #{base_dir}/)
772+
tmp.exec %(git commit --no-gpg-sign -m "feature: 3")
773+
774+
# git-version should not bump version for dir1
775+
version = git.get_new_version
776+
version.should eq("dir1-1.0.0")
777+
ensure
778+
tmp.cleanup
779+
end
780+
end
781+
740782
it "should truncate long branch names in tags" do
741783
tmp = InTmp.new
742784

@@ -750,7 +792,13 @@ describe GitVersion do
750792

751793
version = git.get_new_version
752794
hash = git.current_commit_hash
753-
version.should eq("100.100.101-veryveryveryverylongbranchname.0.#{hash}")
795+
version.should eq("100.100.100-veryveryveryverylongbranchname.0.#{hash}")
796+
797+
tmp.exec %(git commit -m "commit" --allow-empty)
798+
799+
version = git.get_new_version
800+
hash = git.current_commit_hash
801+
version.should eq("100.100.101-veryveryveryverylongbranchname.1.#{hash}")
754802
ensure
755803
tmp.cleanup
756804
end

src/git-version.cr

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -138,55 +138,65 @@ module GitVersion
138138
SemanticVersion.new(
139139
previous_version.major,
140140
previous_version.minor,
141-
previous_version.patch + 1,
141+
previous_version.patch,
142142
nil,
143143
nil,
144144
)
145145

146146
major = false
147+
minor = false
148+
patch = false
147149
get_commits_since(previous_tag).each do |c|
150+
patch = true
148151
commit = c.downcase
149152
match = if @major_id_is_regex
150153
/#{@major_identifier}/.match(commit)
151154
else
152155
commit.includes?(@major_identifier)
153156
end
154157
if match
155-
previous_version =
156-
SemanticVersion.new(
157-
previous_version.major + 1,
158-
0,
159-
0,
160-
previous_version.prerelease,
161-
previous_version.build,
162-
)
163158
major = true
164-
break
165159
end
166-
end
167160

168-
if !major
169-
get_commits_since(previous_tag).each do |c|
170-
commit = c.downcase
171-
match = if @minor_id_is_regex
172-
/#{@minor_identifier}/.match(commit)
173-
else
174-
commit.includes?(@minor_identifier)
175-
end
176-
if match
177-
previous_version =
178-
SemanticVersion.new(
179-
previous_version.major,
180-
previous_version.minor + 1,
181-
0,
182-
previous_version.prerelease,
183-
previous_version.build,
184-
)
185-
break
186-
end
161+
match = if @minor_id_is_regex
162+
/#{@minor_identifier}/.match(commit)
163+
else
164+
commit.includes?(@minor_identifier)
165+
end
166+
if match
167+
minor = true
187168
end
188169
end
189170

171+
if major
172+
previous_version =
173+
SemanticVersion.new(
174+
previous_version.major + 1,
175+
0,
176+
0,
177+
previous_version.prerelease,
178+
previous_version.build,
179+
)
180+
elsif minor
181+
previous_version =
182+
SemanticVersion.new(
183+
previous_version.major,
184+
previous_version.minor + 1,
185+
0,
186+
previous_version.prerelease,
187+
previous_version.build,
188+
)
189+
elsif patch
190+
previous_version =
191+
SemanticVersion.new(
192+
previous_version.major,
193+
previous_version.minor,
194+
previous_version.patch + 1,
195+
previous_version.prerelease,
196+
previous_version.build,
197+
)
198+
end
199+
190200
cb = current_branch_or_tag
191201

192202
if cb == @release_branch

0 commit comments

Comments
 (0)