Shell脚本-自动切换版本git

b5lpy0ml  于 2023-02-17  发布在  Git
关注(0)|答案(2)|浏览(153)

我有以下命令,我想用一个命令通过“makefile”执行,我该怎么做?

1. git tag -a v0.0.1 -m "new release"
2. git push origin v0.0.1

现在,我已经创建了一些用于启动的内容

git:
    git add .
    git commit -m "$m"
    git push origin master

现在我有两个问题,如何解决版本问题,例如,这里是v0.0.1,但对于每个新版本,我都需要对其进行升级,比如第一个版本是v0.0.1,下一个版本应该是v0.0.2,是否可以通过某种方式自动完成(可能有一些计数器...)?如果不能,可能将其作为参数添加到一个命令中

  1. git标签-av0.0.1-m“新版本”
  2. git推送原点v0.0.1

更新

有一个答案看起来很好与以下

git describe --tags --abbrev=0 | awk -F. '{$NF+=1; OFS="."; print $0}'

但是我应该怎么把它和结合起来呢?

  1. git标签-av0.0.1-m“新版本”
  2. git推送原点v0.0.1

更新2

当我按照Kevin回答中的建议尝试以下操作时,我遇到错误:
.电话:git
版本= git describe --tags --abbrev=0 | awk -F. '{$NF+=1; OFS="."; print $0}'

git:
    git add .
    git commit -m "$m"
    git push origin master
    git tag -a $(VERSION) -m "new release"
    git push origin $(VERSION)

错误为:fatal: tag 'ERSION' already exists似乎凸块不起作用,它以某种方式从版本中删除了v

我做了另一个检查,删除了repo,并从零开始手动为第一个版本0.0.1现在我没有改变一个文件,并运行脚本,版本现在应该是0.0.2,如果它成功,但没有我得到错误fatal: tag 'v0.0.1' already exists,这解释了凸块不工作,任何想法的原因?
我猜是和这段代码'{$NF+=1;OFS=".";打印$0}'

gdx19jrr

gdx19jrr1#

使用最后一个推送的标记,您可以自动递增版本号:

git describe --tags --abbrev=0 | awk -F. '{OFS="."; $NF+=1; print $0}'

请记住,您将其存储在变量中,并将其用于tagpush

VERSION=`git describe --tags --abbrev=0 | awk -F. '{OFS="."; $NF+=1; print $0}'`
git tag -a $VERSION -m "new release"
git push origin $VERSION

解释:

git describe-显示提交中可访问的最近标记
--tags-允许匹配轻量(无注解)标记。
--abbrev=0-将抑制长格式,仅显示最近的标记。
awk -F.-使用.“”作为分隔符的处理模式

  • *'{OFS=".";$NF+=1;打印$0}'**-仅递增最后一个数字并与“”联接。

makefile

.PHONY: git

git:
    $(eval VERSION=$(shell git describe --tags --abbrev=0 | awk -F. '{OFS="."; $$NF+=1; print $0}'))
    git add .
    git commit -m "$m"
    git push origin master
    git tag -a $(VERSION) -m "new release"
    git push origin $(VERSION)
7vhp5slm

7vhp5slm2#

根据KevinSandow的回答,在下面找到一个更详细的shell脚本
最新版本:https://gist.github.com/acucchieri/69bc649abde55315fe74bb68be82e0c8

#!/bin/bash

# The bump is performed only on the "main" or "master" branch unless a branch is specified with the -b argument
# Example :
#    bump-version -b staging

# Check that HEAD is not detached
DETACHED=`git branch --show-current | wc -l`
if [ $DETACHED -eq 0 ]; then
    echo "HEAD is detached. Please fix it before."
    exit 1
fi

BUILD_BRANCH=''

# Check if a branch was passed as an argument
while getopts "b:" option
do
    case $option in
        b)
            BUILD_BRANCH=$OPTARG
            ;;
    esac
done

# Determines the build branch ("main" or "master") if no branch was passed as an argument
if [ -z "$BUILD_BRANCH" ]; then
    if [ `git rev-parse --verify main 2>/dev/null` ]
    then
        BUILD_BRANCH='main'
    else
        if [ `git rev-parse --verify master 2>/dev/null` ]
        then
            BUILD_BRANCH='master'
        else
            echo "Unable to find \"main\" or \"master\" branch. Please use -b arg"
            exit 1
        fi
    fi
fi

# Check that local is not behind origin
git fetch 2>/dev/null
if [ "$(git rev-list --count HEAD..$BUILD_BRANCH)" -gt 0 ]; then
    echo "Local is behind Origin. Please run git pull first."
    exit 1
fi

# Guess the next tag
if [[ "$(git tag --merged $BUILD_BRANCH)" ]]; then
    # increment the last tag
    NEXT_TAG=`git describe --tags --abbrev=0 | awk -F. '{OFS="."; $NF+=1; print $0}'`
else
    # there is no tag yet
    NEXT_TAG='0.1.0'
fi

# Ask for next tag
SEMVER_REGEX="^[vV]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
SEMVER_VALID=false
while [[ $SEMVER_VALID == false ]]
do
    read -p "Next tag [$NEXT_TAG]: " TAG
    # empty answer
    if [ -z "$TAG" ]; then
        # set guessed tag
        TAG=$NEXT_TAG
    fi
    # semver validation
    if [[ "$TAG" =~ $SEMVER_REGEX ]]; then
        SEMVER_VALID=true
    else
        echo 'Tag must match the semver scheme X.Y.Z[-PRERELEASE][+BUILD]. See https://semver.org/'
    fi
done

# Release message
if [[ $TAG =~ ^[v] ]]; then
    # remove "v" letter
    MESSAGE="release ${TAG:1:${#TAG}-1}"
else
    MESSAGE="release $TAG"
fi

# Checks if a commit is needed
if [ -n "$(git status --porcelain)" ]; then
    git add -A .
    git commit -am "bump version"
fi

git tag -a "$TAG" -m "$MESSAGE"

# Ask to push new release
read -p "Push new release (Y/n)? [Y]:" -r
REPLY=${REPLY:-Y}
if [[ $REPLY =~ ^[YyOo]$  ]]; then
  git push origin $BUILD_BRANCH --follow-tags
fi

exit 0

相关问题