Skip to content

Commit

Permalink
fix: should not include unselected file when generate commit message #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jialiu-github committed Apr 25, 2024
1 parent 1542b0a commit 97eba22
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/222/main/kotlin/cc/unitmesh/devti/vcs/VcsUtil.kt
@@ -1,13 +1,15 @@
package cc.unitmesh.devti.vcs

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.vcs.VcsDataKeys
import com.intellij.openapi.vcs.changes.Change
import com.intellij.openapi.vcs.changes.CurrentContentRevision
import com.intellij.vcs.commit.AbstractCommitWorkflowHandler
import com.intellij.vcs.commit.CommitWorkflowUi

object VcsUtil {
fun getChanges(e: AnActionEvent): List<Change>? {
val prompting = e.project?.service<VcsPrompting>() ?: return null
val changes = prompting.getChanges()
return changes.ifEmpty { null }
fun getCommitWorkFlowUi(e: AnActionEvent): CommitWorkflowUi? {
val commitWorkFlowHandler = e.getData(VcsDataKeys.COMMIT_WORKFLOW_HANDLER)
return (commitWorkFlowHandler as? AbstractCommitWorkflowHandler<*, *>)?.ui ?: return null
}
}
17 changes: 2 additions & 15 deletions src/233/main/kotlin/cc/unitmesh/devti/vcs/VcsUtil.kt
Expand Up @@ -6,20 +6,7 @@ import com.intellij.openapi.vcs.changes.Change
import com.intellij.openapi.vcs.changes.CurrentContentRevision

object VcsUtil {
fun getChanges(e: AnActionEvent): List<Change>? {
val commitWorkflowUi = e.getData(VcsDataKeys.COMMIT_WORKFLOW_UI) ?: return null

val changes = commitWorkflowUi.getIncludedChanges()
val unversionedFiles = commitWorkflowUi.getIncludedUnversionedFiles()

val unversionedFileChanges = unversionedFiles.map {
Change(null, CurrentContentRevision(it))
}

if (changes.isNotEmpty() || unversionedFileChanges.isNotEmpty()) {
return changes + unversionedFileChanges
}

return null
fun getCommitWorkFlowUi(e: AnActionEvent): CommitWorkflowUi? {
return e.getData(VcsDataKeys.COMMIT_WORKFLOW_UI) ?: return null
}
}
Expand Up @@ -20,8 +20,10 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.VcsDataKeys
import com.intellij.openapi.vcs.changes.Change
import com.intellij.openapi.vcs.changes.CurrentContentRevision
import com.intellij.openapi.vcs.ui.CommitMessage
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.vcs.commit.CommitWorkflowUi
import com.intellij.vcs.log.VcsLogFilterCollection
import com.intellij.vcs.log.VcsLogProvider
import com.intellij.vcs.log.impl.VcsProjectLog
Expand Down Expand Up @@ -60,7 +62,8 @@ class CommitMessageSuggestionAction : ChatBaseAction() {

override fun executeAction(event: AnActionEvent) {
val project = event.project ?: return
val changes = VcsUtil.getChanges(event) ?: return
val commitWorkflowUi = VcsUtil.getCommitWorkFlowUi(event) ?: return
val changes = getChanges(commitWorkflowUi) ?: return
val diffContext = project.service<VcsPrompting>().prepareContext(changes)

if (diffContext.isEmpty() || diffContext == "\n") {
Expand Down Expand Up @@ -172,6 +175,21 @@ class CommitMessageSuggestionAction : ChatBaseAction() {
logger.info("Prompt: $prompter")
return prompter
}

fun getChanges(commitWorkflowUi: CommitWorkflowUi): List<Change>? {
val changes = commitWorkflowUi.getIncludedChanges()
val unversionedFiles = commitWorkflowUi.getIncludedUnversionedFiles()

val unversionedFileChanges = unversionedFiles.map {
Change(null, CurrentContentRevision(it))
}

if (changes.isNotEmpty() || unversionedFileChanges.isNotEmpty()) {
return changes + unversionedFileChanges
}

return null
}
}


Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/vcs/VcsPrompting.kt
Expand Up @@ -81,7 +81,6 @@ class VcsPrompting(private val project: Project) {
return writer.toString()
}


fun getChanges(): List<Change> {
val changeListManager = ChangeListManager.getInstance(project)
return changeListManager.changeLists.flatMap { it.changes }
Expand Down

0 comments on commit 97eba22

Please sign in to comment.