Skip to content

Commit b873633

Browse files
committed
Update example for file history
1 parent c667e1a commit b873633

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

examples/walk-history-for-file.js

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,62 @@
11
var nodegit = require("../"),
2-
path = require("path");
2+
path = require("path"),
3+
historyFile = "generate/input/descriptor.json",
4+
walker,
5+
historyCommits = [],
6+
commit,
7+
repo;
38

49
// This code walks the history of the master branch and prints results
510
// that look very similar to calling `git log` from the command line
611

12+
function compileHistory(resultingArrayOfCommits) {
13+
var lastSha;
14+
if (historyCommits.length > 0) {
15+
lastSha = historyCommits[historyCommits.length - 1].commit.sha();
16+
if (
17+
resultingArrayOfCommits.length == 1 &&
18+
resultingArrayOfCommits[0].commit.sha() == lastSha
19+
) {
20+
return;
21+
}
22+
}
23+
24+
resultingArrayOfCommits.forEach(function(entry) {
25+
historyCommits.push(entry);
26+
});
27+
28+
lastSha = historyCommits[historyCommits.length - 1].commit.sha();
29+
30+
walker = repo.createRevWalk();
31+
walker.push(lastSha);
32+
walker.sorting(nodegit.Revwalk.SORT.TIME);
33+
34+
return walker.fileHistoryWalk(historyFile, 500)
35+
.then(compileHistory);
36+
}
37+
738
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
8-
.then(function(repo) {
39+
.then(function(r) {
40+
repo = r;
941
return repo.getMasterCommit();
1042
})
1143
.then(function(firstCommitOnMaster){
1244
// 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-
diffList.map(function(diff) {
21-
diff.patches().then(function(patches) {
22-
patches.map(function(patch) {
23-
var result =
24-
!!~patch.oldFile().path().indexOf("descriptor.json") ||
25-
!!~patch.newFile().path().indexOf("descriptor.json");
26-
27-
if(result && !~commits.indexOf(commit)) {
28-
commits.push(commit);
29-
}
30-
});
31-
});
32-
});
33-
});
34-
});
45+
walker = repo.createRevWalk();
46+
walker.push(firstCommitOnMaster.sha());
47+
walker.sorting(nodegit.Revwalk.SORT.Time);
3548

36-
history.on("end", function() {
37-
commits.forEach(function(commit) {
38-
console.log("commit " + commit.sha());
39-
console.log("Author:", commit.author().name() +
40-
" <" + commit.author().email() + ">");
41-
console.log("Date:", commit.date());
42-
console.log("\n " + commit.message());
43-
});
49+
return walker.fileHistoryWalk(historyFile, 500);
50+
})
51+
.then(compileHistory)
52+
.then(function() {
53+
historyCommits.forEach(function(entry) {
54+
commit = entry.commit;
55+
console.log("commit " + commit.sha());
56+
console.log("Author:", commit.author().name() +
57+
" <" + commit.author().email() + ">");
58+
console.log("Date:", commit.date());
59+
console.log("\n " + commit.message());
4460
});
45-
46-
// Don't forget to call `start()`!
47-
history.start();
4861
})
4962
.done();

0 commit comments

Comments
 (0)