Skip to content

Commit a929d38

Browse files
committed
Ensures that commits from parent(*) has a repository
Commit has functions that requires a reference to a repository to run. Because parent(*) was simply calling out to libgit2's git_commit_parent directly, its repo field was not being set. Creating a wrapper in the Commit class and assigning a repository to the object before returning it will fix this problem. Signed-off-by: Remy Suen <remy.suen@gmail.com>
1 parent 977251b commit a929d38

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/commit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Commit = NodeGit.Commit;
55
var LookupWrapper = NodeGit.Utils.lookupWrapper;
66

77
var _amend = Commit.prototype.amend;
8+
var _parent = Commit.prototype.parent;
89

910
/**
1011
* Retrieves the commit pointed to by the oid
@@ -394,6 +395,21 @@ Commit.prototype.history = function() {
394395
return event;
395396
};
396397

398+
/**
399+
* Get the specified parent of the commit.
400+
*
401+
* @param {number} the position of the parent, starting from 0
402+
* @async
403+
* @return {Commit} the parent commit at the specified position
404+
*/
405+
Commit.prototype.parent = function (id) {
406+
var repository = this.repo;
407+
return _parent.call(this, id).then(function(parent) {
408+
parent.repo = repository;
409+
return parent;
410+
});
411+
};
412+
397413
/**
398414
* Retrieve the commit's parent shas.
399415
*

test/tests/commit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ describe("Commit", function() {
128128
assert.equal(this.commit.timeOffset(), 780);
129129
});
130130

131+
it("can call getTree on a parent commit", function() {
132+
return this.commit.parent(0)
133+
.then(function(parent) {
134+
return parent.getTree();
135+
})
136+
.then(function(tree) {
137+
assert.equal(
138+
tree.id().toString(), "327ff68e59f94f0c25d2c62fb0938efa01e8a107"
139+
);
140+
});
141+
});
142+
131143
it("can create a commit", function() {
132144
var test = this;
133145
var expectedCommitId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";

0 commit comments

Comments
 (0)