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?
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.
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.
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.
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:
Lib/MoQ
parent directoryMoq.license.txt
<rev>:<path>
segment as it contains forward slashesIn 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With