用于验证 Git 标签或提交是否存在并已推送到远程存储库的 Bash/Shell 脚本函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3418674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash/Shell Script Function to Verify Git Tag or Commit Exists and Has Been Pushed to Remote Repository
提问by Freddie
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 repowhich 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: \"\" ...."
if git show-ref --tags --quiet --verify -- "refs/tags/"
then
log_success "Git tag \"\" verified...."
GIT_TAG_OR_REF=
return 0
elif git rev-list >/dev/null 2>&1
then
log_success "Git commit \"\" verified...."
GIT_TAG_OR_REF=
return 0
else
log_error "\"\" 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
相关: 列出尚未推送到源的 Git 提交
采纳答案by Cascabel
Checking whether a remote has a given tag is pretty simple - you should just need to parse the output of git ls-remote --tagsto see if it contains your tag.
检查遥控器是否具有给定的标签非常简单 - 您只需要解析 的输出git ls-remote --tags以查看它是否包含您的标签。
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.
检查给定的提交是否存在有点棘手。一切都是基于引用的。你知道它应该可以从哪个 ref 访问吗?如果你这样做了,你可能应该只获取那个 ref 并在本地检查提交是否是它的祖先。也就是说,从 origin 获取 master 并查看提交是否在 origin/master 上。
You could also try using git push -nto 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.
您还可以尝试使用git push -n将提交推送到该分支的试运行,看看会发生什么 - 如果它是空操作,则提交已经在该分支上。
If you don't know what branch it should be on... you'll probably just have to fetch and check them all.
如果你不知道它应该在哪个分支上......你可能只需要获取并检查它们。
回答by Freddie
I got this to work - what do you think?
我让这个工作 - 你怎么看?
verify_git_ref() {
log "Verifying git tag or commit: \"\" ...."
if git branch -r --contains $(git rev-parse ) | grep origin>/dev/null 2>&1
then
log_success "Git tag or commit \"\" verified...."
GIT_TAG_OR_REF=
return 0
else
log_error "\"\" 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
}

