Nodegit-获取两次提交之间的差异

Nodegit - get diff between two commits

本文关键字:之间 提交 两次 获取 Nodegit-      更新时间:2023-09-26

我的repo中有两个分支mastermaster.min

假设我当前的分支是master.min

我的主分支处于提交状态-abcd

主分支发生一些推送-efghijkl

我存储主分支的当前提交:

 repo.getBranchCommit("master")
        .then(function(commit) {
            startCommit = commit;
        })

由于分支之间的切换时间很长,我需要完成master.min 上剩余的所有操作

所以,我做了一个提取:

repo.fetch("master");

现在,我需要获得在abcd&ijkl

commit.getDiff() is not enough. I need diff between two commits.

对于那些寻找更明确答案的人:

const nodegit = require('nodegit');
const repo = await nodegit.Repository.open(repoDirectory);
const from = await repo.getCommit(fromCommitSHA);
const fromTree = await from.getTree();
const to = await repo.getCommit(toCommitSHA);
const toTree = await to.getTree();
const diff = await toTree.diff(fromTree);
const patches = await diff.patches();
for (const patch of patches) {
    console.log(patch.newFile().path());
}

每个补丁都代表一个修改过的文件,并且是ConvenientPatch的一个实例。它有两个方法oldFile()newFile(),它们返回代表修改前后文件的DiffFile的实例。

NodeGit API文档:

  • 方便的补丁:https://www.nodegit.org/api/convenient_patch/
  • 差异文件:https://www.nodegit.org/api/#DiffFile
  • 所有API文件:https://www.nodegit.org/api/

我也需要这个,但nodegit似乎还不支持它。

看一看https://github.com/nodegit/nodegit/blob/master/lib/commit.js#L196我看到差异是通过比较提交树和父树来计算的:

return thisTree.diffWithOptions(parentTree, options)

因此,我认为这可以通过实现commit#getDiff的变体来实现,该变体接收另一个提交的OID并调用tree1 = this.getTree()tree2 = getTheOtherCommit(OID).getTree(),然后调用tree1.diffWithOptions(tree2, options)

当然,getTheOtherCommit是伪代码,但它只是为了描绘这个想法。

我会尽快实施它,并在这里发布进展。