fix(snapshot): reverting in a session can wipe out other files that have changed #7774
+32
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7775
Fixes a bug where running opencode from a subdirectory and clicking "undo revert" would overwrite files in other directories with stale versions from weeks ago.
Problem
When running opencode from a subdirectory (e.g.,
project/frontend/), the unrevert operation would restore stale versions of files in other directories (e.g.,project/backend/). This happened because:git add .from a subdirectory only updates that subdirectory's entries in the indexrestore()usedcheckout-index -awhich restores ALL files in the indexdiff()usedcwd(Instance.worktree)showing diffs for unrelated directoriesSolution
Scope both
restore()anddiff()toInstance.directory:restore()now lists files under the session directory prefix usingls-treeand only restores those specific filesdiff()now usescwd(Instance.directory)so the-- .path spec is relative to the session directorySteps to Reproduce
backend/, make a change, then exit:frontend/, make a change, revert, then unrevert:Testing
Bug History
Traced the origin of this bug through git history:
11d042be2isomorphic-gitwith per-session snapshots284c01018checkout-index -a -ff993541e0The original per-session implementation was safe because each session had its own isolated snapshot directory. The bug was introduced when refactoring to shell git commands (
checkout-index -arestores all files regardless of cwd), and became a real problem when snapshots became shared per-project (stale entries from other sessions persist in the shared index).