Answer principles round 2: 0 🔴, 1 🟡; main parses and checks settings.json, so cleaned takes a dict and never raises

This commit is contained in:
2026-10-21 22:14:24 +02:00
parent d08a128d0f
commit b7261987ad
3 changed files with 23 additions and 24 deletions
+11 -10
View File
@@ -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