通过使用食谱示例,我可以在离线 Git 存储库中获取 HEAD 下的所有文件夹(项目),例如
String url = "C:/Users/kishore/git/JavaRepos/.git";
File gitDir = new File(url);
Repository repository = new FileRepository(gitDir);
Ref head = repository.getRef("HEAD");
RevWalk walk = new RevWalk(repository);
RevCommit commit = walk.parseCommit(head.getObjectId());
RevTree tree = commit.getTree();
System.out.println("Having tree: " + tree);
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
treeWalk.setRecursive(false);
while(treeWalk.next()) {
System.out.println("Folder Path: " + treeWalk.getPathString());
System.out.println("Folder Name: " + treeWalk.getNameString());
System.out.println("Folder depth: " + treeWalk.getDepth());
System.out.println("Folder Tree Count: " + treeWalk.getTreeCount());
}
此代码列出了位于我的机器上的给定 Git 存储库 url 的所有文件夹但是
Ref head = repository.getRef("HEAD");
为任何在线 Git 存储库 URL 返回 null 的行,例如https://github.com/github/testrepo.git
在第二种情况下我缺少什么。
提前致谢!!!