Creating a line diff with isomorphic-git

389 Views Asked by At

I am trying to get output from isomorphic-git that is similar to git diff for a GitHub action.

I took a look at the map function on the git.walk implementation in isomorphic-git here but I'm having trouble using it.

I'm successfully able to walk the tree using the following code:

async function main() {
  const ref = 'HEAD'
  const trees = [git.TREE({ref}), git.WORKDIR()]
  const walk = await git.walk({
    fs,
    dir: '.github',
    trees,
    map: (filepath, [workdir, tree]) => [filepath, !!workdir, !!tree]
  })

  console.log(walk)
}

main()

How can I improve the map function here and use jsdiff to output the changes in the files between my current worktree and HEAD to stdout?

1

There are 1 best solutions below

0
Tim Paulaskas On

There is an example code on performing a git diff on the isomorphic-git code snippets page.

https://isomorphic-git.org/docs/en/snippets#git-diff-name-status-commithash1-commithash2

The result looks like this:

[
  { path: '/.gitignore', type: 'equal' },
  { path: '/package.json', type: 'modify' },
]

For each file, it generates the oid

        const Aoid = await A.oid()
        const Boid = await B.oid()

With those, I believe you can use the readObject method to get the content to diff.

https://isomorphic-git.org/docs/en/readObject