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

7

我在我的仓库中有两个分支mastermaster.min

假设当前分支是master.min

我的主分支在提交-abcd

一些推送发生在主分支上-efghijkl

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

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

由于分支之间的切换时间较长,我需要在master.min上完成所有操作。因此,我执行了一次fetch操作:
repo.fetch("master");

现在,我需要获取所有在abcdijkl之间添加、修改或删除的文件列表。
commit.getDiff() is not enough. I need diff between two commits.
2个回答

5

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

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 文档:
- ConvenientPatch: https://www.nodegit.org/api/convenient_patch/ - DiffFile: https://www.nodegit.org/api/#DiffFile - 所有 API 文档: https://www.nodegit.org/api/

3
我也需要这个功能,但似乎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是伪代码,但它只是为了呈现这个想法。

尽快实现并在此处发布进度。


1
最终我使用了git-promise包装器来执行git命令获取差异。因此我的代码同时使用了nodegit和git。 - adnan kamili
1
非常好的方法@jotadepicas。我已经实现了它,而且它很有效。由于大量的Promises,这并不是一件简单的事情。 - Bernhard Pointner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接