Skip to content
Merged
Changes from all commits
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
Update Readme, to improve example code
Refactored code to use promises chaining for more clarity and less callback hell.
  • Loading branch information
nmn committed Oct 22, 2014
commit f417c1847da6f071b3581c5df57ef0bdd3d5e917
54 changes: 30 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,32 @@ npm install nodegit --msvs_version=2013
var clone = require("nodegit").Repository.clone;

// Clone a given repository into a specific folder.
clone("https://github.com/nodegit/nodegit", "tmp", null).then(function(repo) {
// Use a known commit sha from this repository.
var sha = "59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5";

// Look up this known commit.
repo.getCommit(sha).then(function(commit) {
clone("https://github.com/nodegit/nodegit", "tmp", null)
.then(function(repo) {
// Use a known commit sha from this repository.
var sha = "59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5";

// Look up this known commit.
return repo.getCommit(sha);
})
.then(function(commit) {
// Look up a specific file within that commit.
commit.getEntry("README.md").then(function(entry) {
// Get the blob contents from the file.
entry.getBlob().then(function(blob) {
// Show the name, sha, and filesize in byes.
console.log(entry.name() + entry.sha() + blob.size() + "b");

// Show a spacer.
console.log(Array(72).join("=") + "\n\n");

// Show the entire file.
console.log(String(blob));
});
});
return commit.getEntry("README.md");
})
.then(function(entry) {
// Get the blob contents from the file.
return entry.getBlob();
})
.then(function(blob) {
// Show the name, sha, and filesize in byes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some extra space here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some lines already, I left extra spaces before every comment. That's just a style choice. You can make the call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, if you look at the diff the comment aligned with console. Now it's off by a few. Sorry if I wasn't clear.

console.log(entry.name() + entry.sha() + blob.size() + "b");

// Show a spacer.
console.log(Array(72).join("=") + "\n\n");

// Show the entire file.
console.log(String(blob));
});
});
```

### Emulating git log: ###
Expand All @@ -130,9 +134,12 @@ clone("https://github.com/nodegit/nodegit", "tmp", null).then(function(repo) {
var open = require("nodegit").Repository.open;

// Open the repository directory.
open("tmp").then(function(repo) {
// Open the master branch.
repo.getMaster().then(function(branch) {
open("tmp")
.then(function(repo) {
// Open the master branch.
return repo.getMaster();
})
.then(function(branch) {
// Create a new history event emitter.
var history = branch.history();

Expand Down Expand Up @@ -165,7 +172,6 @@ open("tmp").then(function(repo) {
// Start emitting events.
history.start();
});
});
```

## Unit tests. ##
Expand Down