javascript - nodegit 获取所有暂存文件的差异

标签 javascript node.js git

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();

是否有类似的解决方案来获取所有分阶段更改的差异?

最佳答案

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

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();

关于javascript - nodegit 获取所有暂存文件的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36245496/

相关文章:

javascript - 模态增长时如何扩展模态背景?

asp.net - 当页面上有多个按钮时,按钮样式在页面加载时有粗边框

javascript - 删除其他元素后元素不向上移动

git - zsh - 显示 git branch 是否有未推送的提交

python - 如何使用 GitPython pull 远程存储库?

javascript - 为什么通过 Object.defineProperty 添加的属性在 Javascript 中不可迭代

node.js - Firebase Node.js SDK 未经身份验证的访问

javascript - 使用 console.log 时如何隐藏对象上的某些键

javascript - Node.js + Express + MySQL 作为文档存储(结果)

git - 再次在 git 行尾