如何通过 SHA-1 检查提交是否存在于 Git 存储库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18515488/
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
How to check if the commit exists in a Git repository by its SHA-1
提问by skovalyov
In a similar topic Validate if commit existsthey recommend:
在类似的主题验证是否存在提交中,他们建议:
git rev-list HEAD..$sha
If it exits without error code than the commit exists.
如果它在没有错误代码的情况下退出,则提交存在。
But is it efficient enough just for validation?
但是仅用于验证就足够有效了吗?
I was thinking about this option:
我在考虑这个选项:
git cat-file commit $sha
Is it correct for my task and are there any other ideas?
它对我的任务是否正确,还有其他想法吗?
采纳答案by remram
You can just run git cat-file -t $sha
and check it returns "commit". You are right, you don't need to actually print the actual object for that...
您可以运行git cat-file -t $sha
并检查它是否返回“提交”。你是对的,你不需要实际打印实际的对象......
I'm not 100% sure that what goes on behind the scene is more efficient, though.
不过,我不能 100% 确定幕后发生的事情更有效。
test $(git cat-file -t $sha) == commit
test $(git cat-file -t $sha) == commit
回答by Paul Draper
git cat-file -e $sha^{commit}
From git cat-file
docs:
从git cat-file
文档:
-e Suppress all output; instead exit with zero status if <object> exists and is a valid object.
-e Suppress all output; instead exit with zero status if <object> exists and is a valid object.
This (1) shows that this is an intended use case for cat-file
and (2) avoids the resources of actually outputting any commit contents.
这 (1) 表明这是一个预期的用例,cat-file
并且 (2) 避免了实际输出任何提交内容的资源。
Appending ^{commit}
ensures that the object is a commit (i.e. not a tree or blob) or -- as remram points out -- resolves to a commit.
追加^{commit}
确保对象是一个提交(即不是树或 blob)或者——正如 remram 指出的——解析为一个提交。
For example,
例如,
if git cat-file -e $sha^{commit}; then
echo $sha exists
else
echo $sha does not exist
fi
回答by andygavin
If you are sure that the sha are commits then cat-file -e
can be used for example:
如果您确定 sha 是提交,则cat-file -e
可以使用例如:
if git cat-file -e $sha 2> /dev/null
then
echo exists
else
echo missing
fi
This is pretty efficient as this is a built-in and doesn't do anything other than checking the sha exists:
这是非常有效的,因为这是一个内置的,除了检查 sha 是否存在之外不做任何事情:
return !has_sha1_file(sha1);
Otherwise if it is uncertain that the sha are commit objects you would need to determine the type as with the other answer using git cat-file -t
. This is only slightly less performant as git would have to look at the file information. This isn't as costly as unpacking the whole file.
否则,如果不确定 sha 是提交对象,您将需要使用git cat-file -t
. 这只是稍微降低了性能,因为 git 必须查看文件信息。这不像解压整个文件那么昂贵。
回答by Black Mantha
git rev-parse -q --verify "$sha^{commit}" > /dev/null
From the git rev-parse
docs:
从git rev-parse
文档:
--verify Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. If so, emit it to the standard output; otherwise, error out. If you want to make sure that the output actually names an object in your object database and/or can be used as a specific type of object you require, you can add the ^{type} peeling operator to the parameter. For example, git rev-parse "$VAR^{commit}" will make sure $VAR names an existing object that is a commit-ish (i.e. a commit, or an annotated tag that points at a commit). To make sure that $VAR names an existing object of any type, git rev-parse "$VAR^{object}" can be used. -q, --quiet Only meaningful in --verify mode. Do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently. SHA-1s for valid object names are printed to stdout on success.
--verify Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. If so, emit it to the standard output; otherwise, error out. If you want to make sure that the output actually names an object in your object database and/or can be used as a specific type of object you require, you can add the ^{type} peeling operator to the parameter. For example, git rev-parse "$VAR^{commit}" will make sure $VAR names an existing object that is a commit-ish (i.e. a commit, or an annotated tag that points at a commit). To make sure that $VAR names an existing object of any type, git rev-parse "$VAR^{object}" can be used. -q, --quiet Only meaningful in --verify mode. Do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently. SHA-1s for valid object names are printed to stdout on success.
As a bonus, if you don't suppress the output, you can get the full sha.
作为奖励,如果你不抑制输出,你可以获得完整的 sha。