Skip to content

Commit

Permalink
breaking: Add commits distance to the version in pre-releases (#30)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andreaTP authored May 12, 2020
1 parent 4cc0cbd commit 3123cdc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
12 changes: 3 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:
compile:
<<: *default_machine_job
steps:
- attach_workspace:
at: ~/workdir
- checkout
- run:
name: Get version
command: |
Expand All @@ -31,6 +30,7 @@ jobs:
wget https://github.com/crystal-lang/crystal/releases/download/0.30.1/crystal_0.30.1-1_amd64.deb
sudo dpkg -i crystal_0.30.1-1_amd64.deb
sudo make clean test build
./bin/git-version > .version
- persist_to_workspace:
root: ~/workdir
paths:
Expand Down Expand Up @@ -102,13 +102,7 @@ workflows:
version: 2
publish:
jobs:
- codacy/checkout_and_version
- compile:
requires:
- codacy/checkout_and_version
filters:
branches:
ignore: master
- compile
- build_docker:
requires:
- compile
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Versions are incremented since last tag. The patch version is incremented by def

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

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

On the `dev` branch the format is following:

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

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

_Example3 (with breaking message):_
```
---A---B---C <= Master (tag: 1.0.1) L <= Master (git-version: 2.0.0)
\ /
D---E---F---G---H---I---J---K <= Foo (git-version: 2.0.0-foo.5e30d83)
D---E---F---G---H---I---J---K <= Foo (git-version: 2.0.0-foo.8.5e30d83)
\\
message: "breaking: removed api parameter"
```
Expand Down
50 changes: 26 additions & 24 deletions spec/git-version-spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ describe GitVersion do
tmp.exec %(touch file2.txt)
tmp.exec %(git add file2.txt)
tmp.exec %(git commit --no-gpg-sign -m "new file2.txt")

ensure
tmp.cleanup
end
Expand All @@ -66,8 +65,7 @@ describe GitVersion do

version = git.get_version

version.should eq("1.0.1-myfancybranch.#{hash}")

version.should eq("1.0.1-myfancybranch.1.#{hash}")
ensure
tmp.cleanup
end
Expand Down Expand Up @@ -109,7 +107,6 @@ describe GitVersion do
version = git.get_version

version.should eq("2.0.0-SNAPSHOT.#{hash}")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -174,7 +171,6 @@ describe GitVersion do
version = git.get_version

version.should eq("3.1.0")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -203,8 +199,7 @@ describe GitVersion do

version = git.get_version

version.should eq("1.0.1-ft1111.#{hash}")

version.should eq("1.0.1-ft1111.2.#{hash}")
ensure
tmp.cleanup
end
Expand All @@ -223,7 +218,6 @@ describe GitVersion do
version = git.get_version

version.should eq("0.0.1")

ensure
tmp.cleanup
end
Expand All @@ -248,7 +242,6 @@ describe GitVersion do
version = git.get_version

version.should eq("1.2.1")

ensure
tmp.cleanup
end
Expand All @@ -268,14 +261,14 @@ describe GitVersion do
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "feature: 2")
hash = git.current_commit_hash
version = git.get_version
version.should eq("1.1.0-feature1.#{hash}")
version.should eq("1.1.0-feature1.1.#{hash}")

tmp.exec %(git checkout master)
tmp.exec %(git checkout -b feature2)
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 3")
hash = git.current_commit_hash
version = git.get_version
version.should eq("2.0.0-feature2.#{hash}")
version.should eq("2.0.0-feature2.1.#{hash}")

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

tmp.exec %(git checkout master)
tmp.exec %(git merge --no-gpg-sign feature1)
Expand All @@ -299,7 +292,6 @@ describe GitVersion do
version = git.get_version
version.should eq("2.1.1")
tmp.exec %(git tag "2.1.1")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -330,7 +322,6 @@ describe GitVersion do
tmp.exec %(git rebase dev)
version = git.get_version
version.should eq("1.0.1")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -363,7 +354,6 @@ describe GitVersion do

version = git.get_version
version.should eq("2.0.0")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -392,7 +382,6 @@ describe GitVersion do

version = git.get_version
version.should eq("2.0.0")

ensure
tmp.cleanup
end
Expand Down Expand Up @@ -421,7 +410,6 @@ describe GitVersion do

version = git.get_version
version.should eq("1.0.1")

ensure
tmp.cleanup
end
Expand All @@ -441,7 +429,6 @@ describe GitVersion do

version = git.get_version
version.should eq("1.0.0")

ensure
tmp.cleanup
end
Expand All @@ -461,7 +448,6 @@ describe GitVersion do

version = git.get_version
version.should eq("1.0.0")

ensure
tmp.cleanup
end
Expand All @@ -479,7 +465,6 @@ describe GitVersion do

version = git.get_version
version.should eq("1.0.0")

ensure
tmp.cleanup
end
Expand All @@ -499,8 +484,7 @@ describe GitVersion do

version = git.get_version
hash = git.current_commit_hash
version.should eq("1.0.0-v1.#{hash}")

version.should eq("1.0.0-v1.0.#{hash}")
ensure
tmp.cleanup
end
Expand All @@ -519,7 +503,6 @@ describe GitVersion do

version = git.get_version
version.should eq("v1.0.1")

ensure
tmp.cleanup
end
Expand All @@ -538,7 +521,6 @@ describe GitVersion do

version = git.get_version
version.should eq("v0.0.1")

ensure
tmp.cleanup
end
Expand All @@ -557,7 +539,27 @@ describe GitVersion do

version = git.get_version
version.should eq("v0.0.1")
ensure
tmp.cleanup
end
end

it "should count the commits distance" do
tmp = InTmp.new

begin
git = GitVersion::Git.new("dev", "master", tmp.@tmpdir)

tmp.exec %(git init)
tmp.exec %(git checkout -b master)
tmp.exec %(git checkout -b v1)
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 1")
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 2")
tmp.exec %(git commit --no-gpg-sign --allow-empty -m "breaking: 3")

version = git.get_version
hash = git.current_commit_hash
version.should eq("1.0.0-v1.0.#{hash}")
ensure
tmp.cleanup
end
Expand Down
9 changes: 8 additions & 1 deletion src/git-version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ module GitVersion
return (exec cmd)[0].rjust(7, '0')
end

def commits_distance(latest_tagged_version)
return (exec "git rev-list --count HEAD ^#{latest_tagged_version}")[0]
rescue
return 0
end

def get_bumps(latest)
latest_exists = (exec "git tag -l #{latest}")
if latest_exists.any?
Expand Down Expand Up @@ -164,7 +170,8 @@ module GitVersion
nil
)
else
prerelease = [cb.downcase.gsub(/[^a-zA-Z0-9]/, ""), current_commit_hash()] of String | Int32
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")
prerelease = [branch_sanitized_name, commits_distance(latest_tagged_version), current_commit_hash()] of String | Int32
latest_version =
SemanticVersion.new(
latest_version.major,
Expand Down

0 comments on commit 3123cdc

Please sign in to comment.