@@ -16,7 +16,7 @@ import { coalesce } from '../../../util/vs/base/common/arrays';
1616import { Disposable } from '../../../util/vs/base/common/lifecycle' ;
1717import { derived , IObservable } from '../../../util/vs/base/common/observable' ;
1818import * as path from '../../../util/vs/base/common/path' ;
19- import { basename } from '../../../util/vs/base/common/resources' ;
19+ import { basename , isEqual } from '../../../util/vs/base/common/resources' ;
2020import { ChatSessionWorktreeData , ChatSessionWorktreeProperties , IChatSessionWorktreeService } from '../common/chatSessionWorktreeService' ;
2121
2222const CHAT_SESSION_WORKTREE_MEMENTO_KEY = 'github.copilot.cli.sessionWorktrees' ;
@@ -233,24 +233,42 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi
233233 }
234234
235235 async getWorktreeChanges ( sessionId : string ) : Promise < vscode . ChatSessionChangedFile [ ] | undefined > {
236- await this . gitService . initialize ( ) ;
237-
238236 if ( this . _sessionWorktreeChanges . has ( sessionId ) ) {
239237 return this . _sessionWorktreeChanges . get ( sessionId ) ;
240238 }
241239
240+ // Check whether the session has an associated worktree
242241 const worktreePath = this . getWorktreePath ( sessionId ) ;
243- const worktreeProperties = this . getWorktreeProperties ( sessionId ) ;
244-
245242 if ( ! worktreePath ) {
243+ return undefined ;
244+ }
245+
246+ // Ensure the initial repository discovery is completed and the repository
247+ // states are initialized in the vscode.git extension. At this point, the
248+ // worktrees may not have been opened yet so we may need to explicitly open
249+ // them.
250+ await this . gitService . initialize ( ) ;
251+
252+ // Check whether the worktree belongs to any of the discovered repositories
253+ const worktreePaths = this . gitService . repositories . map ( r => r . worktrees . map ( w => w . path ) ) . flat ( ) ;
254+ if ( ! worktreePaths . some ( p => isEqual ( vscode . Uri . file ( p ) , worktreePath ) ) ) {
246255 this . _sessionWorktreeChanges . set ( sessionId , undefined ) ;
247256 return undefined ;
248257 }
249258
259+ // Open the worktree repository. This will initialize the repository state
260+ // in the vscode.git extension but the source control provider will not be
261+ // shown in the Source Control view since it is being hidden.
262+ const repository = await this . gitService . getRepository ( worktreePath ) ;
263+ if ( ! repository ) {
264+ this . _sessionWorktreeChanges . set ( sessionId , undefined ) ;
265+ return undefined ;
266+ }
267+
268+ const worktreeProperties = this . getWorktreeProperties ( sessionId ) ;
250269 if ( worktreeProperties === undefined || worktreeProperties . autoCommit === false ) {
251270 // These changes are staged in the worktree but not yet committed
252- const repository = await this . gitService . getRepository ( worktreePath , false ) ;
253- if ( ! repository ?. changes ) {
271+ if ( ! repository . changes ) {
254272 this . _sessionWorktreeChanges . set ( sessionId , [ ] ) ;
255273 return [ ] ;
256274 }
0 commit comments