"""Move a checkout to this repo's newest release, as a bare `deploy-release` does.""" from failure import Failure from git_repo import git from release import newest_tag, parsed, tagged from markdown_text import sections def deploy(checkout: str) -> str | Failure: """What the move of checkout to the newest release did, as the tool prints it; a Failure where it refused or git failed.""" fetched = git("fetch", "--quiet", "--tags", "origin", cwd=checkout) if isinstance(fetched, Failure): return fetched tag = newest_tag(checkout) if isinstance(tag, Failure): return tag if tag is None: return Failure("origin has no v* tag to move to") # Re-reads each file through the clean filter, so a model pick alone leaves settings.json unchanged. if isinstance(refreshed := git("update-index", "-q", "--refresh", cwd=checkout), Failure): return refreshed changed = git("diff", "--name-only", "HEAD", cwd=checkout) if isinstance(changed, Failure): return changed unreleased = git("diff", "--name-only", tag, "--", *changed.split(), cwd=checkout) if changed.split() else "" if isinstance(unreleased, Failure): return unreleased if unreleased.split(): return Failure(f"{checkout} has uncommitted changes that {tag} doesn't hold, to {', '.join(unreleased.split())}: merge them through the development clone and cut a release first") old = git("describe", "--tags", "--abbrev=0", "--match", "v*", "HEAD", cwd=checkout) old_tag = None if isinstance(old, Failure) else old.strip() at_tag = git("describe", "--tags", "--exact-match", "--match", "v*", "HEAD", cwd=checkout) if not isinstance(at_tag, Failure) and at_tag.strip() == tag: return f"Already on {tag}, the newest release." if isinstance(git("merge-base", "--is-ancestor", "HEAD", tag, cwd=checkout), Failure): return Failure(f"{tag} doesn't hold the checkout's commit, so the move would drop work no release holds: cut a release that holds it first") settings_changed = isinstance(git("diff", "--quiet", "HEAD", tag, "--", "settings.json", cwd=checkout), Failure) # The tag holds each uncommitted change, so undoing them loses nothing, and lets the checkout below take the tag's copy. if changed.split(): restored = git("checkout", "--quiet", "HEAD", "--", *changed.split(), cwd=checkout) if isinstance(restored, Failure): return restored moved = git("checkout", "--quiet", "--detach", tag, cwd=checkout) if isinstance(moved, Failure): return moved changelog = git("show", f"{tag}:CHANGELOG.md", cwd=checkout) lines = [f"Moved from {old_tag or 'an untagged commit'} to {tag}."] if settings_changed: lines.append("settings.json changed: pick the model again with /model.") if not isinstance(changelog, Failure): lines += newer_sections(changelog, old_tag) return "\n\n".join(lines) def newer_sections(changelog: str, old_tag: str | None) -> list[str]: """Each `## ` section of changelog newer than old_tag, newest first, as it reads.""" since = tagged(old_tag) found = [] for heading, bodies in sections(changelog).items(): named = parsed(heading) if named is None: continue if since is None or named > since: found.append(f"## {heading}\n\n" + "\n".join(bodies)) return found