Answer architecture round 5: one shared git command parser for rule-hook and reading-cost, the reflog check unrolled, and rule-hook's Outcome and LogLine named apart from their namesakes
This commit is contained in:
@@ -14,7 +14,7 @@ LOG = "rule-overrides.tsv"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Entry:
|
||||
class LogLine:
|
||||
"""One line of the log, its fields tab-separated in this order."""
|
||||
at: str
|
||||
branch: str
|
||||
@@ -28,12 +28,12 @@ class Entry:
|
||||
return "\t".join(" ".join(field.split()) for field in dataclasses.astuple(self)) + "\n"
|
||||
|
||||
|
||||
def parsed_entry(line: str) -> Entry | None:
|
||||
def parsed_entry(line: str) -> LogLine | None:
|
||||
fields = line.rstrip("\n").split("\t")
|
||||
return Entry(*fields) if len(fields) == len(dataclasses.fields(Entry)) else None
|
||||
return LogLine(*fields) if len(fields) == len(dataclasses.fields(LogLine)) else None
|
||||
|
||||
|
||||
def this_chunks(entry: Entry, reflog: set[str], branch: str, on_default: typing.Callable[[str], bool]) -> bool:
|
||||
def this_chunks(entry: LogLine, reflog: set[str], branch: str, on_default: typing.Callable[[str], bool]) -> bool:
|
||||
"""Whether the override is the current branch's: its commit is in the branch's reflog, which keeps a commit an amend or a rebase replaced, follows `git branch -m`, and holds no other branch's commit."""
|
||||
if entry.commit == "":
|
||||
return True
|
||||
@@ -62,7 +62,7 @@ def log_path(directory: str) -> Log | Failure | None:
|
||||
return Log(pathlib.Path(str(common)) / LOG, str(branch) or "HEAD", "" if isinstance(head, Failure) else head)
|
||||
|
||||
|
||||
def write_override(log: Log, entry: Entry) -> Failure | None:
|
||||
def write_override(log: Log, entry: LogLine) -> Failure | None:
|
||||
try:
|
||||
# No symlink, and no FIFO to hang on: the checkout, and so its git directory, may come from anyone.
|
||||
descriptor = os.open(log.path, os.O_WRONLY | os.O_APPEND | os.O_CREAT | os.O_NOFOLLOW | os.O_NONBLOCK, 0o600)
|
||||
@@ -79,8 +79,8 @@ def write_override(log: Log, entry: Entry) -> Failure | None:
|
||||
return None
|
||||
|
||||
|
||||
def logged(log: Log, breach: Breach, reason: str, what: str, now: float) -> Entry:
|
||||
return Entry(utc(now), log.branch, log.head, breach.rule, reason, what)
|
||||
def logged(log: Log, breach: Breach, reason: str, what: str, now: float) -> LogLine:
|
||||
return LogLine(utc(now), log.branch, log.head, breach.rule, reason, what)
|
||||
|
||||
|
||||
def print_overrides(directory: str) -> int:
|
||||
@@ -92,8 +92,7 @@ def print_overrides(directory: str) -> int:
|
||||
branch = found if isinstance(found, str) and found else "HEAD"
|
||||
reflog = git(directory, "reflog", "show", "--format=%H", branch)
|
||||
head = git(directory, "rev-parse", "HEAD")
|
||||
# A reflog missing HEAD is off or expired, and would hide this chunk's overrides.
|
||||
unread = reflog if isinstance(reflog, Failure) else head if isinstance(head, Failure) else None if head in reflog.split() else Failure("it does not hold HEAD")
|
||||
unread = unread_reflog(reflog, head)
|
||||
if unread:
|
||||
print(f"rule-hook overrides cannot read the branch's reflog, so it cannot tell which overrides are this chunk's: {unread.reason}", file=sys.stderr)
|
||||
return 1
|
||||
@@ -107,6 +106,15 @@ def print_overrides(directory: str) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def unread_reflog(reflog: str | Failure, head: str | Failure) -> Failure | None:
|
||||
if isinstance(reflog, Failure):
|
||||
return reflog
|
||||
if isinstance(head, Failure):
|
||||
return head
|
||||
# A reflog missing HEAD is off or expired, and would hide this chunk's overrides.
|
||||
return None if head in reflog.split() else Failure("it does not hold HEAD")
|
||||
|
||||
|
||||
def default_branch(directory: str) -> str | None:
|
||||
return next((name for name in ("origin/HEAD", "main", "master") if not isinstance(git(directory, "rev-parse", "--verify", "--quiet", name), Failure)), None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user