Get diff between two tags with nodegit

故事扮演 提交于 2021-02-08 08:20:15

问题


How do I get the diff between two tags using nodegit?

On the command line, I can see the diff between two tags, no problemo.

Additionally, I can use nodegit to list the tags in my repo:

const Git = require('nodegit')
const path = require('path')

Git.Repository.open(path.resolve(__dirname, '.git'))
.then((repo) => {
  console.log('Opened repo ...')
  Git.Tag.list(repo).then((array) => {
    console.log('Tags:')
    console.log(array)
  })
})

However, I am not sure how to find the diff between two tags in nodegit.

I tried this, but nothing was printed in the Diff section:

const Git = require('nodegit')
const path = require('path')

Git.Repository.open(path.resolve(__dirname, '.git'))
.then((repo) => {
  console.log('Opened repo ...')
  Git.Tag.list(repo).then((array) => {
    console.log('Tags:')
    console.log(array)
    Git.Diff(array[0], array[1]).then((r) =>  {
      console.log('r', r)
    })
  })
})

回答1:


Here's how you can see the commit data for the last two tags:

nodegit.Repository.open(path.resolve(__dirname, '.git'))
  .then(repo => (
    nodegit.Tag.list(repo).then(tags => {
      return [
        tags[ tags.length - 1 ],
        tags[ tags.length - 2 ],
      ]
    })
    .then(tags => {
      console.log(tags)
      tags.forEach(t => {
        nodegit.Reference.lookup(repo, `refs/tags/${t}`)
        .then(ref => ref.peel(nodegit.Object.TYPE.COMMIT))
        .then(ref => nodegit.Commit.lookup(repo, ref.id()))
        .then(commit => ({
           tag: t,
           hash: commit.sha(),
           date: commit.date().toJSON(),
         }))
         .then(data => {
           console.log(data)
         })
      })
    })
  ))


来源:https://stackoverflow.com/questions/48348179/get-diff-between-two-tags-with-nodegit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!