@@ -90,6 +90,7 @@ export class CodacyCloud implements vscode.Disposable {
9090 private _cli : CodacyCli | undefined
9191
9292 private _disposables : vscode . Disposable [ ] = [ ]
93+ private _stateChangeDisposable : vscode . Disposable | undefined
9394
9495 constructor ( ) {
9596 vscode . commands . executeCommand ( 'setContext' , RM_STATE_CONTEXT_KEY , this . _state )
@@ -103,7 +104,56 @@ export class CodacyCloud implements vscode.Disposable {
103104 this . _current = gitRepository
104105
105106 try {
107+ // Check if repository state is fully populated
108+ if ( ! gitRepository . state . HEAD && gitRepository . state . remotes ?. length === 0 ) {
109+ Logger . debug ( 'Repository state is not fully populated yet, waiting for state change...' )
110+ this . state = CodacyCloudState . Initializing
111+
112+ // Clean up any existing state change listener
113+ if ( this . _stateChangeDisposable ) {
114+ this . _stateChangeDisposable . dispose ( )
115+ this . _stateChangeDisposable = undefined
116+ }
117+
118+ // Set up timeout to prevent memory leak
119+ const timeoutMs = 30000
120+ let hasRepositoryStateTimedOut = false
121+ const repositoryStateTimeout = setTimeout ( ( ) => {
122+ hasRepositoryStateTimedOut = true
123+ Logger . appendLine ( `Repository state change timeout after ${ timeoutMs } ms, assuming repository is not valid` )
124+ if ( this . _stateChangeDisposable ) {
125+ this . _stateChangeDisposable . dispose ( )
126+ this . _stateChangeDisposable = undefined
127+ }
128+ this . state = CodacyCloudState . NoGitRepository
129+ } , timeoutMs )
130+
131+ // Listen for state changes to detect when repository is fully loaded
132+ this . _stateChangeDisposable = gitRepository . state . onDidChange ( ( ) => {
133+ if ( hasRepositoryStateTimedOut ) return // Don't process if we've already timed out
134+
135+ Logger . appendLine (
136+ `Repository state changed - HEAD: ${ gitRepository . state . HEAD ?. name || 'undefined' } , Remotes: ${
137+ gitRepository . state . remotes ?. length || 0
138+ } `
139+ )
140+ if ( gitRepository . state . HEAD || gitRepository . state . remotes ?. length > 0 ) {
141+ clearTimeout ( repositoryStateTimeout )
142+ if ( this . _stateChangeDisposable ) {
143+ this . _stateChangeDisposable . dispose ( )
144+ this . _stateChangeDisposable = undefined
145+ }
146+ openRepository ( )
147+ }
148+ } )
149+
150+ // Add to disposables for cleanup
151+ this . _disposables . push ( this . _stateChangeDisposable )
152+ return
153+ }
154+
106155 if ( gitRepository . state . HEAD === undefined ) {
156+ Logger . appendLine ( 'Repository HEAD is undefined but remotes are available' )
107157 this . state = CodacyCloudState . Initializing
108158 } else {
109159 const remotesWithPushUrl = gitRepository . state . remotes . filter ( ( remote ) => remote . pushUrl )
@@ -473,6 +523,14 @@ export class CodacyCloud implements vscode.Disposable {
473523
474524 public close ( repository : GitRepository ) {
475525 if ( this . _current === repository ) {
526+ Logger . appendLine ( `CodacyCloud close: ${ repository . rootUri . fsPath } ` )
527+
528+ // Clean up state change listener when repository is closed
529+ if ( this . _stateChangeDisposable ) {
530+ this . _stateChangeDisposable . dispose ( )
531+ this . _stateChangeDisposable = undefined
532+ }
533+
476534 this . clear ( )
477535 }
478536 }
@@ -491,7 +549,6 @@ export class CodacyCloud implements vscode.Disposable {
491549
492550 public refresh ( ) {
493551 if ( ! this . _current ) return
494-
495552 switch ( this . _state ) {
496553 case CodacyCloudState . Loaded :
497554 case CodacyCloudState . IsAnalyzing :
0 commit comments