tim*_*c22 40
我写这个是为了帮助逐步更新标签,例如1.0.1到1.0.2等
#!/bin/bash
#get highest tag number
VERSION=`git describe --abbrev=0 --tags`
#replace . with space so can split into an array
VERSION_BITS=(${VERSION//./ })
#get number parts and increase last one by 1
VNUM1=${VERSION_BITS[0]}
VNUM2=${VERSION_BITS[1]}
VNUM3=${VERSION_BITS[2]}
VNUM3=$((VNUM3+1))
#create new tag
NEW_TAG="$VNUM1.$VNUM2.$VNUM3"
echo "Updating $VERSION to $NEW_TAG"
#get current hash and see if it already has a tag
GIT_COMMIT=`git rev-parse HEAD`
NEEDS_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null`
#only tag if no tag already
if [ -z "$NEEDS_TAG" ]; then
git tag $NEW_TAG
echo "Tagged with $NEW_TAG"
git push --tags
else
echo "Already a tag on this commit"
fi
Run Code Online (Sandbox Code Playgroud)
我在 gitlab pipelines 中使用这个函数,所以我希望这对你有帮助
#!/bin/bash
bump_version_tag(){
if [ -z "$(git ls-remote --tags origin)" ]; then
local new_tag="v1.0.0"
else
local current_tag=$(git ls-remote --tags origin | cut -f 3 -d '/')
IFS='.' read -r -a array <<< "${current_tag:1}"
local major=${array[0]}
local minor=${array[1]}
local patch=${array[2]}
if [ "$minor" == 999 ] && [ "$patch" == 999 ]; then
((major++))
minor=0
patch=0
elif [ "$patch" == 999 ]; then
((minor++))
patch=0
else
((patch++))
fi
new_tag="v${major}.${minor}.${patch}"
fi
echo $new_tag
}
git tag $bump_version_tag -a "autogenerated tag"
git push origin $NEW_TAG
Run Code Online (Sandbox Code Playgroud)
v如果在标签开头使用 use ${current_tag:1},否则使用${current_tag}. 另外,如果您只想增加标签的补丁部分,请使用它,
#!/bin/bash
RES=$(git show-ref --tags)
if [ -z "$RES" ]; then
NEW_TAG=v1.0.0
else
NEW_TAG=$(git describe --tags --abbrev=0 | awk -F. '{OFS="."; $NF+=1; print $0}')
fi
git tag $NEW_TAG -a "autogenerated tag"
git push origin $NEW_TAG
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您需要Posix版本,则与上述答案几乎相同
#!/bin/sh
#Get the highest tag number
VERSION=`git describe --abbrev=0 --tags`
VERSION=${VERSION:-'0.0.0'}
#Get number parts
MAJOR="${VERSION%%.*}"; VERSION="${VERSION#*.}"
MINOR="${VERSION%%.*}"; VERSION="${VERSION#*.}"
PATCH="${VERSION%%.*}"; VERSION="${VERSION#*.}"
#Increase version
PATCH=$((PATCH+1))
#Get current hash and see if it already has a tag
GIT_COMMIT=`git rev-parse HEAD`
NEEDS_TAG=`git describe --contains $GIT_COMMIT`
#Create new tag
NEW_TAG="$MAJOR.$MINOR.$PATCH"
echo "Updating to $NEW_TAG"
#Only tag if no tag already (would be better if the git describe command above could have a silent option)
if [ -z "$NEEDS_TAG" ]; then
echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "
git tag $NEW_TAG
else
echo "Already a tag on this commit"
fi
Run Code Online (Sandbox Code Playgroud)
基于 timhc22 答案,但稍作修改以处理初始标记,并在提交时不存在标记的情况下使输出错误静音
#!/bin/bash
#get highest tag number
VERSION=`git describe --abbrev=0 --tags 2>/dev/null`
if [ -z $VERSION ];then
NEW_TAG="1.0.0"
echo "No tag present."
echo "Creating tag: $NEW_TAG"
git tag $NEW_TAG
git push --tags
echo "Tag created and pushed: $NEW_TAG"
exit 0;
fi
#replace . with space so can split into an array
VERSION_BITS=(${VERSION//./ })
#get number parts and increase last one by 1
VNUM1=${VERSION_BITS[0]}
VNUM2=${VERSION_BITS[1]}
VNUM3=${VERSION_BITS[2]}
VNUM3=$((VNUM3+1))
#create new tag
NEW_TAG="${VNUM1}.${VNUM2}.${VNUM3}"
#get current hash and see if it already has a tag
GIT_COMMIT=`git rev-parse HEAD`
CURRENT_COMMIT_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null`
#only tag if no tag already (would be better if the git describe command above could have a silent option)
if [ -z "$CURRENT_COMMIT_TAG" ]; then
echo "Updating $VERSION to $NEW_TAG"
git tag $NEW_TAG
git push --tags
echo "Tag created and pushed: $NEW_TAG"
else
echo "This commit is already tagged as: $CURRENT_COMMIT_TAG"
fi
Run Code Online (Sandbox Code Playgroud)