Skip to content

Commit

Permalink
SLI-1734 Use ProgressIndicator to compute git information
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel committed Dec 6, 2024
1 parent b463c35 commit db049f6
Showing 1 changed file with 25 additions and 17 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
)
}
}

0 comments on commit db049f6

Please sign in to comment.