Carry the edits the first commit left unstaged
CI / tests (pull_request) Has been cancelled

This commit is contained in:
2026-12-03 23:52:10 +01:00
parent 3b07c05eba
commit f3f9c91348
19 changed files with 366 additions and 332 deletions
View File
+39
View File
@@ -0,0 +1,39 @@
"""Move the clone this tool ships in to the setup's newest release.
usage: self-update
Run it by its path in a clone of a release, as in ~/.claude/bin/self-update. It fetches origin's
tags and checks out the newest v* tag, detached, so the clone 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 clone as it was where a file the clone 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 clone'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 sys
from failure import Failure
from loop_state.paths import RELEASE_ROOT
from self_update.move import moved_to_newest
def main(argv: list[str], clone: str = str(RELEASE_ROOT)) -> int:
"""Move clone, which is the clone this tool ships in unless a test names another."""
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if argv:
print("self-update: usage: self-update", file=sys.stderr)
return 1
moved = moved_to_newest(clone)
if isinstance(moved, Failure):
print(f"self-update: {moved.reason}", file=sys.stderr)
return 1
print(moved)
return 0
+19 -19
View File
@@ -1,53 +1,53 @@
"""Move a checkout to this repo's newest release, as a bare `deploy-release` does."""
"""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 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)
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(checkout)
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=checkout), Failure):
if isinstance(refreshed := git("update-index", "-q", "--refresh", cwd=clone), Failure):
return refreshed
changed = git("diff", "--name-only", "HEAD", cwd=checkout)
changed = git("diff", "--name-only", "HEAD", cwd=clone)
if isinstance(changed, Failure):
return changed
unreleased = git("diff", "--name-only", tag, "--", *changed.split(), cwd=checkout) if changed.split() else ""
unreleased = git("diff", "--name-only", tag, "--", *changed.split(), cwd=clone) 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)
return Failure(f"{clone} 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=clone)
old_tag = None if isinstance(old, Failure) else old.strip()
at_tag = git("describe", "--tags", "--exact-match", "--match", "v*", "HEAD", cwd=checkout)
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"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 doesn't lose anything, and lets the checkout below take the tag's copy.
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", "--", "settings.json", cwd=checkout)
tracked = git("ls-files", "--", "settings.json", cwd=clone)
if isinstance(tracked, Failure):
return tracked
set_aside = sorted({*changed.split(), *tracked.split()} if settings_changed else set(changed.split()))
if set_aside:
restored = git("checkout", "--quiet", "HEAD", "--", *set_aside, cwd=checkout)
restored = git("checkout", "--quiet", "HEAD", "--", *set_aside, cwd=clone)
if isinstance(restored, Failure):
return restored
moved = git("checkout", "--quiet", "--detach", tag, cwd=checkout)
moved = git("checkout", "--quiet", "--detach", tag, cwd=clone)
if isinstance(moved, Failure):
return moved
changelog = git("show", f"{tag}:CHANGELOG.md", cwd=checkout)
changelog = git("show", f"{tag}:CHANGELOG.md", cwd=clone)
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.")
+156
View File
@@ -0,0 +1,156 @@
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from test_env import isolated_env
BIN = pathlib.Path(__file__).resolve().parents[1]
# self-update moves the clone it ships in, so most tests hand its main the clone to move.
MOVE = [sys.executable, "-c", "import sys; sys.path.insert(0, sys.argv[1]); from self_update import main; sys.exit(main.main(sys.argv[3:], sys.argv[2]))", str(BIN)]
class SelfUpdate(unittest.TestCase):
def setUp(self):
self.temp = pathlib.Path(tempfile.mkdtemp())
self.addCleanup(subprocess.run, ["rm", "-rf", str(self.temp)])
self.origin = self.temp / "origin.git"
self.git(self.temp, "init", "-q", "--bare", "-b", "main", str(self.origin))
self.source = self.temp / "source"
self.git(self.temp, "clone", "-q", str(self.origin), str(self.source))
self.release("0.1.0", "- First.\n", '{"theme": "dark"}\n')
self.target = self.temp / "release"
self.git(self.temp, "clone", "-q", str(self.origin), str(self.target))
self.git(self.target, "checkout", "-q", "--detach", "v0.1.0")
def git(self, cwd: pathlib.Path, *args: str) -> str:
return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, cwd=cwd, env=isolated_env(), text=True).stdout
def release(self, version: str, entry: str, settings: str) -> None:
"""Commit a CHANGELOG.md whose newest section is version's, holding entry, and settings.json, then tag it and push both."""
changelog = self.source / "CHANGELOG.md"
earlier = changelog.read_text().removeprefix("# Changelog\n\n") if changelog.exists() else ""
changelog.write_text(f"# Changelog\n\n## {version}\n\n{entry}\n{earlier}")
(self.source / "settings.json").write_text(settings)
self.git(self.source, "add", "-A")
self.git(self.source, "commit", "-q", "-m", f"Cut release {version}")
self.git(self.source, "tag", f"v{version}")
self.git(self.source, "push", "-q", "origin", "main", "--tags")
def update(self, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run([*MOVE, str(self.target), *args], capture_output=True, env=isolated_env(), text=True)
def head(self) -> str:
return self.git(self.target, "describe", "--tags", "--exact-match", "HEAD").strip()
def test_moves_the_clone_it_runs_from(self):
# The tools are untracked in this clone, so the move leaves them as they are.
shutil.copytree(BIN, self.target / "bin", ignore=shutil.ignore_patterns("test_*", "__pycache__"))
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
ran = subprocess.run([sys.executable, str(self.target / "bin" / "self-update")], capture_output=True, cwd=self.temp, env=isolated_env(), text=True)
self.assertEqual((ran.returncode, self.head()), (0, "v0.2.0"), ran.stderr)
self.assertIn("Moved from v0.1.0 to v0.2.0.", ran.stdout)
def test_refuses_an_argument_and_leaves_the_clone_as_it_was(self):
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
ran = self.update(str(self.temp))
self.assertEqual((ran.returncode, self.head()), (1, "v0.1.0"))
self.assertIn("self-update: usage: self-update", ran.stderr)
def test_moves_the_clone_to_the_newest_tag_and_prints_each_newer_versions_changelog(self):
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
self.release("0.3.0", "- Third: run `make thing` once.\n", '{"theme": "dark"}\n')
(self.source / "CHANGELOG.md").write_text("# Changelog\n\n## Unreleased\n\n- Later.\n\n" + (self.source / "CHANGELOG.md").read_text().removeprefix("# Changelog\n\n"))
self.git(self.source, "commit", "-q", "-am", "Later work")
self.git(self.source, "push", "-q", "origin", "main")
ran = self.update()
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertEqual(self.head(), "v0.3.0")
self.assertIn("Moved from v0.1.0 to v0.3.0.", ran.stdout)
self.assertIn("## 0.3.0\n\n- Third: run `make thing` once.", ran.stdout)
self.assertIn("## 0.2.0\n\n- Second.", ran.stdout)
self.assertNotIn("First.", ran.stdout)
self.assertNotIn("Later.", ran.stdout)
self.assertNotIn("/model", ran.stdout)
def test_stops_with_gits_error_where_the_index_cannot_be_refreshed(self):
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
(self.target / ".git" / "index").write_bytes(b"DIRC")
ran = self.update()
self.assertNotEqual(ran.returncode, 0)
self.assertIn("index", ran.stderr)
self.assertNotIn("uncommitted", ran.stderr)
self.assertEqual(self.git(self.target, "rev-parse", "HEAD"), self.git(self.source, "rev-parse", "v0.1.0^{commit}"))
def test_says_so_where_the_clone_is_on_the_newest_tag_already(self):
ran = self.update()
self.assertEqual((ran.returncode, ran.stdout), (0, "Already on v0.1.0, the newest release.\n"))
def test_refuses_an_uncommitted_change_to_a_tracked_file_and_leaves_the_clone_as_it_was(self):
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
(self.target / "CHANGELOG.md").write_text("Edited here.\n")
(self.target / "projects").mkdir()
(self.target / "projects" / "session.jsonl").write_text("{}\n")
ran = self.update()
self.assertEqual(ran.returncode, 1)
self.assertIn("CHANGELOG.md", ran.stderr)
self.assertNotIn("projects", ran.stderr, "an untracked file is Claude Code's own state, not a change")
self.assertEqual(self.head(), "v0.1.0")
def test_takes_an_uncommitted_change_the_new_release_holds_as_no_change(self):
(self.target / "settings.json").write_text('{"theme": "light"}\n')
self.release("0.2.0", "- Second.\n", '{"theme": "light"}\n')
ran = self.update()
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertEqual(self.head(), "v0.2.0")
def test_refuses_to_move_a_clone_back_from_commits_no_release_holds(self):
self.git(self.target, "switch", "-q", "main")
(self.source / "new.md").write_text("Unreleased.\n")
self.git(self.source, "add", "-A")
self.git(self.source, "commit", "-q", "-m", "Unreleased work")
self.git(self.source, "push", "-q", "origin", "main")
self.git(self.target, "pull", "-q", "--ff-only")
ran = self.update()
self.assertEqual(ran.returncode, 1)
self.assertIn("v0.1.0 doesn't hold", ran.stderr)
self.assertEqual(self.git(self.target, "branch", "--show-current").strip(), "main")
def test_takes_a_model_picked_with_the_clean_filter_set_as_no_change(self):
self.release("0.2.0", "- Second.\n", '{\n "theme": "dark"\n}\n')
self.git(self.target, "fetch", "-q", "--tags", "origin")
self.git(self.target, "checkout", "-q", "--detach", "v0.2.0")
(self.target / ".gitattributes").write_text("/settings.json filter=settings\n")
self.git(self.target, "config", "filter.settings.clean", f"{sys.executable} {BIN / 'settings_filter.py'}")
(self.target / "settings.json").write_text('{"model": "claude-opus-5-5", "theme": "dark"}\n')
self.release("0.3.0", "- Third.\n", '{\n "theme": "dark"\n}\n')
ran = self.update()
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertEqual(self.head(), "v0.3.0")
self.assertIn('"model"', (self.target / "settings.json").read_text(), "a release that leaves settings.json as it was keeps the pick")
def test_moves_a_settings_json_that_differs_only_by_a_model_pick_and_key_order_where_the_release_changes_it(self):
self.release("0.2.0", "- Second.\n", '{\n "permissions": {},\n "theme": "dark"\n}\n')
self.git(self.target, "fetch", "-q", "--tags", "origin")
self.git(self.target, "checkout", "-q", "--detach", "v0.2.0")
(self.target / ".gitattributes").write_text("/settings.json filter=settings\n")
self.git(self.target, "config", "filter.settings.clean", f"{sys.executable} {BIN / 'settings_filter.py'}")
(self.target / "settings.json").write_text('{\n "theme": "dark",\n "model": "opus",\n "permissions": {}\n}\n')
self.release("0.3.0", "- Third.\n", '{\n "permissions": {},\n "theme": "light"\n}\n')
ran = self.update()
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertEqual(self.head(), "v0.3.0")
self.assertEqual((self.target / "settings.json").read_text(), '{\n "permissions": {},\n "theme": "light"\n}\n')
self.assertIn("settings.json changed: pick the model again with /model.", ran.stdout)
def test_asks_for_the_model_again_where_the_release_changes_settings_json(self):
self.release("0.2.0", "- Second.\n", '{"theme": "light"}\n')
ran = self.update()
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertIn("settings.json changed: pick the model again with /model.", ran.stdout)