7

I wanted to get this question out there to see if I'm doing this right. The following script works except for checking to see if the commit has been pushed to a remote repo which I can't find the right command for:

#!/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
    }

Related: List Git commits not pushed to the origin yet

2 Answers 2

7

Checking whether a remote has a given tag is pretty simple - you should just need to parse the output of git ls-remote --tags to see if it contains your tag.

Checking if a given commit is there is a little trickier. Everything is ref-based. Do you know what ref it should be reachable from? If you do, you should probably just fetch that ref and check locally if the commit is an ancestor of it. That is, fetch master from origin and see if the commit's on origin/master.

You could also try using git push -n to do a dry run of pushing the commit to that branch, and see what happens - if it's a no-op, the commit's already on the branch.

If you don't know what branch it should be on... you'll probably just have to fetch and check them all.

3

I got this to work - what do you think?

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
}
2
  • 1
    The problem with this is that it doesn't actually inspect the remote - it only inspects what you know about the remote (the remote branches, which are stored locally). This will work in most cases, since if you push to the remote, your remote branches will be updated. But if the remote pulls from you, you'll be out of luck - unless you fetch first. (You also don't need the git rev-parse, I don't think.)
    – Cascabel
    Aug 6, 2010 at 3:16
  • I do a git pull before running this command - but you're right and I think you're answer is more inline with a generic answer to the question I posed and I'll mark yours as the actual answer.
    – Freddie
    Aug 9, 2010 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.