From ff4062ce987444b85dd6f9244d7d872279268f22 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Tue, 26 Dec 2017 18:51:05 +0900 Subject: [PATCH] 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 --- lib/repository.js | 4 +--- test/tests/repository.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/repository.js b/lib/repository.js index 56d2f5a6fe..026e3ff75e 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -1055,10 +1055,8 @@ Repository.prototype.getReference = function(name, callback) { * @return {Commit} */ Repository.prototype.getReferenceCommit = function(name, callback) { - var repository = this; - return this.getReference(name).then(function(reference) { - return repository.getCommit(reference.target()).then(function(commit) { + return reference.peel(NodeGit.Object.TYPE.COMMIT).then(function(commit) { if (typeof callback === "function") { callback(null, commit); } diff --git a/test/tests/repository.js b/test/tests/repository.js index be165b3037..e8362c6794 100644 --- a/test/tests/repository.js +++ b/test/tests/repository.js @@ -327,4 +327,40 @@ describe("Repository", function() { assert.equal(numMergeHeads, 1); }); }); + + it("can get reference commit that points at lightweight tag", function() { + var repository = this.repository; + var oid = null; + return repository.getHeadCommit() + .then(function(commit) { + oid = commit.id().toString(); + return repository.createLightweightTag( + oid, "getReferenceCommitLightweight"); + }) + .then(function() { + return repository.getReferenceCommit( + "refs/tags/getReferenceCommitLightweight"); + }) + .then(function(commit) { + assert.equal(commit.id().toString(), oid); + }); + }); + + it("can get reference commit that points at annotated tag", function() { + var repository = this.repository; + var oid = null; + return repository.getHeadCommit() + .then(function(commit) { + oid = commit.id().toString(); + return repository.createTag( + oid, "getReferenceCommitAnnotated", ""); + }) + .then(function() { + return repository.getReferenceCommit( + "refs/tags/getReferenceCommitAnnotated"); + }) + .then(function(commit) { + assert.equal(commit.id().toString(), oid); + }); + }); });