Skip to content

Instantly share code, notes, and snippets.

@liuliangsir
Last active May 23, 2022 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuliangsir/fba96b8d178805f7935d3778e562f277 to your computer and use it in GitHub Desktop.
Save liuliangsir/fba96b8d178805f7935d3778e562f277 to your computer and use it in GitHub Desktop.
auto name tag with a specified rule
#!/usr/bin/env bash
f() {
# 获取当前仓库最新的状态,避免本地 tag 列表不是最新的
# https://stackoverflow.com/questions/8943693/can-git-operate-in-silent-mode
# https://stackoverflow.com/questions/1204190/does-git-fetch-tags-include-git-fetch
# https://stackoverflow.com/questions/16678072/can-we-set-a-git-default-to-fetch-all-tags-during-a-remote-pull
git fetch --tags --prune --prune-tags --quiet
local latestTag
# https://stackoverflow.com/questions/1404796/how-can-i-get-the-latest-tag-name-in-current-branch-in-git
# https://stackoverflow.com/questions/14273531/how-to-sort-git-tags-by-version-string-order-of-form-rc-x-y-z-w
latestTag=$(git tag -l rc* --sort=v:refname | tail -1)
if [[ "$latestTag" =~ ^(rc)([0-9]{4}[0-9]{2}[0-9]{2})_([0-9]+)(_.*)?$ ]]; then
local now
# https://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script
now=$(date '+%Y%m%d')
# 获取 tag 里面的日期
local fullDate="${BASH_REMATCH[2]}"
# 获取 tag 里面的版本号
local version="${BASH_REMATCH[3]}"
local finalDate
local finalVersion
if (( now > fullDate )); then
finalDate="$now"
finalVersion='1'
else
finalDate="$fullDate"
finalVersion="$((version + 1))"
fi
local finalTag="${BASH_REMATCH[1]}${finalDate}_${finalVersion}${BASH_REMATCH[4]}"
git tag "$finalTag"
echo "tag: $finalTag"
else
git tag "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment