Bash / Shell脚本函数验证Git标记或提交存在并已被推送到远程存储库

时间:2010-08-05 19:58:32

标签: git bash

我想在那里得到这个问题,看看我是否正确行事。以下脚本工作,除了检查提交是否已被推送到远程仓库,我无法找到正确的命令:

#!/bin/bash
set -e  # fail on first error    
verify_git_ref() {
        log "Verifying git tag or commit: \"$1\" ...."
        if git show-ref --tags --quiet --verify -- "refs/tags/$1"
        then
            log_success "Git tag \"$1\" verified...."
            GIT_TAG_OR_REF=$1
            return 0
        elif git rev-list $1>/dev/null 2>&1
        then
            log_success "Git commit \"$1\" verified...."
            GIT_TAG_OR_REF=$1
            return 0
        else
            log_error "\"$1\" is not a valid tag or commit, you must use a valid tag or commit in order for this script to continue"
            return 1
        fi
    }

相关: List Git commits not pushed to the origin yet

2 个答案:

答案 0 :(得分:6)

检查遥控器是否具有给定标签非常简单 - 您只需要解析git ls-remote --tags的输出以查看它是否包含您的标签。

检查给定的提交是否有点棘手。一切都是以ref为基础的。你知道它应该可以从哪个参考?如果你这样做,你应该只是获取那个ref并在本地检查提交是否是它的祖先。也就是说,从origin获取master并查看commit是on origin / master。

您也可以尝试使用git push -n将提交推送到该分支,看看会发生什么 - 如果它是无操作,则提交已经在分支上。

如果你不知道它应该是什么分支...你可能只需要获取并检查它们。

答案 1 :(得分:3)

我让这个工作 - 你觉得怎么样?

verify_git_ref() {
    log "Verifying git tag or commit: \"$1\" ...."
    if git branch -r --contains $(git rev-parse $1) | grep origin>/dev/null 2>&1
    then
        log_success "Git tag or commit \"$1\" verified...."
        GIT_TAG_OR_REF=$1
        return 0
    else
        log_error "\"$1\" is not a valid tag or commit that has been pushed, you must use a valid tag or commit in order for this script to continue"
        return 1
    fi
}