From e82fe6dee3adcfe178f2f13540b5dccb1b1af3b9 Mon Sep 17 00:00:00 2001 From: Julian Montez Date: Tue, 8 Sep 2026 19:52:17 -0400 Subject: [PATCH] Stop Claude Code's settings rewrites from dirtying the tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ~/.claude/settings.json is stowed from this repo, but Claude Code owns it: it rewrites the whole file whenever a runtime option changes, so every /model switch left the working tree dirty with a key reorder and a one-line model change. There's no env var or setting to redirect where it persists — only the --settings flag, which is per-invocation and wouldn't cover GUI or IDE launches. Handle it in two halves, because neither is sufficient alone: .gitattributes routes the file through claude-settings-clean.py, which drops the keys Claude Code writes back as machine state (model, theme, sandbox, modelSettings) and sorts the rest so a rewrite in a different key order isn't a diff either. That keeps commits clean but not `git status` — git's index refresh hashes the worktree file raw and only the diff machinery applies the filter, so the file reads as modified even when it cleans to the committed blob. Verified against git 2.55.0; filter..required doesn't change it. skip-worktree is what actually silences the churn. It costs a toggle to commit a deliberate change to the file, which CLAUDE.md documents. Both are per-clone git state that .gitattributes can't carry, so bootstrap.sh sets them and doctor.sh reports a clone that's missing either — the same pattern as the Brewfiles. Also ignore **/.claude/.cc-writes/, which Claude Code creates in the repo. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01AEPYeZgMsmFPVBbRvkFtM2 --- .gitattributes | 1 + CLAUDE.md | 12 ++++++++ bootstrap.sh | 8 +++++ claude-settings-clean.py | 42 +++++++++++++++++++++++++ claude/.claude/settings.json | 60 +++++++++++++++++------------------- doctor.sh | 15 +++++++++ git/.gitignore | 2 ++ lib.sh | 12 ++++++++ 8 files changed, 121 insertions(+), 31 deletions(-) create mode 100644 .gitattributes create mode 100755 claude-settings-clean.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eff19af --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +claude/.claude/settings.json filter=claude-settings diff --git a/CLAUDE.md b/CLAUDE.md index 1535aea..3ebfcb8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -19,6 +19,18 @@ Personal dotfiles managed with GNU stow. Full details: README.md. `doctor.sh` detect it's missing. - The repo must stay cloned at `~/Development/dotfiles`; stow's symlinks are relative to that path. +- `claude/.claude/settings.json` is owned by Claude Code, which rewrites it on + every `/model` switch, so it's handled in two halves. `.gitattributes` runs it + through `claude-settings-clean.py` (drops the volatile keys, sorts the rest) + so commits only ever carry durable config; a new key Claude Code writes back + as state goes in that script's `VOLATILE`. The file is also marked + `skip-worktree`, because a clean filter alone doesn't quiet `git status` — + git's refresh path hashes the file raw. Both halves are per-clone index and + config state: `bootstrap.sh` sets them, `doctor.sh` checks them. +- To commit a deliberate change to that settings file, unhide it first: + `git update-index --no-skip-worktree claude/.claude/settings.json`, commit, + then re-hide with `--skip-worktree`. While it's hidden, git ignores every + local edit to it, including one you meant to keep. - Shell scripts: keep `shellcheck`-clean (`shellcheck bootstrap.sh doctor.sh lib.sh macos/*.sh`). diff --git a/bootstrap.sh b/bootstrap.sh index f9fcb63..5cb65c8 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -119,6 +119,14 @@ else sudo apt-get install -y git curl stow zsh tmux neovim xclip ripgrep fd-find fi +# ---- Register the Claude settings clean filter ---- +# .gitattributes names the filter; git only ever runs one it has been given a +# command for, so an unregistered clone silently commits the volatile keys. +echo ">>> Registering the Claude settings clean filter..." +cd "$DOTFILES_DIR" +git config "$CLAUDE_SETTINGS_FILTER" "$CLAUDE_SETTINGS_FILTER_CMD" +git update-index --skip-worktree "$CLAUDE_SETTINGS_FILE" + # ---- Init git submodules ---- echo ">>> Initializing git submodules..." cd "$DOTFILES_DIR" diff --git a/claude-settings-clean.py b/claude-settings-clean.py new file mode 100755 index 0000000..38fb3e6 --- /dev/null +++ b/claude-settings-clean.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""Git clean filter for claude/.claude/settings.json. + +Claude Code owns that file: it rewrites the whole thing whenever a runtime +option changes (/model, the theme picker, sandbox toggles), so a stowed, +tracked copy is dirty again the moment you switch models. This strips the keys +Claude Code writes back as machine state and sorts what's left, leaving git to +see only the durable config — hooks, permissions, statusLine, plugins. + +Reads the working-tree file on stdin, writes the committed form on stdout. +Wired up by `git config filter.claude-settings.clean` in bootstrap.sh and +pointed at this file by .gitattributes. +""" + +import json +import sys + +# Written back by Claude Code itself; per-machine, never worth a commit. +VOLATILE = ("model", "theme", "sandbox", "modelSettings") + + +def main() -> None: + raw = sys.stdin.read() + try: + settings = json.loads(raw) + except ValueError: + # A half-written or hand-broken file still has to round-trip: a filter + # that errors here wedges every git command touching the file. + sys.stdout.write(raw) + return + + for key in VOLATILE: + settings.pop(key, None) + + # sort_keys because Claude Code's own key order isn't stable across + # versions — without it a rewrite alone shows up as a diff. + json.dump(settings, sys.stdout, indent=2, sort_keys=True) + sys.stdout.write("\n") + + +if __name__ == "__main__": + main() diff --git a/claude/.claude/settings.json b/claude/.claude/settings.json index f3a3973..730cd9e 100644 --- a/claude/.claude/settings.json +++ b/claude/.claude/settings.json @@ -1,45 +1,44 @@ { - "model": "sonnet", + "enabledPlugins": { + "claude-md-management@claude-plugins-official": true, + "code-review@claude-plugins-official": true, + "code-simplifier@claude-plugins-official": true, + "frontend-design@claude-plugins-official": true, + "ponytail@ponytail": true, + "security-guidance@claude-plugins-official": true, + "superpowers@claude-plugins-official": false, + "swift-lsp@claude-plugins-official": true + }, + "extraKnownMarketplaces": { + "ponytail": { + "source": { + "repo": "DietrichGebert/ponytail", + "source": "github" + } + } + }, "hooks": { "Notification": [ { - "matcher": "permission_prompt|agent_needs_input", "hooks": [ { - "type": "command", - "command": "command -v afplay >/dev/null && afplay /System/Library/Sounds/Glass.aiff || true" + "command": "command -v afplay >/dev/null && afplay /System/Library/Sounds/Glass.aiff || true", + "type": "command" } - ] + ], + "matcher": "permission_prompt|agent_needs_input" }, { - "matcher": "idle_prompt|agent_completed", "hooks": [ { - "type": "command", - "command": "command -v afplay >/dev/null && afplay /System/Library/Sounds/Pop.aiff || true" + "command": "command -v afplay >/dev/null && afplay /System/Library/Sounds/Pop.aiff || true", + "type": "command" } - ] + ], + "matcher": "idle_prompt|agent_completed" } ] }, - "enabledPlugins": { - "frontend-design@claude-plugins-official": true, - "superpowers@claude-plugins-official": false, - "code-review@claude-plugins-official": true, - "code-simplifier@claude-plugins-official": true, - "claude-md-management@claude-plugins-official": true, - "security-guidance@claude-plugins-official": true, - "swift-lsp@claude-plugins-official": true, - "ponytail@ponytail": true - }, - "extraKnownMarketplaces": { - "ponytail": { - "source": { - "source": "github", - "repo": "DietrichGebert/ponytail" - } - } - }, "permissions": { "deny": [ "Bash(rm -rf /)", @@ -56,8 +55,7 @@ ] }, "statusLine": { - "type": "command", - "command": "~/.claude/statusline.sh" - }, - "theme": "auto" + "command": "~/.claude/statusline.sh", + "type": "command" + } } diff --git a/doctor.sh b/doctor.sh index 0c7f768..4c5159c 100755 --- a/doctor.sh +++ b/doctor.sh @@ -134,6 +134,21 @@ else ok "submodules at recorded commits" fi +configured_filter="$(cd "$DOTFILES_DIR" && git config --get "$CLAUDE_SETTINGS_FILTER" 2>/dev/null)" +if [[ "$configured_filter" == "$CLAUDE_SETTINGS_FILTER_CMD" ]]; then + ok "Claude settings clean filter registered" +else + bad "Claude settings clean filter not registered" + hint "git -C $DOTFILES_DIR config $CLAUDE_SETTINGS_FILTER $CLAUDE_SETTINGS_FILTER_CMD" +fi + +if [[ "$(cd "$DOTFILES_DIR" && git ls-files -v "$CLAUDE_SETTINGS_FILE" 2>/dev/null)" == S* ]]; then + ok "Claude settings marked skip-worktree" +else + bad "Claude settings not marked skip-worktree" + hint "git -C $DOTFILES_DIR update-index --skip-worktree $CLAUDE_SETTINGS_FILE" +fi + if [[ -n "$(cd "$DOTFILES_DIR" && git status --porcelain 2>/dev/null)" ]]; then bad "working tree is dirty" hint "git -C $DOTFILES_DIR status" diff --git a/git/.gitignore b/git/.gitignore index 63465e3..4dd65e0 100644 --- a/git/.gitignore +++ b/git/.gitignore @@ -1,2 +1,4 @@ .DS_Store **/.claude/settings.local.json + +**/.claude/.cc-writes/ diff --git a/lib.sh b/lib.sh index 2fc1798..9ea9cb8 100644 --- a/lib.sh +++ b/lib.sh @@ -15,6 +15,18 @@ MACHINE_FILE="$DOTFILES_DIR/.machine" # Stow packages carrying no special options. STOW_PACKAGES=(zsh git tmux ghostty nvim base16 claude) +# Claude Code rewrites ~/.claude/settings.json whenever a runtime option +# changes, which dirties the stowed copy on every /model switch. .gitattributes +# routes the file through this clean filter so git only sees the durable config; +# the filter command itself lives in each clone's git config, set by bootstrap. +# The filter keeps volatile keys out of commits; git still reports the file as +# modified though, because its refresh path hashes the file raw and only the +# diff machinery applies the filter. skip-worktree is what actually silences +# that — see CLAUDE.md for how to commit a deliberate change to the file. +CLAUDE_SETTINGS_FILE="claude/.claude/settings.json" +CLAUDE_SETTINGS_FILTER="filter.claude-settings.clean" +CLAUDE_SETTINGS_FILTER_CMD="$DOTFILES_DIR/claude-settings-clean.py" + # ssh is stowed on its own: --no-folding keeps ~/.ssh a real directory holding a # symlinked config. Folded, ~/.ssh would itself be a symlink into this repo, and # every key or known_hosts file written there would land in the working tree.