Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
24 changes: 4 additions & 20 deletions 24 lib/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ Commit.prototype.history = function() {
*/
Commit.prototype.getParents = function(limit, callback) {
var parents = [];
var i = 0;

// Shift arguments.
if (typeof limit === "function") {
Expand All @@ -125,28 +124,13 @@ Commit.prototype.getParents = function(limit, callback) {

// If no limit was set, default to the maximum parents.
limit = typeof limit === "number" ? limit : this.parentcount();
limit = Math.min(limit, this.parentcount());

function processParents(commit, callback) {
var oid = commit.parentId(i);
for (var i = 0; i < limit; i++) {
var oid = this.parentId(i);
var parent = this.repo.getCommit(oid);

var parent = commit.repo.getCommit(oid).then(function(parent) {
if (--limit) {
i = i + 1;
return processParents(parent, callback);
}

return parent;
});

// Add this parent to the list.
parents.push(parent);

return parent;
}

// Process all the parents.
if (limit) {
processParents(this, callback);
}

// Wait for all parents to complete, before returning.
Expand Down
19 changes: 19 additions & 0 deletions 19 test/tests/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ describe("Commit", function() {
});
});

it("can specify limit higher than actual parents", function() {
return this.commit.getParents(2).then(function(parents) {
assert.equal(parents.length, 1);
});
});

it("can fetch parents of a merge commit", function () {
return NodeGit.Repository.open(reposPath)
.then(function (repo) {
return repo.getCommit("bf1da765e357a9b936d6d511f2c7b78e0de53632");
})
.then(function (commit) {
return commit.getParents();
})
.then(function (parents) {
assert.equal(parents.length, 2);
});
});

it("has a parent count", function() {
assert.equal(1, this.commit.parentcount());
});
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.