From d9ef8e1252e290a4710e1ce022224bc177a10801 Mon Sep 17 00:00:00 2001 From: Admin Date: Wed, 21 Jan 2026 07:00:15 +0800 Subject: [PATCH] fix(snapshot): handle git add failures with --ignore-errors fallback When git add fails (e.g., due to Windows reserved filenames like 'nul'), the snapshot initialization silently returns empty tree hash, making undo/revert completely non-functional. This fix: - Checks git add exit code and logs warnings on failure - Retries with --ignore-errors to skip problematic files - Allows snapshot tracking to work even with some problematic files Fixes issue where undo command only reverts conversation but not code changes. --- packages/opencode/src/snapshot/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index 46c97cf8dfd..16aca634ded 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -65,7 +65,22 @@ export namespace Snapshot { await $`git --git-dir ${git} config core.autocrlf false`.quiet().nothrow() log.info("initialized") } - await $`git --git-dir ${git} --work-tree ${Instance.worktree} add .`.quiet().cwd(Instance.directory).nothrow() + // Try to add all files, with fallback for problematic files (e.g., Windows reserved names like 'nul') + const addResult = await $`git --git-dir ${git} --work-tree ${Instance.worktree} add .` + .quiet() + .cwd(Instance.directory) + .nothrow() + if (addResult.exitCode !== 0) { + log.warn("git add failed, retrying with --ignore-errors", { + exitCode: addResult.exitCode, + stderr: addResult.stderr.toString(), + }) + // Retry with --ignore-errors to skip problematic files and continue + await $`git --git-dir ${git} --work-tree ${Instance.worktree} add --ignore-errors .` + .quiet() + .cwd(Instance.directory) + .nothrow() + } const hash = await $`git --git-dir ${git} --work-tree ${Instance.worktree} write-tree` .quiet() .cwd(Instance.directory)