使用nodegit获取两个标记之间的差异

时间:2018-01-19 19:25:00

标签: node.js git diff nodegit

如何使用nodegit获取两个标签之间的差异?

在命令行上I can see the diff between two tags, no problemo

此外,我可以使用nodegit列出我的仓库中的标签:

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

但是,我不知道如何在nodegit中找到两个标签之间的差异。

我试过这个,但Diff部分没有打印任何内容:

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 个答案:

答案 0 :(得分:1)

以下是如何查看最后两个标记的提交数据:

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