Files
claude-config/bin/deploy_release/main.py
T

101 lines
4.8 KiB
Python

"""Move the checkout Claude Code reads its configuration from to this repo's newest release.
usage: deploy-release [<checkout>]
<checkout> defaults to ~/.claude. The tool fetches origin's tags and checks out the newest v* tag,
detached, so the checkout holds only what a release holds. Development happens in another clone,
such as ~/code/claude-config. It prints the version it moved from and to, then CHANGELOG.md's
section for each version since, so you can do what each asks, such as a new step under Install.
Where the move changed settings.json, it asks you to pick the model again with /model, since git's
clean filter keeps the model out of every commit.
It refuses, exit 1, and leaves the checkout as it was where a file the checkout tracks has an
uncommitted change the newest tag doesn't hold: such as a hook Claude Code wrote into settings.json.
Merge the change through the development clone and cut a release first. A model picked with /model
is no such change. It
also refuses where the newest tag doesn't hold the checkout's commit, since the move would drop
work no release holds. Untracked files, such as Claude Code's sessions and memory, stay as they are.
"""
import pathlib
import re
import sys
from failure import Failure
from git_repo import git
from todo_table import release_of
from markdown_text import sections
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if len(argv) > 1:
print("deploy-release: usage: deploy-release [<checkout>]", file=sys.stderr)
return 1
checkout = argv[0] if argv else str(pathlib.Path.home() / ".claude")
moved = deploy(checkout)
if isinstance(moved, Failure):
print(f"deploy-release: {moved.reason}", file=sys.stderr)
return 1
print(moved)
return 0
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
newest = git("-c", "versionsort.suffix=-", "tag", "--list", "v*", "--sort=-v:refname", cwd=checkout)
if isinstance(newest, Failure):
return newest
if not newest.split():
return Failure("origin has no v* tag to move to")
tag = newest.split()[0]
# Re-reads each file through the clean filter, so a model pick alone leaves settings.json unchanged.
git("update-index", "-q", "--refresh", cwd=checkout)
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 `## <version>` section of changelog newer than old_tag, newest first, as it reads."""
since = release_of(old_tag.removeprefix("v")) if old_tag and re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", old_tag) else None
found = []
for heading, bodies in sections(changelog).items():
if not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", heading):
continue
if since is None or release_of(heading) > since:
found.append(f"## {heading}\n\n" + "\n".join(bodies))
return found