Answer architecture round 1: move the base fallback, heading check and seat names into the pure modules, move legacy scores through record-scoring, and read origin as last fetched when a fetch fails
This commit is contained in:
+20
-6
@@ -1,7 +1,9 @@
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from failure import Failure
|
||||
from larv_review.rules import HistoryChange
|
||||
from larv_review.history import HistoryChange
|
||||
|
||||
HISTORY = "docs/comprehension-history.md"
|
||||
|
||||
@@ -26,20 +28,32 @@ def default_branch() -> str | Failure:
|
||||
return remotes
|
||||
if "origin" in remotes.split():
|
||||
if isinstance(fetched := git("fetch", "--quiet", "origin"), Failure):
|
||||
return fetched
|
||||
print(f"reading origin as last fetched: {fetched.reason}", file=sys.stderr)
|
||||
default = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD")
|
||||
if isinstance(default, Failure):
|
||||
return Failure(f"origin names no default branch; set it with `git remote set-head origin --auto`: {default.reason}")
|
||||
return default.strip()
|
||||
return default.strip() if not isinstance(default, Failure) else origin_head()
|
||||
for branch in ("main", "master"):
|
||||
if not isinstance(git("rev-parse", "--quiet", "--verify", f"refs/heads/{branch}"), Failure):
|
||||
return branch
|
||||
return Failure("no origin remote, and no main or master branch to take as the default branch")
|
||||
|
||||
|
||||
def origin_head() -> 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")
|
||||
if isinstance(listed, Failure):
|
||||
return listed
|
||||
if named := re.match(r"ref: refs/heads/(\S+)\tHEAD", listed):
|
||||
return f"origin/{named.group(1)}"
|
||||
return Failure("origin names no default branch")
|
||||
|
||||
|
||||
def commit_exists(commit: str) -> bool:
|
||||
return not isinstance(git("rev-parse", "--quiet", "--verify", f"{commit}^{{commit}}"), Failure)
|
||||
|
||||
|
||||
def file_at(revision: str, path: str) -> str | Failure:
|
||||
"""A file's text at a revision, empty where the revision is not a commit, such as a root commit's parent, or holds no such file."""
|
||||
if isinstance(git("rev-parse", "--quiet", "--verify", f"{revision}^{{commit}}"), Failure):
|
||||
if not commit_exists(revision):
|
||||
return ""
|
||||
listed = git("ls-tree", "--name-only", revision, "--", path)
|
||||
if isinstance(listed, Failure):
|
||||
|
||||
Reference in New Issue
Block a user