Answer architecture round 1: 2 🔴, 4 🟡 (0 misread); one branch_chunks reader with a pure fix rule, routed chunks matched by item
This commit is contained in:
+17
-10
@@ -4,6 +4,7 @@ Every tool in bin/ starts git through `git` here."""
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
|
||||
@@ -42,6 +43,12 @@ def git(*args: str, cwd: str | None = None, timeout: float | None = None, guarde
|
||||
return ran.stdout if ran.returncode == 0 else Failure(f"git {command}: {ran.stderr.strip()}")
|
||||
|
||||
|
||||
class Git(typing.Protocol):
|
||||
"""Runs git as `git` does."""
|
||||
|
||||
def __call__(self, *args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> str | Failure: ...
|
||||
|
||||
|
||||
def top() -> str | Failure:
|
||||
found = git("rev-parse", "--show-toplevel")
|
||||
return found if isinstance(found, Failure) else found.strip()
|
||||
@@ -60,7 +67,7 @@ def default_branch(stale_ok: bool, timeout: float | None = None) -> tuple[str, s
|
||||
return ref if isinstance(ref, Failure) else (ref, note)
|
||||
|
||||
|
||||
def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> str | Failure:
|
||||
def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: bool = False, git: Git = git) -> str | Failure:
|
||||
"""The default branch's ref as last fetched, read in cwd, else in the current directory: `origin/<branch>` with that remote, else the local main or master.
|
||||
|
||||
Where the checkout records no default branch for origin, it asks origin, which fails after timeout seconds. Guarded, it runs git as `git` does with guarded, and fails there instead of asking origin."""
|
||||
@@ -71,7 +78,7 @@ def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: b
|
||||
recorded = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=cwd, guarded=guarded)
|
||||
if not isinstance(recorded, Failure):
|
||||
return recorded.strip()
|
||||
return Failure("origin records no default branch") if guarded else origin_head(cwd, timeout)
|
||||
return Failure("origin records no default branch") if guarded else origin_head(cwd, timeout, git)
|
||||
for branch in ("main", "master"):
|
||||
if not isinstance(git("rev-parse", "--quiet", "--verify", f"refs/heads/{branch}", cwd=cwd, guarded=guarded), Failure):
|
||||
return branch
|
||||
@@ -86,7 +93,7 @@ def branch_titles(default: str, cwd: str | None = None, timeout: float | None =
|
||||
return titles if isinstance(titles, Failure) else titles.splitlines()
|
||||
|
||||
|
||||
def origin_head(cwd: str | None, timeout: float | None) -> str | Failure:
|
||||
def origin_head(cwd: str | None, timeout: float | None, git: Git) -> str | Failure:
|
||||
"""The default branch origin names, for a clone made by `git init` and `git remote add`, which records none."""
|
||||
listed = git("ls-remote", "--symref", "origin", "HEAD", cwd=cwd, timeout=timeout)
|
||||
if isinstance(listed, Failure):
|
||||
@@ -109,18 +116,18 @@ def commits_since(tag: str | None, branch: str, cwd: str | None = None) -> int |
|
||||
return count if isinstance(count, Failure) else int(count)
|
||||
|
||||
|
||||
def commit_exists(commit: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> bool:
|
||||
return not isinstance(git("rev-parse", "--quiet", "--verify", f"{commit}^{{commit}}", cwd=cwd, timeout=timeout, guarded=guarded), Failure)
|
||||
def commit_exists(commit: str, cwd: str | None = None) -> bool:
|
||||
return not isinstance(git("rev-parse", "--quiet", "--verify", f"{commit}^{{commit}}", cwd=cwd), Failure)
|
||||
|
||||
|
||||
def file_at(revision: str, path: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> str | Failure:
|
||||
"""A file's text at a revision, read in cwd, else in the current directory, each git call run as `git` runs it with timeout and guarded; empty where the revision is not a commit, such as a root commit's parent, or holds no such file."""
|
||||
if not commit_exists(revision, cwd, timeout, guarded):
|
||||
def file_at(revision: str, path: str, cwd: str | None = None) -> str | Failure:
|
||||
"""A file's text at a revision, read in cwd, else in the current directory; empty where the revision is not a commit, such as a root commit's parent, or holds no such file."""
|
||||
if not commit_exists(revision, cwd):
|
||||
return ""
|
||||
listed = git("ls-tree", "--name-only", revision, "--", path, cwd=cwd, timeout=timeout, guarded=guarded)
|
||||
listed = git("ls-tree", "--name-only", revision, "--", path, cwd=cwd)
|
||||
if isinstance(listed, Failure):
|
||||
return listed
|
||||
return git("show", f"{revision}:{path}", cwd=cwd, timeout=timeout, guarded=guarded) if listed.strip() else ""
|
||||
return git("show", f"{revision}:{path}", cwd=cwd) if listed.strip() else ""
|
||||
|
||||
|
||||
def full_history() -> Failure | None:
|
||||
|
||||
Reference in New Issue
Block a user