Skip to content

Commit

Permalink
fix: fixed bumping version when there is no matching commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-linz committed Nov 21, 2024
1 parent a7517ab commit a7bf0dd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 35 deletions.
59 changes: 54 additions & 5 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe GitVersion do

version = git.get_new_version

version.should eq("1.0.1")
# no commit since latest tag, expect no version bump
version.should eq("1.0.0")

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

version = git.get_new_version

version.should eq("1.0.1")
# no new commit in master, expect no version bump
version.should eq("1.0.0")

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

Expand All @@ -148,7 +150,8 @@ describe GitVersion do

version = git.get_new_version

version.should eq("2.0.1")
# no new commit in master, expect no version bump
version.should eq("2.0.0")

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

Expand All @@ -164,7 +167,8 @@ describe GitVersion do

version = git.get_new_version

version.should eq("3.0.1")
# no new commit in master, expect no version bump
version.should eq("3.0.0")

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

Expand Down Expand Up @@ -851,6 +855,45 @@ describe GitVersion do
tmp.cleanup
end
end

it "should not bump version if no commit matches log-path filter" do
tmp = InTmp.new

begin
git = GitVersion::Git.new("dev", "master", "feature:", "breaking:", tmp.@tmpdir, "dir1-", "dir1/")

tmp.exec %(git init)
tmp.exec %(git checkout -b master)

# Create dir1 and tag dir1-1.0.0
base_dir = "dir1"
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
tmp.exec %(git add #{base_dir}/)
tmp.exec %(git commit --no-gpg-sign -m "feature: 1")
tmp.exec %(git tag "dir1-1.0.0")

# Create dir2 and tag dir2-1.0.0
base_dir = "dir2"
tmp.exec %(mkdir #{base_dir} && touch #{base_dir}/dummy_file)
tmp.exec %(git add #{base_dir}/)
tmp.exec %(git commit --no-gpg-sign -m "feature: 2")
tmp.exec %(git tag "dir2-1.0.0")

# Commit feature in dir2
base_dir = "dir2"
tmp.exec %(mkdir -p #{base_dir} && touch #{base_dir}/dummy_file_2)
tmp.exec %(git add #{base_dir}/)
tmp.exec %(git commit --no-gpg-sign -m "feature: 3")

# git-version should not bump version for dir1
version = git.get_new_version
hash = git.current_commit_hash
version.should eq("dir1-1.0.0")
ensure
tmp.cleanup
end
end

it "should truncate long branch names in tags" do
tmp = InTmp.new

Expand All @@ -864,7 +907,13 @@ describe GitVersion do

version = git.get_new_version
hash = git.current_commit_hash
version.should eq("100.100.101-veryveryveryverylongbranchname.0.#{hash}")
version.should eq("100.100.100-veryveryveryverylongbranchname.0.#{hash}")

tmp.exec %(git commit -m "commit" --allow-empty)

version = git.get_new_version
hash = git.current_commit_hash
version.should eq("100.100.101-veryveryveryverylongbranchname.1.#{hash}")
ensure
tmp.cleanup
end
Expand Down
70 changes: 40 additions & 30 deletions src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,55 +138,65 @@ module GitVersion
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch + 1,
previous_version.patch,
nil,
nil,
)

major = false
minor = false
patch = false
get_commits_since(previous_tag).each do |c|
patch = true
commit = c.downcase
match = if @major_id_is_regex
/#{@major_identifier}/.match(commit)
else
commit.includes?(@major_identifier)
end
if match
previous_version =
SemanticVersion.new(
previous_version.major + 1,
0,
0,
previous_version.prerelease,
previous_version.build,
)
major = true
break
end
end

if !major
get_commits_since(previous_tag).each do |c|
commit = c.downcase
match = if @minor_id_is_regex
/#{@minor_identifier}/.match(commit)
else
commit.includes?(@minor_identifier)
end
if match
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor + 1,
0,
previous_version.prerelease,
previous_version.build,
)
break
end
match = if @minor_id_is_regex
/#{@minor_identifier}/.match(commit)
else
commit.includes?(@minor_identifier)
end
if match
minor = true
end
end

if major
previous_version =
SemanticVersion.new(
previous_version.major + 1,
0,
0,
previous_version.prerelease,
previous_version.build,
)
elsif minor
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor + 1,
0,
previous_version.prerelease,
previous_version.build,
)
elsif patch
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch + 1,
previous_version.prerelease,
previous_version.build,
)
end

cb = current_branch_or_tag

if cb == @release_branch
Expand Down

0 comments on commit a7bf0dd

Please sign in to comment.