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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句