Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update README.md to show how to _get_ the blob sha of the previous commit. #269

Open
Pomax opened this issue Aug 14, 2018 · 2 comments
Open

Comments

@Pomax
Copy link

Pomax commented Aug 14, 2018

Right now under the read/write/update section of the readme, there is the following text:

To update a file you need the blob SHA of the previous commit:

but the code that follows does not show how to get that SHA at all, it just shows a meaningless dummy sha. Can that example be updated to show how to actually get the previous commit sha? (Do we use fetch? do we call something on commits? I can't tell =S)

(Also, the section is titled read/write/update but the examples shown only show off reading and updating. Plain writing does not appear as one of the examples)

@dhananjayharel
Copy link

dhananjayharel commented Jul 20, 2020

One way you can do this to get the last commit of the specific file and then use that sha

var octo = new Octokat({
    username: 'username',
    password: 'password'
});

var repo = octo.repos('username', 'repoName');
repo.contents('somefile.txt').fetch(function(err, info) {
    if (err) {
        console.log(err);
    } else {
        console.log('somefile.txt's SHA number is: ' + info.sha);
    }
});

Example Using promise =>


var Octokat = require('octokat');
var base64 = require('base-64');


var octo = new Octokat({token: 'your token'})
var repo = octo.repos('username', 'reponame');


repo.contents('somefile.txt').fetch().then((info) =>{
var config = {
  message: 'Updating somefile.txt',
  content: base64.encode('new content'),
  sha:info.sha
}
 
repo.contents('somefile.txt').add(config)
.then((info) => {
  console.log('File Updated. new sha is ', info);

})}).catch(function(err){console.log("err"+err)});

@Pomax
Copy link
Author

Pomax commented Jul 21, 2020

Could you turn that into a PR against the README.md?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants