[PR #8631] fix(snapshot): fix revert not restoring files due to absolute path issue #12815

Open
opened 2026-02-16 18:17:42 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8631

State: open
Merged: No


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 gitPath for 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() before fs.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 fallback
    Testing
  • All 52 snapshot tests pass (39 existing + 13 new)
  • Typecheck passes
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8631 **State:** open **Merged:** No --- 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 `gitPath` for 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()` before `fs.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 fallback` Testing - All 52 snapshot tests pass (39 existing + 13 new) - Typecheck passes
yindo added the pull-request label 2026-02-16 18:17:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12815