"""Move a clone to the setup's newest release, as `self-update` does.""" from failure import Failure from git_cli.git_repo import git from plan.release import newest_tag, parsed, tagged from text_files.markdown_text import sections def moved_to_newest(clone: str) -> str | Failure: """What the move of clone 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=clone) if isinstance(fetched, Failure): return fetched tag = newest_tag(clone) 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=clone), Failure): return refreshed listed = git("diff", "--name-only", "-z", "HEAD", cwd=clone) if isinstance(listed, Failure): return listed changed = names(listed) unreleased = git("diff", "--name-only", "-z", tag, "--", *changed, cwd=clone) if changed else "" if isinstance(unreleased, Failure): return unreleased if names(unreleased): return Failure(f"{clone} has uncommitted changes that {tag} doesn't hold, to {', '.join(names(unreleased))}: merge them through the development clone and cut a release first") old = git("describe", "--tags", "--abbrev=0", "--match", "v*", "HEAD", cwd=clone) old_tag = None if isinstance(old, Failure) else old.strip() at_tag = git("describe", "--tags", "--exact-match", "--match", "v*", "HEAD", cwd=clone) if not isinstance(at_tag, Failure) and at_tag.strip() == tag: return f"{clone} is already on {tag}, the newest release." if isinstance(git("merge-base", "--is-ancestor", "HEAD", tag, cwd=clone), Failure): return Failure(f"{tag} doesn't hold the clone'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=clone), Failure) # The tag holds each uncommitted change, so undoing them doesn't lose anything, and lets the move below take the tag's copy. # A settings.json the clean filter shows unchanged may still differ on disk, by a model pick or Claude Code's key order, which checkout refuses to overwrite; the pick is asked for again below. tracked = git("ls-files", "-z", "--", "settings.json", cwd=clone) if isinstance(tracked, Failure): return tracked set_aside = sorted({*changed, *names(tracked)} if settings_changed else set(changed)) if set_aside: restored = git("checkout", "--quiet", "HEAD", "--", *set_aside, cwd=clone) if isinstance(restored, Failure): return restored moved = git("checkout", "--quiet", "--detach", tag, cwd=clone) if isinstance(moved, Failure): return moved changelog = git("show", f"{tag}:CHANGELOG.md", cwd=clone) lines = [f"Moved {clone} 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 names(listed: str) -> list[str]: """The file names git listed with -z, which ends each with a NUL, so a name holding a space stays whole.""" return [name for name in listed.split("\0") if name] 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