1

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!!!

Kishore_2021
  • 645
  • 12
  • 38

1 Answers1

1

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.

Mahesh Gosemath
  • 416
  • 1
  • 4
  • 12
  • Why authentication is not required for cloning the same repository on _'Git Bash'_ command prompt i.e. **git clone https://github.com/github/testrepo.git** – Kishore_2021 Aug 20 '13 at 06:55
  • 1
    This thread seems relevant to your question - http://stackoverflow.com/questions/7328826/curious-where-does-git-store-user-information – Mahesh Gosemath Aug 20 '13 at 07:22
  • Thx @Mahesh. Yes I got some info about saving user authentication But how to do this in Java by using EGit(http://wiki.eclipse.org/JGit/User_Guide) APIs. – Kishore_2021 Aug 20 '13 at 07:33
  • 1
    I hope this helps - http://stackoverflow.com/questions/10311857/jgit-connect-to-distant-repository – Mahesh Gosemath Aug 20 '13 at 07:50
  • According to this post, User Authentication for remote repository is not possible by Java APIs. Then what should do in this case. – Kishore_2021 Aug 20 '13 at 08:31
  • 1
    what is your purpose for doing this? May be there could be another way round. Are you doing this to test JGIT APIs or something else? – Mahesh Gosemath Aug 20 '13 at 08:40
  • To provide support in my Java application for Git(http://git-scm.com) version control similar to SVN. But in SVN authentication can be given as _org.tmatesoft.svn.core.io.SVNRepository.setAuthenticationManager()_. But how to do this in Git, don't know. – Kishore_2021 Aug 20 '13 at 09:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35830/discussion-between-kishore-and-mahesh-gosemath) – Kishore_2021 Aug 20 '13 at 09:42