Skip to content

Commit db123f9

Browse files
author
Daniel Reigada
authored
Merge pull request #40 from codacy/fix/fix-commit-counting
Fix/fix commit counting
2 parents c2add2d + 8c481e1 commit db123f9

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

spec/git-version-spec.cr

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe GitVersion do
319319
tmp.exec %(git merge myfeature)
320320

321321
tmp.exec %(git checkout master)
322-
tmp.exec %(git rebase dev)
322+
tmp.exec %(git rebase --no-gpg-sign dev)
323323
version = git.get_version
324324
version.should eq("1.0.1")
325325
ensure
@@ -345,7 +345,7 @@ describe GitVersion do
345345

346346
tmp.exec %(git checkout dev)
347347
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "4")
348-
tmp.exec %(git rebase master)
348+
tmp.exec %(git rebase --no-gpg-sign master)
349349

350350
tmp.exec %(git checkout master)
351351
tmp.exec %(git merge --no-gpg-sign --no-ff dev)
@@ -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: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ module GitVersion
2424
end
2525

2626
private def strip_prefix(version : String) : String | Nil
27-
stripped = version.lstrip(@prefix)
28-
if @prefix != "" && stripped.size >= version.size
29-
nil
30-
else
31-
stripped
32-
end
27+
version.lchop?(@prefix)
3328
end
3429

3530
private def exec(cmd)
@@ -75,15 +70,18 @@ module GitVersion
7570
return (exec cmd)[0].rjust(7, '0')
7671
end
7772

78-
def commits_distance(latest_tagged_version)
79-
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
8079
rescue
8180
return 0
8281
end
8382

84-
def get_bumps(latest)
85-
latest_exists = (exec "git tag -l #{latest}")
86-
if latest_exists.any?
83+
def get_bumps(latest : String | Nil)
84+
if !latest.nil? && (exec "git tag -l #{latest}").any?
8785
last_commit = (exec "git show-ref -s #{latest}")[0]
8886
return (exec "git log --pretty=%B #{last_commit}..HEAD #{log_paths_filter}")
8987
else
@@ -99,26 +97,26 @@ module GitVersion
9997
branch_tags = tags_by_branch(cb)
10098

10199
latest_version = BASE_VERSION
100+
latest_tag = nil
102101

103102
branch_tags.each do |tag|
104103
begin
105104
tag_without_prefix = strip_prefix(tag)
106105
if tag_without_prefix.nil?
107106
next
108107
end
109-
current_tag = SemanticVersion.parse(tag_without_prefix)
110-
if !current_tag.prerelease.identifiers.empty?
108+
current_version = SemanticVersion.parse(tag_without_prefix)
109+
if !current_version.prerelease.identifiers.empty?
111110
next
112-
elsif (latest_version < current_tag)
113-
latest_version = current_tag
111+
elsif (latest_version < current_version)
112+
latest_version = current_version
113+
latest_tag = tag
114114
end
115115
rescue
116116
#
117117
end
118118
end
119119

120-
latest_tagged_version = latest_version
121-
122120
latest_version =
123121
SemanticVersion.new(
124122
latest_version.major,
@@ -129,7 +127,7 @@ module GitVersion
129127
)
130128

131129
major = false
132-
get_bumps(latest_tagged_version).each do |bump|
130+
get_bumps(latest_tag).each do |bump|
133131
commit = bump.downcase
134132
if commit.includes?(MAJOR_BUMP_COMMENT)
135133
latest_version =
@@ -146,7 +144,7 @@ module GitVersion
146144
end
147145

148146
if !major
149-
get_bumps(latest_tagged_version).each do |bump|
147+
get_bumps(latest_tag).each do |bump|
150148
commit = bump.downcase
151149
if commit.includes?(MINOR_BUMP_COMMENT)
152150
latest_version =
@@ -165,7 +163,7 @@ module GitVersion
165163
if cb == @release_branch
166164
#
167165
elsif cb == @dev_branch
168-
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
169167
latest_version =
170168
SemanticVersion.new(
171169
latest_version.major,
@@ -176,7 +174,7 @@ module GitVersion
176174
)
177175
else
178176
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")
179-
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
180178
latest_version =
181179
SemanticVersion.new(
182180
latest_version.major,

0 commit comments

Comments
 (0)