Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo.getReferenceCommit() does not work with annotated tags #1370

Open
rhuss opened this issue Sep 10, 2017 · 6 comments · May be fixed by #1420
Open

repo.getReferenceCommit() does not work with annotated tags #1370

rhuss opened this issue Sep 10, 2017 · 6 comments · May be fixed by #1420

Comments

@rhuss
Copy link

rhuss commented Sep 10, 2017

The method getReferenceCommit() on a Repository works with a tag given in the form refs/tags/v0.1.5 only when the tag is a lightweight tag. If it is an annotated tag, calling this method results in an error:

Error: the requested type does not match the type in the ODB

See also the discussion at https://gitter.im/nodegit/nodegit?at=59b5026e66c1c7c477342023

@rcjsuen rcjsuen linked a pull request Dec 26, 2017 that will close this issue
@mojavelinux
Copy link
Contributor

mojavelinux commented Mar 7, 2018

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.

@mojavelinux
Copy link
Contributor

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?

@rcjsuen
Copy link
Member

rcjsuen commented Mar 7, 2018

@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 Tag object to its commit?

@mojavelinux
Copy link
Contributor

@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.

@rcjsuen
Copy link
Member

rcjsuen commented Mar 7, 2018

@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.

@mojavelinux
Copy link
Contributor

Thanks for the pointer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants