How can I receive git repository from plugin?
Answered
I want to get repository url from my plugin.
How can I get it??
Please sign in to leave a comment.
First, you need to add git4idea.jar plugin to your dependencies.
(By adding it as a provided library / to SDK and appending `<depends>Git4Idea</depends>` to plugin.xml)
You can locate it in /plugins/ folder of IDE distribution.
Next, you can use
`GitRepositoryManager.getInstance(project).getRepositories()` to get configured git roots.
`GitRepository.getRemotes()` to get list of remotes in root.
`GitRemote.getUrls()` to get urls for a remote.
Thank you for your reply.
I tried to get Repositories with GitRepositoryManager, but I cannot get repositories.
It is because the repositories can be got after initializing ProjectLevelVcsManager's myMapping.
myMapping is NewMapping class.
This constructor end like this.
vcsManager.addInitializationRequest(VcsInitObject.MAPPINGS, (DumbAwareRunnable)() -> {if (!myProject.isDisposed()) {
activateActiveVcses();
}
});
I'm not sure what these code means.
But I understand this mappings only can be activated after vcsManager's initialization process.
So, I create the code
((ProjectLevelVcsManagerImpl)ProjectLevelVcsManager.getInstance(project)).addInitializationRequest(VcsInitObject.AFTER_COMMON, new Runnable() {@Override
public void run() {
Collection<Repository> repositories = VcsRepositoryManager.getInstance(project).getRepositories();
System.out.println(repositories);
}
});
(Only ProjectLevelVcsManagerImpl) has `addInitializationRequest`...)
This code will wait VCSManager initialization process.
So, I can get repositories safely.
I feel this code is a bit tricky. Is there any other way to get repositories safely?
Probably, it's better to listen for all repository mappings changes (ex: if new root was added by user).
Ex:
By the way, what are you trying to achieve?
Thank you for your advice.
I will change my code.
I want to build a github plugin for intellij.