| 48 | export type Patch = z.infer<typeof Patch> |
| 49 | |
| 50 | export async function patch(hash: string): Promise<Patch> { |
| 51 | const git = gitdir() |
| 52 | await $`git --git-dir ${git} --work-tree ${Instance.worktree} add -A -- .` |
| 53 | .quiet() |
| 54 | .cwd(Instance.worktree) |
| 55 | .nothrow() |
| 56 | const result = |
| 57 | await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff --name-only ${hash} -- .` |
| 58 | .quiet() |
| 59 | .cwd(Instance.directory) |
| 60 | .nothrow() |
| 61 | |
| 62 | // If git diff fails, return empty patch |
| 63 | if (result.exitCode !== 0) { |
| 64 | log.warn("failed to get diff", { hash, exitCode: result.exitCode }) |
| 65 | return { hash, files: [] } |
| 66 | } |
| 67 | |
| 68 | const files = result.text() |
| 69 | return { |
| 70 | hash, |
| 71 | files: files |
| 72 | .trim() |
| 73 | .split("\n") |
| 74 | .map((x) => x.trim()) |
| 75 | .filter(Boolean) |
| 76 | .map((x) => path.join(Instance.worktree, x)), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | export async function restore(snapshot: string, options?: { cleanUntracked?: boolean }) { |
| 81 | log.info("restore", { commit: snapshot }) |