From fb7c286f31bee87565ac83388ac932becfee7daa Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 13 Sep 2024 15:45:29 +0800 Subject: [PATCH] feat(git): enhance commitChanges function in GitFunctionProvider Enhanced the commitChanges function in GitFunctionProvider to add files to the commit. Added helper functions to collect paths from files and get extension for a given VCS. Also, updated the function call to commitChanges to include the project parameter. --- .../git/provider/GitFunctionProvider.kt | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) 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 4821820bb..b80b6f7e7 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 @@ -5,20 +5,26 @@ 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.AbstractVcs +import com.intellij.openapi.vcs.FilePath +import com.intellij.openapi.vcs.ProjectLevelVcsManager +import com.intellij.openapi.vcs.changes.ChangeListManager import com.intellij.openapi.vcs.changes.CommitContext +import com.intellij.openapi.vcs.changes.actions.ScheduleForAdditionActionExtension +import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.ObjectUtils +import com.intellij.vcsUtil.VcsUtil 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 git4idea.util.GitFileUtils import java.util.concurrent.CompletableFuture @@ -56,7 +62,7 @@ class GitFunctionProvider : ToolchainFunctionProvider { return when (gitFunc) { GitFunction.Commit -> { - commitChanges(repository, args.first() as String) + commitChanges(project, repository, args.first() as String) } GitFunction.Push -> { @@ -69,7 +75,19 @@ class GitFunctionProvider : ToolchainFunctionProvider { * * How to find code in IDEA: [GitCommand.Commit] */ - fun commitChanges(repository: GitRepository, commitMessage: String) { + fun commitChanges(project: Project, repository: GitRepository, commitMessage: String) { + val root = repository.root + val changeListManager = ChangeListManager.getInstance(project) + val virtualFiles = changeListManager + .allChanges + .mapNotNull { it.virtualFile } + .asSequence() + + val files = collectPathsFromFiles( + project, virtualFiles + ).toList() + + GitFileUtils.addPaths(project, root, files, false, false) var commitContext: CommitContext = CommitContext() val option: GitCommitOptions = GitCommitOptions(commitContext) @@ -80,6 +98,27 @@ class GitFunctionProvider : ToolchainFunctionProvider { // } } + private fun collectPathsFromFiles(project: Project, allFiles: Sequence): Sequence { + val vcsManager = ProjectLevelVcsManager.getInstance(project) + val changeListManager = ChangeListManager.getInstance(project) + + return allFiles + .filter { file -> + val actionExtension = getExtensionFor(project, vcsManager.getVcsFor(file)) + actionExtension != null && + changeListManager.getStatus(file).let { status -> + if (file.isDirectory) actionExtension.isStatusForDirectoryAddition(status) else actionExtension.isStatusForAddition( + status + ) + } + } + .map(VcsUtil::getFilePath) + } + + private fun getExtensionFor(project: Project, vcs: AbstractVcs?) = + if (vcs == null) null + else ScheduleForAdditionActionExtension.EP_NAME.findFirstSafe { it.getSupportedVcs(project) == vcs } + fun pushChanges(project: Project, repository: GitRepository): CompletableFuture { val progressIndicator = ObjectUtils.notNull(ProgressManager.getInstance().progressIndicator, EmptyProgressIndicator())