opencode
opencode copied to clipboard
fix(snapshot): fix revert not restoring files due to absolute path issue
Fixes #8098
Summary
Fix TUI Revert feature not actually reverting file changes in workspace.
Root Cause
Snapshot.patch() returns absolute paths in the files array, but git checkout expects paths relative to the worktree. This caused checkout to fail silently, leaving files unchanged while the session state showed them as "reverted".
Changes
Core Fix (packages/opencode/src/snapshot/index.ts)
- Convert absolute paths to relative using
path.relative(Instance.worktree, file) - Use normalized
gitPathfor all git commands Safety Guards -
Worktree escape protection: Skip files outside worktree using
Filesystem.contains()+path.isAbsolute()check -
Empty path protection: Skip when
relativePath === "" -
Windows compatibility: Normalize path separators (
\→/) for git pathspec -
Directory creation: Add
fs.mkdir()beforefs.writeFile()in git show fallback Tests (packages/opencode/test/snapshot/snapshot.test.ts) Added 13 new test cases: -
patch returns absolute paths and revert handles them correctly -
revert restores modified file content correctly -
revert handles deeply nested files with absolute paths -
revert handles files with spaces in names -
revert handles multiple sequential patches correctly -
revert with patches array containing multiple items -
revert deletes file created after snapshot -
revert restores deleted file -
patch files relative path computation is correct -
revert works when process cwd differs from worktree -
revert skips files outside worktree -
revert skips patch with empty relativePath -
revert creates parent directory when using fallbackTesting - All 52 snapshot tests pass (39 existing + 13 new)
- Typecheck passes