* Applies the given file snapshot state to the tracked files (writes/deletes * on disk), returning the list of changed file paths. Async IO only.
( state: FileHistoryState, targetSnapshot: FileHistorySnapshot, )
| 535 | * on disk), returning the list of changed file paths. Async IO only. |
| 536 | */ |
| 537 | async function applySnapshot( |
| 538 | state: FileHistoryState, |
| 539 | targetSnapshot: FileHistorySnapshot, |
| 540 | ): Promise<string[]> { |
| 541 | const filesChanged: string[] = [] |
| 542 | for (const trackingPath of state.trackedFiles) { |
| 543 | try { |
| 544 | const filePath = maybeExpandFilePath(trackingPath) |
| 545 | const targetBackup = targetSnapshot.trackedFileBackups[trackingPath] |
| 546 | |
| 547 | const backupFileName: BackupFileName | undefined = targetBackup |
| 548 | ? targetBackup.backupFileName |
| 549 | : getBackupFileNameFirstVersion(trackingPath, state) |
| 550 | |
| 551 | if (backupFileName === undefined) { |
| 552 | // Error resolving the backup, so don't touch the file |
| 553 | logError( |
| 554 | new Error('FileHistory: Error finding the backup file to apply'), |
| 555 | ) |
| 556 | logEvent('tengu_file_history_rewind_restore_file_failed', { |
| 557 | dryRun: false, |
| 558 | }) |
| 559 | continue |
| 560 | } |
| 561 | |
| 562 | if (backupFileName === null) { |
| 563 | // File did not exist at the target version; delete it if present. |
| 564 | try { |
| 565 | await unlink(filePath) |
| 566 | logForDebugging(`FileHistory: [Rewind] Deleted ${filePath}`) |
| 567 | filesChanged.push(filePath) |
| 568 | } catch (e: unknown) { |
| 569 | if (!isENOENT(e)) throw e |
| 570 | // Already absent; nothing to do. |
| 571 | } |
| 572 | continue |
| 573 | } |
| 574 | |
| 575 | // File should exist at a specific version. Restore only if it differs. |
| 576 | if (await checkOriginFileChanged(filePath, backupFileName)) { |
| 577 | await restoreBackup(filePath, backupFileName) |
| 578 | logForDebugging( |
| 579 | `FileHistory: [Rewind] Restored ${filePath} from ${backupFileName}`, |
| 580 | ) |
| 581 | filesChanged.push(filePath) |
| 582 | } |
| 583 | } catch (error) { |
| 584 | logError(error) |
| 585 | logEvent('tengu_file_history_rewind_restore_file_failed', { |
| 586 | dryRun: false, |
| 587 | }) |
| 588 | } |
| 589 | } |
| 590 | return filesChanged |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Checks if the original file has been changed compared to the backup file. |
no test coverage detected