Answer stability round 1: 1 🔴, 5 🟡 (0 misread); hold a pull request's base on the forge to the recorded one, ask origin for the epic branch, and name where a chunk's work landed
CI / tests (pull_request) Successful in 3m46s

This commit is contained in:
Lilleman
2026-11-20 21:42:32 +01:00
parent 525f3f8836
commit d464f271bf
13 changed files with 352 additions and 74 deletions
+30 -11
View File
@@ -8,13 +8,35 @@ from git_cli.git_repo import Git, git
def levelled(branch: str, default: str, cwd: str | None = None, timeout: float | None = None, run: Git = git, again: bool = True) -> str | Failure:
"""Brings origin's branch level with default, the default branch's ref, such as `origin/main`, and fetches it: what it did, as a sentence. Where origin has no such branch, it makes one at default.
It merges with a merge commit, never a rebase, since several chunks share the branch, and without a checkout, since no worktree has the branch checked out. A Failure where the two don't merge cleanly: a person resolves that, never the loop. A call to origin still running after timeout seconds fails. Where the merge's push fails, as when another chunk pushed its own merge first, it fetches and levels once more, unless again is False."""
fetched = run("fetch", "--quiet", "origin", cwd=cwd, timeout=timeout)
if isinstance(fetched, Failure):
return fetched
A call to origin still running after timeout seconds fails. again is False in the second try `merged` makes after a push that failed."""
held = fetched(cwd, timeout, run) or on_origin(branch, cwd, timeout, run)
if isinstance(held, Failure):
return held
if not held:
return pushed(default, branch, cwd, timeout, run) or f"Made origin/{branch} off {default}."
return merged(branch, default, cwd, timeout, run, again)
def fetched(cwd: str | None, timeout: float | None, run: Git) -> Failure | None:
"""Fetches origin, and once more where that fails: chunks that start at once fetch into one checkout, and git fails the fetch that finds a ref another one just moved."""
fetch = ("fetch", "--quiet", "origin")
if not isinstance(run(*fetch, cwd=cwd, timeout=timeout), Failure):
return None
second = run(*fetch, cwd=cwd, timeout=timeout)
return second if isinstance(second, Failure) else None
def on_origin(branch: str, cwd: str | None, timeout: float | None, run: Git) -> bool | Failure:
"""Whether origin has branch, asked of origin itself: a fetch keeps this checkout's `origin/<branch>` after origin deleted the branch."""
listed = run("ls-remote", "--heads", "origin", f"refs/heads/{branch}", cwd=cwd, timeout=timeout)
return listed if isinstance(listed, Failure) else bool(listed.strip())
def merged(branch: str, default: str, cwd: str | None, timeout: float | None, run: Git, again: bool) -> str | Failure:
"""Merges default into origin's branch, as this checkout fetched it, and pushes the merge: what it did, as a sentence.
It merges with a merge commit, never a rebase, since several chunks share the branch, and without a checkout, since no worktree has the branch checked out. A Failure where the two don't merge cleanly: a person resolves that, never the loop. Where the push fails, as when another chunk pushed its own merge first, it levels the branch once more from a fresh fetch, unless again is False."""
ours = f"origin/{branch}"
if isinstance(run("rev-parse", "--quiet", "--verify", f"refs/remotes/{ours}", cwd=cwd), Failure):
return pushed(default, branch, cwd, timeout, run) or f"Made {ours} off {default}."
behind = run("rev-list", "--count", f"{ours}..{default}", cwd=cwd)
if isinstance(behind, Failure):
return behind
@@ -47,8 +69,5 @@ def unmerged(failed: Failure, branch: str, default: str) -> Failure:
def pushed(commit: str, branch: str, cwd: str | None, timeout: float | None, run: Git) -> Failure | None:
"""Pushes commit as origin's branch, without force, so a push that raced another's fails, and fetches it."""
for args in (["push", "--quiet", "origin", f"{commit}:refs/heads/{branch}"], ["fetch", "--quiet", "origin"]):
done = run(*args, cwd=cwd, timeout=timeout)
if isinstance(done, Failure):
return done
return None
done = run("push", "--quiet", "origin", f"{commit}:refs/heads/{branch}", cwd=cwd, timeout=timeout)
return done if isinstance(done, Failure) else fetched(cwd, timeout, run)