Unit-test the patch cut's changelog edits in-process
This commit is contained in:
@@ -112,10 +112,7 @@ def tag_patch(clone: str, base: str, commits: list[str], entries: list[str], nam
|
||||
if isinstance(applied := apply_commit(clone, worktree, commit, base), Failure):
|
||||
return applied
|
||||
changelog = worktree / "CHANGELOG.md"
|
||||
text = changelog.read_text() if changelog.exists() else "# Changelog\n"
|
||||
section_text = f"## {named}\n\n" + "\n".join(entries) + "\n\n"
|
||||
headed, count = re.subn(r"^## ", lambda _: section_text + "## ", text, count=1, flags=re.M)
|
||||
changelog.write_text(headed if count else text.rstrip("\n") + "\n\n" + section_text.rstrip("\n") + "\n")
|
||||
changelog.write_text(headed(changelog.read_text() if changelog.exists() else "# Changelog\n", entries, named))
|
||||
if isinstance(committed := git("commit", "--quiet", "--all", "--message", title, cwd=str(worktree)), Failure):
|
||||
return committed
|
||||
said(f"applied {len(commits)} commit(s) on {base}; testing them with ci.sh")
|
||||
@@ -135,6 +132,15 @@ def tag_patch(clone: str, base: str, commits: list[str], entries: list[str], nam
|
||||
shutil.rmtree(worktree.parent, ignore_errors=True)
|
||||
|
||||
|
||||
def headed(changelog: str, entries: list[str], named: str) -> str:
|
||||
"""changelog with a ## named section holding entries before its first ## section, or at its end where it has none."""
|
||||
added = f"## {named}\n\n" + "\n".join(entries) + "\n"
|
||||
first = re.search(r"^## ", changelog, flags=re.M)
|
||||
if first is None:
|
||||
return changelog.rstrip("\n") + "\n\n" + added
|
||||
return changelog[:first.start()] + added + "\n" + changelog[first.start():]
|
||||
|
||||
|
||||
def apply_commit(clone: str, worktree: pathlib.Path, commit: str, base: str) -> Failure | None:
|
||||
"""Apply commit's change, but for LEFT_AS_TAGGED, to worktree's index and files, merging three ways where the tag base's files differ from commit's parent."""
|
||||
diff = git("diff", "--binary", f"{commit}^", commit, "--", ".", *(f":(exclude){path}" for path in LEFT_AS_TAGGED), cwd=clone)
|
||||
|
||||
Reference in New Issue
Block a user