Repository navigation
repo.getReferenceCommit() does not work with annotated tags #1370
Description
Activity
I managed to get this working by following the referenced discussion. To save other people time, here's the code I ended up using.
const git = require('nodegit') ;(async () => { const repo = await git.Repository.open('.') const refs = await repo.getReferences(git.Reference.TYPE.OID) const tagRefs = refs.filter((ref) => ref.isTag()) const tagRef = tagRefs[0] const targetRef = await tagRef.peel(git.Object.TYPE.COMMIT) const commit = await repo.getCommit(targetRef) })()
The important line is this one:
const targetRef = await tagRef.peel(git.Object.TYPE.COMMIT)
Unlike with a branch reference, you have to peel a tag reference back to the commit to which it points.
I found this is necessary regardless of whether the tag is annotated or lightweight.
This really feels like a dirty part of the API. Can we agree on an API that allows a program to navigate from a tag to a commit in a more elegant way?
@mojavelinux The following code should work.
const repository = await git.Repository.open('.') const reference = await repository.getReference("refs/tags/tagName"); const commit = await reference.peel(NodeGit.Object.TYPE.COMMIT):
It's not completely clear to me what you are looking for though. You want a way to go from the name of a tag to its commit or from an actual
Tagobject to its commit?Reacted by Jon Clark@rcjsuen That's precisely what the code I showed is doing. We're just getting the references a different way. (My code is discovering the references since I don't know the names up front).
It's not completely clear to me what you are looking for though.
My use case is simple. I want to read the files in a tag by walking the tree. For that, I need to resolve the tree. The tree is accessible from a commit. So the first thing I need to do is find the commit.
I'm using this in a content system that reads files from the branches and/or tags of a bare git repository.
@mojavelinux Never mind, I think I misunderstood what you originally were trying to say. Sorry about that.
There is a related discussion about these APIs at #403.
Thanks for the pointer!
The method
getReferenceCommit()on aRepositoryworks with a tag given in the formrefs/tags/v0.1.5only when the tag is a lightweight tag. If it is an annotated tag, calling this method results in an error:See also the discussion at https://gitter.im/nodegit/nodegit?at=59b5026e66c1c7c477342023