验证提交是否存在 [英] Validate if commit exists

查看:21
本文介绍了验证提交是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证当前分支中是否存在给定sha的提交?

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

解析输出的方法有很多,但我需要返回布尔值的最佳方法(用于 bash 脚本).

There are many ways to parse outputs, but I need optimal way which returns boolean (for usage in bash script).

例如

sha=$1
if [ -z `git magic --validate $sha` ]; then
  echo "Invalid commit sha: $sha"
  exit 1
fi

推荐答案

rev-list |grep 方法工作正常;有一点点开销,因为 git 必须打印出所有的 SHA1 以供 grep 看到,但这并不是什么大问题.

The rev-list | grep method works fine; there's the tiniest bit of overhead because git has to print out all the SHA1s for grep to see, but it's not really a big deal.

如果你愿意,你也可以用 git merge-base 来做 - 如果目标提交和分支的合并基础是目标提交,分支包含目标提交:

You can also do it with git merge-base if you like - if the merge base of the target commit and the branch is the target commit, the branch contains the target commit:

if [ "$(git merge-base $commit $branch)" = "$commit" ]; then
    ...
fi

无论您采用哪种方式,请注意 rev-listmerge-base 将打印出 SHA1,因此如果您正在测试包含的提交由分支或标签命名,您需要先使用 git rev-parse 将其转换为 SHA1.

Either way you do it, note that rev-list and merge-base are going to be printing out SHA1s, so if the commit you're testing for inclusion is named by a branch or tag, you'll want to use git rev-parse to turn it into an SHA1 first.

这篇关于验证提交是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆