Skip to content
Merged
Show file tree
Hide file tree
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
83 changes: 48 additions & 35 deletions examples/walk-history-for-file.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,62 @@
var nodegit = require("../"),
path = require("path");
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(repo) {
.then(function(r) {
repo = r;
return repo.getMasterCommit();
})
.then(function(firstCommitOnMaster){
// History returns an event.
var history = firstCommitOnMaster.history(nodegit.Revwalk.SORT.Time);
var commits = [];

// History emits "commit" event for each commit in the branch's history
history.on("commit", function(commit) {
return commit.getDiff()
.then(function(diffList) {
diffList.map(function(diff) {
diff.patches().then(function(patches) {
patches.map(function(patch) {
var result =
!!~patch.oldFile().path().indexOf("descriptor.json") ||
!!~patch.newFile().path().indexOf("descriptor.json");

if(result && !~commits.indexOf(commit)) {
commits.push(commit);
}
});
});
});
});
});
walker = repo.createRevWalk();
walker.push(firstCommitOnMaster.sha());
walker.sorting(nodegit.Revwalk.SORT.Time);

history.on("end", function() {
commits.forEach(function(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());
});
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());
});

// Don't forget to call `start()`!
history.start();
})
.done();
4 changes: 4 additions & 0 deletions generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,10 @@
"ignore": true
},
"revwalk": {
"dependencies": [
"../include/commit.h",
"../include/functions/copy.h"
],
"functions": {
"git_revwalk_add_hide_cb": {
"ignore": true
Expand Down
33 changes: 32 additions & 1 deletion generate/input/libgit2-supplement.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,36 @@
"isErrorCode": true
}
},
"git_revwalk_file_history_walk": {
"args": [
{
"name": "file_path",
"type": "const char *"
},
{
"name": "max_count",
"type": "int"
},
{
"name": "out",
"type": "std::vector< std::pair<git_commit *, std::pair<char *, git_delta_t> > *> *"
},
{
"name": "walk",
"type": "git_revwalk *"
}
],
"type": "function",
"isManual": true,
"cFile": "generate/templates/manual/revwalk/file_history_walk.cc",
"isAsync": true,
"isPrototypeMethod": true,
"group": "revwalk",
"return": {
"type": "int",
"isErrorCode": true
}
},
"git_stash_save": {
"type": "function",
"file": "stash.h",
Expand Down Expand Up @@ -263,7 +293,8 @@
[
"revwalk",
[
"git_revwalk_fast_walk"
"git_revwalk_fast_walk",
"git_revwalk_file_history_walk"
]
],
[
Expand Down
Loading