Refactor: Git commit message generation as a module (#5031)
* Refactor: Git commit message generation as a module Implement Git commit message generation as a module Refactors the Git commit message generation functionality into a separate module for better organization and maintainability. The changes include: - Moving the commit message generation logic from `src/core/controller/index.ts` to a new module `src/integrations/git/commit-message-generator.ts`. - Adding a command to abort commit message generation. - Updating the `cline.generateGitCommitMessage` command to use the new module. - Adding a context key to disable the generate commit message button when a commit is generating. This refactoring improves the codebase by: - Separating concerns: The commit message generation logic is now isolated in its own module, making it easier to understand and maintain. - Improving testability: The new module can be tested independently of the controller. - Promoting code reuse: The commit message generation logic can be reused in other parts of the application if needed. * clean up * changeset added
This commit is contained in:
5 files changed
+148
-170
No files matched your search
@@ -34,7 +34,7 @@ import { sendStateUpdate } from "./state/subscribeToState"
|
||||
import { sendAddToInputEvent } from "./ui/subscribeToAddToInput"
|
||||
import { sendMcpMarketplaceCatalogEvent } from "./mcp/subscribeToMcpMarketplaceCatalog"
|
||||
import { AuthService } from "@/services/auth/AuthService"
|
||||
import { ShowMessageRequest, ShowMessageType } from "@/shared/proto/host/window"
|
||||
import { ShowMessageType } from "@/shared/proto/host/window"
|
||||
import { getHostBridgeProvider } from "@/hosts/host-providers"
|
||||
import { clineEnvConfig } from "@/config"
|
||||
|
||||
@@ -852,135 +852,5 @@ export class Controller {
|
||||
|
||||
// secrets
|
||||
|
||||
// Git commit message generation
|
||||
|
||||
async generateGitCommitMessage() {
|
||||
try {
|
||||
// Check if there's a workspace folder open
|
||||
const cwd = await getCwd()
|
||||
if (!cwd) {
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message: "No workspace folder open",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get the git diff
|
||||
const gitDiff = await getWorkingState(cwd)
|
||||
if (gitDiff === "No changes in working directory") {
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.INFORMATION,
|
||||
message: "No changes in workspace for commit message",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Show a progress notification
|
||||
await vscode.window.withProgress(
|
||||
{
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: "Generating commit message...",
|
||||
cancellable: false,
|
||||
},
|
||||
async (progress, token) => {
|
||||
try {
|
||||
// Format the git diff into a prompt
|
||||
const prompt = `Based on the following git diff, generate a concise and descriptive commit message:
|
||||
|
||||
${gitDiff.length > 5000 ? gitDiff.substring(0, 5000) + "\n\n[Diff truncated due to size]" : gitDiff}
|
||||
|
||||
The commit message should:
|
||||
1. Start with a short summary (50-72 characters)
|
||||
2. Use the imperative mood (e.g., "Add feature" not "Added feature")
|
||||
3. Describe what was changed and why
|
||||
4. Be clear and descriptive
|
||||
|
||||
Commit message:`
|
||||
|
||||
// Get the current API configuration
|
||||
const { apiConfiguration } = await getAllExtensionState(this.context)
|
||||
const currentMode = await this.getCurrentMode()
|
||||
|
||||
// Build the API handler
|
||||
const apiHandler = buildApiHandler(apiConfiguration, currentMode)
|
||||
|
||||
// Create a system prompt
|
||||
const systemPrompt =
|
||||
"You are a helpful assistant that generates concise and descriptive git commit messages based on git diffs."
|
||||
|
||||
// Create a message for the API
|
||||
const messages = [
|
||||
{
|
||||
role: "user" as const,
|
||||
content: prompt,
|
||||
},
|
||||
]
|
||||
|
||||
// Call the API directly
|
||||
const stream = apiHandler.createMessage(systemPrompt, messages)
|
||||
|
||||
// Collect the response
|
||||
let response = ""
|
||||
for await (const chunk of stream) {
|
||||
if (chunk.type === "text") {
|
||||
response += chunk.text
|
||||
}
|
||||
}
|
||||
|
||||
// Extract the commit message
|
||||
const commitMessage = extractCommitMessage(response)
|
||||
|
||||
// Apply the commit message to the Git input box
|
||||
if (commitMessage) {
|
||||
// Get the Git extension API
|
||||
const gitExtension = vscode.extensions.getExtension("vscode.git")?.exports
|
||||
if (gitExtension) {
|
||||
const api = gitExtension.getAPI(1)
|
||||
if (api && api.repositories.length > 0) {
|
||||
const repo = api.repositories[0]
|
||||
repo.inputBox.value = commitMessage
|
||||
const message = "Commit message generated and applied"
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.INFORMATION,
|
||||
message,
|
||||
})
|
||||
} else {
|
||||
const message = "No Git repositories found"
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
const message = "Git extension not found"
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
const message = "Failed to generate commit message"
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message,
|
||||
})
|
||||
}
|
||||
} catch (innerError) {
|
||||
const innerErrorMessage = innerError instanceof Error ? innerError.message : String(innerError)
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message: `Failed to generate commit message: ${innerErrorMessage}`,
|
||||
})
|
||||
}
|
||||
},
|
||||
)
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error)
|
||||
getHostBridgeProvider().windowClient.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message: `Failed to generate commit message: ${errorMessage}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
// dev
|
||||
}
|
||||
Reference in new issue
Block a user