Answer product-owner round 1: 0 🔴, 4 🟡 (0 misread); move through the open status, refuse open-epic without a branch, tell a conflict from git's other failures

This commit is contained in:
Lilleman
2026-11-20 20:15:33 +01:00
parent 9f367e90ca
commit ce2e9dc0e5
9 changed files with 143 additions and 21 deletions
+21 -4
View File
@@ -1,12 +1,14 @@
"""Keep an epic's working branch on origin: made off the default branch by the epic's first chunk, and level with it before each chunk starts and before the epic's own pull request opens (docs/decisions.md → `epic-branch: an epic's chunks branch off and merge into a working branch …`)."""
import re
from failure import Failure
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) -> str | Failure:
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."""
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
@@ -21,11 +23,26 @@ def levelled(branch: str, default: str, cwd: str | None = None, timeout: float |
# git 2.38 or later: merges two commits into a tree without a checkout.
tree = run("merge-tree", "--write-tree", ours, default, cwd=cwd)
if isinstance(tree, Failure):
return Failure(f"{default} doesn't merge into {ours} cleanly, so a person merges it: check {branch} out, run `git merge {default}`, resolve the conflicts and push")
return unmerged(tree, branch, default)
commit = run("commit-tree", tree.strip(), "-p", ours, "-p", default, "-m", f"Merge remote-tracking branch '{default}' into {branch}", cwd=cwd)
if isinstance(commit, Failure):
return commit
return pushed(commit.strip(), branch, cwd, timeout, run) or f"Merged {default} into {ours}."
failed = pushed(commit.strip(), branch, cwd, timeout, run)
if failed and again:
return levelled(branch, default, cwd, timeout, run, again=False)
return failed or f"Merged {default} into {ours}."
def unmerged(failed: Failure, branch: str, default: str) -> Failure:
"""Why `git merge-tree --write-tree` didn't merge default into origin's branch, from its Failure.
A conflict prints nothing on stderr, so its reason ends at `git <command>:`, or, from a runner that shows stdout in place of an empty stderr, goes on with the merged tree's id. Git older than 2.38 reads --write-tree as a commit, which it doesn't find. Any other Failure is git's own."""
ours = f"origin/{branch}"
if re.fullmatch(r"git merge-tree [^:]*:\s*(?:[0-9a-f]{40,64}\b.*)?", failed.reason, re.DOTALL):
return Failure(f"{default} doesn't merge into {ours} cleanly, so a person merges it: check {branch} out, run `git merge {default}`, resolve the conflicts and push")
if "unknown rev --write-tree" in failed.reason:
return Failure(f"levelling {ours} with {default} needs git 2.38 or later: {failed.reason}")
return failed
def pushed(commit: str, branch: str, cwd: str | None, timeout: float | None, run: Git) -> Failure | None:
+56
View File
@@ -9,12 +9,23 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from git_cli import git_repo
from git_cli.epic_branch import levelled
from forge.commands import runner
def sh(cwd: pathlib.Path, *args: str) -> str:
return subprocess.run(["git", "-c", "user.name=T", "-c", "user.email=t@example.com", *args], cwd=cwd, check=True, capture_output=True, text=True).stdout.strip()
def scripted(calls: list[tuple[str, ...]], **answers: str | Failure) -> git_repo.Git:
"""A git that records each command in calls and answers it by its first word: as answers has it, else as a checkout where origin/ABC-1 lacks a commit of origin/main that merges in cleanly."""
answered = {"fetch": "", "rev-parse": "abc\n", "rev-list": "1\n", "merge-tree": "7ree\n", "commit-tree": "c0mm17\n", "push": ""} | {name.replace("_", "-"): answer for name, answer in answers.items()}
def run(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
calls.append(args)
return answered[args[0]]
return run
class Levelled(unittest.TestCase):
def setUp(self) -> None:
folder = tempfile.TemporaryDirectory()
@@ -69,6 +80,51 @@ class Levelled(unittest.TestCase):
self.assertEqual(done.reason, "origin/main doesn't merge into origin/ABC-1 cleanly, so a person merges it: check ABC-1 out, run `git merge origin/main`, resolve the conflicts and push")
self.assertEqual(sh(self.origin, "rev-parse", "ABC-1"), before)
def test_a_conflict_fails_for_a_person_from_a_runner_that_shows_stdout_where_stderr_is_empty(self) -> None:
self.commit("ABC-1", "a.txt", "epic\n")
self.commit("main", "a.txt", "main\n")
run = runner(str(self.clone))
done = levelled("ABC-1", "origin/main", run=lambda *args, **more: run(["git", *args]))
self.assertEqual(done, Failure("origin/main doesn't merge into origin/ABC-1 cleanly, so a person merges it: check ABC-1 out, run `git merge origin/main`, resolve the conflicts and push"))
def test_a_merge_that_fails_other_than_by_a_conflict_fails_with_gits_reason(self) -> None:
sh(self.clone, "checkout", "--quiet", "--orphan", "stray")
sh(self.clone, "commit", "--quiet", "-m", "stray")
sh(self.clone, "push", "--quiet", "origin", "HEAD:refs/heads/ABC-1")
before = sh(self.origin, "rev-parse", "ABC-1")
self.assertEqual(levelled("ABC-1", "origin/main", cwd=str(self.clone)), Failure("git merge-tree --write-tree origin/ABC-1 origin/main: fatal: refusing to merge unrelated histories"))
self.assertEqual(sh(self.origin, "rev-parse", "ABC-1"), before)
def test_a_git_older_than_2_38_fails_naming_the_git_levelling_needs(self) -> None:
# What git 2.36 prints: it reads --write-tree as a commit.
old = Failure("git merge-tree --write-tree origin/ABC-1 origin/main: fatal: unknown rev --write-tree")
self.assertEqual(levelled("ABC-1", "origin/main", run=scripted([], merge_tree=old)), Failure("levelling origin/ABC-1 with origin/main needs git 2.38 or later: git merge-tree --write-tree origin/ABC-1 origin/main: fatal: unknown rev --write-tree"))
def test_a_merge_another_chunk_pushed_first_is_fetched_and_found_level(self) -> None:
self.commit("ABC-1", "b.txt", "epic\n")
self.commit("main", "c.txt", "main\n")
other = self.clone.parent / "other"
sh(self.clone.parent, "clone", "--quiet", str(self.origin), str(other))
sh(other, "config", "user.name", "Other")
sh(other, "config", "user.email", "other@example.com")
raced: list[str | Failure] = []
def racing(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
"""Runs git, after the other clone levels the branch just before this clone's first push."""
if args[0] == "push" and not raced:
raced.append(levelled("ABC-1", "origin/main", cwd=str(other)))
return git_repo.git(*args, cwd=cwd, timeout=timeout, guarded=guarded, errors=errors)
self.assertEqual(levelled("ABC-1", "origin/main", cwd=str(self.clone), run=racing), "origin/ABC-1 already holds origin/main.")
self.assertEqual(raced, ["Merged origin/main into origin/ABC-1."])
self.assertEqual(sh(self.clone, "rev-parse", "origin/ABC-1"), sh(other, "rev-parse", "origin/ABC-1"))
def test_a_push_that_fails_again_after_a_fresh_fetch_fails_with_gits_reason(self) -> None:
calls: list[tuple[str, ...]] = []
declined = Failure("git push --quiet origin c0mm17:refs/heads/ABC-1: ! [remote rejected] c0mm17 -> ABC-1 (protected branch hook declined)")
self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, push=declined)), declined)
self.assertEqual([args[0] for args in calls], ["fetch", "rev-parse", "rev-list", "merge-tree", "commit-tree", "push"] * 2)
class ChunkBase(unittest.TestCase):
def setUp(self) -> None: