Skip to content

Commit 42b48c0

Browse files
committed
"feat: Add translations for generate commit message command"
1 parent 8bb7ef0 commit 42b48c0

22 files changed

+133
-0
lines changed

packages/types/src/vscode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export const commandIds = [
5353
"focusInput",
5454
"acceptInput",
5555
"focusPanel",
56+
57+
"git.generateCommitMessage",
5658
] as const
5759

5860
export type CommandId = (typeof commandIds)[number]

src/activate/registerCommands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { registerHumanRelayCallback, unregisterHumanRelayCallback, handleHumanRe
1414
import { handleNewTask } from "./handleTask"
1515
import { CodeIndexManager } from "../services/code-index/manager"
1616
import { importSettingsWithFeedback } from "../core/config/importExport"
17+
import { generateCommitMessage } from "../integrations/git/generateCommitMessage"
1718
import { t } from "../i18n"
1819

1920
/**
@@ -217,6 +218,7 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
217218

218219
visibleProvider.postMessageToWebview({ type: "acceptInput" })
219220
},
221+
"git.generateCommitMessage": () => generateCommitMessage(context),
220222
})
221223

222224
export const openClineInNewTab = async ({ context, outputChannel }: Omit<RegisterCommandOptions, "provider">) => {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as vscode from "vscode"
2+
import * as path from "path"
3+
import * as fs from "fs/promises"
4+
import { buildApiHandler } from "../../api"
5+
import { simpleGit, SimpleGit } from "simple-git"
6+
import { ContextProxy } from "../../core/config/ContextProxy"
7+
import { Anthropic } from "@anthropic-ai/sdk"
8+
9+
async function getGitApi(): Promise<any | undefined> {
10+
const extension = vscode.extensions.getExtension("vscode.git")
11+
if (!extension) {
12+
vscode.window.showErrorMessage("Git extension not found.")
13+
return
14+
}
15+
await extension.activate()
16+
return extension.exports.getAPI(1)
17+
}
18+
19+
async function getChanges(git: SimpleGit, repoPath: string): Promise<string> {
20+
const trackedFilesDiff = await git.diff()
21+
const status = await git.status()
22+
const untrackedFiles = status.not_added
23+
24+
let untrackedFilesContent = ""
25+
for (const file of untrackedFiles) {
26+
const filePath = path.join(repoPath, file)
27+
try {
28+
const content = await fs.readFile(filePath, "utf-8")
29+
untrackedFilesContent += `\n--- a/${file}\n+++ b/${file}\n${content}`
30+
} catch (e) {
31+
console.error(`Could not read untracked file ${file}`, e)
32+
}
33+
}
34+
35+
return `${trackedFilesDiff}\n${untrackedFilesContent}`.trim()
36+
}
37+
38+
function createPrompt(diff: string): string {
39+
return `Create a git commit message from the following diff:\n${diff}`
40+
}
41+
42+
export async function generateCommitMessage(context: vscode.ExtensionContext) {
43+
const gitApi = await getGitApi()
44+
if (!gitApi) {
45+
return
46+
}
47+
48+
if (gitApi.repositories.length === 0) {
49+
vscode.window.showErrorMessage("No Git repository found.")
50+
return
51+
}
52+
53+
const repoPath = gitApi.repositories[0].rootUri.fsPath
54+
const git = simpleGit(repoPath)
55+
const diff = await getChanges(git, repoPath)
56+
57+
if (!diff) {
58+
vscode.window.showInformationMessage("No changes found.")
59+
return
60+
}
61+
62+
const contextProxy = await ContextProxy.getInstance(context)
63+
const providerSettings = contextProxy.getProviderSettings()
64+
if (!providerSettings) {
65+
vscode.window.showErrorMessage("AI provider not configured.")
66+
return
67+
}
68+
const provider = buildApiHandler(providerSettings)
69+
const modelName = provider.getModel().id
70+
const prompt = createPrompt(diff)
71+
const messages: Anthropic.Messages.MessageParam[] = [
72+
{
73+
role: "user",
74+
content: prompt,
75+
},
76+
]
77+
78+
await vscode.window.withProgress(
79+
{
80+
location: vscode.ProgressLocation.Notification,
81+
title: `Generating commit message with ${modelName}...`,
82+
cancellable: false,
83+
},
84+
async () => {
85+
const stream = provider.createMessage("", messages, { taskId: "generate-commit" })
86+
87+
let commitMessage = ""
88+
for await (const chunk of stream) {
89+
if (chunk.type === "text") {
90+
commitMessage += chunk.text
91+
}
92+
}
93+
94+
const finalMessage = commitMessage.replace(/```/g, "").trim()
95+
gitApi.repositories[0].inputBox.value = finalMessage
96+
},
97+
)
98+
}

src/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
"command": "roo-cline.acceptInput",
170170
"title": "%command.acceptInput.title%",
171171
"category": "%configuration.title%"
172+
},
173+
{
174+
"command": "roo-cline.git.generateCommitMessage",
175+
"title": "%command.git.generateCommitMessage.title%",
176+
"category": "Roo Code",
177+
"icon": "$(sparkle)"
172178
}
173179
],
174180
"menus": {
@@ -280,6 +286,13 @@
280286
"group": "navigation@6",
281287
"when": "activeWebviewPanelId == roo-cline.TabPanelProvider"
282288
}
289+
],
290+
"scm/title": [
291+
{
292+
"command": "roo-cline.git.generateCommitMessage",
293+
"group": "navigation",
294+
"when": "scmProvider == git"
295+
}
283296
]
284297
},
285298
"submenus": [

src/package.nls.ca.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.terminal.fixCommand.title": "Corregir Aquesta Ordre",
1515
"command.terminal.explainCommand.title": "Explicar Aquesta Ordre",
1616
"command.acceptInput.title": "Acceptar Entrada/Suggeriment",
17+
"command.git.generateCommitMessage.title": "Genera el missatge de commit",
1718
"views.activitybar.title": "Roo Code",
1819
"views.contextMenu.label": "Roo Code",
1920
"views.terminalMenu.label": "Roo Code",

src/package.nls.de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.terminal.fixCommand.title": "Diesen Befehl Reparieren",
1515
"command.terminal.explainCommand.title": "Diesen Befehl Erklären",
1616
"command.acceptInput.title": "Eingabe/Vorschlag Akzeptieren",
17+
"command.git.generateCommitMessage.title": "Commit-Nachricht generieren",
1718
"views.activitybar.title": "Roo Code",
1819
"views.contextMenu.label": "Roo Code",
1920
"views.terminalMenu.label": "Roo Code",

src/package.nls.es.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.terminal.fixCommand.title": "Corregir Este Comando",
1515
"command.terminal.explainCommand.title": "Explicar Este Comando",
1616
"command.acceptInput.title": "Aceptar Entrada/Sugerencia",
17+
"command.git.generateCommitMessage.title": "Generar mensaje de commit",
1718
"views.activitybar.title": "Roo Code",
1819
"views.contextMenu.label": "Roo Code",
1920
"views.terminalMenu.label": "Roo Code",

src/package.nls.fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.terminal.fixCommand.title": "Corriger cette Commande",
1515
"command.terminal.explainCommand.title": "Expliquer cette Commande",
1616
"command.acceptInput.title": "Accepter l'Entrée/Suggestion",
17+
"command.git.generateCommitMessage.title": "Générer le message de commit",
1718
"views.activitybar.title": "Roo Code",
1819
"views.contextMenu.label": "Roo Code",
1920
"views.terminalMenu.label": "Roo Code",

src/package.nls.hi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.terminal.fixCommand.title": "यह कमांड ठीक करें",
1515
"command.terminal.explainCommand.title": "यह कमांड समझाएं",
1616
"command.acceptInput.title": "इनपुट/सुझाव स्वीकारें",
17+
"command.git.generateCommitMessage.title": "कमिट संदेश उत्पन्न करें",
1718
"views.activitybar.title": "Roo Code",
1819
"views.contextMenu.label": "Roo Code",
1920
"views.terminalMenu.label": "Roo Code",

src/package.nls.id.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"command.settings.title": "Pengaturan",
1515
"command.documentation.title": "Dokumentasi",
1616
"command.openInNewTab.title": "Buka di Tab Baru",
17+
"command.git.generateCommitMessage.title": "Hasilkan Pesan Komit",
1718
"command.explainCode.title": "Jelaskan Kode",
1819
"command.fixCode.title": "Perbaiki Kode",
1920
"command.improveCode.title": "Tingkatkan Kode",

0 commit comments

Comments
 (0)