Skip to content

Commit 3123cdc

Browse files
authored
breaking: Add commits distance to the version in pre-releases (#30)
* breaking: Add commits distance to the version in pre-releases * fix: Versioning in this repository should be done with the current binary * fix: Calculate distance from latest tagged version
1 parent 4cc0cbd commit 3123cdc

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

.circleci/config.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ jobs:
2121
compile:
2222
<<: *default_machine_job
2323
steps:
24-
- attach_workspace:
25-
at: ~/workdir
24+
- checkout
2625
- run:
2726
name: Get version
2827
command: |
@@ -31,6 +30,7 @@ jobs:
3130
wget https://github.com/crystal-lang/crystal/releases/download/0.30.1/crystal_0.30.1-1_amd64.deb
3231
sudo dpkg -i crystal_0.30.1-1_amd64.deb
3332
sudo make clean test build
33+
./bin/git-version > .version
3434
- persist_to_workspace:
3535
root: ~/workdir
3636
paths:
@@ -102,13 +102,7 @@ workflows:
102102
version: 2
103103
publish:
104104
jobs:
105-
- codacy/checkout_and_version
106-
- compile:
107-
requires:
108-
- codacy/checkout_and_version
109-
filters:
110-
branches:
111-
ignore: master
105+
- compile
112106
- build_docker:
113107
requires:
114108
- compile

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Versions are incremented since last tag. The patch version is incremented by def
5858

5959
On branches other than master and `dev` the version is a variation of the latest common tag with master, and has the following format:
6060

61-
`{MAJOR}.{MINOR}.{PATCH}-{sanitized-branch-name}.{hash}`
61+
`{MAJOR}.{MINOR}.{PATCH}-{sanitized-branch-name}.{commits-distance}.{hash}`
6262

6363
On the `dev` branch the format is following:
6464

@@ -68,7 +68,7 @@ _Example:_
6868
```
6969
---A---B---C <= Master (tag: 1.0.1) L <= Master (git-version: 1.0.2)
7070
\ /
71-
D---E---F---G---H---I---J---K <= Foo (git-version: 1.0.2-foo.5e30d83)
71+
D---E---F---G---H---I---J---K <= Foo (git-version: 1.0.2-foo.8.5e30d83)
7272
```
7373

7474
_Example2 (with dev branch):_
@@ -77,14 +77,14 @@ _Example2 (with dev branch):_
7777
\ / <= Fast-forward merges to master (same commit id)
7878
C L <= Dev (git-version: 1.0.2-SNAPSHOT.5e30d83)
7979
\ /
80-
E---F---G---H---I---J---K <= Foo (new_version: 1.0.1-foo.5e30d83)
80+
E---F---G---H---I---J---K <= Foo (new_version: 1.0.1-foo.7.5e30d83)
8181
```
8282

8383
_Example3 (with breaking message):_
8484
```
8585
---A---B---C <= Master (tag: 1.0.1) L <= Master (git-version: 2.0.0)
8686
\ /
87-
D---E---F---G---H---I---J---K <= Foo (git-version: 2.0.0-foo.5e30d83)
87+
D---E---F---G---H---I---J---K <= Foo (git-version: 2.0.0-foo.8.5e30d83)
8888
\\
8989
message: "breaking: removed api parameter"
9090
```

spec/git-version-spec.cr

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ describe GitVersion do
4242
tmp.exec %(touch file2.txt)
4343
tmp.exec %(git add file2.txt)
4444
tmp.exec %(git commit --no-gpg-sign -m "new file2.txt")
45-
4645
ensure
4746
tmp.cleanup
4847
end
@@ -66,8 +65,7 @@ describe GitVersion do
6665

6766
version = git.get_version
6867

69-
version.should eq("1.0.1-myfancybranch.#{hash}")
70-
68+
version.should eq("1.0.1-myfancybranch.1.#{hash}")
7169
ensure
7270
tmp.cleanup
7371
end
@@ -109,7 +107,6 @@ describe GitVersion do
109107
version = git.get_version
110108

111109
version.should eq("2.0.0-SNAPSHOT.#{hash}")
112-
113110
ensure
114111
tmp.cleanup
115112
end
@@ -174,7 +171,6 @@ describe GitVersion do
174171
version = git.get_version
175172

176173
version.should eq("3.1.0")
177-
178174
ensure
179175
tmp.cleanup
180176
end
@@ -203,8 +199,7 @@ describe GitVersion do
203199

204200
version = git.get_version
205201

206-
version.should eq("1.0.1-ft1111.#{hash}")
207-
202+
version.should eq("1.0.1-ft1111.2.#{hash}")
208203
ensure
209204
tmp.cleanup
210205
end
@@ -223,7 +218,6 @@ describe GitVersion do
223218
version = git.get_version
224219

225220
version.should eq("0.0.1")
226-
227221
ensure
228222
tmp.cleanup
229223
end
@@ -248,7 +242,6 @@ describe GitVersion do
248242
version = git.get_version
249243

250244
version.should eq("1.2.1")
251-
252245
ensure
253246
tmp.cleanup
254247
end
@@ -268,14 +261,14 @@ describe GitVersion do
268261
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 2")
269262
hash = git.current_commit_hash
270263
version = git.get_version
271-
version.should eq("1.1.0-feature1.#{hash}")
264+
version.should eq("1.1.0-feature1.1.#{hash}")
272265

273266
tmp.exec %(git checkout master)
274267
tmp.exec %(git checkout -b feature2)
275268
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 3")
276269
hash = git.current_commit_hash
277270
version = git.get_version
278-
version.should eq("2.0.0-feature2.#{hash}")
271+
version.should eq("2.0.0-feature2.1.#{hash}")
279272

280273
tmp.exec %(git checkout master)
281274
tmp.exec %(git merge feature2)
@@ -287,7 +280,7 @@ describe GitVersion do
287280
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "4")
288281
hash = git.current_commit_hash
289282
version = git.get_version
290-
version.should eq("2.0.1-feature3.#{hash}")
283+
version.should eq("2.0.1-feature3.1.#{hash}")
291284

292285
tmp.exec %(git checkout master)
293286
tmp.exec %(git merge --no-gpg-sign feature1)
@@ -299,7 +292,6 @@ describe GitVersion do
299292
version = git.get_version
300293
version.should eq("2.1.1")
301294
tmp.exec %(git tag "2.1.1")
302-
303295
ensure
304296
tmp.cleanup
305297
end
@@ -330,7 +322,6 @@ describe GitVersion do
330322
tmp.exec %(git rebase dev)
331323
version = git.get_version
332324
version.should eq("1.0.1")
333-
334325
ensure
335326
tmp.cleanup
336327
end
@@ -363,7 +354,6 @@ describe GitVersion do
363354

364355
version = git.get_version
365356
version.should eq("2.0.0")
366-
367357
ensure
368358
tmp.cleanup
369359
end
@@ -392,7 +382,6 @@ describe GitVersion do
392382

393383
version = git.get_version
394384
version.should eq("2.0.0")
395-
396385
ensure
397386
tmp.cleanup
398387
end
@@ -421,7 +410,6 @@ describe GitVersion do
421410

422411
version = git.get_version
423412
version.should eq("1.0.1")
424-
425413
ensure
426414
tmp.cleanup
427415
end
@@ -441,7 +429,6 @@ describe GitVersion do
441429

442430
version = git.get_version
443431
version.should eq("1.0.0")
444-
445432
ensure
446433
tmp.cleanup
447434
end
@@ -461,7 +448,6 @@ describe GitVersion do
461448

462449
version = git.get_version
463450
version.should eq("1.0.0")
464-
465451
ensure
466452
tmp.cleanup
467453
end
@@ -479,7 +465,6 @@ describe GitVersion do
479465

480466
version = git.get_version
481467
version.should eq("1.0.0")
482-
483468
ensure
484469
tmp.cleanup
485470
end
@@ -499,8 +484,7 @@ describe GitVersion do
499484

500485
version = git.get_version
501486
hash = git.current_commit_hash
502-
version.should eq("1.0.0-v1.#{hash}")
503-
487+
version.should eq("1.0.0-v1.0.#{hash}")
504488
ensure
505489
tmp.cleanup
506490
end
@@ -519,7 +503,6 @@ describe GitVersion do
519503

520504
version = git.get_version
521505
version.should eq("v1.0.1")
522-
523506
ensure
524507
tmp.cleanup
525508
end
@@ -538,7 +521,6 @@ describe GitVersion do
538521

539522
version = git.get_version
540523
version.should eq("v0.0.1")
541-
542524
ensure
543525
tmp.cleanup
544526
end
@@ -557,7 +539,27 @@ describe GitVersion do
557539

558540
version = git.get_version
559541
version.should eq("v0.0.1")
542+
ensure
543+
tmp.cleanup
544+
end
545+
end
546+
547+
it "should count the commits distance" do
548+
tmp = InTmp.new
549+
550+
begin
551+
git = GitVersion::Git.new("dev", "master", tmp.@tmpdir)
560552

553+
tmp.exec %(git init)
554+
tmp.exec %(git checkout -b master)
555+
tmp.exec %(git checkout -b v1)
556+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 1")
557+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 2")
558+
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 3")
559+
560+
version = git.get_version
561+
hash = git.current_commit_hash
562+
version.should eq("1.0.0-v1.0.#{hash}")
561563
ensure
562564
tmp.cleanup
563565
end

src/git-version.cr

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ 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]
75+
rescue
76+
return 0
77+
end
78+
7379
def get_bumps(latest)
7480
latest_exists = (exec "git tag -l #{latest}")
7581
if latest_exists.any?
@@ -164,7 +170,8 @@ module GitVersion
164170
nil
165171
)
166172
else
167-
prerelease = [cb.downcase.gsub(/[^a-zA-Z0-9]/, ""), current_commit_hash()] of String | Int32
173+
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
168175
latest_version =
169176
SemanticVersion.new(
170177
latest_version.major,

0 commit comments

Comments
 (0)