Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update read-file example to use promises
  • Loading branch information
maxkorp committed Nov 19, 2014
commit 6d872496e16e9b6818474886d7f9fb4723284aaa
39 changes: 16 additions & 23 deletions example/read-file.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
var git = require('../'),
var nodegit = require('../'),
path = require('path');

// This example opens a certain file, `README.md`, at a particular commit,
// and prints the first 10 lines as well as some metadata.

git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
if (error) throw error;

repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) {
if (error) throw error;

commit.getEntry('README.md', function(error, entry) {
if (error) throw error;

entry.getBlob(function(error, blob) {
if (error) throw error;

console.log(entry.name(), entry.sha(), blob.size() + 'b');
console.log('========================================================\n\n');
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
console.log(firstTenLines);
console.log('...');
});
});
});
});
var _entry;
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
return repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5');
}).then(function(commit) {
return commit.getEntry('README.md');
}).then(function(entry) {
_entry = entry;
return _entry.getBlob();
}).then(function(blob) {
console.log(_entry.filename(), _entry.sha(), blob.rawsize() + 'b');
console.log('========================================================\n\n');
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
console.log(firstTenLines);
console.log('...');
}).done();