Reputation: 91554
we have a build system that uses the svn ID as an imput to a VM builder appliance that required a five digit number. When I build from git I have been faking this by counting the number of commits in the git repo. This only sort-of-works :-/ I'm tyring to figure out:
Upvotes: 2
Views: 1185
Reputation: 3840
You want to use git describe as said earlier, here is my rake task that prints out incrementing semver compliant version numbers automatically:
task :version do
git_describe = `git describe --dirty --tags --match 'v*'`
version, since, sha, dirty = git_describe.strip.split("-")
major, minor, patch = version.split(".")
version = "#{major}.#{minor}.#{patch}"
if sha
patch = String(Integer(patch) + 1)
version = "#{version}pre#{since}-#{sha[1..sha.length]}"
end
if [since, dirty].include?("dirty")
version = "#{version}-dirty"
end
puts version
end
Used like this:
$> rake version
v0.9.8pre32-fcb661d
Upvotes: 2
Reputation: 117018
You're looking for git describe:
The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.
$ git describe master
v1.10-5-g4efc7e1
Upvotes: 2
Reputation: 5372
In git, every commit generates a unique SHA1 hash id. You can see the id for each commit when running git log
. If you want a 5 digit number for the most recent commit, you could do something like git log --pretty=oneline --abbrev-commit --abbrev=5 -1
. For one of my repos the output looks like this:
$ git log --pretty=oneline --abbrev-commit --abbrev=5 -1
3b405... fixed css for page title.
You can experiment with the other options to git log
to customize the format if needed. Of course, if the repository has enough commits, there's always the possibility that 5 digits will not be enough to guarantee uniqueness, but for small enough projects it may do.
Upvotes: 0