Skip to content

Commit

Permalink
feat(git): enhance commitChanges function in GitFunctionProvider
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Sep 13, 2024
1 parent e048ab9 commit fb7c286
Showing 1 changed file with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 -> {
Expand All @@ -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)

Expand All @@ -80,6 +98,27 @@ class GitFunctionProvider : ToolchainFunctionProvider {
// }
}

private fun collectPathsFromFiles(project: Project, allFiles: Sequence<VirtualFile>): Sequence<FilePath> {
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<GitRemoteBranch> {
val progressIndicator =
ObjectUtils.notNull(ProgressManager.getInstance().progressIndicator, EmptyProgressIndicator())
Expand Down

0 comments on commit fb7c286

Please sign in to comment.