我想使用 JGit 显示所有文件和文件夹的列表,以进行头部修订。我可以使用 TreeWalk 列出所有文件,但这不会列出文件夹。
这是我到目前为止所拥有的:
public class MainClass {
public static void main(String[] args) throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder
.setGitDir(new File("C:\\temp\\git\\.git")).readEnvironment()
.findGitDir().build();
listRepositoryContents(repository);
repository.close();
}
private static void listRepositoryContents(Repository repository) throws IOException {
Ref head = repository.getRef("HEAD");
// a RevWalk allows to walk over commits based on some filtering that is defined
RevWalk walk = new RevWalk(repository);
RevCommit commit = walk.parseCommit(head.getObjectId());
RevTree tree = commit.getTree();
System.out.println("Having tree: " + tree);
// now use a TreeWalk to iterate over all files in the Tree recursively
// you can set Filters to narrow down the results if needed
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
while (treeWalk.next()) {
System.out.println("found: " + treeWalk.getPathString());
}
}
}