Skip to content

Commit 00a62f8

Browse files
committed
Commit.getParents working with merge commits
getParents failed when more than one commit was present, since it attempted to get the second parent of commit's parent instead of it's own second parent. The promise for the second parent was also pushed onto the array for Promise.all() too late to actually have an effect. Limit was not working properly as a limit, since it attempted to retrieve the set limit regardless of how many parents a commit had, failing if the limit was set to more than Commit.parentcount().
1 parent a7b1839 commit 00a62f8

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

lib/commit.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ Commit.prototype.history = function() {
116116
*/
117117
Commit.prototype.getParents = function(limit, callback) {
118118
var parents = [];
119-
var i = 0;
120119

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

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

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

132-
var parent = commit.repo.getCommit(oid).then(function(parent) {
133-
if (--limit) {
134-
i = i + 1;
135-
return processParents(parent, callback);
136-
}
137-
138-
return parent;
139-
});
140-
141-
// Add this parent to the list.
142133
parents.push(parent);
143-
144-
return parent;
145-
}
146-
147-
// Process all the parents.
148-
if (limit) {
149-
processParents(this, callback);
150134
}
151135

152136
// Wait for all parents to complete, before returning.

test/tests/commit.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ describe("Commit", function() {
215215
});
216216
});
217217

218+
it("can specify limit higher than actual parents", function() {
219+
return this.commit.getParents(2).then(function(parents) {
220+
assert.equal(parents.length, 1);
221+
});
222+
});
223+
224+
it("can fetch parents of a merge commit", function () {
225+
return NodeGit.Repository.open(reposPath)
226+
.then(function (repo) {
227+
return repo.getCommit("bf1da765e357a9b936d6d511f2c7b78e0de53632");
228+
})
229+
.then(function (commit) {
230+
return commit.getParents();
231+
})
232+
.then(function (parents) {
233+
assert.equal(parents.length, 2);
234+
});
235+
});
236+
218237
it("has a parent count", function() {
219238
assert.equal(1, this.commit.parentcount());
220239
});

0 commit comments

Comments
 (0)