deriving version numbers from a git repository

2019-05-19 06:55发布

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:

  • how can I get a unique 5 digit number from the git repo.

3条回答
劳资没心,怎么记你
2楼-- · 2019-05-19 07:36

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
查看更多
叛逆
3楼-- · 2019-05-19 07:37

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-05-19 07:54

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.

查看更多
登录 后发表回答