Skip to content

Commit 74f31bf

Browse files
author
John Haley
committed
Add example for file history
This fixes nodegit#220
1 parent 2627e28 commit 74f31bf

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

examples/walk-history-for-file.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var nodegit = require("../"),
2+
path = require("path");
3+
4+
// This code walks the history of the master branch and prints results
5+
// that look very similar to calling `git log` from the command line
6+
7+
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
8+
.then(function(repo) {
9+
return repo.getMasterCommit();
10+
})
11+
.then(function(firstCommitOnMaster){
12+
// History returns an event.
13+
var history = firstCommitOnMaster.history(nodegit.Revwalk.SORT.Time);
14+
var commits = [];
15+
16+
// History emits "commit" event for each commit in the branch's history
17+
history.on("commit", function(commit) {
18+
return commit.getDiff()
19+
.then(function(diffList) {
20+
var addCommit = diffList.reduce(function(prevVal, diff) {
21+
var result = prevVal || diff.patches().reduce(function(prevValDiff, patch) {
22+
var result =
23+
prevValDiff ||
24+
!!~patch.oldFile().path().indexOf("descriptor.json") ||
25+
!!~patch.newFile().path().indexOf("descriptor.json");
26+
27+
return result;
28+
}, false);
29+
30+
return result;
31+
}, false);
32+
33+
if (addCommit) {
34+
commits.push(commit);
35+
}
36+
});
37+
});
38+
39+
history.on("end", function() {
40+
commits.forEach(function(commit) {
41+
console.log("commit " + commit.sha());
42+
console.log("Author:", commit.author().name() +
43+
" <" + commit.author().email() + ">");
44+
console.log("Date:", commit.date());
45+
console.log("\n " + commit.message());
46+
});
47+
});
48+
49+
// Don't forget to call `start()`!
50+
history.start();
51+
})
52+
.done();

0 commit comments

Comments
 (0)