Let a project's config name a working branch per epic that chunks branch off and merge into
CI / tests (pull_request) Successful in 5m4s

This commit is contained in:
Lilleman
2026-11-20 19:32:59 +01:00
parent 9531e5c337
commit 72a4d346d7
33 changed files with 991 additions and 56 deletions
+22 -1
View File
@@ -23,6 +23,10 @@ GUARD_SETTINGS = [
]
# The key, under `branch.<a chunk's branch>` in a checkout's git config, that names the epic's working branch the chunk merges into (docs/decisions.md → `epic-branch: an epic's chunks branch off and merge into a working branch …`).
CHUNK_BASE = "chunk-base"
def git(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict", env: typing.Mapping[str, str] | None = None) -> str | Failure:
"""What git printed, run in cwd, else in the current directory.
@@ -72,13 +76,15 @@ def default_branch(stale_ok: bool, timeout: float | None = None) -> tuple[str, s
def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: bool = False, run: 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. run runs git.
"""The ref the branch checked out merges into, as last fetched, read in cwd, else in the current directory. That is the default branch's: `origin/<branch>` with that remote, else the local main or master. On a chunk's branch that merges into an epic's working branch (`chunk_base`), it is `origin/<that branch>`, so each tool that measures a chunk against its base reads the chunk's own commits and not the epic's. run runs git.
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."""
remotes = run("remote", cwd=cwd, guarded=guarded)
if isinstance(remotes, Failure):
return remotes
if "origin" in remotes.split():
if base := chunk_base(cwd, guarded, run):
return f"origin/{base}"
recorded = run("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=cwd, guarded=guarded)
if not isinstance(recorded, Failure):
return recorded.strip()
@@ -89,6 +95,21 @@ def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: b
return Failure("no origin remote, and no main or master branch to take as the default branch")
def chunk_base(cwd: str | None = None, guarded: bool = False, run: Git = git) -> str | None:
"""The epic's working branch that the branch checked out in cwd merges into, as `record_chunk_base` recorded it; None where none is recorded, as for every branch that merges into the default branch, and on a detached HEAD."""
branch = run("symbolic-ref", "--quiet", "--short", "HEAD", cwd=cwd, guarded=guarded)
if isinstance(branch, Failure):
return None
base = run("config", "--get", f"branch.{branch.strip()}.{CHUNK_BASE}", cwd=cwd, guarded=guarded)
return None if isinstance(base, Failure) else base.strip() or None
def record_chunk_base(branch: str, base: str, cwd: str | None = None, run: Git = git) -> Failure | None:
"""Records in the checkout's git config that the chunk's branch merges into base, an epic's working branch. The branch need not exist yet, and deleting it removes the record."""
recorded = run("config", f"branch.{branch}.{CHUNK_BASE}", base, cwd=cwd)
return recorded if isinstance(recorded, Failure) else None
def branch_titles(default: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> list[str] | Failure:
"""The titles of the branch's own commits since default, newest first, read in cwd, else in the current directory.