Skip to content

fix: fixed bumping version when there is no matching commit #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 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 @@ -737,6 +741,44 @@ 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
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 @@ -750,7 +792,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