|
@@ -55,7 +55,15 @@ export namespace Snapshot {
|
|
|
export async function patch(hash: string): Promise<Patch> {
|
|
export async function patch(hash: string): Promise<Patch> {
|
|
|
const git = gitdir()
|
|
const git = gitdir()
|
|
|
await $`git --git-dir ${git} add .`.quiet().cwd(Instance.directory).nothrow()
|
|
await $`git --git-dir ${git} add .`.quiet().cwd(Instance.directory).nothrow()
|
|
|
- const files = await $`git --git-dir ${git} diff --name-only ${hash} -- .`.cwd(Instance.directory).text()
|
|
|
|
|
|
|
+ const result = await $`git --git-dir ${git} diff --name-only ${hash} -- .`.quiet().cwd(Instance.directory).nothrow()
|
|
|
|
|
+
|
|
|
|
|
+ // If git diff fails, return empty patch
|
|
|
|
|
+ if (result.exitCode !== 0) {
|
|
|
|
|
+ log.warn("failed to get diff", { hash, exitCode: result.exitCode })
|
|
|
|
|
+ return { hash, files: [] }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const files = result.text()
|
|
|
return {
|
|
return {
|
|
|
hash,
|
|
hash,
|
|
|
files: files
|
|
files: files
|
|
@@ -70,9 +78,19 @@ export namespace Snapshot {
|
|
|
export async function restore(snapshot: string) {
|
|
export async function restore(snapshot: string) {
|
|
|
log.info("restore", { commit: snapshot })
|
|
log.info("restore", { commit: snapshot })
|
|
|
const git = gitdir()
|
|
const git = gitdir()
|
|
|
- await $`git --git-dir=${git} read-tree ${snapshot} && git --git-dir=${git} checkout-index -a -f`
|
|
|
|
|
|
|
+ const result = await $`git --git-dir=${git} read-tree ${snapshot} && git --git-dir=${git} checkout-index -a -f`
|
|
|
.quiet()
|
|
.quiet()
|
|
|
.cwd(Instance.worktree)
|
|
.cwd(Instance.worktree)
|
|
|
|
|
+ .nothrow()
|
|
|
|
|
+
|
|
|
|
|
+ if (result.exitCode !== 0) {
|
|
|
|
|
+ log.error("failed to restore snapshot", {
|
|
|
|
|
+ snapshot,
|
|
|
|
|
+ exitCode: result.exitCode,
|
|
|
|
|
+ stderr: result.stderr.toString(),
|
|
|
|
|
+ stdout: result.stdout.toString(),
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export async function revert(patches: Patch[]) {
|
|
export async function revert(patches: Patch[]) {
|
|
@@ -97,8 +115,19 @@ export namespace Snapshot {
|
|
|
|
|
|
|
|
export async function diff(hash: string) {
|
|
export async function diff(hash: string) {
|
|
|
const git = gitdir()
|
|
const git = gitdir()
|
|
|
- const result = await $`git --git-dir=${git} diff ${hash} -- .`.quiet().cwd(Instance.worktree).text()
|
|
|
|
|
- return result.trim()
|
|
|
|
|
|
|
+ const result = await $`git --git-dir=${git} diff ${hash} -- .`.quiet().cwd(Instance.worktree).nothrow()
|
|
|
|
|
+
|
|
|
|
|
+ if (result.exitCode !== 0) {
|
|
|
|
|
+ log.warn("failed to get diff", {
|
|
|
|
|
+ hash,
|
|
|
|
|
+ exitCode: result.exitCode,
|
|
|
|
|
+ stderr: result.stderr.toString(),
|
|
|
|
|
+ stdout: result.stdout.toString(),
|
|
|
|
|
+ })
|
|
|
|
|
+ return ""
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result.text().trim()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function gitdir() {
|
|
function gitdir() {
|