Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/opencode/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export namespace Session {
partID: z.string().optional(),
snapshot: z.string().optional(),
diff: z.string().optional(),
files: z.string().array().optional(),
})
.optional(),
})
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/session/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export namespace SessionRevert {
if (revert) {
const session = await Session.get(input.sessionID)
revert.snapshot = session.revert?.snapshot ?? (await Snapshot.track())
revert.files = [...new Set(patches.flatMap((p) => p.files))]
await Snapshot.revert(patches)
if (revert.snapshot) revert.diff = await Snapshot.diff(revert.snapshot)
const rangeMessages = all.filter((msg) => msg.info.id >= revert!.messageID)
Expand Down Expand Up @@ -84,7 +85,7 @@ export namespace SessionRevert {
SessionPrompt.assertNotBusy(input.sessionID)
const session = await Session.get(input.sessionID)
if (!session.revert) return session
if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot)
if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot, session.revert.files)
return Session.clearRevert(input.sessionID)
}

Expand Down
33 changes: 31 additions & 2 deletions packages/opencode/src/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,38 @@ export namespace Snapshot {
}
}

export async function restore(snapshot: string) {
log.info("restore", { commit: snapshot })
export async function restore(snapshot: string, files?: string[]) {
log.info("restore", { commit: snapshot, files: files?.length })
const git = gitdir()

if (files?.length) {
const restored = new Set<string>()
for (const file of files) {
if (restored.has(file)) continue
const result =
await $`git -c core.longpaths=true -c core.symlinks=true --git-dir ${git} --work-tree ${Instance.worktree} checkout ${snapshot} -- ${file}`
.quiet()
.cwd(Instance.worktree)
.nothrow()
if (result.exitCode !== 0) {
const relative = path.relative(Instance.worktree, file)
const check =
await $`git -c core.longpaths=true -c core.symlinks=true --git-dir ${git} --work-tree ${Instance.worktree} ls-tree ${snapshot} -- ${relative}`
.quiet()
.cwd(Instance.worktree)
.nothrow()
if (check.exitCode === 0 && check.text().trim()) {
log.info("file existed in snapshot but checkout failed, keeping", { file })
} else {
log.info("file did not exist in snapshot, deleting", { file })
await fs.unlink(file).catch(() => {})
}
}
restored.add(file)
}
return
}

const result =
await $`git -c core.longpaths=true -c core.symlinks=true --git-dir ${git} --work-tree ${Instance.worktree} read-tree ${snapshot} && git -c core.longpaths=true -c core.symlinks=true --git-dir ${git} --work-tree ${Instance.worktree} checkout-index -a -f`
.quiet()
Expand Down
Loading