Repository navigation
fix(sandbox): stop discard from deleting renamed files with no recovery - #5124
Merged
Merged
Conversation
Discarding a renamed file (git/discard) checked the new path against HEAD, found no entry there (renames land at a new path HEAD never had), and treated it as untracked — unlinking it outright. Since the original path was already gone from the working tree post-rename, this destroyed the file's content entirely with git status left showing a broken RD state.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found by reading
packages/sandbox/daemon/routes/git.tsfor race/edge-case bugs in the sandbox daemon git route.The bug:
discard()ingit.tsdecides whether a requested file is "new" (delete it) or "modified" (restore it from HEAD/index) by checkingreadRefFile(repoDir, "HEAD", fp) === null. For a renamed file,fpis the new path — which never existed at HEAD under that name — so the check always resolves to "new" and the code justunlinkSyncs the working-tree file. Since the original path is already gone from disk (it was renamed away), this deletes the file's content completely, with no way to recover it, and leavesgit statusin a brokenRD(renamed+deleted) state.Repro (before the fix):
The fix: when the requested path corresponds to a rename (has an
origPathin the parsed status), discard now unstages the rename and restores the original file from HEAD at its original path, then deletes the new path — actually undoing the rename instead of just deleting the renamed-to file.Added a regression test (
discard on a renamed file restores the original instead of deleting both) that reproduces the exact repro above and asserts the original file survives with its content intact andgit statusis clean afterward.To verify:
bun test packages/sandbox/daemon/routes/git.test.ts(31 pass). Also ranbunx tsc --noEmitinpackages/sandbox(clean) andbun run fmt. Full CI validates the rest.Summary by cubic
Fixes sandbox git discard to undo renames instead of deleting the renamed file and losing content. Discarding a renamed path now restores the original from HEAD, unstages the rename, deletes the new path, and leaves git status clean.
discard()inpackages/sandbox/daemon/routes/git.tsto detect renames via status, reset the new path, checkout the original fromHEAD, then remove the new path.packages/sandbox/daemon/routes/git.test.tsto ensure the original file is restored and the working tree is clean.Written for commit 02f45cb. Summary will update on new commits.