如何找到Github文件的SHA blob

Kid*_*ddo 21 git github sha github-api

我正在使用此API更新我的repo上的文件,它要求我为要更新的文件提供有效的SHA blob:

http://developer.github.com/v3/repos/contents/

如何找到特定文件的SHA blob?我在测试帐户的testrepo中提供了什么,test.txt文件的SHA blob是什么?

https://github.com/testacc01/testrepo01

非常感谢!

use*_*893 15

更新文件的文档指定您需要为要替换的文件提供SHA.最简单的方法是查询github.例如:

> curl https://api.github.com/repos/testacc01/testrepo01/contents/test.txt
{
  "name": "test.txt",
  "path": "test.txt",
  "sha": "4f8a0fd8ab3537b85a64dcffa1487f4196164d78",
  "size": 13,
 …
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到SHA在JSON响应的"sha"字段中的含义.在使用新版本制定更新文件的请求时使用它.成功更新文件后,该文件将具有一个新的SHA,您需要先请求它才能再次更新.(除非,我猜,你的下一次更新会在不同的分支上进行.)

  • 关于使用的 SHA 哈希函数:https://developer.github.com/v3/git/blobs/#get-a-blob 指出“文件的 SHA-1 哈希值被计算并存储在 blob 对象中” 。所以它是 SHA-1。 (2认同)

fre*_*ptf 11

如果您不想使用 api,您可以自己生成 SHA。Git 通过连接文件形式的标头blob {content.length} {null byte}和文件内容来生成 SHA。例如:

content = "what is up, doc?"
header = "blob #{content.bytesize}\0"
combined = header + content # will be "blob 16\u0000what is up, doc?"
sha1 = Digest::SHA1.hexdigest(combined)
Run Code Online (Sandbox Code Playgroud)

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


Ber*_*tel 7

如果您使用GraphQL API v4,则可以使用以下命令查找特定文件的 sha:

{
  repository(owner: "testacc01", name: "testrepo01") {
    object(expression: "master:test.txt") {
      ... on Blob {
        oid
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在资源管理器中尝试