Skip to content

Commit 9ff5708

Browse files
committed
Change how getReferenceCommit() retrieves commits
If a tag is annotated, its target() will return a tag object instead of the tag's underlying commit. This causes Repository's getReferenceCommit() to not work as it will try to find a commit based on the tag's oid. By replacing target() with peel(), the code can now find the actual underlying commit regardless of whether a tag is annotated or not. Signed-off-by: Remy Suen <remy.suen@gmail.com>
1 parent a76a79f commit 9ff5708

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/repository.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,8 @@ Repository.prototype.getReference = function(name, callback) {
10551055
* @return {Commit}
10561056
*/
10571057
Repository.prototype.getReferenceCommit = function(name, callback) {
1058-
var repository = this;
1059-
10601058
return this.getReference(name).then(function(reference) {
1061-
return repository.getCommit(reference.target()).then(function(commit) {
1059+
return reference.peel(NodeGit.Object.TYPE.COMMIT).then(function(commit) {
10621060
if (typeof callback === "function") {
10631061
callback(null, commit);
10641062
}

test/tests/repository.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,40 @@ describe("Repository", function() {
327327
assert.equal(numMergeHeads, 1);
328328
});
329329
});
330+
331+
it("can get reference commit that points at lightweight tag", function() {
332+
var repository = this.repository;
333+
var oid = null;
334+
return repository.getHeadCommit()
335+
.then(function(commit) {
336+
oid = commit.id().toString();
337+
return repository.createLightweightTag(
338+
oid, "getReferenceCommitLightweight");
339+
})
340+
.then(function() {
341+
return repository.getReferenceCommit(
342+
"refs/tags/getReferenceCommitLightweight");
343+
})
344+
.then(function(commit) {
345+
assert.equal(commit.id().toString(), oid);
346+
});
347+
});
348+
349+
it("can get reference commit that points at annotated tag", function() {
350+
var repository = this.repository;
351+
var oid = null;
352+
return repository.getHeadCommit()
353+
.then(function(commit) {
354+
oid = commit.id().toString();
355+
return repository.createTag(
356+
oid, "getReferenceCommitAnnotated", "");
357+
})
358+
.then(function() {
359+
return repository.getReferenceCommit(
360+
"refs/tags/getReferenceCommitAnnotated");
361+
})
362+
.then(function(commit) {
363+
assert.equal(commit.id().toString(), oid);
364+
});
365+
});
330366
});

0 commit comments

Comments
 (0)