Skip to content

Commit 9b42b35

Browse files
committed
feat: add {branch} variable (#43)
Compute the most common branch from all files that are being committed and use it to replace {branch} variable in prompt.
1 parent e480e3a commit 9b42b35

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- Add `{branch}` variable for prompt customisation.
6+
47
### Fixed
58
- Commit message generation does not respect locale.
69

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/AICommitAction.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ class AICommitAction : AnAction(), DumbAware {
3636

3737
runBackgroundableTask(message("action.background"), project) {
3838
val diff = computeDiff(includedChanges, project)
39-
4039
if (diff.isBlank()) {
4140
sendNotification(Notification.emptyDiff())
4241
return@runBackgroundableTask
4342
}
4443

45-
val prompt = AppSettings.instance.getPrompt(diff)
44+
var branch = commonBranch(includedChanges, project)
45+
if (branch == null) {
46+
sendNotification(Notification.noCommonBranch())
47+
// hardcoded fallback branch
48+
branch = "main"
49+
}
50+
51+
val prompt = AppSettings.instance.getPrompt(diff, branch)
4652
if (isPromptTooLarge(prompt)) {
4753
sendNotification(Notification.promptTooLarge())
4854
return@runBackgroundableTask
@@ -120,4 +126,11 @@ class AICommitAction : AnAction(), DumbAware {
120126
val encoding = registry.getEncoding(modelType.encodingType)
121127
return encoding.countTokens(prompt) > modelType.maxContextLength
122128
}
129+
130+
private fun commonBranch(changes: List<Change>, project: Project): String? {
131+
val repositoryManager = GitRepositoryManager.getInstance(project)
132+
return changes.map {
133+
repositoryManager.getRepositoryForFileQuick(it.virtualFile)?.currentBranchName
134+
}.groupingBy { it }.eachCount().maxByOrNull { it.value }?.key
135+
}
123136
}

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/notifications/Notification.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ data class Notification(
4242
fun emptyDiff() = Notification(DEFAULT_TITLE, message = message("notifications.empty-diff"))
4343
fun promptTooLarge() = Notification(DEFAULT_TITLE, message = message("notifications.prompt-too-large"))
4444
fun unsuccessfulRequest(message: String) = Notification(message = message("notifications.unsuccessful-request", message))
45-
fun noCommitMessage(): Notification = Notification(message = message("notifications.no-commit-message"))
46-
fun unableToSaveToken(): Notification = Notification(message = message("notifications.unable-to-save-token"))
45+
fun noCommitMessage() = Notification(message = message("notifications.no-commit-message"))
46+
fun unableToSaveToken() = Notification(message = message("notifications.unable-to-save-token"))
47+
fun noCommonBranch() = Notification(message = message("notifications.no-common-branch"))
4748

4849
}
4950

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/AppSettings.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,26 @@ class AppSettings : PersistentStateComponent<AppSettings> {
3131

3232
var requestSupport = true
3333
var lastVersion: String? = null
34-
var openAIHost: String = OpenAIHost.OpenAI.baseUrl
35-
var openAIHosts: MutableSet<String> = mutableSetOf(OpenAIHost.OpenAI.baseUrl)
34+
var openAIHost = OpenAIHost.OpenAI.baseUrl
35+
var openAIHosts = mutableSetOf(OpenAIHost.OpenAI.baseUrl)
3636
var proxyUrl: String? = null
3737

38-
var prompts: MutableMap<String, Prompt> = initPrompts()
39-
var currentPrompt: Prompt = prompts["basic"]!!
38+
var prompts = initPrompts()
39+
var currentPrompt = prompts["basic"]!!
4040

41-
var openAIModelId: String = "gpt-3.5-turbo"
42-
var openAIModelIds: List<String> = listOf("gpt-3.5-turbo", "gpt-4")
41+
var openAIModelId = "gpt-3.5-turbo"
42+
var openAIModelIds = listOf("gpt-3.5-turbo", "gpt-4")
4343

4444
companion object {
4545
const val SERVICE_NAME = "com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings"
4646
val instance: AppSettings
4747
get() = ApplicationManager.getApplication().getService(AppSettings::class.java)
4848
}
4949

50-
fun getPrompt(diff: String): String {
50+
fun getPrompt(diff: String, branch: String): String {
5151
var content = currentPrompt.content
5252
content = content.replace("{locale}", locale.displayLanguage)
53+
content = content.replace("{branch}", branch)
5354

5455
return if (content.contains("{diff}")) {
5556
content.replace("{diff}", diff)

src/main/resources/messages/MyBundle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ notifications.unknown-error=Unknown error ({0}).
2828
notification.group.important.name=AI Commits important
2929
notification.group.general.name=AI Commits general
3030
notifications.empty-diff=Git diff is empty.
31+
notifications.no-common-branch=Branch could not be found. Replacing {branch} with main.
3132

3233
actions.do-not-ask-again = Do not ask me again.
3334
actions.take-me-there = Take me there.
@@ -40,7 +41,7 @@ settings.prompt.name=Name
4041
settings.prompt.content=Content
4142
validation.required=This value is required.
4243
validation.number=Value is not a number.
43-
settings.prompt.comment=You can use {locale} and {diff} variables to customise your prompt.
44+
settings.prompt.comment=You can use {locale}, {diff}, {branch} variables to customise your prompt.
4445
actions.update=Update
4546
actions.add=Add
4647
settings.prompt.edit.title=Edit Prompt

0 commit comments

Comments
 (0)