使用JGit访问存储库中的已修改文件

时间:2017-07-28 08:28:13

标签: git jgit

我正在使用JGit使用Java访问本地存储库。我需要访问存储库的更改文件,通常使用git中的git status命令执行。这个命令的JGit实现是什么?

所以基本上我需要一个典型的JGit表示:

  

git status

我目前的实施:

private void initRepository(String path){
    try {
        File workTree = new File(path);
        Git git = Git.open(workTree);
        //I need to get all the modified/changed files here     
    } catch (IOException ex) {
        //handle exception
    }
}

1 个答案:

答案 0 :(得分:3)

git status命令的等效命令可以按如下方式运行

Status status = git.status().call();

随后从状态对象中检索各种信息:

System.out.println("Added: " + status.getAdded());
System.out.println("Changed: " + status.getChanged());
System.out.println("Conflicting: " + status.getConflicting());
System.out.println("ConflictingStageState: " + status.getConflictingStageState());
System.out.println("IgnoredNotInIndex: " + status.getIgnoredNotInIndex());
System.out.println("Missing: " + status.getMissing());
System.out.println("Modified: " + status.getModified());
System.out.println("Removed: " + status.getRemoved());
System.out.println("Untracked: " + status.getUntracked());
System.out.println("UntrackedFolders: " + status.getUntrackedFolders());

来源:JGit Cookbook