-
-
Save luw2007/234a21c525677c9d17800a6313ab0d03 to your computer and use it in GitHub Desktop.
自动为git项目打版本号。保存为/usr/loca/bin/tag,chmod +x /usr/loca/bin/tag。 在git目录执行tag 即可
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# 版本号类似于 MAJOR.MINOR.PATCH 遵循 Semantic Versioning 2.0.0: | |
# MAJOR: 主版本号,不兼容的修改,如:底层重构、框架层变动 | |
# MINOR:次版本号,向前兼容的修改,如:增加新功能、代码优化 | |
# PATCH: 小版本号,修复bug | |
PREFIX=v | |
DEFAULT_VERSION=0.0.0 | |
# 获取git版本号 | |
# 注意:这里获取的版本号与分支无关 | |
VERSION=`git describe --tags $(git rev-list --tags --max-count=1)` | |
if [ -z $VERSION ]; then | |
VERSION=$DEFAULT_VERSION | |
fi | |
# 检查当前提交是否已有版本号 | |
GIT_COMMIT=`git rev-parse HEAD` | |
NEEDS_TAG=`git describe --contains $GIT_COMMIT` | |
if [ -z "$NEEDS_TAG" ]; then | |
echo "上次版本号: $VERSION" | |
else | |
echo "已打版本号: $VERSION" | |
exit 1 | |
fi | |
# 去掉当前版本的前缀'v' | |
VERSION=${VERSION##${PREFIX}} | |
# 将版本号按照'.'分隔成3部分 | |
VERSION_BITS=(${VERSION//./ }) | |
VNUM1=${VERSION_BITS[0]} | |
VNUM2=${VERSION_BITS[1]} | |
VNUM3=${VERSION_BITS[2]} | |
# 根据当前提交内容判断变动版本 | |
# MAJOR: 主版本号: '(breaking|major)' | |
# MINOR:次版本号: '(feature|minor)' | |
# PATCH: 小版本号: '\+semver:\s?(fix|patch)' | |
GIT_LAST_COMMENT=`git log -1 --pretty=%B` | |
COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MAJOR=`echo $GIT_LAST_COMMENT | egrep -c '(breaking|major)'` | |
COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MINOR=`echo $GIT_LAST_COMMENT | egrep -c '(feature|minor)'` | |
# 按照从前到后的顺序判断版本号,版本号一次只能变更一次 | |
if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MAJOR -gt 0 ]; then | |
VNUM1=$((VNUM1+1)) | |
VNUM2=0 | |
VNUM3=0 | |
else | |
if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MINOR -gt 0 ]; then | |
VNUM2=$((VNUM2+1)) | |
VNUM3=0 | |
else | |
# not major and not minor, general add patch num | |
VNUM3=$((VNUM3+1)) | |
fi | |
fi | |
# 创建新版本号 | |
NEW_TAG="${PREFIX}$VNUM1.$VNUM2.$VNUM3" | |
echo "git tag -a $NEW_TAG -m '$GIT_LAST_COMMENT'" | |
git tag "$NEW_TAG" -m "$GIT_LAST_COMMENT" | |
# 推送新版本号 | |
echo "git push origin $NEW_TAG" | |
git push origin $NEW_TAG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment