Skip to content

Commit ae00f6e

Browse files
committed
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
1 parent 342ee70 commit ae00f6e

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/services/checkpoints/ShadowCheckpointService.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,31 @@ export abstract class ShadowCheckpointService extends EventEmitter {
247247
}
248248

249249
const start = Date.now()
250-
await this.git.clean("f", ["-d", "-f"])
251-
await this.git.reset(["--hard", commitHash])
250+
251+
// Get list of files in the target commit
252+
const filesInCommit = await this.git.raw(["ls-tree", "-r", "--name-only", commitHash])
253+
const files = filesInCommit
254+
.trim()
255+
.split("\n")
256+
.filter((f) => f.length > 0)
257+
258+
if (files.length > 0) {
259+
// If there are files in the commit, checkout only those files
260+
// This restores tracked files without affecting untracked files
261+
await this.git.checkout([commitHash, "--", ...files])
262+
}
263+
264+
// Remove files that exist in working directory but not in the target commit
265+
const currentFiles = await this.git.raw(["ls-files"])
266+
const currentFilesList = currentFiles
267+
.trim()
268+
.split("\n")
269+
.filter((f) => f.length > 0)
270+
const filesToRemove = currentFilesList.filter((f) => !files.includes(f))
271+
272+
if (filesToRemove.length > 0) {
273+
await this.git.rm(filesToRemove)
274+
}
252275

253276
// Remove all checkpoints after the specified commitHash.
254277
const checkpointIndex = this._checkpoints.indexOf(commitHash)

0 commit comments

Comments
 (0)