What is the best way to wait for the VCS system to be ready in both new and existing projects? In other words, I want to wait until the Git Repository information is loaded into the IDE (including what remotes exist).
I know com.intellij.openapi.vcs.ProjectLevelVcsManager#awaitInitialization exists but I am not sure if that waits for everything to be complete.
Tracing the code it seems that the initial scan for a new project/module is scheduled (with a delay) in com.intellij.openapi.vcs.impl.ModuleVcsDetector.ModuleVcsDetectorStartUpActivity at VCS init order VcsInitObject.MAPPINGS.order + 10, but nothing in the VCS init system seems to wait for it to be complete.
There is a method com.intellij.openapi.vcs.impl.ModuleVcsDetector#awaitInitialDetection which is @Internal but nothing in the codebase seems to call it.
Was this just an oversight and ProjectLevelVcsManager#awaitInitialization should invoke it?
Please understand that in fact there is no such state, you basically can only prepare for some initial consistent state but the system state may constantly change. What tasks are you trying to solve?
Please understand that in fact there is no such state, you basically can only prepare for some initial consistent state but the system state may constantly change. What tasks are you trying to solve?
Waiting for the initial set of remotes to perform a background API call, I then listen to VcsRepositoryManager.VCS_REPOSITORY_MAPPING_UPDATED and GitRepository.GIT_REPO_CHANGE to invalidate the cache, but trying to cut down on blinking and invalid state when IDE opens.
What are your thoughts on awaitInitialization not calling ModuleVcsDetector#awaitInitialDetection?
Ya, I do have one. I was trying to improve the UX though in the case the IDE isn’t ready yet when we need to build the default filters. The set of defaults are based on the results of the API call and the async nature can throw them off which result in inaccurate results / slower API calls by not reducing the search space.
Was looking for a good way to have the tool window stay in loading mode with spinner until the IDE is more “ready” so we don’t build inaccurate filters. One of the filters is the Git remotes.
@abrooksv will something like this work for you? Repositories state should be loaded at this point. Also you can add debounce to the flow collection to avoid blinking in the unlikely case of very frequent updates.
Thx, I used that but had to add this spin loop to the end to try to to give time for the new modules to load in
// Wait up to 3 seconds for repositories to be available before considering the initialization complete.
// There is a race condition in VCS init systems that the initial ModuleVcsDetectorStartUpActivity
// will be delayed by 1s (see time in the merging update queue) such that a brand-new project can
// have its initial mappings delayed till after the ProjectLevelVcsManager says we are init-ed
repeat(30) {
val repositories = GitUtil.getRepositories(project)
if (repositories.isNotEmpty()) {
return@repeat
}
thisLogger().info("Waiting for git repositories to be initialized...")
delay(100)
}
Worst case is its a project with no Git data at all (or new repo) so the user will see a spinner for 3 more secs. It is still best effort, because I really don’t want to have to wait for the first indexing to be complete