Reputation: 699
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!!!
Upvotes: 1
Views: 2963
Reputation: 426
I think authentication is the reason behind this. Your code nowhere mentions username and password and while accessing remote git branches you need to provide your credentials. Check if the available resources provide any way of authentication.
Upvotes: 1