By using the cookbook examples, I am able to fetch the all folders (projects) under HEAD in a offline Git repository e.g.
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());
}
This code lists the all folders for a given Git repository url located on my machine But
Ref head = repository.getRef("HEAD");
line returning null for any online Git repository URL e.g. https://github.com/github/testrepo.git
What I am missing in the second case.
Thanks in advance!!!