fix: remove git clean and reset --hard from checkpoint restore

- Removed dangerous git clean command that was deleting untracked files
- Replaced git reset --hard with a safer approach that only restores tracked files
- Preserves untracked files during checkpoint restoration to prevent data loss
- Fixes #6209
This commit is contained in:
Roo Code 2025-07-28 15:03:34 +00:00
parent 342ee70fb4
commit ae00f6e0c2

View file

@ -247,8 +247,31 @@ export abstract class ShadowCheckpointService extends EventEmitter {
}
const start = Date.now()
await this.git.clean("f", ["-d", "-f"])
await this.git.reset(["--hard", commitHash])
// Get list of files in the target commit
const filesInCommit = await this.git.raw(["ls-tree", "-r", "--name-only", commitHash])
const files = filesInCommit
.trim()
.split("\n")
.filter((f) => f.length > 0)
if (files.length > 0) {
// If there are files in the commit, checkout only those files
// This restores tracked files without affecting untracked files
await this.git.checkout([commitHash, "--", ...files])
}
// Remove files that exist in working directory but not in the target commit
const currentFiles = await this.git.raw(["ls-files"])
const currentFilesList = currentFiles
.trim()
.split("\n")
.filter((f) => f.length > 0)
const filesToRemove = currentFilesList.filter((f) => !files.includes(f))
if (filesToRemove.length > 0) {
await this.git.rm(filesToRemove)
}
// Remove all checkpoints after the specified commitHash.
const checkpointIndex = this._checkpoints.indexOf(commitHash)