utils/devstats/discover.py
Krzysztof kuhy Rudnicki 6e5b5d1c3a Add devstats: kuhy vs the 100x article
`python -m devstats compare` walks ~/src, counts kuhy's default-branch
commits once per piece of work (worktrees, clones and filter-repo copies
folded in), drops lockfiles/generated/vendored/data via excludes.py, and
prints 30/90/300-day net LoC/day, per-active-day vs 194, commits and
tags beside the article's Cmdr figures. --write regenerates
devstats/DOCS-comparison.md (aggregates only); --by-repo is terminal-only.
Read-only: only rev-parse, for-each-ref and log, GIT_OPTIONAL_LOCKS=0.

Bug-class-check: none — new tool, not a bug fix
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 12:18:05 +01:00

48 lines
1.6 KiB
Python

"""Find repos one level under each root and fold worktrees into their repo.
Worktrees (e.g. the sb-wt-* signal-bot checkouts) share one git common dir
with their main repo, so deduplicating on it counts every repo once, named
after the common dir's parent whichever checkout was walked first.
"""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
from devstats.gitcmd import GitError, git
@dataclass(frozen=True)
class Repo:
"""One repository: display name and a checkout path to run git in."""
name: str
path: Path
def _candidates(root: Path) -> list[Path]:
"""The root itself if it is a checkout, else its direct children that are."""
if (root / ".git").exists():
return [root]
if not root.is_dir():
return []
return sorted(p for p in root.iterdir() if p.is_dir() and (p / ".git").exists())
def discover(roots: Sequence[Path]) -> tuple[list[Repo], list[str]]:
"""Repos under `roots`, one per git common dir, plus warnings for broken ones."""
seen: dict[Path, Repo] = {}
warnings: list[str] = []
for root in roots:
for path in _candidates(root):
try:
out = git(path, "rev-parse", "--path-format=absolute", "--git-common-dir")
except GitError:
warnings.append(f"{path.name}: not a readable git repo, skipped")
continue
common = Path(out.decode().strip())
seen.setdefault(common, Repo(name=common.parent.name, path=path))
return sorted(seen.values(), key=lambda r: r.name), warnings