JGit : connect to distant repository

别说谁变了你拦得住时间么 提交于 2019-12-07 02:46:33

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10311857/jgit-connect-to-distant-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!