Count directories under .claude/worktrees/. Iterates with an explicit loop so failures surface a descriptive error on stderr (per coding-standards "no silent fallbacks") while still keeping the stop-hook itself non-fatal — the loop body propagates the underlying OSError up.
()
| 268 | |
| 269 | |
| 270 | def count_stale_worktrees() -> int: |
| 271 | """Count directories under .claude/worktrees/. |
| 272 | |
| 273 | Iterates with an explicit loop so failures surface a descriptive error |
| 274 | on stderr (per coding-standards "no silent fallbacks") while still |
| 275 | keeping the stop-hook itself non-fatal — the loop body propagates the |
| 276 | underlying OSError up. |
| 277 | """ |
| 278 | if not WORKTREES_DIR.exists(): |
| 279 | return 0 |
| 280 | try: |
| 281 | iterator = WORKTREES_DIR.iterdir() |
| 282 | except KeyboardInterrupt: |
| 283 | import _thread |
| 284 | |
| 285 | _thread.interrupt_main() |
| 286 | raise |
| 287 | except OSError as exc: |
| 288 | raise OSError( |
| 289 | f"count_stale_worktrees: cannot iterate {WORKTREES_DIR}: {exc}" |
| 290 | ) from exc |
| 291 | count = 0 |
| 292 | for p in iterator: |
| 293 | if p.is_dir() and not p.is_symlink(): |
| 294 | count += 1 |
| 295 | return count |
| 296 | |
| 297 | |
| 298 | def warn_stale_worktrees() -> None: |
no outgoing calls
no test coverage detected