Skip to content

fix: store switch history outside the shared git config - #4928

Merged
max-sixty merged 4 commits into
max-sixty:mainfrom
Oseenix:fix/switch-history-file
Nov 28, 2026
Merged

max-sixty merged 4 commits into
max-sixty:mainfrom
Oseenix:fix/switch-history-file

Conversation

@Oseenix

@Oseenix Oseenix commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

wt switch recorded the worktree it left in worktrunk.history and worktrunk.history-path in the repository's shared .git/config. #4866 skipped unchanged writes, but every changed value still took config.lock, so switches in parallel worktrees could fail concurrent config writers elsewhere.

Switch history now lives in .git/wt/switch-history.json, replaced atomically through utils::write_atomically without any lock. Branch and path are written together, so they can no longer disagree. The Repository API (record_switch_source, set_switch_previous, clear_switch_previous, switch_previous, switch_previous_worktree) keeps its signatures, so callers are unchanged.

Values recorded in git config by older versions are no longer read: the first wt switch - after upgrading finds no history, and the next switch records it again.

Tests: the three config.lock unit tests from #4866 are replaced by one that holds config.lock while recording, reading and clearing history, and asserts .git/config is byte-identical afterwards. Integration tests that seeded history with git config worktrunk.history now use TestRepo::set_switch_previous.

Closes #4863

Oseenix and others added 2 commits November 27, 2026 23:29
`wt switch` recorded the worktree it left in `worktrunk.history` and
`worktrunk.history-path` in the repository's shared `.git/config`. Every
change took `config.lock`, so switches in parallel worktrees failed
concurrent config writers elsewhere (max-sixty#4863).

History now lives in `.git/wt/switch-history.json`, replaced atomically
without a lock. Values recorded in git config by older versions are no
longer read; the first `wt switch -` after upgrading finds no history.

Closes max-sixty#4863

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repos upgraded from an older wt keep worktrunk.history and worktrunk.history-path in .git/config, and nothing removes them any more. wt config state clear and wt config state previous-branch clear used to unset these keys. Now they only delete the JSON file, so the keys stay in the shared config permanently. That contradicts the FAQ's "wt config state clear removes repository state: config keys, …". The inline suggestion unsets them in clear_switch_previous. That is a user-initiated path where a config write is fine. It is gated on the cached snapshot, so a repo without the keys still never takes config.lock, and the new lock-held test keeps passing.

Outside the diff: the doc comment on test_switch_previous_roundtrip in tests/integration_tests/repository.rs still says it "Exercises worktrunk.history read + write through the bulk-config helpers". The "Populate the cache first to hit the in-memory update branch" line inside it no longer describes anything either.

Comment thread src/git/repository/config.rs Resolved
Older versions stored switch history in worktrunk.history and
worktrunk.history-path in the shared git config. Nothing else removes
them now, so wt config state clear and previous-branch clear unset them,
checking the config snapshot first so a repository without them still
never takes config.lock.

Also covers the read and remove error paths and fixes a stale test
comment.

Refs max-sixty#4863
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

At the maintainer's request in #4863, I pushed one commit on top of this PR (e69c4720590d941288fcee58f6286f6e08a20d59):

  • clear_switch_previous now unsets older versions' worktrunk.history / worktrunk.history-path keys. This is the review suggestion, and each key is checked in the config snapshot first.
  • Added tests for that and for the read/remove error paths, which were the uncovered codecov/patch lines.
  • Fixed the stale test_switch_previous_roundtrip comment.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_switch_history_io_errors_surface never reaches the remove-error branch of clear_switch_previous. clear_switch_previous calls load_switch_history()? before remove_file. With a directory at the history path, the read fails first, and the asserted message is the read error ("Failed to read …switch-history.json"). So the Failed to remove arm is still uncovered, which may be the line codecov/patch flags again. To exercise it on Unix, leave a readable history file in a wt dir set to mode 0o555. If the read path is all the test is meant to cover, the comment should say so (suggestion inline).

Comment thread src/git/repository/config.rs Resolved
Reading the file before removing it made the remove error path
unreachable behind the read error. The removal itself says whether
anything was recorded.

Refs max-sixty#4863
@max-sixty
max-sixty merged commit e97b6cd into max-sixty:main Nov 28, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt switch writes switch history to the shared .git/config on every switch, racing parallel worktrees

3 participants