最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodegit get diff of all staged files - Stack Overflow

programmeradmin3浏览0评论

NodeGit offers an easy way to get a diff of all current changes without the staged changes:

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

Is there a similar solution to get a diff of all staged changes?

NodeGit offers an easy way to get a diff of all current changes without the staged changes:

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

Is there a similar solution to get a diff of all staged changes?

Share Improve this question edited Apr 4, 2016 at 13:35 jantimon asked Mar 27, 2016 at 8:45 jantimonjantimon 38.2k23 gold badges126 silver badges193 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Okay I found a way - however it will not work before the first mit was made:

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

That is strange that you doesn't see staged changes, because indexToWorkdir works exactly like git diff and show only staged changes. I wrote example, that works for me. It show both staged and unstaged files in diff, please try it. If skip options, only staged files showed.

Also take care about replacement of Diff.OPTION.INCLUDE_UNTRACKED to Diff.OPTION.SHOW_UNTRACKED_CONTENT

import path from 'path';
import Git from 'nodegit';

async function print() {
    const repo = await Git.Repository.open(path.resolve(__dirname, '.git'));
    const diff = await Git.Diff.indexToWorkdir(repo, null, {
        flags: Git.Diff.OPTION.SHOW_UNTRACKED_CONTENT | Git.Diff.OPTION.RECURSE_UNTRACKED_DIRS
    });

    // you can't simple log diff here, it logs as empty object
    // console.log(diff); // -> {}

    diff.patches().then((patches) => {
        patches.forEach((patch) => {
            patch.hunks().then((hunks) => {
                hunks.forEach((hunk) => {
                    hunk.lines().then((lines) => {
                        console.log("diff", patch.oldFile().path(), patch.newFile().path());
                        console.log(hunk.header().trim());
                        lines.forEach((line) => {
                            console.log(String.fromCharCode(line.origin()) + line.content().trim());
                        });
                    });
                });
            });
        });
    });

    return diff;
}

print().catch(err => console.error(err));

This way will work even if there's no existing mit, by using the empty tree (the hash for a tree which contains nothing).

import { Repository, Tree, Diff } from 'nodegit';

const emptyTree = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

function getStagedChanges(path) {
  const repo = await Repository.open(path);

  const head = await repository.getHeadCommit();
  const tree = await (head ? head.getTree() : Tree.lookup(repo, emptyTree));

  const diff = await Diff.treeToIndex(repo, await head.getTree(), null);
  const patches = await diff.patches();
  console.log(patches.map((patch) => patch.newFile().path()));
}

getStagedChanges('/path/to/repo');
发布评论

评论列表(0)

  1. 暂无评论