使用 isomorphic-git 创建行差异

jax*_*orm 5 javascript git isomorphic-javascript

我正在尝试从isomorphic-git获取类似于git diffGitHub 操作的输出。

我在这里查看了isomorphic-git 中实现的map函数,但我在使用它时遇到了问题。git.walk

我可以使用以下代码成功地遍历树:

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()
Run Code Online (Sandbox Code Playgroud)

如何改进map这里的功能并使用jsdiffHEAD将当前工作树和stdout之间的文件更改输出?

小智 0

在 isomorphic-git 代码片段页面上有一个关于执行 git diff 的示例代码。

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

结果如下:

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

Run Code Online (Sandbox Code Playgroud)

对于每个文件,它都会生成 oid

        const Aoid = await A.oid()
        const Boid = await B.oid()
Run Code Online (Sandbox Code Playgroud)

有了这些,我相信您可以使用 readObject 方法来获取要比较的内容。

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