* Watch the loose ref file for the current branch. * Called on startup and whenever HEAD changes (branch switch).
()
| 389 | * Called on startup and whenever HEAD changes (branch switch). |
| 390 | */ |
| 391 | private async watchCurrentBranchRef(): Promise<void> { |
| 392 | if (!this.gitDir) { |
| 393 | return |
| 394 | } |
| 395 | |
| 396 | const head = await readGitHead(this.gitDir) |
| 397 | // Branch refs live in commonDir for worktrees (gitDir for regular repos) |
| 398 | const refsDir = this.commonDir ?? this.gitDir |
| 399 | const refPath = |
| 400 | head?.type === 'branch' ? join(refsDir, 'refs', 'heads', head.name) : null |
| 401 | |
| 402 | // Already watching this ref (or already not watching anything) |
| 403 | if (refPath === this.branchRefPath) { |
| 404 | return |
| 405 | } |
| 406 | |
| 407 | // Stop watching old branch ref. Runs for branch→branch AND |
| 408 | // branch→detached (checkout --detach, rebase, bisect). |
| 409 | if (this.branchRefPath) { |
| 410 | unwatchFile(this.branchRefPath) |
| 411 | this.watchedPaths = this.watchedPaths.filter( |
| 412 | p => p !== this.branchRefPath, |
| 413 | ) |
| 414 | } |
| 415 | |
| 416 | this.branchRefPath = refPath |
| 417 | |
| 418 | if (!refPath) { |
| 419 | return |
| 420 | } |
| 421 | |
| 422 | // The ref file may not exist yet (new branch before first commit). |
| 423 | // watchFile works on nonexistent files — it fires when the file appears. |
| 424 | this.watchPath(refPath, () => { |
| 425 | this.invalidate() |
| 426 | }) |
| 427 | } |
| 428 | |
| 429 | private async onHeadChanged(): Promise<void> { |
| 430 | // HEAD changed — could be a branch switch or detach. |
no test coverage detected