@@ -12,12 +12,17 @@ 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' ;
15+ import {
16+ isCommandContextViewNodeHasRepoPath ,
17+ isCommandContextViewNodeHasRepository ,
18+ isCommandContextViewNodeHasWorktree ,
19+ } from './commandContext.utils' ;
1620
1721export interface ExplainWipCommandArgs {
1822 repoPath ?: string | Uri ;
1923 staged ?: boolean ;
2024 source ?: AIExplainSource ;
25+ worktreePath ?: string ;
2126}
2227
2328@command ( )
@@ -27,7 +32,12 @@ export class ExplainWipCommand extends GlCommandBase {
2732 }
2833
2934 protected override preExecute ( context : CommandContext , args ?: ExplainWipCommandArgs ) : Promise < void > {
30- if ( isCommandContextViewNodeHasRepository ( context ) ) {
35+ if ( isCommandContextViewNodeHasWorktree ( context ) ) {
36+ args = { ...args } ;
37+ args . repoPath = context . node . worktree . repoPath ;
38+ args . worktreePath = context . node . worktree . path ;
39+ args . source = args . source ?? { source : 'view' , type : 'wip' } ;
40+ } else if ( isCommandContextViewNodeHasRepository ( context ) ) {
3141 args = { ...args } ;
3242 args . repoPath = context . node . repo . path ;
3343 args . source = args . source ?? { source : 'view' , type : 'wip' } ;
@@ -45,10 +55,13 @@ export class ExplainWipCommand extends GlCommandBase {
4555
4656 let repository ;
4757 if ( args ?. repoPath != null ) {
48- repository = this . container . git . getRepository ( args . repoPath ) ;
49- }
50-
51- if ( repository == null ) {
58+ if ( args . worktreePath ) {
59+ // TODO use await this.container.git.diff(args.worktreePath) instead, this will be more performant
60+ repository = await this . container . git . getOrOpenRepository ( args . worktreePath , { closeOnOpen : true } ) ;
61+ } else {
62+ repository = this . container . git . getRepository ( args . repoPath ) ;
63+ }
64+ } else {
5265 uri = getCommandUri ( uri , editor ) ;
5366 const gitUri = uri != null ? await GitUri . fromUri ( uri ) : undefined ;
5467 repository = await getBestRepositoryOrShowPicker (
@@ -80,26 +93,50 @@ export class ExplainWipCommand extends GlCommandBase {
8093 to = uncommitted ;
8194 }
8295
83- const diff = await diffService . getDiff ( to ) ;
96+ // If a worktree path is specified, use it for the diff
97+ // const options = args?.worktreePath ? { uris: [Uri.file(args.worktreePath)] } : undefined;
98+ const options = undefined ;
99+ const diff = await diffService . getDiff ( to , undefined , options ) ;
84100 if ( ! diff ?. contents ) {
85101 void showGenericErrorMessage ( 'No working changes found to explain' ) ;
86102 return ;
87103 }
88104
105+ // Get worktree info
106+ let worktreeInfo = '' ;
107+ let worktreeDisplayName = '' ;
108+
109+ if ( args ?. worktreePath ) {
110+ // Get the worktree name if available
111+ const worktrees = await repository . git . worktrees ( ) ?. getWorktrees ( ) ;
112+ const worktree = worktrees ?. find ( w => w . path === args . worktreePath ) ;
113+
114+ if ( worktree ) {
115+ worktreeInfo = ` in ${ worktree . name } ` ;
116+ worktreeDisplayName = ` (${ worktree . name } )` ;
117+ } else {
118+ worktreeInfo = ` in worktree` ;
119+ worktreeDisplayName = ` (${ args . worktreePath } )` ;
120+ }
121+ }
122+
89123 // Call the AI service to explain the changes
90124 const result = await this . container . ai . explainChanges (
91125 {
92126 diff : diff . contents ,
93- message : `${ stagedLabel } working changes` ,
127+ message : `${ stagedLabel } working changes${ worktreeInfo } ` ,
94128 } ,
95129 args . source ?? { source : 'commandPalette' , type : 'wip' } ,
96130 {
97- progress : { location : ProgressLocation . Notification , title : 'Explaining working changes...' } ,
131+ progress : {
132+ location : ProgressLocation . Notification ,
133+ title : `Explaining working changes${ worktreeInfo } ...` ,
134+ } ,
98135 } ,
99136 ) ;
100137
101- // Display the result
102- let content = `# Working Changes Summary \n\n` ;
138+ const title = `Working Changes Summary ${ worktreeDisplayName } ` ;
139+ let content = `# ${ title } \n\n` ;
103140 if ( result != null ) {
104141 content += `> Generated by ${ result . model . name } \n\n## ${ stagedLabel } Changes\n\n${ result ?. parsed . summary } \n\n${ result ?. parsed . body } ` ;
105142 } else {
0 commit comments