8

I searched through Google, forums and JGit user guide but couldn't find how to connect to a distant repository with the API. Anyone has an example or just an idea on how to do that?

Thanks for your help.

Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
Brice
  • 81
  • 1
  • 2

2 Answers2

6

Currently, JGit 2.0.0-SNAPSHOT does only offer

org.eclipse.jgit.storage.file.FileRepository
org.eclipse.jgit.storage.dfs.InMemoryRepository

concrete Repository classes, meaning that since org.eclipse.jgit.api.Git takes a Repository, it is not possible to work remotely. Since Git by itself is not designed to operate remotely in the way I think you mean, I doubt we will see such a feature any time soon.

MORE ON THAT:

Consequently, you will need to clone locally. You do that by issuing

    Git.cloneRepository()
       .setURI(myRemoteURIString)
       .setDirectory(new File(myLocalPathString))
       .call();

However, for reasons of consistency in Git you should clone a bare repository only, so a non-bare repository in a remote location, while not technically, is practically inaccessible.

Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
  • Hi and thank for your answer Luca. So the best way to get the last version of a project id to clone it in a new local directory using the `CloneCommand clone = Git.cloneRepository();` Am i right ? – Brice Apr 25 '12 at 15:32
  • Partially. As I added above, if you have a non-bare repository somewhere, it is practically infeasible to work on it, since you should not clone it. – Luca Geretti Apr 25 '12 at 15:40
0

I am not sure I understand the question since Git is made for accessing other repositories, this is what is meant by "Git is distributed".

If you want to connect to ONE distant repository, then yeah you should clone it.

I don't know if that is what you are looking for, but you can also use multiple remotes. Adding one more is done with Git using git remote add <remote_name> <remote_uri>. As for Jgit, I unfortunately can't remember the code to do it simply, but you can figure this out.

At least it's possible by modifying the configuration, calling getConfig() from a Repository object and then calling setString(...) on it - don't forget to save the configuration in the end. But before modifying the configuration, I think you should first get to know more about Git and JGit.

I recommend you to read more about it, and play a bit with your repository. Take a look at this article : http://caiustheory.com/adding-a-remote-to-existing-git-repo . Another one that will help you down the road is How do I do the equivalent of “git remote update” with jgit?

Maybe someone else knows exactly which commands to run and can help.

Community
  • 1
  • 1
Vince
  • 1,570
  • 3
  • 27
  • 48