Skip to content

Commit 868a163

Browse files
committed
Implement the Generate Commit Message command and integrate it into the Source Control view title menu
1 parent 70e002f commit 868a163

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@
277277
"command": "codeWebChat.settings",
278278
"title": "Settings",
279279
"category": "Code Web Chat"
280+
},
281+
{
282+
"command": "codeWebChat.generateCommitMessage",
283+
"title": "Generate Commit Message with CWC",
284+
"category": "Code Web Chat",
285+
"icon": "$(sparkle)"
280286
}
281287
],
282288
"menus": {
@@ -459,6 +465,13 @@
459465
"group": "navigation@1"
460466
}
461467
],
468+
"scm/title": [
469+
{
470+
"command": "codeWebChat.generateCommitMessage",
471+
"group": "navigation",
472+
"when": "scmProvider == git"
473+
}
474+
],
462475
"view/item/context": [
463476
{
464477
"command": "codeWebChat.closeEditor",

‎packages/vscode/src/extension.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as vscode from 'vscode'
22
import { context_initialization } from './context/context-initialization'
3+
import {
4+
get_git_repository,
5+
prepare_staged_changes
6+
} from './utils/git-repository-utils'
7+
import { generate_commit_message_from_diff } from './views/panel/backend/message-handlers/handle-commit-changes/utils'
38
import { PanelProvider } from './views/panel/backend/panel-provider'
49
import { WebSocketManager } from './services/websocket-manager'
510
import {
@@ -107,4 +112,27 @@ export async function activate(context: vscode.ExtensionContext) {
107112
}
108113
)
109114
)
115+
116+
context.subscriptions.push(
117+
vscode.commands.registerCommand(
118+
'codeWebChat.generateCommitMessage',
119+
async (source_control?: vscode.SourceControl) => {
120+
const repository = get_git_repository(source_control)
121+
if (!repository) return
122+
123+
const diff = await prepare_staged_changes(repository)
124+
125+
if (!diff) return
126+
127+
const commit_message = await generate_commit_message_from_diff({
128+
context,
129+
repository,
130+
diff
131+
})
132+
if (commit_message) {
133+
repository.inputBox.value = commit_message
134+
}
135+
}
136+
)
137+
)
110138
}

‎packages/vscode/src/utils/git-repository-utils.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,19 @@ export function get_git_repository(
6363
export async function prepare_staged_changes(
6464
repository: GitRepository
6565
): Promise<string | null> {
66+
await repository.status()
6667
const staged_changes = repository.state.indexChanges || []
6768

68-
if (staged_changes.length === 0) {
69-
await repository.add([])
69+
if (
70+
staged_changes.length === 0 &&
71+
repository.state.workingTreeChanges.length > 0
72+
) {
73+
const uris_to_stage = repository.state.workingTreeChanges.map(
74+
(change: any) => change.uri.fsPath
75+
)
76+
await repository.add(uris_to_stage)
7077
await new Promise((resolve) => setTimeout(resolve, 500))
78+
await repository.status()
7179
}
7280

7381
const diff = execSync('git diff --staged', {

0 commit comments

Comments
 (0)