"""Open the chunk's pull request as a draft, bring its branch level with origin's default branch, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere; once it has merged, remove the chunk's worktree and branch. usage: chunk-pr open [<body>] chunk-pr sync chunk-pr ready [<title>] chunk-pr merge chunk-pr clean Run it in the chunk's worktree, on the chunk's branch. ready and merge act on the branch's open pull request only while its head is the commit checked out, so push before you run them. open pushes the branch, setting its upstream, and opens a draft pull request into origin's default branch. <title> is one line, as ~/.claude/CLAUDE.md → "Git, commits, and PRs" says, and <body> is the pull request's summary. Where the branch's pull request is open already, it opens no second one. On origin's default branch it refuses without pushing anything. sync fetches origin and merges its default branch into the branch checked out, where the branch does not hold it already, and prints which it did. Then, where the project's config names a todo.md that exists, it runs `rank_todo.py --check` on it and prints the result, failing where the check fails or no config names the checkout. On a merge conflict it names the conflicted files and leaves the merge in progress, to resolve and commit. On origin's default branch it refuses and doesn't fetch anything. ready marks the pull request ready for review. On Gitea a draft is a pull request whose title starts with `WIP: `, so there it drops that prefix. Given <title>, one line, it sets that as the pull request's title first; where that fails, the pull request stays a draft. merge waits for the pull request's checks, and those origin's default branch requires, to pass on the head, then merges the pull request, and only while its head is still the commit checked out, so a push after the gate is never merged. Where no check reports within a minute, it takes the pull request as having none. It refuses where a check fails, or, on Gitea, where the checks have not all passed within 30 minutes; on GitHub, gh watches them until they end. Run it in the background. It takes the first merge method the repository allows of: a merge commit, fast-forward only, a rebase followed by a merge commit, a rebase, a squash. GitHub offers only the merge commit, the rebase and the squash. It also refuses where `larv-review scoring-run-owed` exits 1, because the chunk owes a scoring run it has not recorded. clean acts once the forge reports the branch's pull request merged at the commit checked out, by any merge method. It fetches origin, fast-forwards the main checkout to origin's default branch, removes the chunk's worktree, then deletes the local branch; origin's branch stays. Where the worktree has uncommitted or untracked files, it refuses and doesn't touch anything. Where the main checkout has uncommitted or untracked files, another branch checked out, or commits origin's default branch lacks, it leaves that as it was, still removes the worktree and the branch, and fails saying why. It prints what it did, exit 0, or why it failed, exit 1. """ import dataclasses import json import os import pathlib import sys import time import typing from deploy_release.forge import checks_passed, gitea, merged_pull, open_pull, required_checks, Run, runner from failure import Failure from chunk_preflight.gathered import Forge from chunk_preflight.choices import forge_of BIN = pathlib.Path(__file__).resolve().parents[1] # How long merge waits for a check to report before it takes a pull request without any as passed: CI queues its checks within seconds of a push, and merge may run right after one. NO_CHECKS_SECONDS = 60 USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge | chunk-pr clean" # Each forge's merge methods in the order merge prefers them, as the repository setting that allows the method and the method's name in the forge's merge call. A merge commit and a fast-forward keep the branch's commits as they are; a rebase or a squash rewrites them. METHODS: dict[Forge, tuple[tuple[str, str], ...]] = { "gh": (("mergeCommitAllowed", "--merge"), ("rebaseMergeAllowed", "--rebase"), ("squashMergeAllowed", "--squash")), "tea": (("allow_merge_commits", "merge"), ("allow_fast_forward_only_merge", "fast-forward-only"), ("allow_rebase_explicit", "rebase-merge"), ("allow_rebase", "rebase"), ("allow_squash_merge", "squash")), } def main(argv: list[str]) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 done = step(argv, runner(os.getcwd())) if isinstance(done, Failure): print(f"chunk-pr: {done.reason}", file=sys.stderr) return 1 print(done) return 0 def step(argv: list[str], run: Run) -> str | Failure: """Run the step argv names: the line to print, or a Failure. A malformed argv doesn't run anything.""" match argv: case ["open", title, *body] if len(body) <= 1 and not title.startswith("-"): return Failure("give a title of one line") if not one_line(title) else opened(title, body[0] if body else "", run) case ["sync"]: return synced(run) case ["ready", *title] if len(title) <= 1 and not any(given.startswith("-") for given in title): return Failure("give a title of one line") if title and not one_line(title[0]) else marked_ready(run, title[0] if title else None) case ["merge"]: return merged(run) case ["clean"]: return cleaned(run) return Failure(USAGE) def one_line(title: str) -> bool: return bool(title.strip()) and "\n" not in title def answers(run: Run, *commands: list[str]) -> list[str] | Failure: """What each git command printed, stripped, or the first Failure.""" answered = [run(["git", *args]) for args in commands] failed = next((answer for answer in answered if isinstance(answer, Failure)), None) return failed if failed is not None else [str(answer).strip() for answer in answered] def checkout(run: Run) -> tuple[Forge, str, str] | Failure: """The forge, the branch checked out and its head commit.""" found = answers(run, ["remote", "get-url", "origin"], ["symbolic-ref", "--short", "HEAD"], ["rev-parse", "HEAD"]) if isinstance(found, Failure): return found origin, branch, head = found return forge_of(origin), branch, head def default_branch(run: Run) -> str | Failure: """origin's default branch, such as main. It differs from git_cli's default_ref, which falls back to a local main or master and asks origin where nothing is recorded. A pull request's base is origin's alone, so this fails instead, and says how to record it.""" default = run(["git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]) if isinstance(default, Failure): return Failure(f"origin records no default branch; set it with `git remote set-head origin --auto`: {default.reason}") return default.strip().removeprefix("origin/") def pull(run: Run) -> tuple[Forge, str, str] | Failure: """The forge, the number of the branch's open pull request at its head, and that head.""" found = checkout(run) if isinstance(found, Failure): return found forge, branch, head = found number = open_pull(forge, branch, head, run) if number is None: return Failure(f"no pull request from origin's {branch} is open at {head[:12]}; push the branch, or open one with chunk-pr open") return number if isinstance(number, Failure) else (forge, number, head) def opened(title: str, body: str, run: Run) -> str | Failure: found = checkout(run) if isinstance(found, Failure): return found forge, branch, head = found base = default_branch(run) if isinstance(base, Failure): return base if branch == base: return Failure(f"{branch} is origin's default branch; open the pull request from the chunk's branch") pushed = run(["git", "push", "--quiet", "--set-upstream", "origin", branch]) if isinstance(pushed, Failure): return pushed number = open_pull(forge, branch, head, run) if isinstance(number, str): return f"Pull request #{number} is open already." if isinstance(number, Failure): return number command = ["gh", "pr", "create"] if forge == "gh" else ["tea", "pulls", "create"] created = run([*command, "--draft", "--head", branch, "--base", base, "--title", title, "--body" if forge == "gh" else "--description", body]) if isinstance(created, Failure): return created number = open_pull(forge, branch, head, run) if number is None: return Failure(f"{command[0]} created the pull request, but no pull request from origin's {branch} is listed open at {head[:12]}") return number if isinstance(number, Failure) else f"Opened draft pull request #{number}." def synced(run: Run) -> str | Failure: found = checkout(run) if isinstance(found, Failure): return found _, branch, _ = found base = default_branch(run) if isinstance(base, Failure): return base if branch == base: return Failure(f"{branch} is origin's default branch; sync the chunk's branch") fetched = run(["git", "fetch", "--quiet", "origin"]) if isinstance(fetched, Failure): return fetched behind = answers(run, ["rev-list", "--count", f"HEAD..origin/{base}"], ["rev-parse", "--show-toplevel"]) if isinstance(behind, Failure): return behind count, root = behind if count == "0": done = f"{branch} already holds origin/{base}." else: merged = run(["git", "merge", "--quiet", "--no-edit", f"origin/{base}"]) if isinstance(merged, Failure): conflicted = answers(run, ["diff", "--name-only", "--diff-filter=U"]) if isinstance(conflicted, Failure) or not conflicted[0]: return merged return Failure(f"merging origin/{base} into {branch} conflicts in {', '.join(conflicted[0].splitlines())}; the merge stays in progress: resolve them and commit") done = f"Merged origin/{base} into {branch}." plan = run([str(BIN / "project-config"), "plan"]) if isinstance(plan, Failure): return Failure(f"{done.removesuffix('.')}, but the plan to check cannot be found: {plan.reason}") todo = pathlib.Path(plan.strip()) if not todo.is_file(): return done ranked = run([str(BIN / "rank_todo.py"), "--check", str(todo)]) if isinstance(ranked, Failure): return Failure(f"{done.removesuffix('.')}, but rank_todo.py --check todo.md failed: {ranked.reason}") return f"{done}\nrank_todo.py --check todo.md passed." def marked_ready(run: Run, title: str | None) -> str | Failure: found = pull(run) if isinstance(found, Failure): return found forge, number, _ = found retitle = ["--title", title] if title is not None else [] if forge == "gh": # gh marks a pull request ready only with `gh pr ready`, which takes no title. titled = run(["gh", "pr", "edit", number, *retitle]) if retitle else "" ready = titled if isinstance(titled, Failure) else run(["gh", "pr", "ready", number]) else: ready = run(["tea", "pulls", "edit", number, *retitle, "--ready"]) if isinstance(ready, Failure): return ready return f"Marked pull request #{number} ready, titled “{title}”." if title is not None else f"Marked pull request #{number} ready." def merged(run: Run, sleep: typing.Callable[[float], None] = time.sleep) -> str | Failure: found = pull(run) if isinstance(found, Failure): return found forge, number, head = found owed = run([str(BIN / "larv-review"), "scoring-run-owed"]) if isinstance(owed, Failure): return owed method = merge_method(forge, run) if isinstance(method, Failure): return method base = default_branch(run) required = base if isinstance(base, Failure) else required_checks(forge, base, run) checked = required if isinstance(required, Failure) else checks_passed(forge, number, head, required, run, sleep, NO_CHECKS_SECONDS) if isinstance(checked, Failure): return checked if forge == "gh": done = run(["gh", "pr", "merge", number, method, "--match-head-commit", head]) else: done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", f"Do={method}", f"head_commit_id={head}") return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')})." def merge_method(forge: Forge, run: Run) -> str | Failure: """The first of METHODS[forge] that the repository's settings allow.""" if forge == "gh": viewed = run(["gh", "repo", "view", "--json", ",".join(setting for setting, _ in METHODS["gh"])]) try: settings = viewed if isinstance(viewed, Failure) else json.loads(viewed) except ValueError: return Failure(f"gh repo view printed no JSON: {str(viewed).strip()[:200]}") else: settings = gitea(run, "repos/{owner}/{repo}") if isinstance(settings, Failure): return settings allowed = next((method for setting, method in METHODS[forge] if isinstance(settings, dict) and settings.get(setting) is True), None) return allowed if allowed is not None else Failure(f"the repository allows none of the merge methods chunk-pr knows: {', '.join(method for _, method in METHODS[forge])}") @dataclasses.dataclass(frozen=True) class Chunk: """A merged chunk's worktree and branch, the main checkout of their repository, and origin's default branch.""" worktree: str branch: str main: str default: str def cleaned(run: Run, at: typing.Callable[[str], Run] = runner) -> str | Failure: """Remove the worktree and branch of the merged chunk run is in, and fast-forward the main checkout; at(folder) runs commands in folder, here the main checkout.""" chunk = merged_chunk(run) if isinstance(chunk, Failure): return chunk at_main = at(chunk.main) left = kept_as_it_was(at_main, chunk.default) files = unsaved(run) if isinstance(files, Failure): return files if files: also = f"\nIt would also have left {chunk.main} as it was: {left}" if left else "" return Failure(f"left everything as it was: {chunk.worktree} has uncommitted or untracked files:\n{files}{also}") return removed(chunk, at_main, left) def merged_chunk(run: Run) -> Chunk | Failure: """The chunk run is in, once the forge reports its pull request merged at its head. It fetches origin, so the main checkout can fast-forward to the merge.""" found = checkout(run) if isinstance(found, Failure): return found forge, branch, head = found default = default_branch(run) if isinstance(default, Failure): return default folders = worktree_and_main(run) if isinstance(folders, Failure): return folders number = merged_pull(forge, branch, head, run) if number is None: return Failure(f"no merged pull request from {branch} at {head[:12]}, so its worktree and branch stay") fetched = number if isinstance(number, Failure) else run(["git", "fetch", "--quiet", "origin"]) return fetched if isinstance(fetched, Failure) else Chunk(folders[0], branch, folders[1], default) def worktree_and_main(run: Run) -> tuple[str, str] | Failure: """The worktree run is in, and the main checkout of its repository. Run in the main checkout itself, it fails.""" folders = answers(run, ["rev-parse", "--show-toplevel"], ["worktree", "list", "--porcelain"]) if isinstance(folders, Failure): return folders # git lists the main checkout first. worktree, main = folders[0], folders[1].splitlines()[0].removeprefix("worktree ") return Failure(f"{main} is the main checkout; run clean in the chunk's worktree") if worktree == main else (worktree, main) def unsaved(run: Run) -> str | Failure: """The uncommitted and untracked files of the checkout run is in, as git status lists them; empty where there are none.""" status = run(["git", "status", "--porcelain"]) return status if isinstance(status, Failure) else status.rstrip() def kept_as_it_was(at_main: Run, default: str) -> str | None: """Why the main checkout must not move, or None where it may fast-forward to origin's default.""" branch = at_main(["git", "symbolic-ref", "--quiet", "--short", "HEAD"]) if isinstance(branch, Failure) or branch.strip() != default: return f"it has {'no branch' if isinstance(branch, Failure) else branch.strip()} checked out, not {default}" files = unsaved(at_main) if isinstance(files, Failure): return files.reason if files: return f"it has uncommitted or untracked files:\n{files}" if isinstance(at_main(["git", "merge-base", "--is-ancestor", "HEAD", f"origin/{default}"]), Failure): return f"it holds commits origin/{default} lacks" return None def removed(chunk: Chunk, at_main: Run, left: str | None) -> str | Failure: """Fast-forward the main checkout unless left says why not, then remove the chunk's worktree and branch. Every command runs in the main checkout, since the worktree is gone after the removal.""" if left is None: forwarded = at_main(["git", "merge", "--quiet", "--ff-only", f"origin/{chunk.default}"]) if isinstance(forwarded, Failure): return forwarded removal = at_main(["git", "worktree", "remove", chunk.worktree]) # Not -d: merged_chunk found the forge's pull request merged at the branch's head. -d would compare the branch with the main checkout's HEAD or its upstream instead, and after a rebase or squash merge neither need hold its commits. deleted = removal if isinstance(removal, Failure) else at_main(["git", "branch", "--quiet", "-D", chunk.branch]) if isinstance(deleted, Failure): return deleted done = f"Removed worktree {chunk.worktree} and branch {chunk.branch}" if left is not None: return Failure(f"{done}, but left {chunk.main} as it was: {left}") return f"{done}, and fast-forwarded {chunk.main} to origin/{chunk.default}."