20

I have been trying to update a file in a GitHub repository by using GitHub API while having only a path to that file. My plan was first, get the file contents like described here: https://developer.github.com/v3/repos/contents/ , and then use the "sha" field to "update a file".

It worked fine just as described in the answer here How to find a Github file 's SHA blob .

However, using GET /repos/:owner/:repo/contents/:path , downloads the whole file, as a field in the returned JSON, which is inefficient. So, my question is: is there a way to get just the "sha" field without downloading the entire file?

Dekel Adler
  • 261
  • 1
  • 2
  • 7
  • Does this answer your question? [How to find a Github file 's SHA blob](https://stackoverflow.com/questions/20207594/how-to-find-a-github-file-s-sha-blob) – Jeff Puckett Aug 12 '20 at 15:55

4 Answers4

22

You could leverage the <rev>:<path> extended SHA-1 syntax to retrieve some meta data about the Blob from its parent Tree.

For instance, provided you work with the libgit2/libgit2sharp repository, and you'd like to retrieve the sha of the file Lib/MoQ/Moq.license.txt from the master branch:

  • Leverage the GitHub Trees API to retrieve the content of the Tree matching the Lib/MoQ parent directory
  • Retrieve from the returned Json payload the sha of the blob which path is Moq.license.txt
  • Make sure to url encode the the <rev>:<path> segment as it contains forward slashes

In brief:

The example link above will return the following payload

{
  "sha": "2f2c87728225f9cbb6e2d8c5997b6031e72ddca4",
  "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/trees/2f2c87728225f9cbb6e2d8c5997b6031e72ddca4",
  "tree": [
    {
      "path": "Moq.dll",
      "mode": "100644",
      "type": "blob",
      "sha": "bdd4235f215541017a9f37b6155f18e309573838",
      "size": 659968,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/bdd4235f215541017a9f37b6155f18e309573838"
    },
    {
      "path": "Moq.license.txt",
      "mode": "100644",
      "type": "blob",
      "sha": "c9216ccba318292d76fd308f232e7871bbbe77be",
      "size": 1748,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/c9216ccba318292d76fd308f232e7871bbbe77be"
    },
    {
      "path": "Moq.xml",
      "mode": "100644",
      "type": "blob",
      "sha": "160c1b5165fd967f4c79bc6043f0cc2ec28710d8",
      "size": 314572,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/160c1b5165fd967f4c79bc6043f0cc2ec28710d8"
    }
  ],
  "truncated": false
}
nulltoken
  • 64,429
  • 20
  • 138
  • 130
1

https://github.com/api/v3/repos/(organization-name)/(repo-name)/git/trees/(branch-name):(parent-folder-path) send a Get request to this end-point and u will get an array of json objects which contains the sha's of all files inside the parent folder and from there u just manage to extract the sha you need you can test it using postman or insomnia and good luck

hocine
  • 11
  • 1
-1

Or do a:

GET /repos/:owner/:repo/contents/:path

on the path to the file and then pick out the sha from the returned data.

aaa
  • 7
  • 1
-1

Do a HEAD request instead of a GET. It returns the sha as the Etag header.

Mark Melville
  • 783
  • 8
  • 13