node.jsgitdiffnodegit

Get diff between two tags with nodegit


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

Solution

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