MCPcopy
hub / github.com/claude-code-best/claude-code / restoreBackup

Function restoreBackup

src/utils/fileHistory.ts:806–839  ·  view source on GitHub ↗

* Restores a file from its backup path with proper directory creation and permissions. * Lazy mkdir: tries copyFile first, creates the directory on ENOENT.

(
  filePath: string,
  backupFileName: string,
)

Source from the content-addressed store, hash-verified

804 * Lazy mkdir: tries copyFile first, creates the directory on ENOENT.
805 */
806async function restoreBackup(
807 filePath: string,
808 backupFileName: string,
809): Promise<void> {
810 const backupPath = resolveBackupPath(backupFileName)
811
812 // Stat first: if the backup is missing, log and bail before attempting
813 // the copy. Separates "backup missing" from "destination dir missing".
814 let backupStats: Stats
815 try {
816 backupStats = await stat(backupPath)
817 } catch (e: unknown) {
818 if (isENOENT(e)) {
819 logEvent('tengu_file_history_rewind_restore_file_failed', {})
820 logError(
821 new Error(`FileHistory: [Rewind] Backup file not found: ${backupPath}`),
822 )
823 return
824 }
825 throw e
826 }
827
828 // Lazy mkdir: 99% of calls hit the fast path (destination dir exists).
829 try {
830 await copyFile(backupPath, filePath)
831 } catch (e: unknown) {
832 if (!isENOENT(e)) throw e
833 await mkdir(dirname(filePath), { recursive: true })
834 await copyFile(backupPath, filePath)
835 }
836
837 // Restore the file permissions
838 await chmod(filePath, backupStats.mode)
839}
840
841/**
842 * Gets the first (earliest) backup version for a file, used when rewinding

Callers 1

applySnapshotFunction · 0.85

Calls 6

resolveBackupPathFunction · 0.85
statFunction · 0.85
isENOENTFunction · 0.85
logEventFunction · 0.85
mkdirFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected