| 255 | } |
| 256 | |
| 257 | export async function applyPatch(patch: string, options?: { threeWay?: boolean; reverse?: boolean }) { |
| 258 | if (!patch.trim()) return true |
| 259 | const git = gitdir() |
| 260 | const dir = path.join(Global.Path.data, "tmp") |
| 261 | await fs.mkdir(dir, { recursive: true }).catch(() => {}) |
| 262 | const filename = `snapshot-${Date.now()}-${Math.random().toString(16).slice(2)}.patch` |
| 263 | const filepath = path.join(dir, filename) |
| 264 | await fs.writeFile(filepath, patch) |
| 265 | const applyArgs = [ |
| 266 | "apply", |
| 267 | options?.reverse ? "-R" : null, |
| 268 | options?.threeWay ? "--3way" : null, |
| 269 | "--whitespace=nowarn", |
| 270 | "--unsafe-paths", |
| 271 | filepath, |
| 272 | ].filter(Boolean) |
| 273 | const result = await $`git --git-dir ${git} --work-tree ${Instance.worktree} ${applyArgs}` |
| 274 | .quiet() |
| 275 | .cwd(Instance.worktree) |
| 276 | .nothrow() |
| 277 | await fs.unlink(filepath).catch(() => {}) |
| 278 | if (result.exitCode !== 0) { |
| 279 | log.warn("failed to apply patch", { |
| 280 | exitCode: result.exitCode, |
| 281 | stderr: result.stderr.toString(), |
| 282 | stdout: result.stdout.toString(), |
| 283 | }) |
| 284 | return false |
| 285 | } |
| 286 | return true |
| 287 | } |
| 288 | |
| 289 | export async function diffBetween(from: string, to: string) { |
| 290 | const git = gitdir() |