Validate if commit exists

后端 未结 6 1049
走了就别回头了
走了就别回头了 2020-12-10 02:45

How to validate whether the commit with given sha exists in current branch?

There are many ways to parse outputs, but I need optimal way which returns boolean (for u

6条回答
  •  甜味超标
    2020-12-10 03:14

    You can look at the output of

    git rev-list HEAD..$sha
    

    If this command fails because the sha1 does not exist at all in the repository, the exit code will be non-zero. If the output is empty, the commit is in the current branch, and if it is non-empty, it is not. So your script would look like

    if revlist=`git rev-list HEAD..$sha 2>/dev/null` && [ -z "$revlist" ]; then
        :
    fi
    

    In case you already know that $sha really names a commit (as a SHA1 hash or in any other way), this simplifies to

    if [ -z "`git rev-list HEAD..$sha`" ]; then
        :
    fi
    

提交回复
热议问题