是否可以使用 git / github 添加版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6920548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to add a version number using git / github
提问by Senica Gonzalez
I'm using Github, and realized they have a nice little api for accessing repo information like commits, who did it, etc.
我正在使用 Github,并意识到他们有一个很好的小 api 来访问 repo 信息,如提交、谁做的等等。
This would be a great way to show previous versions of the project on an external site, but I was wondering if there is a known way to add a Version Number to the master commit?
这将是在外部站点上显示项目以前版本的好方法,但我想知道是否有一种已知的方法可以将版本号添加到主提交?
So the version number would either automatically increase with each master commit or I can manually set it.
所以版本号会随着每个主提交自动增加,或者我可以手动设置它。
I know I can add it in the notes, but I'm not familiar if there is a way to separate it.
我知道我可以在笔记中添加它,但我不熟悉是否有办法将它分开。
采纳答案by Nathan Fox
You can use a tag to set a version number. You can read about the tag command on the git tag man page. At work I setup our build server to automatically increment a build version number which is then applied using a tag. I think this will meet your needs?
您可以使用标签来设置版本号。您可以在 git tag 手册页上阅读有关 tag 命令的信息。在工作中,我将构建服务器设置为自动增加构建版本号,然后使用标签应用该版本号。我认为这将满足您的需求?
回答by Bill Door
There are two kinds tags to consider, a build number and a version number. A version number can be applied as a tag by a person when the product ships. This tag is historical and identifies significant events (e.g. shipping the product).
有两种标签需要考虑,内部版本号和版本号。当产品发货时,版本号可以被人用作标签。该标签是历史标签,用于标识重要事件(例如运送产品)。
The build number is useful for identifying which build you are on relative to some starting point. The combination of git-tag and git-describe provide a nice means of generating a build number that can be embedded into a build. git-describe can locate a specific previous tag with a glob pattern. The results of git describe will be formatted as:
内部版本号对于识别您相对于某个起点所在的版本非常有用。git-tag 和 git-describe 的组合提供了一种很好的方法来生成可以嵌入到构建中的构建号。git-describe 可以使用 glob 模式定位特定的先前标签。git describe 的结果将被格式化为:
tagname-[0-9]+-g[0-9a-f]+
Where the first pattern is the number of commits from the tag and the second pattern is the hash of the current commit. This can be nicely formatted into a build number. Including the hash (at least the first 7 characters) makes it simple to identify the specific commit associated with the build.
其中第一个模式是来自标签的提交次数,第二个模式是当前提交的哈希值。这可以很好地格式化为内部版本号。包含散列(至少前 7 个字符)可以轻松识别与构建关联的特定提交。
For example, git describe could return release-2.2-42-gd788e0e
. This could be formatted to become release-2.2 build 42 (d788e0e)
.
例如, git describe 可以返回release-2.2-42-gd788e0e
. 这可以格式化为release-2.2 build 42 (d788e0e)
.
回答by Wissam Youssef
As previously said, check the git-tag command and you can combine it with a hook so it automatically updates upon doing a certain thing for example pushing out master.
如前所述,检查 git-tag 命令,你可以将它与一个钩子结合起来,这样它就会在做某件事时自动更新,例如推出 master。
check out http://git-scm.com/book/en/Customizing-Git-Git-Hooks
回答by Sam Toliman
I'm using this:
我正在使用这个:
function git_func {
GITBIN=/usr/bin/git
if [[ == "commit" ]] && [[ "$#" -ne 1 ]]
then
GIT_VERSION=`$GITBIN rev-list HEAD | wc -l`
let GIT_VERSION+=1
perl -e "s/(\d+\.\d+\.)\d+/${1}$GIT_VERSION/;" -pi.save config.json
rm config.json.save
fi
GITCMD="$GITBIN "
for var in "$@"
do
GITCMD="$GITCMD \"$var\""
done
eval $GITCMD
}
alias git='git_func'
config.json
contains this:
config.json
包含这个:
"version": "0.1.44"