Ability to generate commit message with cline. (#3318)

* Add handling of git message

* ✨ Add commit message generation feature

- Implemented commit message generation functionality in controller
- Added new command to generate commit messages from git diff
- Added error handling for commit message generation
- Updated API handler to support commit message generation
- Added new icon for commit message command
- Updated keybindings for commit message generation
- Added command to command palette for easier access
- Improved error handling and logging
- Added support for generating commit messages from staged changes
- Updated documentation and comments

* Handle user dismissing the dialog (selectedAction is undefined)

* Apply code review suggestions

No default keybinding
The task is not cancelable
Unused import removed
Cleaner message
This commit is contained in:
zapp88 authored and GitHub committed 2025-05-06 16:26:35 -07:00
1 parent d14345f605
commit dbba0ef776
5 files changed
+288 -1

No files matched your search

+105 -1
View File
@@ -30,7 +30,8 @@ import { McpDownloadResponse, McpMarketplaceCatalog, McpServer } from "@shared/m
import { TelemetrySetting } from "@shared/TelemetrySetting"
import { ClineCheckpointRestore, WebviewMessage } from "@shared/WebviewMessage"
import { fileExistsAtPath } from "@utils/fs"
import { searchCommits } from "@utils/git"
import { searchCommits, getWorkingState } from "@utils/git"
import { extractCommitMessage } from "@integrations/git/commit-message-generator"
import { getWorkspacePath } from "@utils/path"
import { getTotalTasksSize } from "@utils/storage"
import { openMention } from "../mentions"
@@ -1813,6 +1814,109 @@ Here is the project's README to help you get started:\n\n${mcpDetails.readmeCont
}
}
// Git commit message generation
async generateGitCommitMessage() {
try {
// Check if there's a workspace folder open
const cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath
if (!cwd) {
vscode.window.showErrorMessage("No workspace folder open")
return
}
// Get the git diff
const gitDiff = await getWorkingState(cwd)
if (gitDiff === "No changes in working directory") {
vscode.window.showInformationMessage("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)
// Build the API handler
const apiHandler = buildApiHandler(apiConfiguration)
// 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
vscode.window.showInformationMessage("Commit message generated and applied")
} else {
vscode.window.showErrorMessage("No Git repositories found")
}
} else {
vscode.window.showErrorMessage("Git extension not found")
}
} else {
vscode.window.showErrorMessage("Failed to generate commit message")
}
} catch (innerError) {
const innerErrorMessage = innerError instanceof Error ? innerError.message : String(innerError)
vscode.window.showErrorMessage(`Failed to generate commit message: ${innerErrorMessage}`)
}
},
)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error)
vscode.window.showErrorMessage(`Failed to generate commit message: ${errorMessage}`)
}
}
// dev
async resetState() {