Answer security round 1: 0 🔴, 0 🟡 (0 misread); keep a file name with a space whole in self-update, sort its imports and name its path as <clone>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
usage: self-update
|
||||
|
||||
Run it by its path in a clone of a release, as in ~/.claude/bin/self-update. It fetches origin's
|
||||
Run it by its path in a clone of a release, as in <clone>/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
|
||||
@@ -19,8 +19,8 @@ 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
|
||||
from loop_state.paths import RELEASE_ROOT
|
||||
|
||||
|
||||
def main(argv: list[str], clone: str = str(RELEASE_ROOT)) -> int:
|
||||
|
||||
+14
-8
@@ -18,14 +18,15 @@ def moved_to_newest(clone: str) -> str | Failure:
|
||||
# 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
|
||||
changed = git("diff", "--name-only", "HEAD", cwd=clone)
|
||||
if isinstance(changed, Failure):
|
||||
return changed
|
||||
unreleased = git("diff", "--name-only", tag, "--", *changed.split(), cwd=clone) if changed.split() else ""
|
||||
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 unreleased.split():
|
||||
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")
|
||||
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)
|
||||
@@ -36,10 +37,10 @@ def moved_to_newest(clone: str) -> str | Failure:
|
||||
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=clone)
|
||||
tracked = git("ls-files", "-z", "--", "settings.json", cwd=clone)
|
||||
if isinstance(tracked, Failure):
|
||||
return tracked
|
||||
set_aside = sorted({*changed.split(), *tracked.split()} if settings_changed else set(changed.split()))
|
||||
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):
|
||||
@@ -56,6 +57,11 @@ def moved_to_newest(clone: str) -> str | Failure:
|
||||
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 `## <version>` section of changelog newer than old_tag, newest first, as it reads."""
|
||||
since = tagged(old_tag)
|
||||
|
||||
@@ -103,6 +103,17 @@ class SelfUpdate(unittest.TestCase):
|
||||
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_names_whole_a_changed_file_whose_name_holds_a_space(self):
|
||||
(self.source / "my notes.md").write_text("Released.\n")
|
||||
self.release("0.2.0", "- Second.\n", '{"theme": "dark"}\n')
|
||||
self.git(self.target, "fetch", "-q", "--tags", "origin")
|
||||
self.git(self.target, "checkout", "-q", "--detach", "v0.2.0")
|
||||
self.release("0.3.0", "- Third.\n", '{"theme": "dark"}\n')
|
||||
(self.target / "my notes.md").write_text("Edited here.\n")
|
||||
ran = self.update()
|
||||
self.assertEqual((ran.returncode, self.head()), (1, "v0.2.0"))
|
||||
self.assertIn("uncommitted changes that v0.3.0 doesn't hold, to my notes.md:", ran.stderr)
|
||||
|
||||
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')
|
||||
|
||||
Reference in New Issue
Block a user