Answer architecture round 3: name the fetch flag stale_ok, and say in --help which commands exit 1 when origin cannot be fetched

This commit is contained in:
2026-10-09 01:26:30 +02:00
parent fb764a7549
commit e2cb4a26f8
2 changed files with 12 additions and 12 deletions
+3 -3
View File
@@ -20,14 +20,14 @@ def top() -> str | Failure:
return found if isinstance(found, Failure) else found.strip() return found if isinstance(found, Failure) else found.strip()
def default_branch(fetched: bool) -> tuple[str, str | None] | Failure: def default_branch(stale_ok: bool) -> tuple[str, str | None] | Failure:
"""The default branch's ref, `origin/<branch>` after a fetch with that remote, else the local main or master, and a note for stderr. Unless fetched, a failed fetch reads origin as last fetched, and the note says so.""" """The default branch's ref, `origin/<branch>` after a fetch with that remote, else the local main or master, and a note for stderr. With stale_ok, a failed fetch reads origin as last fetched, and the note says so."""
remotes = git("remote") remotes = git("remote")
if isinstance(remotes, Failure): if isinstance(remotes, Failure):
return remotes return remotes
if "origin" in remotes.split(): if "origin" in remotes.split():
fetch = git("fetch", "--quiet", "origin") fetch = git("fetch", "--quiet", "origin")
if isinstance(fetch, Failure) and fetched: if isinstance(fetch, Failure) and not stale_ok:
return fetch return fetch
note = f"reading origin as last fetched: {fetch.reason}" if isinstance(fetch, Failure) else None note = f"reading origin as last fetched: {fetch.reason}" if isinstance(fetch, Failure) else None
default = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD") default = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD")
+9 -9
View File
@@ -4,7 +4,8 @@ usage: larv-review ruling-base | round-count | needs-scoring-run <item ID>... |
Run it inside a project checkout or one of its worktrees. ruling-base, round-count and Run it inside a project checkout or one of its worktrees. ruling-base, round-count and
needs-scoring-run first fetch origin, when the checkout has that remote, and take origin's default needs-scoring-run first fetch origin, when the checkout has that remote, and take origin's default
branch, or else the local main or master, as the default branch. The history is branch, or else the local main or master, as the default branch. When the fetch fails, round-count
reads origin as last fetched and says so on stderr; the other two exit 1. The history is
docs/comprehension-history.md in the checkout. docs/comprehension-history.md in the checkout.
ruling-base prints the commit a paired ruling reads against, then how it was found: the default ruling-base prints the commit a paired ruling reads against, then how it was found: the default
@@ -53,8 +54,8 @@ time and the seats file's first line, or of that line alone when it opens with a
the line names must be in the checkout. the line names must be in the checkout.
Exit 1, saying why, on unknown arguments, an unknown item ID, a seats file it cannot read or that Exit 1, saying why, on unknown arguments, an unknown item ID, a seats file it cannot read or that
breaks the shape above, or when git cannot start, finds no checkout or no default branch, or the breaks the shape above, or when git cannot start, finds no checkout or no default branch, cannot
history or todo.md cannot be read or written. fetch origin for ruling-base or needs-scoring-run, or cannot read or write the history or todo.md.
""" """
import pathlib import pathlib
import sys import sys
@@ -78,11 +79,11 @@ def main(argv: list[str]) -> int:
return 0 return 0
command, *given = argv or [""] command, *given = argv or [""]
if command == "ruling-base" and not given: if command == "ruling-base" and not given:
done = with_default_branch(ruling_base, fetched=True) done = with_default_branch(ruling_base, stale_ok=False)
elif command == "round-count" and not given: elif command == "round-count" and not given:
done = with_default_branch(round_count, fetched=False) done = with_default_branch(round_count, stale_ok=True)
elif command == "needs-scoring-run" and given and all(id_.isdigit() for id_ in given): elif command == "needs-scoring-run" and given and all(id_.isdigit() for id_ in given):
done = with_default_branch(lambda default: needs_scoring_run(default, [int(id_) for id_ in given]), fetched=True) done = with_default_branch(lambda default: needs_scoring_run(default, [int(id_) for id_ in given]), stale_ok=False)
elif command == "record-ruling" and len(given) == 1: elif command == "record-ruling" and len(given) == 1:
done = record_ruling(pathlib.Path(given[0])) done = record_ruling(pathlib.Path(given[0]))
elif command == "record-scoring" and len(given) == 1: elif command == "record-scoring" and len(given) == 1:
@@ -96,9 +97,8 @@ def main(argv: list[str]) -> int:
return 0 return 0
def with_default_branch(command: typing.Callable[[str], Failure | None], fetched: bool) -> Failure | None: def with_default_branch(command: typing.Callable[[str], Failure | None], stale_ok: bool) -> Failure | None:
"""fetched: whether the command needs origin as it is now, or may read it as last fetched.""" default = default_branch(stale_ok)
default = default_branch(fetched)
if isinstance(default, Failure): if isinstance(default, Failure):
return default return default
branch, note = default branch, note = default