【问题标题】:Nodegit - get diff between two commitsNodegit - 获取两个提交之间的差异
【发布时间】:2016-04-10 13:37:52
【问题描述】:

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

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

我的主分支正在提交 - abcd

一些推送发生在主分支 - efgh, ijkl

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

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

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

所以,我做了一个抓取:

repo.fetch("master");

现在,我需要获取在abcdijkl 之间添加、修改或删除的所有文件的列表

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

【问题讨论】:

    标签: javascript node.js git nodegit


    【解决方案1】:

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

    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 文档:

    【讨论】:

      【解决方案2】:

      我也需要这个,但是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 是伪代码,但这只是为了描绘这个想法。

      我会尽快尝试实施并在此处发布进度。

      【讨论】:

      • 我最终使用 git-promise 包装器为 git 命令获取差异。所以我的代码使用 nodegit 以及 git
      • 非常好的方法@jotadepicas。我实现了它并且它有效。由于 Promise 的数量巨大,这不是微不足道的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多