Skip to content

Commit 88bd2a8

Browse files
authored
fix: optimize git branch monitoring and reduce CPU load (#58)
1 parent 1159845 commit 88bd2a8

5 files changed

Lines changed: 52 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ For technical details about development, release process, and internal architect
105105

106106
## Changelog
107107

108+
### [0.4.1] - 2025-07-26
109+
- Fixed issue with excessive git processes being spawned
110+
- Optimized git branch monitoring to reduce CPU load
111+
- Reduced frequency of git checks from every 1 second to every 5 seconds
112+
- Improved cleanup of interval timers to prevent memory leaks
113+
108114
### [0.4.0] - 2025-06-28
109115
- Added Git branch tracking to monitor time spent on different branches
110116
- Enhanced project view with branch-specific time tracking

TECHNICAL.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ The release process is defined in two GitHub Actions workflow files:
152152
- Data processing
153153
- Helper functions
154154

155+
### Git Branch Tracking
156+
157+
The extension uses the `simple-git` library to monitor git branch changes and associate coding time with specific branches:
158+
159+
1. **Initialization**
160+
- The git watcher is initialized in `timeTracker.ts` through the `setupGitWatcher()` method
161+
- It checks if the current workspace is a git repository
162+
- Sets up an interval to periodically check for branch changes
163+
164+
2. **Branch Monitoring**
165+
- Checks for branch changes every 5 seconds (optimized from 1 second to reduce CPU load)
166+
- Uses `git.branch()` to get the current branch name
167+
- When a branch change is detected, the current session is saved and a new one is started
168+
169+
3. **Optimization Considerations**
170+
- The interval is cleared and recreated when appropriate to prevent multiple overlapping intervals
171+
- A locking mechanism prevents concurrent executions of branch checking
172+
- Proper cleanup is implemented when the extension is deactivated
173+
155174
### Data Flow
156175

157176
1. User activity triggers events in VS Code

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Quick capture for ideas and tasks while coding.
2424
- A variable maybe for tracking the active coding session duration, and it will notify when it exceeds a certain threshold.
2525

2626
## Bug Fixes
27+
- [X] Fix excessive git processes being spawned during branch tracking (2025-07-26)
28+
- [X] Optimize CPU usage when monitoring git branches (2025-07-26)
2729

2830

2931
## Ideas

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "simple-coding-time-tracker",
33
"displayName": "Simple Coding Time Tracker",
44
"description": "Track and visualize your coding time across projects",
5-
"version": "0.4.0",
5+
"version": "0.4.1",
66
"publisher": "noorashuvo",
77
"license": "MIT",
88
"icon": "icon-sctt.png",

src/timeTracker.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class TimeTracker implements vscode.Disposable {
2525
private focusTimeoutSeconds: number = 180;
2626
private gitWatcher: GitWatcher | null = null;
2727
private branchCheckInterval: NodeJS.Timeout | null = null;
28+
private isCheckingBranch: boolean = false;
2829
private lastUpdateTime: number = Date.now();
2930
private lastFocusTime: number = Date.now();
3031
// Track time between updates for validation
@@ -445,7 +446,17 @@ export class TimeTracker implements vscode.Disposable {
445446
}
446447

447448
dispose() {
448-
this.stopTracking();
449+
// Make sure all intervals are stopped
450+
this.stopTracking('extension disposed');
451+
452+
// Ensure git watcher is completely stopped
453+
this.stopGitWatcher();
454+
455+
// Clear any other potentially running intervals
456+
if (this.saveInterval) {
457+
clearInterval(this.saveInterval);
458+
this.saveInterval = null;
459+
}
449460
}
450461

451462
public registerStatusBarCommand(command: string) {
@@ -481,6 +492,9 @@ export class TimeTracker implements vscode.Disposable {
481492
}
482493

483494
private async setupGitWatcher() {
495+
// Clear any existing interval first to prevent duplicate watchers
496+
this.stopGitWatcher();
497+
484498
try {
485499
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
486500
if (!workspaceFolder) {
@@ -510,10 +524,11 @@ export class TimeTracker implements vscode.Disposable {
510524
lastKnownBranch: branchInfo.current || 'unknown'
511525
};
512526

513-
// Check for branch changes every second
527+
// Check for branch changes less frequently to reduce CPU load
528+
// Changed from 1 second to 5 seconds to reduce the number of git processes spawned
514529
this.branchCheckInterval = setInterval(async () => {
515530
await this.checkBranchChanges();
516-
}, 1000);
531+
}, 5000);
517532

518533
} catch (error) {
519534
this.logger.logEvent('branch_check_error', {
@@ -529,11 +544,13 @@ export class TimeTracker implements vscode.Disposable {
529544
}
530545

531546
private async checkBranchChanges() {
532-
if (!this.gitWatcher || !this.isTracking) {
547+
// Prevent concurrent executions of branch checking
548+
if (this.isCheckingBranch || !this.gitWatcher || !this.isTracking) {
533549
return;
534550
}
535551

536552
try {
553+
this.isCheckingBranch = true;
537554
const branchInfo = await this.gitWatcher.git.branch();
538555
const currentBranch = branchInfo.current || 'unknown';
539556

@@ -566,6 +583,9 @@ export class TimeTracker implements vscode.Disposable {
566583
location: 'checkBranchChanges'
567584
});
568585
console.error('Error checking branch changes:', error);
586+
} finally {
587+
// Always reset the flag to allow future checks
588+
this.isCheckingBranch = false;
569589
}
570590
}
571591

0 commit comments

Comments
 (0)