From a519a6cdae24b9c41722a2b0dff267071ac7d3e0 Mon Sep 17 00:00:00 2001 From: Hermann Kao <38349706+itishermann@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:53:43 +0200 Subject: [PATCH 1/3] Feat: update commit message prompt to include branch name - #4 --- gradle.properties | 2 +- .../actions/GenerateCommitAction.kt | 26 ++++++++++++++++--- .../settings/OllamaSettingsComponent.kt | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index d5684bb..d5a32df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ platformVersion = 2023.2.7 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 -platformPlugins = +platformPlugins = Git4Idea # Gradle Releases -> https://github.com/gradle/gradle/releases gradleVersion = 8.8 diff --git a/src/main/kotlin/me/itishermann/ollamacommitsummarizer/actions/GenerateCommitAction.kt b/src/main/kotlin/me/itishermann/ollamacommitsummarizer/actions/GenerateCommitAction.kt index bc9ab11..67f4b68 100644 --- a/src/main/kotlin/me/itishermann/ollamacommitsummarizer/actions/GenerateCommitAction.kt +++ b/src/main/kotlin/me/itishermann/ollamacommitsummarizer/actions/GenerateCommitAction.kt @@ -3,7 +3,6 @@ package me.itishermann.ollamacommitsummarizer.actions import com.github.difflib.DiffUtils import com.github.difflib.UnifiedDiffUtils import com.github.difflib.patch.Patch -import com.github.weisj.jsvg.e import com.intellij.notification.Notification import com.intellij.notification.NotificationType import com.intellij.notification.Notifications @@ -15,6 +14,7 @@ import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.DumbAware +import com.intellij.openapi.project.Project import com.intellij.openapi.vcs.CommitMessageI import com.intellij.openapi.vcs.VcsDataKeys import com.intellij.openapi.vcs.VcsException @@ -27,9 +27,10 @@ import me.itishermann.ollamacommitsummarizer.exceptions.NoChangeToCommitExceptio import me.itishermann.ollamacommitsummarizer.services.OllamaService import me.itishermann.ollamacommitsummarizer.settings.OllamaSettingsState import org.jetbrains.annotations.NotNull +import git4idea.repo.GitRepository +import git4idea.repo.GitRepositoryManager import java.util.* - class GenerateCommitAction: AnAction(), DumbAware { private var processing: Boolean = false @@ -56,8 +57,9 @@ class GenerateCommitAction: AnAction(), DumbAware { ?: return val includedChanges = abstractCommitWorkflowHandler.ui.getIncludedChanges() val baseDir = project.basePath + val currentBranch = getCurrentBranchName(project) try { - val prompt = buildPrompt(includedChanges, baseDir!!) + val prompt = buildPrompt(includedChanges, baseDir!!, currentBranch!!) generateCommitMessage(prompt, commitPanel, indicator) } catch (e: NoChangeToCommitException) { processing = false @@ -109,7 +111,7 @@ class GenerateCommitAction: AnAction(), DumbAware { } } - private fun buildPrompt(includedChanges: List, baseDir: String): String { + private fun buildPrompt(includedChanges: List, baseDir: String, branchName: String): String { val totalUnifiedDiffs: MutableList = ArrayList() if(includedChanges.isEmpty()) { processing = false @@ -139,6 +141,7 @@ class GenerateCommitAction: AnAction(), DumbAware { var prompt = OllamaSettingsState.instance.state.prompt ?: throw IllegalStateException("Prompt is null") prompt = prompt.replace("{{gitDiff}}", java.lang.String.join("\n", totalUnifiedDiffs)) prompt = prompt.replace("{{fileCount}}", includedChanges.size.toString()) + prompt = prompt.replace("{{branchName}}", branchName) return prompt } @@ -152,4 +155,19 @@ class GenerateCommitAction: AnAction(), DumbAware { } return VcsDataKeys.COMMIT_MESSAGE_CONTROL.getData(e.dataContext) } + + fun getCurrentBranchName(project: Project): String? { + // Get the GitRepositoryManager for the project + val repositoryManager = GitRepositoryManager.getInstance(project) + // Get the list of repositories in the project + val repositories = repositoryManager.repositories + // If there are repositories available, get the current branch of the first repository + return if (repositories.isNotEmpty()) { + // TODO: Handle multiple repositoriess + val repository: GitRepository = repositories[0] + repository.currentBranch?.name + } else { + null + } + } } diff --git a/src/main/kotlin/me/itishermann/ollamacommitsummarizer/settings/OllamaSettingsComponent.kt b/src/main/kotlin/me/itishermann/ollamacommitsummarizer/settings/OllamaSettingsComponent.kt index f4a811a..80672df 100644 --- a/src/main/kotlin/me/itishermann/ollamacommitsummarizer/settings/OllamaSettingsComponent.kt +++ b/src/main/kotlin/me/itishermann/ollamacommitsummarizer/settings/OllamaSettingsComponent.kt @@ -51,6 +51,7 @@ class OllamaSettingsComponent {
  • {{gitDiff}}: The git diff of the changes
  • {{fileCount}}: The number of files changed
  • +
  • {{branchName}}: The name of the current branch
""".trimIndent()) .createPanel() From 90e876ffb9cf7256f5155fa85580636ba7c114d8 Mon Sep 17 00:00:00 2001 From: Hermann Kao <38349706+itishermann@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:00:23 +0200 Subject: [PATCH 2/3] chore: bump package version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index d5a32df..9e6cbb6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = me.itishermann.ollamacommitsummarizer pluginName = ollama-commit-summarizer pluginRepositoryUrl = https://github.com/itishermann/ollama-commit-summarizer # SemVer format -> https://semver.org -pluginVersion = 0.1.0 +pluginVersion = 0.1.1 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 232 From 279d4ba52fa996378b426dd78354f9a6259b378d Mon Sep 17 00:00:00 2001 From: Hermann Kao <38349706+itishermann@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:04:31 +0200 Subject: [PATCH 3/3] dependencies: add Git4Idea dependency --- src/main/resources/META-INF/plugin.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 6f71507..9f83e28 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -5,6 +5,7 @@ itishermann com.intellij.modules.platform + Git4Idea