|
| 1 | +import type { TextEditor, Uri } from 'vscode'; |
| 2 | +import { ProgressLocation } from 'vscode'; |
| 3 | +import type { Container } from '../container'; |
| 4 | +import { GitUri } from '../git/gitUri'; |
| 5 | +import type { GitStashCommit } from '../git/models/commit'; |
| 6 | +import { showGenericErrorMessage } from '../messages'; |
| 7 | +import type { AIExplainSource } from '../plus/ai/aiProviderService'; |
| 8 | +import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker'; |
| 9 | +import { showStashPicker } from '../quickpicks/stashPicker'; |
| 10 | +import { command } from '../system/-webview/command'; |
| 11 | +import { showMarkdownPreview } from '../system/-webview/markdown'; |
| 12 | +import { Logger } from '../system/logger'; |
| 13 | +import { GlCommandBase } from './commandBase'; |
| 14 | +import { getCommandUri } from './commandBase.utils'; |
| 15 | +import type { CommandContext } from './commandContext'; |
| 16 | +import { isCommandContextViewNodeHasCommit } from './commandContext.utils'; |
| 17 | + |
| 18 | +export interface ExplainStashCommandArgs { |
| 19 | + repoPath?: string | Uri; |
| 20 | + ref?: string; |
| 21 | + source?: AIExplainSource; |
| 22 | +} |
| 23 | + |
| 24 | +@command() |
| 25 | +export class ExplainStashCommand extends GlCommandBase { |
| 26 | + constructor(private readonly container: Container) { |
| 27 | + super('gitlens.ai.explainStash'); |
| 28 | + } |
| 29 | + |
| 30 | + protected override preExecute(context: CommandContext, args?: ExplainStashCommandArgs): Promise<void> { |
| 31 | + // Check if the command is being called from a CommitNode |
| 32 | + if (isCommandContextViewNodeHasCommit<GitStashCommit>(context)) { |
| 33 | + args = { ...args }; |
| 34 | + args.repoPath = args.repoPath ?? context.node.commit.repoPath; |
| 35 | + args.ref = args.ref ?? context.node.commit.sha; |
| 36 | + args.source = args.source ?? { source: 'view', type: 'stash' }; |
| 37 | + } |
| 38 | + |
| 39 | + return this.execute(context.editor, context.uri, args); |
| 40 | + } |
| 41 | + |
| 42 | + async execute(editor?: TextEditor, uri?: Uri, args?: ExplainStashCommandArgs): Promise<void> { |
| 43 | + args = { ...args }; |
| 44 | + |
| 45 | + let repository; |
| 46 | + if (args?.repoPath != null) { |
| 47 | + repository = this.container.git.getRepository(args.repoPath); |
| 48 | + } |
| 49 | + |
| 50 | + if (repository == null) { |
| 51 | + uri = getCommandUri(uri, editor); |
| 52 | + const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; |
| 53 | + repository = await getBestRepositoryOrShowPicker( |
| 54 | + gitUri, |
| 55 | + editor, |
| 56 | + 'Explain Stash', |
| 57 | + 'Choose which repository to explain a stash from', |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + if (repository == null) return; |
| 62 | + |
| 63 | + try { |
| 64 | + // If no ref is provided, show a picker to select a stash |
| 65 | + if (args.ref == null) { |
| 66 | + const pick = await showStashPicker('Explain Stash', 'Choose a stash to explain', repository); |
| 67 | + if (pick?.ref == null) return; |
| 68 | + args.ref = pick.ref; |
| 69 | + } |
| 70 | + |
| 71 | + // Get the stash commit |
| 72 | + const commit = await repository.git.commits().getCommit(args.ref); |
| 73 | + if (commit == null) { |
| 74 | + void showGenericErrorMessage('Unable to find the specified stash commit'); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Call the AI service to explain the stash |
| 79 | + const result = await this.container.ai.explainCommit( |
| 80 | + commit, |
| 81 | + args.source ?? { source: 'commandPalette', type: 'stash' }, |
| 82 | + { |
| 83 | + progress: { location: ProgressLocation.Notification, title: 'Explaining stash...' }, |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + // Display the result |
| 88 | + let content = `# Stash Summary\n\n`; |
| 89 | + if (result != null) { |
| 90 | + content += `> Generated by ${result.model.name}\n\n## ${commit.message || commit.ref}}\n\n${result |
| 91 | + ?.parsed.summary}\n\n${result?.parsed.body}`; |
| 92 | + } else { |
| 93 | + content += `> No changes found to explain.`; |
| 94 | + } |
| 95 | + void showMarkdownPreview(content); |
| 96 | + } catch (ex) { |
| 97 | + Logger.error(ex, 'ExplainStashCommand', 'execute'); |
| 98 | + void showGenericErrorMessage('Unable to explain stash'); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments