简体   繁体   English

从git仓库获取版本号

[英]deriving version numbers from a git repository

we have a build system that uses the svn ID as an imput to a VM builder appliance that required a five digit number. 我们有一个构建系统,该构建系统使用svn ID作为需要五位数数字的VM Builder设备的输入。 When I build from git I have been faking this by counting the number of commits in the git repo. 当我从git编译时,我一直在通过计算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. 我怎样才能从git repo中获得一个唯一的5位数字。

You're looking for git describe : 您正在寻找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

You want to use git describe as said earlier, here is my rake task that prints out incrementing semver compliant version numbers automatically: 您要像之前所说的那样使用git describe ,这是我的rake任务,该任务自动打印出符合semver的递增版本号

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

In git, every commit generates a unique SHA1 hash id. 在git中,每次提交都会生成一个唯一的SHA1哈希ID。 You can see the id for each commit when running git log . 您可以在运行git log时看到每个提交的ID。 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 . 如果您希望最近一次提交使用5位数字,则可以执行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. 您可以尝试使用其他选项来生成git log以自定义格式。 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. 当然,如果存储库具有足够的提交,则总有可能5位数字不足以保证唯一性,但是对于足够小的项目,它可能会做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM