|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { ProviderSettingsManager } from "../../core/config/ProviderSettingsManager" |
| 3 | +import { t } from "../../i18n" |
| 4 | +import { Package } from "../../shared/package" |
| 5 | +import { GitChange, GitContextCollector } from "../git-context" |
| 6 | + |
| 7 | +import { CommitMessageGenerator } from "./CommitMessageGenerator" |
| 8 | +import { getCommitMessageGitContextSettings, toGitContextCollectorOptions } from "./gitContextSettings" |
| 9 | + |
| 10 | +interface VscGenerationRequest { |
| 11 | + inputBox: { value: string } |
| 12 | + rootUri?: vscode.Uri |
| 13 | +} |
| 14 | + |
| 15 | +export class CommitMessageProvider implements vscode.Disposable { |
| 16 | + private generator: CommitMessageGenerator |
| 17 | + |
| 18 | + constructor( |
| 19 | + private context: vscode.ExtensionContext, |
| 20 | + private outputChannel: vscode.OutputChannel, |
| 21 | + ) { |
| 22 | + const providerSettingsManager = new ProviderSettingsManager(this.context) |
| 23 | + |
| 24 | + this.generator = new CommitMessageGenerator(providerSettingsManager) |
| 25 | + } |
| 26 | + |
| 27 | + public async activate(): Promise<void> { |
| 28 | + this.outputChannel.appendLine(t("common:commitMessage.activated")) |
| 29 | + |
| 30 | + const disposables = [ |
| 31 | + vscode.commands.registerCommand( |
| 32 | + `${Package.name}.generateCommitMessage`, |
| 33 | + (vsRequest?: VscGenerationRequest) => this.handleVSCodeCommand(vsRequest), |
| 34 | + ), |
| 35 | + ] |
| 36 | + this.context.subscriptions.push(...disposables) |
| 37 | + } |
| 38 | + |
| 39 | + private async handleVSCodeCommand(vsRequest?: VscGenerationRequest): Promise<void> { |
| 40 | + try { |
| 41 | + const workspacePath = this.determineWorkspacePath(vsRequest?.rootUri) |
| 42 | + const targetRepository = await this.determineTargetRepository(workspacePath) |
| 43 | + if (!targetRepository?.rootUri) { |
| 44 | + throw new Error("Could not determine Git repository") |
| 45 | + } |
| 46 | + |
| 47 | + await vscode.window.withProgress( |
| 48 | + { |
| 49 | + location: vscode.ProgressLocation.SourceControl, |
| 50 | + title: t("common:commitMessage.generating"), |
| 51 | + cancellable: false, |
| 52 | + }, |
| 53 | + async (progress) => { |
| 54 | + let lastPercentage = 0 |
| 55 | + const reportProgress = (percentage: number, message?: string) => { |
| 56 | + progress.report({ |
| 57 | + increment: Math.max(0, percentage - lastPercentage), |
| 58 | + message: message || t("common:commitMessage.generating"), |
| 59 | + }) |
| 60 | + lastPercentage = percentage |
| 61 | + } |
| 62 | + |
| 63 | + reportProgress(5, t("common:commitMessage.initializing")) |
| 64 | + const gitCollector = new GitContextCollector(workspacePath) |
| 65 | + |
| 66 | + try { |
| 67 | + reportProgress(15, t("common:commitMessage.discoveringFiles")) |
| 68 | + const resolution = await this.resolveCommitChanges(gitCollector) |
| 69 | + const gitContextSettings = getCommitMessageGitContextSettings() |
| 70 | + |
| 71 | + if (resolution.changes.length === 0) { |
| 72 | + vscode.window.showInformationMessage(t("common:commitMessage.noChanges")) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + reportProgress(25, t("common:commitMessage.foundChanges", { count: resolution.changes.length })) |
| 77 | + |
| 78 | + if (!resolution.usedStaged) { |
| 79 | + vscode.window.showInformationMessage(t("common:commitMessage.generatingFromUnstaged")) |
| 80 | + } |
| 81 | + |
| 82 | + reportProgress(40, t("common:commitMessage.gettingContext")) |
| 83 | + const gitContextResult = await gitCollector.collectContext( |
| 84 | + resolution.changes, |
| 85 | + toGitContextCollectorOptions(resolution.usedStaged, gitContextSettings), |
| 86 | + resolution.files, |
| 87 | + ) |
| 88 | + if (gitContextResult.warnings.length > 0) { |
| 89 | + vscode.window.showWarningMessage( |
| 90 | + t("common:commitMessage.contextWarnings", { |
| 91 | + warnings: gitContextResult.warnings.join("; "), |
| 92 | + }), |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + reportProgress(70, t("common:commitMessage.generating")) |
| 97 | + const message = await this.generator.generateMessage({ |
| 98 | + workspacePath, |
| 99 | + selectedFiles: resolution.files, |
| 100 | + gitContext: gitContextResult.context, |
| 101 | + onProgress: (update) => { |
| 102 | + if (update.percentage !== undefined) { |
| 103 | + reportProgress(70 + update.percentage * 0.25, update.message) |
| 104 | + } |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + targetRepository.inputBox.value = message |
| 109 | + reportProgress(100, t("common:commitMessage.generated")) |
| 110 | + } finally { |
| 111 | + gitCollector.dispose() |
| 112 | + } |
| 113 | + }, |
| 114 | + ) |
| 115 | + } catch (error) { |
| 116 | + const errorMessage = error instanceof Error ? error.message : "Unknown error occurred" |
| 117 | + vscode.window.showErrorMessage(t("common:commitMessage.generationFailed", { errorMessage })) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private async resolveCommitChanges(gitCollector: GitContextCollector): Promise<{ |
| 122 | + changes: GitChange[] |
| 123 | + files: string[] |
| 124 | + usedStaged: boolean |
| 125 | + }> { |
| 126 | + let changes = await gitCollector.gatherChanges({ staged: true }) |
| 127 | + let usedStaged = true |
| 128 | + |
| 129 | + if (changes.length === 0) { |
| 130 | + changes = await gitCollector.gatherChanges({ staged: false }) |
| 131 | + usedStaged = false |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + changes, |
| 136 | + files: changes.map((change) => change.filePath), |
| 137 | + usedStaged, |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private async determineTargetRepository(workspacePath: string): Promise<VscGenerationRequest | null> { |
| 142 | + try { |
| 143 | + const gitExtension = vscode.extensions.getExtension("vscode.git") |
| 144 | + if (!gitExtension) { |
| 145 | + return null |
| 146 | + } |
| 147 | + |
| 148 | + if (!gitExtension.isActive) { |
| 149 | + await gitExtension.activate() |
| 150 | + } |
| 151 | + |
| 152 | + const gitApi = gitExtension.exports.getAPI(1) |
| 153 | + if (!gitApi) { |
| 154 | + return null |
| 155 | + } |
| 156 | + |
| 157 | + for (const repo of gitApi.repositories ?? []) { |
| 158 | + if (repo.rootUri && workspacePath.startsWith(repo.rootUri.fsPath)) { |
| 159 | + return repo |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return gitApi.repositories[0] ?? null |
| 164 | + } catch (error) { |
| 165 | + return null |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private determineWorkspacePath(resourceUri?: vscode.Uri): string { |
| 170 | + if (resourceUri) { |
| 171 | + return resourceUri.fsPath |
| 172 | + } |
| 173 | + |
| 174 | + const workspaceFolders = vscode.workspace.workspaceFolders |
| 175 | + if (workspaceFolders && workspaceFolders.length > 0) { |
| 176 | + return workspaceFolders[0].uri.fsPath |
| 177 | + } |
| 178 | + |
| 179 | + throw new Error("Could not determine workspace path") |
| 180 | + } |
| 181 | + |
| 182 | + public dispose(): void {} |
| 183 | +} |
0 commit comments