11package com.github.blarc.ai.commits.intellij.plugin
22
33import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
4+ import com.github.blarc.ai.commits.intellij.plugin.notifications.Notification
5+ import com.github.blarc.ai.commits.intellij.plugin.notifications.sendNotification
46import com.intellij.openapi.actionSystem.AnAction
57import com.intellij.openapi.actionSystem.AnActionEvent
68import com.intellij.openapi.progress.runBackgroundableTask
79import com.intellij.openapi.project.DumbAware
10+ import com.intellij.openapi.project.Project
811import com.intellij.openapi.vcs.VcsDataKeys
12+ import com.intellij.openapi.vcs.changes.Change
913import com.intellij.vcs.commit.AbstractCommitWorkflowHandler
1014import git4idea.commands.Git
1115import git4idea.commands.GitCommand
@@ -21,22 +25,17 @@ class AICommitAction : AnAction(), DumbAware {
2125 override fun actionPerformed (e : AnActionEvent ) {
2226 val project = e.project ? : return
2327
28+ val commitWorkflowHandler =
29+ e.getData(VcsDataKeys .COMMIT_WORKFLOW_HANDLER ) as AbstractCommitWorkflowHandler <* , * >
30+ val includedChanges = commitWorkflowHandler.ui.getIncludedChanges()
2431 val commitMessage = VcsDataKeys .COMMIT_MESSAGE_CONTROL .getData(e.dataContext)
25- val commitWorkflowHandler = e.getData(VcsDataKeys .COMMIT_WORKFLOW_HANDLER ) as AbstractCommitWorkflowHandler <* , * >
2632
2733 runBackgroundableTask(message(" action.background" ), project) {
28- val diff = commitWorkflowHandler.ui.getIncludedChanges().joinToString(" \n " ) {
29-
30- val repository = GitRepositoryManager .getInstance(project).getRepositoryForFile(it.virtualFile)
31- val diffCommand = GitLineHandler (
32- project,
33- repository!! .root,
34- GitCommand .DIFF
35- )
36- diffCommand.addParameters(it.virtualFile!! .path)
37-
38- val commandResult = Git .getInstance().runCommand(diffCommand)
39- commandResult.outputAsJoinedString
34+ val diff = computeDiff(includedChanges, project)
35+
36+ if (diff.isBlank()) {
37+ sendNotification(Notification .emptyDiff())
38+ return @runBackgroundableTask
4039 }
4140
4241 val openAIService = OpenAIService .instance
@@ -46,4 +45,30 @@ class AICommitAction : AnAction(), DumbAware {
4645 }
4746 }
4847 }
48+ private fun computeDiff (
49+ includedChanges : List <Change >,
50+ project : Project
51+ ): String {
52+ val diff = includedChanges.joinToString(" \n " ) {
53+
54+ // Try to get the virtual file for the change and return an empty string if it doesn't exist
55+ val file = it.virtualFile ? : return @joinToString " "
56+
57+ // Try to get the git repository for the file and return an empty string if it doesn't exist
58+ val repository = GitRepositoryManager .getInstance(project).getRepositoryForFile(file)
59+ ? : return @joinToString " "
60+
61+ val diffCommand = GitLineHandler (
62+ project,
63+ repository.root,
64+ GitCommand .DIFF
65+ )
66+ diffCommand.addParameters(" --cached" )
67+ diffCommand.addParameters(file.path)
68+
69+ val commandResult = Git .getInstance().runCommand(diffCommand)
70+ commandResult.outputAsJoinedString
71+ }
72+ return diff
73+ }
4974}
0 commit comments