Git: 获取 .git 目录中的 blob 路径

Git: get blob path in .git directory

我想在回购历史的某个时刻访问一些 blob。

目前,我使用 git show $REV:$PATH 来做到这一点。但是文件非常大,我不希望它们在脚本中被读取和传输。我想获取他们的路径,然后作为普通文件读取。

我可以依赖当前文档中描述的布局(例如 .git/objects/ee/2403ffd236587a2b17ddc35b0e711fc99ba6a0),获取文件哈希并将其手动转换为路径吗?我的意思是它在未来的版本中不会很快改变,对象目录总是有这个结构。 有没有更简单的方法可以使用一些管道命令来做到这一点?

虽然 blob 数据 inviolable and sacrosanct,但它的格式也是普通人无法使用的:

  • 作为 ,它是 zlib-deflated(但这是一个实现细节,而不是一个承诺,即你不应该只是打开并阅读它并使用 zlib 充气器要恢复它,您应该让 Git 为您完成)。

  • 作为, it may have been packed, in which case there is no unpacked object file to open and read in the first place. Instead, you would have to open the pack index files (to find the correct pack file) and then the correct pack file (to find the packing data with the directory that locates the object and its bases), and then undo the xdelta style, but not actually xdelta,压缩那些项目。

如果你想用管道命令读取文件,你可以先找到散列:

$ git rev-parse HEAD~20:Makefile
bdb55792f11a9f9565c4aad147a492caed7f09c3

然后使用 git cat-file -p 提取原始对象,或 git cat-file -t 获取其类型(或 --batch-check 读取有关对象的信息等)。请注意,您实际上也可以将路径直接传递给 git cat-file 本身:

$ git cat-file -t HEAD~20:Makefile
blob

但是请注意,还有一个潜在的绊脚石:当使用 git cat-file -p <blob-specifier>git show <blob-specifier> 访问 blob 的内容时,您会得到 存储库格式 的数据。也就是说,当签出一个特定的提交时(使用 git checkout),Git 将提取一个 .gitattributes 文件 and/or 使用 git config 设置来查找污迹过滤器 and/or 要进行的 CR-LF 调整。这些过滤器应用于存储库中的数据以生成文件的工作树副本。但是当您使用 git showgit cat-file -p 访问存储库数据时,没有使用过滤器