Skip to content

Commit ddbead7

Browse files
committed
feat(commit): add commit message generation with template rendering
This commit adds the functionality to generate commit messages using a template rendering approach. It introduces a new dependency on `cc.unitmesh.devti.template.TemplateRender` and modifies the `generateCommitMessage` method in `CommitMessageSuggestionAction.kt`. The template used for rendering is `gen-commit-msg.vm` located in `src/main/resources/genius/practises`. The generated commit message includes a history example and the diff content.
1 parent 41f4317 commit ddbead7

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/main/kotlin/cc/unitmesh/devti/actions/vcs/CommitMessageSuggestionAction.kt

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.gui.chat.ChatActionType
66
import cc.unitmesh.devti.llms.LlmFactory
77
import cc.unitmesh.devti.vcs.VcsPrompting
88
import cc.unitmesh.devti.statusbar.AutoDevStatus
9+
import cc.unitmesh.devti.template.TemplateRender
910
import cc.unitmesh.devti.vcs.VcsUtil
1011
import com.intellij.openapi.actionSystem.ActionUpdateThread
1112
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -129,27 +130,23 @@ class CommitMessageSuggestionAction : ChatBaseAction() {
129130
}
130131

131132
private fun generateCommitMessage(diff: String, project: Project): String {
132-
return """Write a cohesive yet descriptive commit message for a given diff.
133-
Make sure to include both information What was changed and Why.
134-
Start with a short sentence in imperative form, no more than 50 characters long.
135-
Then leave an empty line and continue with a more detailed explanation, if necessary.
136-
Explanation should have less than 200 characters.
137-
138-
Examples:
139-
- fix(authentication): add password regex pattern
140-
- feat(storage): add new test cases
141-
- test(java): fix test case for user controller
142-
History Examples:
143-
${findExampleCommitMessages(project) ?: "No example found."}
144-
145-
Diff:
146-
147-
```diff
148-
$diff
149-
```
150-
151-
"""
133+
val templateRender = TemplateRender("genius/practises")
134+
val template = templateRender.getTemplate("gen-commit-msg.vm")
135+
136+
val historyExample = findExampleCommitMessages(project) ?: ""
137+
templateRender.context = CommitMsgGenContext(
138+
historyExample = historyExample,
139+
diffContent = diff,
140+
)
141+
val prompter = templateRender.renderTemplate(template)
142+
143+
logger.info("Prompt: $prompter")
144+
return prompter
152145
}
153-
154146
}
155147

148+
149+
data class CommitMsgGenContext(
150+
var historyExample: String = "",
151+
var diffContent: String = "",
152+
)

src/main/resources/genius/practises/gen-commit-msg.vm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ examples:
1010
- test(java): fix test case for user controller
1111

1212
Diff:
13+
#if( $context.historyExamples.length() > 0 )
14+
History Examples:
15+
$context.historyExamples
16+
#end
1317

1418
```diff
15-
${context.diff}
19+
${context.diffContent}
1620
```
1721

0 commit comments

Comments
 (0)