如何使用nodegit从标记名称获取提交sha?

时间:2017-07-21 14:57:54

标签: javascript nodegit

我有这个:

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(),
  }))

如果tagName只是提交的别名,则此代码有效但如果标记是使用nodegit创建的正确标记,则会给出错误:

the requested type does not match the type in the ODB

使用git show [tagname]时会显示:

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)

那么如何从这个标签引用中获取提交本身(c465e)?

1 个答案:

答案 0 :(得分:3)

使用peel(type)有效:

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(),
  }))
相关问题