|
|
@@ -301,28 +301,113 @@ export namespace Snapshot {
|
|
|
const revert = Effect.fnUntraced(function* (patches: Snapshot.Patch[]) {
|
|
|
return yield* locked(
|
|
|
Effect.gen(function* () {
|
|
|
+ const ops: { hash: string; file: string; rel: string }[] = []
|
|
|
const seen = new Set<string>()
|
|
|
for (const item of patches) {
|
|
|
for (const file of item.files) {
|
|
|
if (seen.has(file)) continue
|
|
|
seen.add(file)
|
|
|
- log.info("reverting", { file, hash: item.hash })
|
|
|
- const result = yield* git([...core, ...args(["checkout", item.hash, "--", file])], {
|
|
|
+ ops.push({
|
|
|
+ hash: item.hash,
|
|
|
+ file,
|
|
|
+ rel: path.relative(state.worktree, file).replaceAll("\\", "/"),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const single = Effect.fnUntraced(function* (op: (typeof ops)[number]) {
|
|
|
+ log.info("reverting", { file: op.file, hash: op.hash })
|
|
|
+ const result = yield* git([...core, ...args(["checkout", op.hash, "--", op.file])], {
|
|
|
+ cwd: state.worktree,
|
|
|
+ })
|
|
|
+ if (result.code === 0) return
|
|
|
+ const tree = yield* git([...core, ...args(["ls-tree", op.hash, "--", op.rel])], {
|
|
|
+ cwd: state.worktree,
|
|
|
+ })
|
|
|
+ if (tree.code === 0 && tree.text.trim()) {
|
|
|
+ log.info("file existed in snapshot but checkout failed, keeping", { file: op.file, hash: op.hash })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ log.info("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
|
|
+ yield* remove(op.file)
|
|
|
+ })
|
|
|
+
|
|
|
+ const clash = (a: string, b: string) => a === b || a.startsWith(`${b}/`) || b.startsWith(`${a}/`)
|
|
|
+
|
|
|
+ for (let i = 0; i < ops.length; ) {
|
|
|
+ const first = ops[i]!
|
|
|
+ const run = [first]
|
|
|
+ let j = i + 1
|
|
|
+ // Only batch adjacent files when their paths cannot affect each other.
|
|
|
+ while (j < ops.length && run.length < 100) {
|
|
|
+ const next = ops[j]!
|
|
|
+ if (next.hash !== first.hash) break
|
|
|
+ if (run.some((item) => clash(item.rel, next.rel))) break
|
|
|
+ run.push(next)
|
|
|
+ j += 1
|
|
|
+ }
|
|
|
+
|
|
|
+ if (run.length === 1) {
|
|
|
+ yield* single(first)
|
|
|
+ i = j
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ const tree = yield* git(
|
|
|
+ [...core, ...args(["ls-tree", "--name-only", first.hash, "--", ...run.map((item) => item.rel)])],
|
|
|
+ {
|
|
|
cwd: state.worktree,
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ if (tree.code !== 0) {
|
|
|
+ log.info("batched ls-tree failed, falling back to single-file revert", {
|
|
|
+ hash: first.hash,
|
|
|
+ files: run.length,
|
|
|
})
|
|
|
- if (result.code !== 0) {
|
|
|
- const rel = path.relative(state.worktree, file)
|
|
|
- const tree = yield* git([...core, ...args(["ls-tree", item.hash, "--", rel])], {
|
|
|
+ for (const op of run) {
|
|
|
+ yield* single(op)
|
|
|
+ }
|
|
|
+ i = j
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ const have = new Set(
|
|
|
+ tree.text
|
|
|
+ .trim()
|
|
|
+ .split("\n")
|
|
|
+ .map((item) => item.trim())
|
|
|
+ .filter(Boolean),
|
|
|
+ )
|
|
|
+ const list = run.filter((item) => have.has(item.rel))
|
|
|
+ if (list.length) {
|
|
|
+ log.info("reverting", { hash: first.hash, files: list.length })
|
|
|
+ const result = yield* git(
|
|
|
+ [...core, ...args(["checkout", first.hash, "--", ...list.map((item) => item.file)])],
|
|
|
+ {
|
|
|
cwd: state.worktree,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ if (result.code !== 0) {
|
|
|
+ log.info("batched checkout failed, falling back to single-file revert", {
|
|
|
+ hash: first.hash,
|
|
|
+ files: list.length,
|
|
|
})
|
|
|
- if (tree.code === 0 && tree.text.trim()) {
|
|
|
- log.info("file existed in snapshot but checkout failed, keeping", { file })
|
|
|
- } else {
|
|
|
- log.info("file did not exist in snapshot, deleting", { file })
|
|
|
- yield* remove(file)
|
|
|
+ for (const op of run) {
|
|
|
+ yield* single(op)
|
|
|
}
|
|
|
+ i = j
|
|
|
+ continue
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ for (const op of run) {
|
|
|
+ if (have.has(op.rel)) continue
|
|
|
+ log.info("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
|
|
+ yield* remove(op.file)
|
|
|
+ }
|
|
|
+
|
|
|
+ i = j
|
|
|
}
|
|
|
}),
|
|
|
)
|