fix: include selected changes diff only

This commit is contained in:
Carl-Robert Linnupuu 2024-09-10 22:39:09 +03:00
parent 6641daf6fd
commit 3abd981b49
2 changed files with 32 additions and 8 deletions

View file

@ -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(

View file

@ -11,19 +11,28 @@ object GitUtil {
@Throws(VcsException::class)
@JvmStatic
fun getStagedDiff(project: Project, gitRepository: GitRepository): List<String> {
return getGitDiff(project, gitRepository, true)
fun getStagedDiff(
project: Project,
gitRepository: GitRepository,
includedVersionedFilePaths: List<String> = emptyList()
): List<String> {
return getGitDiff(project, gitRepository, includedVersionedFilePaths, true)
}
@Throws(VcsException::class)
@JvmStatic
fun getUnstagedDiff(project: Project, gitRepository: GitRepository): List<String> {
return getGitDiff(project, gitRepository, false)
fun getUnstagedDiff(
project: Project,
gitRepository: GitRepository,
includedUnversionedFilePaths: List<String> = emptyList()
): List<String> {
return getGitDiff(project, gitRepository, includedUnversionedFilePaths, false)
}
private fun getGitDiff(
project: Project,
gitRepository: GitRepository,
filePaths: List<String>,
staged: Boolean
): List<String> {
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) }
}
}
}