我想使用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());
}
}
}
Git不会跟踪自己的目录。您只能从从TreeWalk获取的路径字符串派生非空目录名称。
有关详细说明和可能的解决方法,请参见Git常见问题解答(搜索“空目录”)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句