Visitar URL original
repo.getReferenceCommit() does not work with annotated tags · Issue #1370 · nodegit/nodegit · GitHub
Skip to content

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

Description

@rhuss

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

Activity

  1. mojavelinux commented on Mar 7, 2018

    @mojavelinux
    Contributor

    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.

  2. mojavelinux commented on Mar 7, 2018

    @mojavelinux
    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?

  3. rcjsuen commented on Mar 7, 2018

    @rcjsuen
    Member

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

  4. mojavelinux commented on Mar 7, 2018

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

  5. rcjsuen commented on Mar 7, 2018

    @rcjsuen
    Member

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

  6. mojavelinux commented on Mar 7, 2018

    @mojavelinux
    Contributor

    Thanks for the pointer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions