简体   繁体   中英

How to List all folders (projects) of a Git Repository

By using the cookbook examples, I am able to fetch the all folders (projects) under HEAD in a offline Git repository eg

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 eg https://github.com/github/testrepo.git

What I am missing in the second case.

Thanks in advance!!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM