验证是否存在提交 [英] Validate if commit exists
问题描述
解析输出的方法很多,但我需要返回布尔值的最优方法(用于用于bash脚本)。
例如
sha = $ 1
如果[-z`git magic --validate $ sha`];然后
echoInvalid commit sha:$ sha
exit 1
fi
rev-list | grep
方法正常工作;因为git必须打印出 grep
的所有SHA1来查看,但这并不是什么大不了的事。
如果你愿意,你也可以用 git merge-base
来完成 - 如果目标提交和分支的合并基础是目标提交,则分支包含目标提交:
$ $ p $
if [$(git merge-base $ commit $ branch)=$ commit ]。然后
...
fi
无论您如何操作,请注意 rev-list
和 merge-base
将会打印出SHA1,所以如果你正在测试的提交包含以分支或标签命名,您需要使用 git rev-parse
将其转换为SHA1。
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 usage in bash script).
e.g.
sha=$1
if [ -z `git magic --validate $sha` ]; then
echo "Invalid commit sha: $sha"
exit 1
fi
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.
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
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屋!