Skip to content

Commit e7a692e

Browse files
authored
Merge pull request #161 from jialiu-github/master
fix: should not include unselected file when generate commit message …
2 parents 1542b0a + b604867 commit e7a692e

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cc.unitmesh.devti.vcs
22

33
import com.intellij.openapi.actionSystem.AnActionEvent
4-
import com.intellij.openapi.components.service
4+
import com.intellij.openapi.vcs.VcsDataKeys
55
import com.intellij.openapi.vcs.changes.Change
6+
import com.intellij.openapi.vcs.changes.CurrentContentRevision
7+
import com.intellij.vcs.commit.AbstractCommitWorkflowHandler
8+
import com.intellij.vcs.commit.CommitWorkflowUi
69

710
object VcsUtil {
8-
fun getChanges(e: AnActionEvent): List<Change>? {
9-
val prompting = e.project?.service<VcsPrompting>() ?: return null
10-
val changes = prompting.getChanges()
11-
return changes.ifEmpty { null }
11+
fun getCommitWorkFlowUi(e: AnActionEvent): CommitWorkflowUi? {
12+
val commitWorkFlowHandler = e.getData(VcsDataKeys.COMMIT_WORKFLOW_HANDLER)
13+
return (commitWorkFlowHandler as? AbstractCommitWorkflowHandler<*, *>)?.ui ?: return null
1214
}
1315
}

src/233/main/kotlin/cc/unitmesh/devti/vcs/VcsUtil.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,10 @@ import com.intellij.openapi.actionSystem.AnActionEvent
44
import com.intellij.openapi.vcs.VcsDataKeys
55
import com.intellij.openapi.vcs.changes.Change
66
import com.intellij.openapi.vcs.changes.CurrentContentRevision
7+
import com.intellij.vcs.commit.CommitWorkflowUi
78

89
object VcsUtil {
9-
fun getChanges(e: AnActionEvent): List<Change>? {
10-
val commitWorkflowUi = e.getData(VcsDataKeys.COMMIT_WORKFLOW_UI) ?: return null
11-
12-
val changes = commitWorkflowUi.getIncludedChanges()
13-
val unversionedFiles = commitWorkflowUi.getIncludedUnversionedFiles()
14-
15-
val unversionedFileChanges = unversionedFiles.map {
16-
Change(null, CurrentContentRevision(it))
17-
}
18-
19-
if (changes.isNotEmpty() || unversionedFileChanges.isNotEmpty()) {
20-
return changes + unversionedFileChanges
21-
}
22-
23-
return null
10+
fun getCommitWorkFlowUi(e: AnActionEvent): CommitWorkflowUi? {
11+
return e.getData(VcsDataKeys.COMMIT_WORKFLOW_UI) ?: return null
2412
}
2513
}

src/main/kotlin/cc/unitmesh/devti/actions/vcs/CommitMessageSuggestionAction.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import com.intellij.openapi.diagnostic.logger
2020
import com.intellij.openapi.project.Project
2121
import com.intellij.openapi.vcs.VcsDataKeys
2222
import com.intellij.openapi.vcs.changes.Change
23+
import com.intellij.openapi.vcs.changes.CurrentContentRevision
2324
import com.intellij.openapi.vcs.ui.CommitMessage
2425
import com.intellij.openapi.vfs.VirtualFile
26+
import com.intellij.vcs.commit.CommitWorkflowUi
2527
import com.intellij.vcs.log.VcsLogFilterCollection
2628
import com.intellij.vcs.log.VcsLogProvider
2729
import com.intellij.vcs.log.impl.VcsProjectLog
@@ -60,7 +62,8 @@ class CommitMessageSuggestionAction : ChatBaseAction() {
6062

6163
override fun executeAction(event: AnActionEvent) {
6264
val project = event.project ?: return
63-
val changes = VcsUtil.getChanges(event) ?: return
65+
val commitWorkflowUi = VcsUtil.getCommitWorkFlowUi(event) ?: return
66+
val changes = getChanges(commitWorkflowUi) ?: return
6467
val diffContext = project.service<VcsPrompting>().prepareContext(changes)
6568

6669
if (diffContext.isEmpty() || diffContext == "\n") {
@@ -172,6 +175,21 @@ class CommitMessageSuggestionAction : ChatBaseAction() {
172175
logger.info("Prompt: $prompter")
173176
return prompter
174177
}
178+
179+
fun getChanges(commitWorkflowUi: CommitWorkflowUi): List<Change>? {
180+
val changes = commitWorkflowUi.getIncludedChanges()
181+
val unversionedFiles = commitWorkflowUi.getIncludedUnversionedFiles()
182+
183+
val unversionedFileChanges = unversionedFiles.map {
184+
Change(null, CurrentContentRevision(it))
185+
}
186+
187+
if (changes.isNotEmpty() || unversionedFileChanges.isNotEmpty()) {
188+
return changes + unversionedFileChanges
189+
}
190+
191+
return null
192+
}
175193
}
176194

177195

src/main/kotlin/cc/unitmesh/devti/vcs/VcsPrompting.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class VcsPrompting(private val project: Project) {
8181
return writer.toString()
8282
}
8383

84-
8584
fun getChanges(): List<Change> {
8685
val changeListManager = ChangeListManager.getInstance(project)
8786
return changeListManager.changeLists.flatMap { it.changes }

0 commit comments

Comments
 (0)