opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(snapshot): fix revert not restoring files due to absolute path issue

Open Twisted928 opened this issue 1 day ago • 2 comments

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

Twisted928 avatar Jan 15 '26 08:01 Twisted928