Skip to content
Open
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
44 changes: 32 additions & 12 deletions packages/opencode/src/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,41 @@ export namespace Snapshot {
}

export async function restore(snapshot: string) {
log.info("restore", { commit: snapshot })
log.info("restore", { commit: snapshot, cwd: Instance.directory })
const git = gitdir()
const result =
await $`git --git-dir ${git} --work-tree ${Instance.worktree} read-tree ${snapshot} && git --git-dir ${git} --work-tree ${Instance.worktree} checkout-index -a -f`
.quiet()
.cwd(Instance.worktree)
.nothrow()
const prefix = path.relative(Instance.worktree, Instance.directory)
const pathSpec = prefix ? `${prefix}/` : ""

if (result.exitCode !== 0) {
log.error("failed to restore snapshot", {
const listResult = await $`git --git-dir ${git} ls-tree -r --name-only ${snapshot} -- ${pathSpec}`.quiet().nothrow()
if (listResult.exitCode !== 0) {
log.error("failed to list snapshot files", {
snapshot,
exitCode: result.exitCode,
stderr: result.stderr.toString(),
stdout: result.stdout.toString(),
exitCode: listResult.exitCode,
stderr: listResult.stderr.toString(),
})
return
}

const files = listResult.text().trim().split("\n").filter(Boolean)
if (files.length === 0) {
log.info("no files to restore", { snapshot, prefix })
return
}

const readResult = await $`git --git-dir ${git} --work-tree ${Instance.worktree} read-tree ${snapshot}`
.quiet()
.nothrow()
if (readResult.exitCode !== 0) {
log.error("failed to read-tree snapshot", {
snapshot,
exitCode: readResult.exitCode,
stderr: readResult.stderr.toString(),
})
return
}

for (const file of files) {
await $`git --git-dir ${git} --work-tree ${Instance.worktree} checkout-index -f -- ${file}`.quiet().nothrow()
}
}

Expand Down Expand Up @@ -128,7 +148,7 @@ export namespace Snapshot {
const result =
await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff ${hash} -- .`
.quiet()
.cwd(Instance.worktree)
.cwd(Instance.directory)
.nothrow()

if (result.exitCode !== 0) {
Expand Down