Skip to content

Commit

Permalink
fix(patternaction): 修复PatternActionFunc中的空指针异常
Browse files Browse the repository at this point in the history
- 修改PatternActionFunc以处理null类型,避免空指针异常。
- 添加else分支以处理未知的PatternActionFuncType。

feat(git): 添加对Git函数的支持
- 添加对Git函数的支持,包括提交和推送功能。
- 修复了提交和推送过程中的错误处理。
- 添加了必要的导入和注释以提高代码可读性。
  • Loading branch information
phodal committed Sep 13, 2024
1 parent 820ec89 commit e048ab9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ sealed class PatternActionFunc(val type: PatternActionFuncType) {
ToolchainFunction(funcName, args)
}

PatternActionFuncType.TOOLCHAIN_FUNCTION -> ToolchainFunction(funcName, args)
PatternActionFuncType.BATCH -> {
Batch(args[0], args.drop(1))
}
PatternActionFuncType.DESTROY -> {
Destroy()
}
null -> {
PatternActionFuncType.TOOLCHAIN_FUNCTION -> ToolchainFunction(funcName, args)
else -> {
ToolchainFunction(funcName, args)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package com.phodal.shirelang.git.provider

import com.intellij.dvcs.DvcsUtil
import com.intellij.dvcs.push.PushSpec
import com.intellij.dvcs.ui.DvcsBundle
import com.intellij.openapi.progress.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.changes.CommitContext
import com.intellij.util.ObjectUtils
import com.phodal.shirecore.provider.function.ToolchainFunctionProvider
import git4idea.GitRemoteBranch
import git4idea.GitVcs
import git4idea.branch.GitBranchesCollection
import git4idea.commands.Git
import git4idea.commands.GitStandardProgressAnalyzer
import git4idea.i18n.GitBundle
import git4idea.push.GitPushOperation
import git4idea.push.GitPushSource
import git4idea.push.GitPushSupport
import git4idea.repo.GitRepository
import git4idea.repo.GitRepositoryManager
import java.util.concurrent.CompletableFuture


/**
Expand Down Expand Up @@ -34,33 +50,87 @@ class GitFunctionProvider : ToolchainFunctionProvider {
val gitFunc = GitFunction.fromString(funcName)
?: throw IllegalArgumentException("Shire[GitTool]: Invalid Git function name")

val repositoryManager = GitRepositoryManager.getInstance(project)
val repository = repositoryManager.repositories.stream().findFirst().orElse(null)
?: return throw IllegalArgumentException("Shire[GitTool]: No git repository found")

return when (gitFunc) {
GitFunction.Commit -> {
commitChanges(project, args.first() as String)
commitChanges(repository, args.first() as String)
}

GitFunction.Push -> {

pushChanges(project, repository)
}
}
}

fun commitChanges(project: Project, commitMessage: String) {
val repositoryManager = GitRepositoryManager.getInstance(project)
val repository = repositoryManager.repositories.stream().findFirst().orElse(null)
?: return throw IllegalArgumentException("Shire[GitTool]: No git repository found")

/**
*
* How to find code in IDEA: [GitCommand.Commit]
*/
fun commitChanges(repository: GitRepository, commitMessage: String) {
var commitContext: CommitContext = CommitContext()
val option: GitCommitOptions = GitCommitOptions(commitContext)

try {
GitRepositoryCommitter(repository, option).commitStaged(commitMessage)
} catch (e: Exception) {
throw RuntimeException("Shire[GitTool]: Failed to commit changes")
}
// try {
GitRepositoryCommitter(repository, option).commitStaged(commitMessage)
// } catch (e: Exception) {
// throw RuntimeException("Shire[GitTool]: Failed to commit changes")
// }
}

fun pushChanges(project: Project) {
fun pushChanges(project: Project, repository: GitRepository): CompletableFuture<GitRemoteBranch> {
val progressIndicator =
ObjectUtils.notNull(ProgressManager.getInstance().progressIndicator, EmptyProgressIndicator())

val branchesCollection: GitBranchesCollection = repository.branches
val future = CompletableFuture<GitRemoteBranch>()

for (branch in branchesCollection.localBranches) {
val pushTarget = GitPushSupport.getPushTargetIfExist(repository, branch!!)
?: continue

val gitPushSupport = DvcsUtil.getPushSupport(GitVcs.getInstance(project)) as? GitPushSupport
?: return CompletableFuture.failedFuture(ProcessCanceledException())

ProgressManager.getInstance().runProcessWithProgressAsynchronously(
object : Task.Backgroundable(repository.project, DvcsBundle.message("push.process.pushing"), true) {

override fun run(indicator: ProgressIndicator) {
indicator.text = DvcsBundle.message("push.process.pushing")
val pushSpec = PushSpec(GitPushSource.create(branch), pushTarget)
val pushResult = GitPushOperation(
repository.project,
gitPushSupport,
mapOf(repository to pushSpec),
null,
false,
false
)
.execute().results[repository] ?: error("Missing push result")
check(pushResult.error == null) {
GitBundle.message("push.failed.error.message", pushResult.error.orEmpty())
}
}

override fun onSuccess() {
future.complete(pushTarget.branch)
}

override fun onThrowable(error: Throwable) {
future.completeExceptionally(error)
}

override fun onCancel() {
future.completeExceptionally(ProcessCanceledException())
}
}, progressIndicator
)

return future
}

return future
}
}

0 comments on commit e048ab9

Please sign in to comment.