Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash/Shell Script Function to Verify Git Tag or Commit Exists and Has Been Pushed to Remote Repository

Tags:

git

bash

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

like image 682
Freddie Avatar asked Aug 05 '10 19:08

Freddie


People also ask

How do you check if there are new commits in git?

The most basic and powerful tool to do this is the git log command. By default, with no arguments, git log lists the commits made in that repository in reverse chronological order; that is, the most recent commits show up first.

What is the function of git push in git?

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch , but whereas fetching imports commits to local branches, pushing exports commits to remote branches.

Are tags pushed git?

Sharing tags is similar to pushing branches. By default, git push will not push tags. Tags have to be explicitly passed to git push .


2 Answers

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.

like image 148
Cascabel Avatar answered Sep 21 '22 05:09

Cascabel


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
}
like image 27
Freddie Avatar answered Sep 19 '22 05:09

Freddie