Skip to content

Commit

Permalink
SLI-1734 Use ProgressIndicator to compute git information (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel authored Dec 6, 2024
1 parent 549fdef commit 002171a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
42 changes: 25 additions & 17 deletions git/src/main/kotlin/org/sonarlint/intellij/git/GitRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<GitRevisionNumber, Exception>(
{ 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
}

Expand All @@ -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<Int?, Exception>(
{
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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) }
Expand Down

0 comments on commit 002171a

Please sign in to comment.