给某个提交加标签
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$ git branch -av
* master 227df08 mark 0.1.18
remotes/origin/master 227df08 mark 0.1.18
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$ git log -2
commit 227df084b8954be37f6cff78a4dc325a14c1c151
Author: idonans <[email protected]>
Date: Wed Jul 6 11:11:51 2016 +0800
mark 0.1.18
commit 062570d59213b91248825b81d24261c39f98fb15
Author: idonans <[email protected]>
Date: Wed Jul 6 10:57:21 2016 +0800
add SystemUtil method addToMediaStore
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$ git tag -a 0.2 227df084b8954be37f6cff78a4dc325a14c1c151 -m "add tag 0.2"
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$ git tag
0.2
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$
将标签信息提交到服务器
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$ git push origin master --tags
对象计数中: 1, 完成.
写入对象中: 100% (1/1), 149 bytes | 0 bytes/s, 完成.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:idonans/acommon.git
* [new tag] 0.2 -> 0.2
pengji@pengji-ThinkPad-E450:~/idonans/workspace/acommon$
删除远程标签
git push origin :refs/tags/0.1
在 gradle 编译脚本中动态计算版本号和版本名
// 使用 git commit 总数作为 version code
def int getAutoVersionCode() {
def cmd = "git rev-list HEAD --count"
return cmd.execute().text.trim().toInteger()
}
// 使用距离最近的标签名 + "." + 距离该标签的 commit 数量 作为版本名称, 通常标签使用如 0.1, 1.0, 1.89等命名方式
def String getAutoVersionName() {
def cmd = "git describe --tags"
def tagDesc = cmd.execute().text.trim()
def pattern = "-(\\d+)-g"
def matcher = tagDesc =~ pattern
def tagName
def commitCount
if (matcher) {
tagName = tagDesc.substring(0, matcher.start())
commitCount = matcher[0][1]
} else {
tagName = tagDesc;
commitCount = "0";
}
return tagName + "." + commitCount
}
def autoVersionCode = getAutoVersionCode()
def autoVersionName = getAutoVersionName()
// 例如:
// 当前 commit 总数是 208, 距离最后一个提交最近的 tag 是 0.2, 当前位置距离该 tag 有 2 个 commit
// 那么:
// autoVersionCode = 208
// autoVersionName = 0.2.2