Problem with GitRepository detection

Answered

Hi! I have such a problem - my plugin turns itself on/off by listening Application message bus with ProjectManagerListener and evaluating certain predicate upon Project instance - i.e., in the projectOpened(Project project) handler method of this listener I pass the "project" argument to a separate util method, that tries to determine whether project being opened is under specific Git repository or not.

Code of that method:

public static boolean isWebtestsProject(Project project) {
final GitRepository repo = GitRepositoryManager.getInstance(project).getRepositoryForRoot(ProjectUtil.guessProjectDir(project));
return Optional.ofNullable(repo)
.map(GitRepository::getInfo)
.map(GitRepoInfo::getRemotes)
.map(Collection::stream)
.map(stream ->
stream.anyMatch(remote ->
Optional.ofNullable(remote.getFirstUrl())
.map(url -> url.contains("qa-automation/web_tests.git"))
.orElse(false)
)
)
.orElse(false);
}

The problem is that repo is always null. What am I doing wrong? In git4idea.jar sources I found that GetRepositoryManager#getRepositoryForRoot() calls

checkAndUpdateRepositoriesCollection()

under the hood, so it can't be a "freshness" issues. ProjectUtil.guessProjectDir(project).findChild(".git") is not null too ... 

0
2 comments

I had this issue in my plugin because of a change in API behaviour. See: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006677620-New-Behavior-for-VCS-Root-related-messages-How-do-I-know-when-all-VCS-roots-for-the-project-have-initialized- 

 

Solution: register post startup activity with StartupManager. It is fired when all roots are loaded and indexing is complete.

override fun projectOpened() {
StartupManager.getInstance(project).registerPostStartupActivity { projectInitialized() }
// NOTE: line below fires before vcs roots are initialized.
// (ProjectLevelVcsManagerImpl.getInstance(project) as ProjectLevelVcsManagerImpl).addInitializationRequest(VcsInitObject.AFTER_COMMON) { projectInitialized() }
}

 

1
Avatar
Permanently deleted user

You saved my day, huge thanks! Works like a charm.

0

Please sign in to leave a comment.