Skip to content

Commit

Permalink
feat(git-provider): implement lookupGitData and ShireVcsCommit updates
Browse files Browse the repository at this point in the history
…#41

- Add implementation for retrieving git commit data in GitQLDataProvider.
- Update ShireVcsCommit data class to include hash, authorDate, and committerDate fields.
- Utilize IntelliJ's progress API to fetch commit history asynchronously.
  • Loading branch information
phodal committed Jul 30, 2024
1 parent 5af617e commit 2d56eaf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package com.phodal.shirecore.vcs
* author, authorEmail, committer, committerEmail, hash, date, message, fullMessage
*/
data class ShireVcsCommit(
val hash: String,
val authorName: String,
val authorEmail: String,
val authorDate: Long,
val committerName: String,
val committerEmail: String,
val hash: String,
val date: String,
val committerDate: Long,
val message: String,
val fullMessage: String
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,89 @@
package com.phodal.shirelang.git.provider

import com.intellij.ide.DataManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.progress.runBlockingCancellable
import com.intellij.openapi.project.Project
import com.phodal.shirecore.ShireCoreBundle
import com.phodal.shirecore.provider.shire.ShireQLDataProvider
import com.phodal.shirecore.provider.shire.ShireQLDataType
import com.phodal.shirecore.vcs.ShireVcsCommit
import git4idea.GitCommit
import git4idea.history.GitHistoryUtils
import git4idea.repo.GitRepositoryManager
import kotlinx.coroutines.future.await
import java.util.concurrent.CompletableFuture

class GitQLDataProvider : ShireQLDataProvider {
override fun lookupGitData(myProject: Project, dataTypes: List<ShireQLDataType>): Map<ShireQLDataType, Any?> {
TODO("Not yet implemented")
val result = mutableMapOf<ShireQLDataType, Any?>()
val dataContext = DataManager.getInstance().getDataContextFromFocus().result

Check notice on line 23 in toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitQLDataProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Accessor call that can be replaced with property access syntax

Use of getter method instead of property access syntax

dataTypes.forEach {
when (it) {
ShireQLDataType.GIT_COMMIT -> {
val commits = buildCommits(myProject)
result[it] = commits
}
ShireQLDataType.GIT_BRANCH -> TODO()
ShireQLDataType.GIT_FILE_COMMIT -> TODO()
ShireQLDataType.GIT_FILE_BRANCH -> TODO()
else -> {
result[it] = null
}
}
}

return result
}

private fun buildCommits(myProject: Project): List<ShireVcsCommit> {
val repository = GitRepositoryManager.getInstance(myProject).repositories.firstOrNull() ?: return emptyList()
val branchName = repository.currentBranchName

/**
* Refs to [com.intellij.execution.process.OSProcessHandler.checkEdtAndReadAction], we should handle in this
* way, another example can see in [git4idea.GitPushUtil.findOrPushRemoteBranch]
*/
val future = CompletableFuture<List<GitCommit>>()
val task = object : Task.Backgroundable(myProject, ShireCoreBundle.message("shire.ref.loading"), false) {
override fun run(indicator: ProgressIndicator) {
val commits: List<GitCommit> = try {
// in some case, maybe not repo or branch, so we should handle it
GitHistoryUtils.history(project, repository.root, branchName)
} catch (e: Exception) {
emptyList()
}

future.complete(commits)
}
}

ProgressManager.getInstance()
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))

val results: MutableList<ShireVcsCommit> = mutableListOf()
runBlockingCancellable {

Check warning on line 69 in toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitQLDataProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'runBlockingCancellable(kotlin.jvm.functions.Function2,? extends java.lang.Object\>)' is declared in unstable 'com.intellij.openapi.progress.CoroutinesKt' marked with @ApiStatus.Experimental
future.await().forEach {
val commit = ShireVcsCommit(
it.id.asString(),
it.author.name,
it.author.email,
it.authorTime,
it.committer.name,
it.committer.email,
it.commitTime,
it.fullMessage,
it.fullMessage,
)

results.add(commit)
}
}

return results.toList()
}
}

0 comments on commit 2d56eaf

Please sign in to comment.