How to find a Github file 's SHA blob

后端 未结 3 1271
囚心锁ツ
囚心锁ツ 2021-02-05 02:46

I\'m using this API to update a file on my repo, it requires me to have a valid SHA blob for a file that I want to update:

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

相关标签:
3条回答
  • 2021-02-05 03:21

    If you don't want to hit the api, you could generate the SHA yourself. Git generates the SHA by concatenating a header in the form of blob {content.length} {null byte} and the contents of your file. For example:

    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)
    

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

    0 讨论(0)
  • 2021-02-05 03:23

    The docs for updating a file specify that you will need to provide the SHA for the file you will be replacing. The easiest way would be to query github for that, too. For example:

    > curl https://api.github.com/repos/testacc01/testrepo01/contents/test.txt
    {
      "name": "test.txt",
      "path": "test.txt",
      "sha": "4f8a0fd8ab3537b85a64dcffa1487f4196164d78",
      "size": 13,
     …
    

    So, you can see what the SHA is in the "sha" field of the JSON response. Use that when you formulate your request to update the file with a new version. After you have successfully updated the file, the file will have a new SHA that you will need to request before it can be updated again. (Unless, I guess, your next update goes on a different branch.)

    0 讨论(0)
  • 2021-02-05 03:33

    If you use GraphQL API v4, you can use the following to find the sha of a specific file :

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

    Try it in the explorer

    0 讨论(0)
提交回复
热议问题