Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get commit sha from tag name with nodegit?

I have this:

nodegit.Reference
  .lookup(repo, `refs/tags/${tagName}`)
  .then(ref => nodegit.Commit.lookup(repo, ref.target()))
  .then(commit => ({
    tag: tagName,
    hash: commit.sha(),
    date: commit.date().toJSON(),
  }))

This code works if the tagName is just an alias to a commit but it gives me an error if the tag is a proper Tag created with nodegit:

the requested type does not match the type in the ODB

When using git show [tagname] it shows this:

tag release_2017-07-21_1413
Tagger: xxx
Date:   Fri Jul 21 16:13:47 2017 +0200


commit c465e3323fc2c63fbeb91f9b9b43379d28f9b761 (tag: release_2017-07-21_1413, initialRelease)

So how do I get from this tag reference to the commit itself (c465e) ?

like image 369
Anna B Avatar asked Jul 21 '17 14:07

Anna B


1 Answers

Using peel(type) works:

nodegit.Reference
  .lookup(repo, `refs/tags/${tagName}`)
  // This resolves the tag (annotated or not) to a commit ref
  .then(ref => ref.peel(nodegit.Object.TYPE.COMMIT))
  .then(ref => nodegit.Commit.lookup(repo, ref.id())) // ref.id() now
  .then(commit => ({
    tag: tagName,
    hash: commit.sha(),
    date: commit.date().toJSON(),
  }))
like image 70
Anna B Avatar answered Sep 30 '22 04:09

Anna B