Skip to content

Commit

Permalink
Change how getReferenceCommit() retrieves commits
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rcjsuen committed Dec 26, 2017
1 parent d3fcc53 commit ff4062c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions test/tests/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit ff4062c

Please sign in to comment.