diff --git a/bin/larv_review/git.py b/bin/larv_review/git.py index 93783a1c..031e3c0a 100644 --- a/bin/larv_review/git.py +++ b/bin/larv_review/git.py @@ -20,14 +20,14 @@ def top() -> str | Failure: return found if isinstance(found, Failure) else found.strip() -def default_branch(fetched: bool) -> tuple[str, str | None] | Failure: - """The default branch's ref, `origin/` 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.""" +def default_branch(stale_ok: bool) -> tuple[str, str | None] | Failure: + """The default branch's ref, `origin/` 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") if isinstance(remotes, Failure): return remotes if "origin" in remotes.split(): fetch = git("fetch", "--quiet", "origin") - if isinstance(fetch, Failure) and fetched: + if isinstance(fetch, Failure) and not stale_ok: return fetch note = f"reading origin as last fetched: {fetch.reason}" if isinstance(fetch, Failure) else None default = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD") diff --git a/bin/larv_review/main.py b/bin/larv_review/main.py index 1b441050..4a35adb0 100644 --- a/bin/larv_review/main.py +++ b/bin/larv_review/main.py @@ -4,7 +4,8 @@ usage: larv-review ruling-base | round-count | needs-scoring-run ... | 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 -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. 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. 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 -history or todo.md cannot be read or written. +breaks the shape above, or when git cannot start, finds no checkout or no default branch, cannot +fetch origin for ruling-base or needs-scoring-run, or cannot read or write the history or todo.md. """ import pathlib import sys @@ -78,11 +79,11 @@ def main(argv: list[str]) -> int: return 0 command, *given = argv or [""] 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: - 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): - 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: done = record_ruling(pathlib.Path(given[0])) elif command == "record-scoring" and len(given) == 1: @@ -96,9 +97,8 @@ def main(argv: list[str]) -> int: return 0 -def with_default_branch(command: typing.Callable[[str], Failure | None], fetched: bool) -> Failure | None: - """fetched: whether the command needs origin as it is now, or may read it as last fetched.""" - default = default_branch(fetched) +def with_default_branch(command: typing.Callable[[str], Failure | None], stale_ok: bool) -> Failure | None: + default = default_branch(stale_ok) if isinstance(default, Failure): return default branch, note = default