2016-03-27 6 views
13

NodeGitが上演変更することなく、現在のすべての変更点の差分を取得する簡単な方法を提供していますファイルを上演し得るnodegit?は、すべてのdiffが

答えて

4

オーケーにDiff.OPTION.INCLUDE_UNTRACKEDの交換についての世話をする、私は方法を見つけた - 最初にコミットする前に、それは動作しませんがなされた。

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

indexToWorkdirはちょうどgit diffのように機能し、段階的な変更のみを表示するので、段階的な変更が表示されないのは奇妙です。私は、私のために働く例を書いた。それは、diffでステージングされたファイルとステージングされていないファイルの両方を表示します。試してみてください。スキップオプションを指定すると、ステージファイルのみが表示されます。

はまた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)); 
+0

indexToWorkdirは反対WORKDIRと一致します(ステージングされた)現在のインデックスには、ステージングされていないファイルのみが表示されます。 – jantimon

+0

リポジトリにコミットせずに試してみました。 –

+0

上部(ステージングされたファイル)を再現しようとしています: http://blog.sourcetreeapp.com/files/2014/08/Screen-Shot-2014-08-19-at-2.02.03-PM.png – jantimon

関連する問題