Repository navigation
fix: store switch history outside the shared git config - #4928
Conversation
`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
left a comment
There was a problem hiding this comment.
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.
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
|
At the maintainer's request in #4863, I pushed one commit on top of this PR (
|
worktrunk-bot
left a comment
There was a problem hiding this comment.
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).
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
wt switchrecorded the worktree it left inworktrunk.historyandworktrunk.history-pathin the repository's shared.git/config. #4866 skipped unchanged writes, but every changed value still tookconfig.lock, so switches in parallel worktrees could fail concurrent config writers elsewhere.Switch history now lives in
.git/wt/switch-history.json, replaced atomically throughutils::write_atomicallywithout any lock. Branch and path are written together, so they can no longer disagree. TheRepositoryAPI (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.lockunit tests from #4866 are replaced by one that holdsconfig.lockwhile recording, reading and clearing history, and asserts.git/configis byte-identical afterwards. Integration tests that seeded history withgit config worktrunk.historynow useTestRepo::set_switch_previous.Closes #4863