简体   繁体   English

git cat-file -t 是怎么做的<object id>确定对象的类型?

[英]How does git cat-file -t <object id> determine the type of object?

The following link explains how Git computes object IDs (ruby code snippet).以下链接解释了 Git 如何计算对象 ID(ruby 代码片段)。 The object type (blob, tree, commit) is encoded in the header which is concatenated with the content and the SHA1 is computed on the concatenated string.对象类型(blob、tree、commit)编码在与内容连接的标头中,并在连接的字符串上计算 SHA1。

https://git-scm.com/book/en/v2/Git-Internals-Git-Objects https://git-scm.com/book/en/v2/Git-Internals-Git-Objects

One can use git cat-file -t <object id> to determine the type of the object (blob, tree, commit).可以使用git cat-file -t <object id>来确定对象的类型(blob、tree、commit)。

I'm wondering how does this command extract the type from the object ID given that SHA1 is a oneway hashing function?我想知道鉴于 SHA1 是单向散列函数,此命令如何从对象 ID 中提取类型?

"You're holding it upside down." “你把它倒过来拿着。” 😀 😀

While it's true that SHA is a one-way hash, that's not a problem: you're supplying the hash yourself , which Git uses as a key in a key-value database , allowing Git to retrieve the data.虽然 SHA 确实是一种单向散列,但这不是问题:您自己提供散列,Git 将其用作键值数据库中的,从而允许 Git 检索数据。 (If you supply part of the hash, rather than the whole thing, Git looks for keys that match that prefix; if the prefix is unique, Git assumes that the resulting matching key is the right key.) (如果您提供散列的一部分,而不是整个内容,Git 会查找与该前缀匹配的键;如果前缀是唯一的,Git 会假定生成的匹配键是正确的键。)

Having obtained the data—the zlib-compressed object—Git now needs only to uncompress the first few bytes of that data.获得数据(zlib 压缩对象)后,Git 现在只需要解压缩该数据的前几个字节。 These begin with one of the four object type strings: blob , commit , tag , or tree (followed by a space and then the decimal-expansion-in-ASCII of the size and the '\\0' byte).它们以四种对象类型字符串之一开头: blobcommittagtree (后跟一个空格,然后是大小和 '\\0' 字节的十进制扩展在 ASCII 中)。

If Git extracts the entire object—the -t code can take a shortcut and stop decompressing early—Git will then verify that the bytes of the object, including the header, fed back through the hash function, produce the key that was used to retrieve the object.如果 Git 提取了整个对象-t代码可以走捷径并提前停止解压——然后 Git 将验证对象的字节,包括通过哈希函数反馈的标头,生成用于检索的密钥物体。 If Git stops short (as it does for -t ), Git skips the verification step.如果 Git 停止(就像-t ),Git 会跳过验证步骤。

given that SHA1 is a oneway hashing function鉴于 SHA1 是单向散列函数

That's irrelevant.那无关紧要。 The SHA is not concealing anything. SHA 没有隐瞒任何事情。 On the contrary.相反。 Think of the SHA as an address.将 SHA 视为地址。 The file at that address is readable and states the type.该地址处的文件是可读的并说明了类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM