Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the "sha" parameter from GitHub API without downloading the whole file?

Tags:

git

github

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?

like image 855
Dekel Adler Avatar asked Oct 05 '14 14:10

Dekel Adler


People also ask

How do I find a file SHA in GitHub?

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.

How do I access GitHub REST API?

Go to Developer Settings ->Personal Access Tokens. Add a name and select the scope for the API access and click on Create Token. In the next screen, make sure to copy the token and save it in a file. This token will be used in the command line to access GitHub API.

What is GitHub Sha?

When you make a commit to save your work, Git creates a unique ID (a.k.a. the "SHA" or "hash") that allows you to keep record of the specific changes committed along with who made them and when. Commits usually contain a commit message which is a brief description of what changes were made.


1 Answers

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:

  • Syntax: GET /repos/<owner>/<repo>/git/trees/url_encode(<branch_name>:<parent_path>)
  • Example: https://api.github.com/repos/libgit2/libgit2sharp/git/trees/master:Lib%2FMoQ

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
}
like image 79
nulltoken Avatar answered Sep 17 '22 15:09

nulltoken