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