|
| 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