509 lines
28 KiB
Python
509 lines
28 KiB
Python
"""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 <title> [<body>]
|
|
chunk-pr sync
|
|
chunk-pr ready [<title>]
|
|
chunk-pr merge [--finish <ID> [--changelog <bullet> | --no-changelog]]...
|
|
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. On a branch that fits the config's `branch`
|
|
template, it refuses a <title> that doesn't fit the `pull-request-title` template the
|
|
project's config names (`project-config --help`); on any other branch, as for a
|
|
chunk with no plan item, the title may be the bare summary. It fails where no config names the checkout. 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, which it holds
|
|
to the template as open does, it sets that
|
|
as the pull request's title first; where that fails, the pull request stays a draft.
|
|
merge merges the pull request in its turn: the chunks of one project on this machine merge one
|
|
at a time, each waiting until the one before it has merged or failed. In its turn it
|
|
fetches origin and merges its default branch into the branch, as sync does; finishes each
|
|
plan item a --finish names with `todo-item finish`, passing on --changelog or
|
|
--no-changelog, and commits what that changed in the plan and the changelog; and pushes.
|
|
So the plan and the changelog change only at merge time, on top of every chunk merged
|
|
before. An item the plan no longer holds was finished by an earlier run, and stays so.
|
|
On a merge conflict it names the conflicted files and leaves the merge in progress:
|
|
resolve them, commit, push, and run it again. Where the default branch brought changes in
|
|
and the branch has recorded a comprehension check, it pushes and stops, since a paired
|
|
ruling read the head before them: run the ruling again, then merge again.
|
|
Then it 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.
|
|
Where the project's config sets `wait-for-approval = true` (`project-config --help`), it
|
|
also waits, once the checks have passed, until GitHub reports the pull request approved
|
|
as its branch protection requires. It waits outside its turn, so other chunks merge
|
|
meanwhile, and takes a new turn once approved. It refuses at once where a reviewer requested changes,
|
|
where the protection requires no review, on Gitea, and where it cannot read the config.
|
|
It stops too where a reviewer's comment waits for an answer: a review thread nobody
|
|
resolved whose last comment is not the pull request author's. Answer it in its thread,
|
|
push any fix, and run merge again to go on waiting.
|
|
After 90 minutes without an approval it refuses too; run it again once the pull request
|
|
is approved.
|
|
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 contextlib
|
|
import dataclasses
|
|
import fcntl
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import time
|
|
import typing
|
|
|
|
from chunk_pr.approval import approval, approved, awaited
|
|
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
|
|
from project_config import templates
|
|
|
|
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
|
|
# The file, in the git folder that every worktree of the checkout shares, that a merge keeps locked with flock for its turn: so every chunk of the project on this machine queues on it.
|
|
QUEUE = "chunk-pr-merge-queue"
|
|
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge [--finish <ID> [--changelog <bullet> | --no-changelog]]... | 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 misfit(title, run) or 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 (misfit(title[0], run) if title else None) or marked_ready(run, title[0] if title else None)
|
|
case ["merge", *given]:
|
|
finishes = finishes_of(given)
|
|
return Failure(USAGE) if finishes is None else merged(run, finishes=finishes)
|
|
case ["clean"]:
|
|
return cleaned(run)
|
|
return Failure(USAGE)
|
|
|
|
|
|
def misfit(title: str, run: Run) -> Failure | None:
|
|
"""Why title doesn't fit the `pull-request-title` template the project's config names; None where it fits. A branch that doesn't fit the config's `branch` template is no plan item's, so it has no ID for {id}, and any title fits there. A config that cannot be read fails, so that no pull request reaches the project's owners under a title they don't use."""
|
|
read = [run([str(BIN / "project-config"), "pull-request-title"]), run([str(BIN / "project-config"), "branch"]), run(["git", "symbolic-ref", "--short", "HEAD"])]
|
|
if failed := next((answer for answer in read if isinstance(answer, Failure)), None):
|
|
return failed
|
|
template, branch_template, branch = (answer.strip("\n") for answer in read if isinstance(answer, str))
|
|
if not templates.branch_fits(branch_template, branch) or templates.fits(template, title.strip()):
|
|
return None
|
|
return Failure(f"title the pull request as the project's config asks, `{template}`, where {{id}} is the ID of the chunk's first item; {title.strip()!r} doesn't fit")
|
|
|
|
|
|
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")
|
|
level = levelled(run, branch, base)
|
|
if isinstance(level, Failure):
|
|
return level
|
|
done = f"Merged origin/{base} into {branch}." if level else f"{branch} already holds origin/{base}."
|
|
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 levelled(run: Run, branch: str, base: str) -> bool | Failure:
|
|
"""Fetch origin and merge its base into branch, the one checked out: whether that brought anything in. On a conflict the merge stays in progress, and the Failure names the conflicted files."""
|
|
fetched = run(["git", "fetch", "--quiet", "origin"])
|
|
if isinstance(fetched, Failure):
|
|
return fetched
|
|
behind = answers(run, ["rev-list", "--count", f"HEAD..origin/{base}"])
|
|
if isinstance(behind, Failure):
|
|
return behind
|
|
if behind[0] == "0":
|
|
return False
|
|
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")
|
|
return True
|
|
|
|
|
|
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 finishes_of(given: list[str]) -> list[list[str]] | None:
|
|
"""`todo-item finish`'s arguments for each item that merge's flags name, in their order; None where the flags are malformed."""
|
|
finishes, rest = [], given
|
|
while rest:
|
|
match rest:
|
|
case ["--finish", item, "--changelog", bullet, *rest] if not item.startswith("-"):
|
|
finishes.append([item, "--changelog", bullet])
|
|
case ["--finish", item, "--no-changelog", *rest] if not item.startswith("-"):
|
|
finishes.append([item, "--no-changelog"])
|
|
case ["--finish", item, *rest] if not item.startswith("-"):
|
|
finishes.append([item])
|
|
case _:
|
|
return None
|
|
return finishes
|
|
|
|
|
|
def merged(run: Run, sleep: typing.Callable[[float], None] = time.sleep, finishes: typing.Sequence[list[str]] = ()) -> str | Failure:
|
|
"""Merge the branch's pull request in the project's merge queue. Each pass of the loop is one turn in the queue; a pull request that still wants its approval leaves the queue while it waits, so the next chunk merges meanwhile, and takes a new turn once approved."""
|
|
found = pull(run)
|
|
if isinstance(found, Failure):
|
|
return found
|
|
forge, number, _ = found
|
|
waits = awaited(forge, run)
|
|
if isinstance(waits, Failure):
|
|
return waits
|
|
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)
|
|
if isinstance(required, Failure):
|
|
return required
|
|
queue = answers(run, ["rev-parse", "--path-format=absolute", "--git-common-dir"])
|
|
if isinstance(queue, Failure):
|
|
return queue
|
|
while True:
|
|
try:
|
|
with turn(pathlib.Path(queue[0]) / QUEUE):
|
|
head = levelled_and_finished(run, str(base), finishes)
|
|
checked = head if isinstance(head, Failure) else checks_passed(forge, number, head, required, run, sleep, NO_CHECKS_SECONDS)
|
|
if isinstance(checked, Failure):
|
|
return checked
|
|
ready = approval(number, run) if waits else True
|
|
if isinstance(ready, Failure):
|
|
return ready
|
|
if ready and forge == "gh":
|
|
done = run(["gh", "pr", "merge", number, method, "--match-head-commit", str(head)])
|
|
elif ready:
|
|
done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", f"Do={method}", f"head_commit_id={head}")
|
|
if ready:
|
|
return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')})."
|
|
except OSError as failure:
|
|
return Failure(f"cannot take a turn in the merge queue, {failure.filename}: {failure.strerror}")
|
|
waited = approved(number, run, sleep)
|
|
if isinstance(waited, Failure):
|
|
return waited
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def turn(queue: pathlib.Path) -> typing.Iterator[None]:
|
|
"""Hold the project's merge queue: wait until no other chunk-pr merge on this machine holds it, and free it on the way out, however that is. Raises OSError where the file cannot be opened or locked."""
|
|
fd = os.open(queue, os.O_RDWR | os.O_CREAT, 0o644)
|
|
try:
|
|
fcntl.flock(fd, fcntl.LOCK_EX)
|
|
yield
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def levelled_and_finished(run: Run, base: str, finishes: typing.Sequence[list[str]]) -> str | Failure:
|
|
"""What merge does to the branch inside its turn, before the checks: merge origin's base into it, finish the plan items, and push. The head to merge, or why the worker must act first."""
|
|
found = checkout(run)
|
|
if isinstance(found, Failure):
|
|
return found
|
|
_, branch, before = found
|
|
moved = levelled(run, branch, base)
|
|
failed = moved if isinstance(moved, Failure) else next((failure for finish in finishes if (failure := finished(run, branch, finish))), None)
|
|
head = failed or answers(run, ["rev-parse", "HEAD"])
|
|
if isinstance(head, Failure):
|
|
return head
|
|
pushed = run(["git", "push", "--quiet", "origin", branch]) if head[0] != before else ""
|
|
if isinstance(pushed, Failure):
|
|
return pushed
|
|
return (remeasure(run, base) if moved else None) or head[0]
|
|
|
|
|
|
def finished(run: Run, branch: str, finish: list[str]) -> Failure | None:
|
|
"""Finish one plan item with `todo-item finish` and commit what that changed. An item the plan no longer holds was finished by an earlier run of merge, and stays as it is."""
|
|
shown = run([str(BIN / "todo-item"), "show", finish[0]])
|
|
if isinstance(shown, Failure):
|
|
return None if f"holds no item {finish[0]}" in shown.reason else shown
|
|
read = [run([str(BIN / "todo-item"), "finish", *finish]), run([str(BIN / "project-config"), "commit-title"]), run([str(BIN / "project-config"), "branch"]), run([str(BIN / "project-config"), "plan"]), run([str(BIN / "project-config"), "changelog"])]
|
|
if failed := next((answer for answer in read if isinstance(answer, Failure)), None):
|
|
return failed
|
|
_, template, branch_template, *records = (str(answer).strip() for answer in read)
|
|
summary = f"Finish item {finish[0]} in the plan"
|
|
# A branch that doesn't fit the config's `branch` template has no ID for {id}, so its titles are the summary alone, as CLAUDE.md → "Git, commits, and PRs" says.
|
|
title = template.replace(templates.ID, finish[0]).replace(templates.SUMMARY, summary) if templates.branch_fits(branch_template, branch) else summary
|
|
return committed(run, title, [pathlib.Path(record) for record in records if pathlib.Path(record).is_file()])
|
|
|
|
|
|
def committed(run: Run, title: str, files: list[pathlib.Path]) -> Failure | None:
|
|
"""Commit what changed in files under title, each in the git repository that holds it, and only those files: a record the config keeps in its own folder is committed there, where another chunk may commit at once."""
|
|
held: dict[str, list[str]] = {}
|
|
for file in files:
|
|
top = run(["git", "-C", str(file.parent), "rev-parse", "--show-toplevel"])
|
|
if isinstance(top, Failure):
|
|
return top
|
|
held.setdefault(top.strip(), []).append(str(file))
|
|
for top, paths in held.items():
|
|
changed = run(["git", "-C", top, "status", "--porcelain", "--", *paths])
|
|
steps = [changed] if isinstance(changed, Failure) or not changed.strip() else [run(["git", "-C", top, "add", "--", *paths]), run(["git", "-C", top, "commit", "--quiet", "-m", title, "--", *paths])]
|
|
if failed := next((step for step in steps if isinstance(step, Failure)), None):
|
|
return failed
|
|
return None
|
|
|
|
|
|
def remeasure(run: Run, base: str) -> Failure | None:
|
|
"""A Failure where the branch recorded a comprehension check before origin's base, just merged in, brought in another chunk's change: the check read a head that no longer merges. None where the branch recorded none, or the history is no file in the checkout."""
|
|
history = run([str(BIN / "project-config"), "comprehension-history"])
|
|
if isinstance(history, Failure):
|
|
return history
|
|
if not pathlib.Path(history.strip()).is_file():
|
|
return None
|
|
recorded = answers(run, ["diff", "--name-only", f"origin/{base}...HEAD", "--", history.strip()])
|
|
if isinstance(recorded, Failure) or not recorded[0]:
|
|
return recorded if isinstance(recorded, Failure) else None
|
|
return Failure(f"origin/{base} moved after the branch recorded its comprehension check, and its changes are merged in and pushed; where the chunk recorded a paired ruling, run it again at the new head, record it, and run chunk-pr merge again")
|
|
|
|
|
|
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}."
|