【问题标题】:Get diff between two tags with nodegit使用 nodegit 获取两个标签之间的差异
【发布时间】:2018-01-19 19:24:59
【问题描述】:

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

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

另外,我可以使用 nodegit 列出我的 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)
  })
})

但是,我不确定如何在 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)
    })
  })
})

【问题讨论】:

    标签: node.js git diff nodegit


    【解决方案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)
             })
          })
        })
      ))
    

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2017-02-14
      相关资源
      最近更新 更多