Answer architecture round 1: 1 🔴, 2 🟡; split the filter's pure core out of main, stage settings.json before Update's move, read the stored settings in a repo check

This commit is contained in:
2026-10-21 21:38:50 +02:00
parent 7c3800337a
commit 7665087e1e
6 changed files with 54 additions and 27 deletions
+21
View File
@@ -0,0 +1,21 @@
import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from git_repo import git
from repo_checks.test_python_floor import ROOT
from settings_filter import cleaned
class Settings(unittest.TestCase):
def test_the_stored_settings_hold_no_model_and_sorted_keys(self):
# 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.assertEqual(stored, cleaned(stored))
def test_the_readme_installs_the_filter_git_attributes_name(self):
self.assertIn("settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8"))
self.assertIn("git config filter.settings.clean bin/settings_filter.py\n", (ROOT / "README.md").read_text(encoding="utf-8"))
+8 -4
View File
@@ -9,11 +9,15 @@ import json
import sys
def main() -> None:
settings = json.load(sys.stdin)
def cleaned(written: str) -> str:
"""settings.json as git stores it: without "model", every key sorted."""
settings = json.loads(written)
settings.pop("model", None)
json.dump(settings, sys.stdout, ensure_ascii=False, indent=2, sort_keys=True)
sys.stdout.write("\n")
return json.dumps(settings, ensure_ascii=False, indent=2, sort_keys=True) + "\n"
def main() -> None:
sys.stdout.write(cleaned(sys.stdin.read()))
if __name__ == "__main__":
+20 -19
View File
@@ -5,14 +5,11 @@ import sys
import tempfile
import unittest
BIN = pathlib.Path(__file__).resolve().parent
ROOT = BIN.parent
FILTER = BIN / "settings_filter.py"
INSTALL = "git config filter.settings.clean bin/settings_filter.py"
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from settings_filter import cleaned
def cleaned(text):
return subprocess.run([sys.executable, FILTER], input=text, capture_output=True, check=True, text=True).stdout
FILTER = pathlib.Path(__file__).resolve().parent / "settings_filter.py"
class Clean(unittest.TestCase):
@@ -20,35 +17,39 @@ class Clean(unittest.TestCase):
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_the_tracked_settings_hold_no_model_and_sorted_keys(self):
tracked = (ROOT / "settings.json").read_text(encoding="utf-8")
self.assertEqual(tracked, cleaned(tracked))
def test_the_readme_installs_the_filter_git_attributes_name(self):
self.assertIn(f"{INSTALL}\n", (ROOT / "README.md").read_text(encoding="utf-8"))
self.assertIn("settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8"))
def test_keeps_text_outside_ascii(self):
self.assertEqual(cleaned('{"name": "Lilleman på Larv"}'), '{\n "name": "Lilleman på Larv"\n}\n')
class Checkout(unittest.TestCase):
def test_a_model_pick_and_claude_codes_key_order_leave_nothing_to_commit(self):
def test_a_model_pick_in_claude_codes_key_order_leaves_nothing_to_commit_and_outlasts_an_update(self):
with tempfile.TemporaryDirectory() as repo:
def git(*args):
return subprocess.run(["git", "-C", repo, *args], capture_output=True, check=True, text=True).stdout
return subprocess.run(["git", "-C", repo, "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, text=True).stdout
git("init", "-q")
(pathlib.Path(repo) / "bin").mkdir()
(pathlib.Path(repo) / "bin" / "settings_filter.py").symlink_to(FILTER)
(pathlib.Path(repo) / ".gitattributes").write_text("settings.json filter=settings\n")
git(*INSTALL.split()[1:])
git("config", "filter.settings.clean", "bin/settings_filter.py")
settings = pathlib.Path(repo) / "settings.json"
settings.write_text(cleaned('{"env": {}, "theme": "dark"}'))
git("add", ".")
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "settings")
settings.write_text(json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2))
git("commit", "-qm", "settings")
git("tag", "v1")
(pathlib.Path(repo) / "other").write_text("")
git("add", "other")
git("commit", "-qm", "a tag that leaves settings.json alone")
git("tag", "v2")
git("checkout", "-q", "v1")
picked = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2)
settings.write_text(picked)
self.assertEqual(git("diff"), "")
# git status lists a file whose size changed until it is staged; staging adds no change.
# README → Update: git status lists a file whose size changed until it is staged; staging adds no change, and keeps the model through the move.
git("add", "settings.json")
self.assertEqual(git("status", "--porcelain"), "")
git("checkout", "-q", "-f", "v2")
self.assertEqual(settings.read_text(), picked)
if __name__ == "__main__":