Use JGit TreeWalk to list files and folders

前端 未结 2 1500
野的像风
野的像风 2021-02-19 04:25

I\'d like to use JGit to display a list of all files and folders for the head revision. I\'m able to list all files using TreeWalk, but this does not list folders.

Here

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 04:58

    You need to set recursive to false (see documentation) and then walk like this:

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(false);
    while (treeWalk.next()) {
        if (treeWalk.isSubtree()) {
            System.out.println("dir: " + treeWalk.getPathString());
            treeWalk.enterSubtree();
        } else {
            System.out.println("file: " + treeWalk.getPathString());
        }
    }
    

提交回复
热议问题