From 002171acf31aa14a56639ffde2194586cc2c12f5 Mon Sep 17 00:00:00 2001 From: Nicolas QUINQUENEL Date: Fri, 6 Dec 2024 15:35:36 +0100 Subject: [PATCH] SLI-1734 Use ProgressIndicator to compute git information (#1261) --- .../org/sonarlint/intellij/git/GitRepo.kt | 42 +++++++++++-------- .../intellij/SonarLintIntelliJClient.kt | 6 ++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/git/src/main/kotlin/org/sonarlint/intellij/git/GitRepo.kt b/git/src/main/kotlin/org/sonarlint/intellij/git/GitRepo.kt index 1fa9c8640..9fe7acd5b 100644 --- a/git/src/main/kotlin/org/sonarlint/intellij/git/GitRepo.kt +++ b/git/src/main/kotlin/org/sonarlint/intellij/git/GitRepo.kt @@ -19,7 +19,9 @@ */ package org.sonarlint.intellij.git +import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project +import git4idea.GitRevisionNumber import git4idea.commands.Git import git4idea.commands.GitCommand import git4idea.commands.GitLineHandler @@ -74,15 +76,14 @@ class GitRepo(private val repo: GitRepository, private val project: Project) : V } private fun distance(project: Project, repository: GitRepository, from: String, to: String): Int? { - val mergeBase = try { - GitHistoryUtils.getMergeBase(project, repository.root, from, to) ?: return null - } catch (e: IllegalStateException) { - // SLI-1381: "There is no ProgressIndicator or Job in this thread" should simply be a loud error - SonarLintConsole.get(project).debug("Couldn't compute the git distance, reason: ${e.message}") - return null - } - val aheadCount = getNumberOfCommitsBetween(repository, from, mergeBase.asString()) ?: return null - val behindCount = getNumberOfCommitsBetween(repository, to, mergeBase.asString()) ?: return null + val revisionNumber = ProgressManager.getInstance().runProcessWithProgressSynchronously( + { GitHistoryUtils.getMergeBase(project, repository.root, from, to) }, + "SonarQube: Computing branch information", + true, + repository.project + ) + val aheadCount = getNumberOfCommitsBetween(repository, from, revisionNumber.asString()) ?: return null + val behindCount = getNumberOfCommitsBetween(repository, to, revisionNumber.asString()) ?: return null return aheadCount + behindCount } @@ -91,13 +92,20 @@ class GitRepo(private val repo: GitRepository, private val project: Project) : V from: String, to: String, ): Int? { - val handler = GitLineHandler(repository.project, repository.root, GitCommand.REV_LIST) - handler.addParameters("--count", "$from..$to") - handler.setSilent(true) - return try { - Integer.parseInt(Git.getInstance().runCommand(handler).getOutputOrThrow().trim()) - } catch (e: Exception) { - throw Exception("Cannot get number of commits between '$from' and '$to'", e) - } + return ProgressManager.getInstance().runProcessWithProgressSynchronously( + { + val handler = GitLineHandler(repository.project, repository.root, GitCommand.REV_LIST) + handler.addParameters("--count", "$from..$to") + handler.setSilent(true) + try { + Integer.parseInt(Git.getInstance().runCommand(handler).getOutputOrThrow().trim()) + } catch (e: Exception) { + throw Exception("Cannot get number of commits between '$from' and '$to'", e) + } + }, + "SonarQube: Computing branch information", + true, + repository.project + ) } } diff --git a/src/main/java/org/sonarlint/intellij/SonarLintIntelliJClient.kt b/src/main/java/org/sonarlint/intellij/SonarLintIntelliJClient.kt index f7e135513..f479c23b3 100644 --- a/src/main/java/org/sonarlint/intellij/SonarLintIntelliJClient.kt +++ b/src/main/java/org/sonarlint/intellij/SonarLintIntelliJClient.kt @@ -628,7 +628,9 @@ object SonarLintIntelliJClient : SonarLintRpcClientDelegate { } } ?: return null val repo = repositories.first() - return repo.electBestMatchingServerBranchForCurrentHead(mainBranchName, allBranchesNames) ?: mainBranchName + return computeOnPooledThread("Electing best matching branch") { + repo.electBestMatchingServerBranchForCurrentHead(mainBranchName, allBranchesNames) ?: mainBranchName + } } override fun matchProjectBranch( @@ -743,7 +745,7 @@ object SonarLintIntelliJClient : SonarLintRpcClientDelegate { filesInContentRoots.addAll(it.listFiles(module)) } - val forcedLanguages = collectContributedLanguages(module, filesInContentRoots) + val forcedLanguages = collectContributedLanguages(module, filesInContentRoots).toMap() val clientFiles = filesInContentRoots.mapNotNull { file -> val forcedLanguage = forcedLanguages[file]?.let { fl -> Language.valueOf(fl.name) }