diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/patternaction/PatternActionFunc.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/patternaction/PatternActionFunc.kt index 523a612b7..6ff6785b4 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/patternaction/PatternActionFunc.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/patternaction/PatternActionFunc.kt @@ -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) } } diff --git a/toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitFunctionProvider.kt b/toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitFunctionProvider.kt index 21ab52d3a..4821820bb 100644 --- a/toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitFunctionProvider.kt +++ b/toolsets/git/src/main/kotlin/com/phodal/shirelang/git/provider/GitFunctionProvider.kt @@ -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 /** @@ -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 { + val progressIndicator = + ObjectUtils.notNull(ProgressManager.getInstance().progressIndicator, EmptyProgressIndicator()) + + val branchesCollection: GitBranchesCollection = repository.branches + val future = CompletableFuture() + + 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 } } \ No newline at end of file