From 3abd981b49c980dec986cfc23526571f6846ddd0 Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Tue, 10 Sep 2024 22:39:09 +0300 Subject: [PATCH] fix: include selected changes diff only --- .../GenerateGitCommitMessageAction.java | 11 +++++-- .../ee/carlrobert/codegpt/util/GitUtil.kt | 29 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java b/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java index 9052cae2..07829fb1 100644 --- a/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java +++ b/src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java @@ -12,7 +12,6 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.vcs.VcsDataKeys; import com.intellij.openapi.vcs.VcsException; import com.intellij.vcs.commit.CommitWorkflowUi; @@ -113,8 +112,14 @@ public class GenerateGitCommitMessageAction extends AnAction { return ""; } - var stagedGitDiff = String.join("\n", GitUtil.getStagedDiff(project, repository)); - var unstagedGitDiff = String.join("\n", GitUtil.getUnstagedDiff(project, repository)); + var stagedGitDiff = String.join("\n", GitUtil.getStagedDiff( + project, + repository, + changes.getIncludedVersionedFilePaths())); + var unstagedGitDiff = String.join("\n", GitUtil.getUnstagedDiff( + project, + repository, + changes.getIncludedUnversionedFilePaths())); var newFilesContent = getNewFilesDiff(projectBasePath, changes.getIncludedUnversionedFilePaths()); return Map.of( diff --git a/src/main/kotlin/ee/carlrobert/codegpt/util/GitUtil.kt b/src/main/kotlin/ee/carlrobert/codegpt/util/GitUtil.kt index 8212d55c..f05e70c9 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/util/GitUtil.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/util/GitUtil.kt @@ -11,19 +11,28 @@ object GitUtil { @Throws(VcsException::class) @JvmStatic - fun getStagedDiff(project: Project, gitRepository: GitRepository): List { - return getGitDiff(project, gitRepository, true) + fun getStagedDiff( + project: Project, + gitRepository: GitRepository, + includedVersionedFilePaths: List = emptyList() + ): List { + return getGitDiff(project, gitRepository, includedVersionedFilePaths, true) } @Throws(VcsException::class) @JvmStatic - fun getUnstagedDiff(project: Project, gitRepository: GitRepository): List { - return getGitDiff(project, gitRepository, false) + fun getUnstagedDiff( + project: Project, + gitRepository: GitRepository, + includedUnversionedFilePaths: List = emptyList() + ): List { + return getGitDiff(project, gitRepository, includedUnversionedFilePaths, false) } private fun getGitDiff( project: Project, gitRepository: GitRepository, + filePaths: List, staged: Boolean ): List { val handler = GitLineHandler(project, gitRepository.root, GitCommand.DIFF) @@ -37,9 +46,19 @@ object GitUtil { "--no-color", ) + filePaths.forEach { path -> + handler.addParameters(path) + } + val commandResult = Git.getInstance().runCommand(handler) return commandResult.output.filter { - listOf("diff --git", "index ", "---", "- ", "+++").none { prefix -> it.startsWith(prefix) } + listOf( + "diff --git", + "index ", + "---", + "- ", + "+++" + ).none { prefix -> it.startsWith(prefix) } } } }