Skip to content

Commit

Permalink
Merge pull request #161 from jialiu-github/master
Browse files Browse the repository at this point in the history
fix: should not include unselected file when generate commit message …
  • Loading branch information
phodal authored Apr 25, 2024
2 parents 1542b0a + b604867 commit e7a692e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/222/main/kotlin/cc/unitmesh/devti/vcs/VcsUtil.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
18 changes: 3 additions & 15 deletions src/233/main/kotlin/cc/unitmesh/devti/vcs/VcsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@ import com.intellij.openapi.actionSystem.AnActionEvent
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.CommitWorkflowUi

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
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 e7a692e

Please sign in to comment.