forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk-history-for-file.js
62 lines (54 loc) · 1.7 KB
/
walk-history-for-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var nodegit = require("../"),
path = require("path"),
historyFile = "generate/input/descriptor.json",
walker,
historyCommits = [],
commit,
repo;
// This code walks the history of the master branch and prints results
// that look very similar to calling `git log` from the command line
function compileHistory(resultingArrayOfCommits) {
var lastSha;
if (historyCommits.length > 0) {
lastSha = historyCommits[historyCommits.length - 1].commit.sha();
if (
resultingArrayOfCommits.length == 1 &&
resultingArrayOfCommits[0].commit.sha() == lastSha
) {
return;
}
}
resultingArrayOfCommits.forEach(function(entry) {
historyCommits.push(entry);
});
lastSha = historyCommits[historyCommits.length - 1].commit.sha();
walker = repo.createRevWalk();
walker.push(lastSha);
walker.sorting(nodegit.Revwalk.SORT.TIME);
return walker.fileHistoryWalk(historyFile, 500)
.then(compileHistory);
}
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(r) {
repo = r;
return repo.getMasterCommit();
})
.then(function(firstCommitOnMaster){
// History returns an event.
walker = repo.createRevWalk();
walker.push(firstCommitOnMaster.sha());
walker.sorting(nodegit.Revwalk.SORT.Time);
return walker.fileHistoryWalk(historyFile, 500);
})
.then(compileHistory)
.then(function() {
historyCommits.forEach(function(entry) {
commit = entry.commit;
console.log("commit " + commit.sha());
console.log("Author:", commit.author().name() +
" <" + commit.author().email() + ">");
console.log("Date:", commit.date());
console.log("\n " + commit.message());
});
})
.done();