utils/devstats/refs.py
Krzysztof kuhy Rudnicki f370d313b5
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
devstats: run.sh, HTML report, origin refs
devstats/run.sh installs python3/git via pacman when missing, refuses
python < 3.11, runs `compare` (capped when capped.sh exists), prints the
tables, writes ~/data/devstats/report.html (bar chart above the tables)
and opens it unless --no-open.

The local default branch is now logged together with its origin/
tracking ref (union, each SHA once), so the 16 "behind its upstream
tracking ref" warnings are gone without pulling any checkout.

Bug-class-check: devstats/tests/test_refs.py
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:32:40 +01:00

67 lines
2.5 KiB
Python

"""Default-branch resolution from refs on disk (never fetches, never pulls).
The local default branch is logged together with its `origin/` tracking ref
when the two tips differ. Work landed with `git push origin HEAD:main` from a
worktree sits only on the tracking ref until the checkout is pulled, and kuhy's
live checkouts (tutor, todo) must not be pulled by a stats run. `git log` walks
the union, so each SHA counts once and a diverged repo counts both sides.
origin/HEAD only names the branch; a branch that exists only on origin/ is not
counted (no local checkout of that work).
"""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from devstats.gitcmd import git
_FORMAT = "%(refname)%09%(symref)%09%(objectname)%09%(committerdate:unix)%09%(creatordate:unix)"
_ORIGIN = "refs/remotes/origin/"
@dataclass(frozen=True)
class Ref:
"""One ref: tip sha, tip committer ts (0 for tag objects), symref target, creator ts."""
sha: str
ts: int
symref: str
created: int
@dataclass(frozen=True)
class DefaultRefs:
"""The default branch name and the refs whose union is logged."""
branch: str | None
log_refs: tuple[str, ...]
def list_refs(repo: Path) -> dict[str, Ref]:
"""Every local branch, origin remote-tracking ref and tag, in one git call."""
out = git(repo, "for-each-ref", f"--format={_FORMAT}", "refs/heads", "refs/remotes/origin",
"refs/tags").decode(errors="replace")
refs: dict[str, Ref] = {}
for line in out.splitlines():
name, symref, sha, ts, created = line.split("\t")
refs[name] = Ref(sha=sha, ts=int(ts or 0), symref=symref, created=int(created or 0))
return refs
def tag_times(refs: dict[str, Ref]) -> list[tuple[str, int]]:
"""(object sha, creator ts) of every tag: tagger date, or the commit date if lightweight."""
return [(r.sha, r.created) for name, r in refs.items() if name.startswith("refs/tags/")]
def resolve(refs: dict[str, Ref]) -> DefaultRefs:
"""origin/HEAD's branch, else main, else master: the first that exists locally."""
head = refs.get(_ORIGIN + "HEAD")
candidates = [head.symref.removeprefix(_ORIGIN)] if head and head.symref else []
for branch in (*candidates, "main", "master"):
local, remote = refs.get("refs/heads/" + branch), refs.get(_ORIGIN + branch)
if local:
tracking = (_ORIGIN + branch,) if remote and remote.sha != local.sha else ()
return DefaultRefs(branch, ("refs/heads/" + branch, *tracking))
return DefaultRefs(None, ())