From b7261987ad3884af4e86156ceadd5715c3798e3b Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 21 Oct 2026 22:14:24 +0200 Subject: [PATCH] =?UTF-8?q?Answer=20principles=20round=202:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1;=20main=20parses=20and=20check?= =?UTF-8?q?s=20settings.json,=20so=20cleaned=20takes=20a=20dict=20and=20ne?= =?UTF-8?q?ver=20raises?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/repo_checks/test_settings.py | 5 +++-- bin/settings_filter.py | 21 +++++++++++---------- bin/test_settings_filter.py | 21 +++++++++------------ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/bin/repo_checks/test_settings.py b/bin/repo_checks/test_settings.py index e769a22a..debbcfca 100644 --- a/bin/repo_checks/test_settings.py +++ b/bin/repo_checks/test_settings.py @@ -17,8 +17,9 @@ class Settings(unittest.TestCase): # The index, not the working file: the working file holds this machine's model in Claude Code's key order. stored = git("show", ":settings.json", cwd=str(ROOT)) self.assertIsInstance(stored, str, stored) - self.assertNotIn("model", json.loads(stored)) - self.assertEqual(stored, cleaned(stored), f"commit settings.json with the filter on: {INSTALL}") + fix = f"commit settings.json with the filter on: {INSTALL}" + self.assertNotIn("model", json.loads(stored), fix) + self.assertEqual(stored, cleaned(json.loads(stored)), fix) def test_the_readme_installs_the_filter_git_attributes_names_for_the_root_settings_only(self): self.assertEqual("/settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8")) diff --git a/bin/settings_filter.py b/bin/settings_filter.py index 4f9ab157..7c60b1cf 100755 --- a/bin/settings_filter.py +++ b/bin/settings_filter.py @@ -2,29 +2,30 @@ """Git's clean filter for settings.json: drop "model" and sort every key. Claude Code rewrites settings.json, in its own key order, whenever a model is -picked or a plugin installed. Each machine owns its model, so git stores the -file without it, and neither write reaches a commit. +picked or a plugin installed. Each machine owns its model, so neither the +model nor Claude Code's key order reaches a commit. """ import json import sys -def cleaned(written: str) -> str: - """settings.json as git stores it: without "model", every key sorted. ValueError where it holds no JSON object.""" - settings = json.loads(written) - if not isinstance(settings, dict): - raise ValueError("settings.json holds no JSON object") - settings.pop("model", None) - return json.dumps(settings, ensure_ascii=False, indent=2, sort_keys=True) + "\n" +def cleaned(settings: dict[str, object]) -> str: + """settings.json as git stores it: without "model", every key sorted.""" + kept = {key: value for key, value in settings.items() if key != "model"} + return json.dumps(kept, ensure_ascii=False, indent=2, sort_keys=True) + "\n" def main() -> int: # On a non-zero exit git warns and stores the file as written. try: - sys.stdout.write(cleaned(sys.stdin.read())) + settings = json.loads(sys.stdin.read()) except ValueError as error: print(f"settings_filter.py: {error}", file=sys.stderr) return 1 + if not isinstance(settings, dict): + print("settings_filter.py: settings.json holds no JSON object", file=sys.stderr) + return 1 + sys.stdout.write(cleaned(settings)) return 0 diff --git a/bin/test_settings_filter.py b/bin/test_settings_filter.py index 0bfb633a..d0d8cb28 100644 --- a/bin/test_settings_filter.py +++ b/bin/test_settings_filter.py @@ -18,24 +18,21 @@ PICKED = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2) class Clean(unittest.TestCase): def test_drops_the_model_and_sorts_every_key(self): - written = '{"theme": "dark", "model": "opus[1m]", "env": {"B": "1", "A": "2"}}' + written = {"theme": "dark", "model": "opus[1m]", "env": {"B": "1", "A": "2"}} self.assertEqual(cleaned(written), '{\n "env": {\n "A": "2",\n "B": "1"\n },\n "theme": "dark"\n}\n') def test_keeps_text_outside_ascii(self): - self.assertEqual(cleaned('{"name": "Lilleman på Larv"}'), '{\n "name": "Lilleman på Larv"\n}\n') + self.assertEqual(cleaned({"name": "Lilleman på Larv"}), '{\n "name": "Lilleman på Larv"\n}\n') - def test_refuses_json_that_is_no_object(self): - with self.assertRaises(ValueError): - cleaned("[]") - - def test_a_broken_file_exits_1_naming_the_tool(self): - ran = subprocess.run([sys.executable, FILTER], input="{", capture_output=True, text=True) - self.assertEqual((ran.returncode, ran.stdout), (1, "")) - self.assertIn("settings_filter.py: ", ran.stderr) + def test_a_file_that_holds_no_json_object_exits_1_naming_the_tool(self): + for written in ("{", "[]"): + ran = subprocess.run([sys.executable, FILTER], input=written, capture_output=True, text=True) + self.assertEqual((ran.returncode, ran.stdout), (1, ""), written) + self.assertIn("settings_filter.py: ", ran.stderr) class Checkout(unittest.TestCase): - """A repo as README → Install leaves it, at tag v1, and v2, which leaves settings.json alone.""" + """A repo as README → Install leaves it, checked out at tag v1; tag v2 doesn't change settings.json.""" def setUp(self): temp = tempfile.TemporaryDirectory() @@ -48,7 +45,7 @@ class Checkout(unittest.TestCase): (self.repo / ".gitattributes").write_text((ROOT / ".gitattributes").read_text()) self.git("config", "filter.settings.clean", "bin/settings_filter.py") self.settings = self.repo / "settings.json" - self.settings.write_text(cleaned('{"env": {}, "theme": "dark"}')) + self.settings.write_text(cleaned({"env": {}, "theme": "dark"})) (self.repo / ".claude").mkdir() (self.repo / ".claude" / "settings.json").write_text('{\n "model": "claude-opus-5-5"\n}\n') self.git("add", ".")