@@ -29,7 +29,7 @@ export class TimeTracker implements vscode.Disposable {
2929 private focusTimeoutSeconds : number = 180 ; // Default 3 minutes * 60 = 180 seconds
3030 private gitWatcher : GitWatcher | null = null ;
3131 private branchCheckInterval : NodeJS . Timeout | null = null ;
32- private isCheckingBranch : boolean = false ;
32+ private branchCheckPromise : Promise < void > | null = null ;
3333 private lastUpdateTime : number = Date . now ( ) ;
3434 private lastFocusTime : number = Date . now ( ) ;
3535 private healthManager : HealthNotificationManager ;
@@ -661,11 +661,14 @@ export class TimeTracker implements vscode.Disposable {
661661
662662 // Check for branch changes less frequently to reduce CPU load
663663 // Changed from 1 second to 5 seconds to reduce the number of git processes spawned
664+ // Only set interval if git setup was successful
664665 this . branchCheckInterval = setInterval ( async ( ) => {
665666 await this . checkBranchChanges ( ) ;
666667 } , 5000 ) ;
667668
668669 } catch ( error ) {
670+ // Ensure interval is not running if git setup failed
671+ this . stopGitWatcher ( ) ;
669672 this . logger . logEvent ( 'branch_check_error' , {
670673 project : this . currentProject ,
671674 currentBranch : this . currentBranch ,
@@ -681,50 +684,59 @@ export class TimeTracker implements vscode.Disposable {
681684
682685 private async checkBranchChanges ( ) {
683686 // Prevent concurrent executions of branch checking
684- if ( this . isCheckingBranch || ! this . gitWatcher || ! this . isTracking ) {
687+ if ( this . branchCheckPromise || ! this . gitWatcher || ! this . isTracking ) {
685688 return ;
686689 }
687690
688- try {
689- this . isCheckingBranch = true ;
690- const branchInfo = await this . gitWatcher . git . branch ( ) ;
691- const currentBranch = branchInfo . current || 'unknown' ;
692-
693- // If branch has changed
694- if ( currentBranch !== this . gitWatcher . lastKnownBranch ) {
695- // Log branch change event
696- this . logger . logEvent ( 'branch_changed' , {
697- project : this . currentProject ,
698- language : this . currentLanguage ,
699- oldBranch : this . gitWatcher . lastKnownBranch ,
700- newBranch : currentBranch
701- } ) ;
691+ // Create a promise chain to serialize branch checks
692+ this . branchCheckPromise = ( async ( ) => {
693+ try {
694+ const branchInfo = await this . gitWatcher ! . git . branch ( ) ;
695+ const currentBranch = branchInfo . current || 'unknown' ;
702696
703- // Save the current session with the old branch
704- await this . saveCurrentSession ( `branch change from ${ this . gitWatcher . lastKnownBranch } to ${ currentBranch } ` ) ;
697+ // If branch has changed
698+ if ( currentBranch !== this . gitWatcher ! . lastKnownBranch ) {
699+ // Log branch change event
700+ this . logger . logEvent ( 'branch_changed' , {
701+ project : this . currentProject ,
702+ language : this . currentLanguage ,
703+ oldBranch : this . gitWatcher ! . lastKnownBranch ,
704+ newBranch : currentBranch
705+ } ) ;
705706
706- // Update branch tracking
707- this . gitWatcher . lastKnownBranch = currentBranch ;
708- this . currentBranch = currentBranch ;
707+ // Save the current session with the old branch
708+ await this . saveCurrentSession ( `branch change from ${ this . gitWatcher ! . lastKnownBranch } to ${ currentBranch } ` ) ;
709709
710- // Start a new session from this point
711- this . startTime = Date . now ( ) ;
710+ // Update branch tracking only after save completes
711+ this . gitWatcher ! . lastKnownBranch = currentBranch ;
712+ this . currentBranch = currentBranch ;
713+
714+ // Start a new session from this point
715+ this . startTime = Date . now ( ) ;
716+ }
717+ } catch ( error ) {
718+ this . logger . logEvent ( 'branch_check_error' , {
719+ project : this . currentProject ,
720+ currentBranch : this . currentBranch ,
721+ currentLanguage : this . currentLanguage ,
722+ error : error instanceof Error ?
723+ `Branch check error: ${ error . message } ` :
724+ 'Unknown branch check error' ,
725+ location : 'checkBranchChanges'
726+ } ) ;
727+ console . error ( 'Error checking branch changes:' , error ) ;
728+
729+ // Stop git watcher on persistent errors to prevent resource waste
730+ if ( error instanceof Error && error . message . includes ( 'Not a git repository' ) ) {
731+ this . stopGitWatcher ( ) ;
732+ }
733+ } finally {
734+ // Always reset the promise to allow future checks
735+ this . branchCheckPromise = null ;
712736 }
713- } catch ( error ) {
714- this . logger . logEvent ( 'branch_check_error' , {
715- project : this . currentProject ,
716- currentBranch : this . currentBranch ,
717- currentLanguage : this . currentLanguage ,
718- error : error instanceof Error ?
719- `Branch check error: ${ error . message } ` :
720- 'Unknown branch check error' ,
721- location : 'checkBranchChanges'
722- } ) ;
723- console . error ( 'Error checking branch changes:' , error ) ;
724- } finally {
725- // Always reset the flag to allow future checks
726- this . isCheckingBranch = false ;
727- }
737+ } ) ( ) ;
738+
739+ await this . branchCheckPromise ;
728740 }
729741
730742 private stopGitWatcher ( ) {
0 commit comments