utils/devstats/gitcmd.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

27 lines
774 B
Python

"""Read-only git subprocess wrapper.
GIT_OPTIONAL_LOCKS=0 stops even read commands from refreshing the index of a
walked repo, so a run never changes anything in kuhy's checkouts.
"""
from __future__ import annotations
import os
import subprocess
from pathlib import Path
class GitError(RuntimeError):
"""git exited non-zero."""
def git(repo: Path, *args: str) -> bytes:
"""Run `git -C repo args...` and return raw stdout, raising GitError on failure."""
env = {**os.environ, "GIT_OPTIONAL_LOCKS": "0", "LC_ALL": "C"}
proc = subprocess.run(
["git", "-C", str(repo), *args], env=env, capture_output=True, check=False
)
if proc.returncode != 0:
raise GitError(proc.stderr.decode(errors="replace").strip())
return proc.stdout