Deriving application build version from `git describe` - how to get a relatively straightforward string?

和自甴很熟 提交于 2019-11-27 11:43:37

The thing you have you to understand about git is that branches are essentially merely commit bookmarks. The fact that you were on the foo branch when you made the 0deadbeef commit is immaterial to the commit itself; the branch is not part of its identity.

(Mercurial bakes the branch name into the commit. In a variety of ways, this is inferior, as Dustin Sallings explains.)

Even assuming that git describe would just use the currently checked out branch – if you have a mergy history, there could be multiple paths leading to the same most recent tagged commit that git describe would use. So there isn’t even necessarily any one branch.

Another note: you may object that even if “3rd commit from tag X” is ambiguous in the general case, git describe could just look at the graph and figure out whether it is ambiguous and if not, leave out the hash. However, there is nothing stopping anyone starting a branch on top of that tag at a later time – so then your describe string would become ambiguous retrospectively.

Bottom line is that the only unambiguous identifier of a commit is its hash. So that must be in there. What git describe does is add some redundant (and in case of the commit number, ambiguous) information that makes the description more useful to the kind of spatial/relational comprehension that humans orient themselves with, within the confines of the Git model.

Here is what I use:

echo "`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"

It produces something like:

master-6de772e

As noted by Aristotle, in actuality the SHA-1 by itself is all that is necessary and sufficient to provide an unambiguous build tag, as well as full information regarding the developmental historical context. Everything else is redundant, in the sense that any information they provide can be figured out or derived from the SHA-1. However, humans might like the supplementary contextual information of the actual branch immediately evident as well (or, at least, this human does), and hence the embedding of the branch name into the label. For this reason also (i.e. immediate human parsing of the information), most of my projects also use a longer build identity 'description' that includes the date and time of the commit that the build was based on in addition to the build identity 'label' given above.

git describe --long would always output version number like this: v1.2-10-gdeadbee, which means 10th commit since annotated tag 'v1.2' that points at commit with shortened SHA-1 'deadbee'. So all you have to do is to tag branch start (branching point of a branch) e.g. <branch>-start.

The abbreviated commit SHA-1 hash is required to distinguish between ambiguous situations, because "3rd commit since tag 'x'" (for example) does not uniquely distinguish a commit; there can be more than one commit that fits mentioned description in the presence of nonlinear, branchy development. For example in the situation shown on the ASCII-art diagram below both commits marked with * fits "3rd commit since tag 'x'" description.

          /-.---*---.-\                   
         /             \                  
.---x---.---.---*---.---M---.    <--- branch

Note that in "merged in" case as shown above you can't use branch name to distinguish between those two commits with the same description.

So what you have to do would be to take git describe --long output (the --long option is here to avoid ambiguities with parsing, see git describe manpage), parse it, and add current branch info (from e.g. git symbolic-ref HEAD, not from pasing git branch output) yourself.

Official releases should have a tag with their version number. In this case I suggest the following approach:

  1. If the current commit has a tag, use that tag
  2. If no tag is available, use the branch name and the SHA1-key

This single command should work:

git describe --exact-match 2> /dev/null || echo "`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!