39

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/

How do I find the SHA blob for the specific file? Supposed in my testrepo in the test account here, what is the SHA blob for the test.txt file?

https://github.com/testacc01/testrepo01

Thank you so much!

Kiddo
  • 1,910
  • 8
  • 30
  • 54

4 Answers4

37

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.)

user3033893
  • 900
  • 11
  • 11
  • 1
    thanks that works for me, I forget that the SHA will be automatically updated after the file is updated. – Kiddo Nov 26 '13 at 19:24
  • 1
    I wasn't able to find in GitHub's v3 API docs which specific SHA hashing function they're using to generate these - SHA1, SHA256, etc (so that I could verify the file contents against them myself). I believe git uses SHA-1 for commit hashes but it was unclear to me which hashing function GitHub is using for hashing file contents. I've opened a ticket with them requesting this information. – Taylor D. Edmiston Aug 30 '17 at 21:33
  • @TaylorEdmiston Hi, Did you get any response from github? – Maorg Jun 06 '18 at 07:55
  • Hmm, I'm not sure. I searched my email for it but I didn't find a reply from GitHub. – Taylor D. Edmiston Jun 15 '18 at 17:58
  • 2
    Regarding the SHA hashing function used: https://developer.github.com/v3/git/blobs/#get-a-blob states that `[t]he file's SHA-1 hash is computed and stored in the blob object`. So it's SHA-1. – jfix Sep 30 '18 at 09:08
  • Using get repository contents to obtain the SHA of a file doesn't work if the file is larger than 1MB: https://developer.github.com/v3/repos/contents/#get-repository-content – Chuck Doucette Jul 08 '20 at 14:37
10

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

freddieptf
  • 2,134
  • 2
  • 15
  • 16
7

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

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
3

Using Octokit Rest API:

import { Octokit } from "@octokit/rest";
    
const { data: { sha } } = await octokit.request('GET /repos/{owner}/{repo}/contents/{file_path}', {
  owner: "owner-name",
  repo: "repo-name",
  file_path: "file-path-with-extension-from-root"
});
Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
Ashish Singh
  • 1,004
  • 11
  • 23