nodegit获取所有staged文件的diff

jan*_*mon 15 javascript git node.js

NodeGit提供了一种简单的方法来获取所有当前更改的差异,而无需进行分阶段更改:

import NodeGit, { Diff } from 'nodegit';

function getUnstagedChanges(path) {
  const repo = await NodeGit.Repository.open(path);
  const diff = await Diff.indexToWorkdir(repo, null, {
    flags: Diff.OPTION.INCLUDE_UNTRACKED |
           Diff.OPTION.RECURSE_UNTRACKED_DIRS
    });
  console.log(await diff.patches());
}

getUnstagedChanges();
Run Code Online (Sandbox Code Playgroud)

是否有类似的解决方案来获得所有阶段性变化的差异?

jan*_*mon 7

好的,我找到了一种方法 - 但是在第一次提交之前它不会起作用:

import NodeGit, { Diff } from 'nodegit';

function getStagedChanges(path) {
  const repo = await NodeGit.Repository.open(path);
  const head = await repository.getHeadCommit();
  if (!head) {
    return [];
  }
  const diff = await Diff.treeToIndex(repo, await head.getTree(), null);
  const patches = await diff.patches();
  console.log(patches.map((patch) => patch.newFile().path()));
}

getStagedChanges();
Run Code Online (Sandbox Code Playgroud)