@@ -12,6 +12,7 @@ import { Logger } from '../system/logger';
1212import { GlCommandBase } from './commandBase' ;
1313import { getCommandUri } from './commandBase.utils' ;
1414import type { CommandContext } from './commandContext' ;
15+ import { isCommandContextViewNodeHasRepoPath , isCommandContextViewNodeHasRepository } from './commandContext.utils' ;
1516
1617export interface ExplainWipCommandArgs {
1718 repoPath ?: string | Uri ;
@@ -26,21 +27,38 @@ export class ExplainWipCommand extends GlCommandBase {
2627 }
2728
2829 protected override preExecute ( context : CommandContext , args ?: ExplainWipCommandArgs ) : Promise < void > {
30+ if ( isCommandContextViewNodeHasRepository ( context ) ) {
31+ args = { ...args } ;
32+ args . repoPath = context . node . repo . path ;
33+ args . source = args . source ?? { source : 'view' , type : 'wip' } ;
34+ } else if ( isCommandContextViewNodeHasRepoPath ( context ) ) {
35+ args = { ...args } ;
36+ args . repoPath = context . node . repoPath ;
37+ args . source = args . source ?? { source : 'view' , type : 'wip' } ;
38+ }
39+
2940 return this . execute ( context . editor , context . uri , args ) ;
3041 }
3142
3243 async execute ( editor ?: TextEditor , uri ?: Uri , args ?: ExplainWipCommandArgs ) : Promise < void > {
3344 args = { ...args } ;
34- uri = getCommandUri ( uri , editor ) ;
3545
36- const gitUri = uri != null ? await GitUri . fromUri ( uri ) : undefined ;
46+ let repository ;
47+ if ( args ?. repoPath != null ) {
48+ repository = this . container . git . getRepository ( args . repoPath ) ;
49+ }
50+
51+ if ( repository == null ) {
52+ uri = getCommandUri ( uri , editor ) ;
53+ const gitUri = uri != null ? await GitUri . fromUri ( uri ) : undefined ;
54+ repository = await getBestRepositoryOrShowPicker (
55+ gitUri ,
56+ editor ,
57+ 'Explain Working Changes' ,
58+ 'Choose which repository to explain working changes from' ,
59+ ) ;
60+ }
3761
38- const repository = await getBestRepositoryOrShowPicker (
39- gitUri ,
40- editor ,
41- 'Explain Working Changes' ,
42- 'Choose which repository to explain working changes from' ,
43- ) ;
4462 if ( repository == null ) return ;
4563
4664 try {
@@ -51,7 +69,18 @@ export class ExplainWipCommand extends GlCommandBase {
5169 return ;
5270 }
5371
54- const diff = await diffService . getDiff ( args ?. staged ? uncommittedStaged : uncommitted ) ;
72+ // If args?.staged is undefined, should we get all changes (staged and unstaged)?
73+ let stagedLabel ;
74+ let to ;
75+ if ( args ?. staged === true ) {
76+ stagedLabel = 'Staged' ;
77+ to = uncommittedStaged ;
78+ } else {
79+ stagedLabel = 'Unstaged' ;
80+ to = uncommitted ;
81+ }
82+
83+ const diff = await diffService . getDiff ( to ) ;
5584 if ( ! diff ?. contents ) {
5685 void showGenericErrorMessage ( 'No working changes found to explain' ) ;
5786 return ;
@@ -61,7 +90,7 @@ export class ExplainWipCommand extends GlCommandBase {
6190 const result = await this . container . ai . explainChanges (
6291 {
6392 diff : diff . contents ,
64- message : args ?. staged ? 'Staged working changes' : 'Unstaged working changes' ,
93+ message : ` ${ stagedLabel } working changes` ,
6594 } ,
6695 args . source ?? { source : 'commandPalette' , type : 'wip' } ,
6796 {
@@ -70,9 +99,9 @@ export class ExplainWipCommand extends GlCommandBase {
7099 ) ;
71100
72101 // Display the result
73- let content = `# ${ args ?. staged ? 'Staged' : 'Unstaged' } Working Changes\n` ;
102+ let content = `# Working Changes Summary\n \n` ;
74103 if ( result != null ) {
75- content += `> Generated by ${ result . model . name } \n\n---- \n\n${ result ?. parsed . summary } \n\n${ result ?. parsed . body } ` ;
104+ content += `> Generated by ${ result . model . name } \n\n## ${ stagedLabel } Changes \n\n${ result ?. parsed . summary } \n\n${ result ?. parsed . body } ` ;
76105 } else {
77106 content += `> No changes found to explain.` ;
78107 }
0 commit comments