MCPcopy Index your code
hub / github.com/liuup/claude-code-analysis / copyFileHistoryForResume

Function copyFileHistoryForResume

src/utils/fileHistory.ts:922–1046  ·  view source on GitHub ↗
(log: LogOption)

Source from the content-addressed store, hash-verified

920 * Copy file history snapshots for a given log option.
921 */
922export async function copyFileHistoryForResume(log: LogOption): Promise<void> {
923 if (!fileHistoryEnabled()) {
924 return
925 }
926
927 const fileHistorySnapshots = log.fileHistorySnapshots
928 if (!fileHistorySnapshots || log.messages.length === 0) {
929 return
930 }
931 const lastMessage = log.messages[log.messages.length - 1]
932 const previousSessionId = lastMessage?.sessionId
933 if (!previousSessionId) {
934 logError(
935 new Error(
936 `FileHistory: Failed to copy backups on restore (no previous session id)`,
937 ),
938 )
939 return
940 }
941
942 const sessionId = getSessionId()
943 if (previousSessionId === sessionId) {
944 logForDebugging(
945 `FileHistory: No need to copy file history for resuming with same session id: ${sessionId}`,
946 )
947 return
948 }
949
950 try {
951 // All backups share the same directory: {configDir}/file-history/{sessionId}/
952 // Create it once upfront instead of once per backup file
953 const newBackupDir = join(
954 getClaudeConfigHomeDir(),
955 'file-history',
956 sessionId,
957 )
958 await mkdir(newBackupDir, { recursive: true })
959
960 // Migrate all backup files from the previous session to current session.
961 // Process all snapshots in parallel; within each snapshot, links also run in parallel.
962 let failedSnapshots = 0
963 await Promise.allSettled(
964 fileHistorySnapshots.map(async snapshot => {
965 const backupEntries = Object.values(snapshot.trackedFileBackups).filter(
966 (backup): backup is typeof backup & { backupFileName: string } =>
967 backup.backupFileName !== null,
968 )
969
970 const results = await Promise.allSettled(
971 backupEntries.map(async ({ backupFileName }) => {
972 const oldBackupPath = resolveBackupPath(
973 backupFileName,
974 previousSessionId,
975 )
976 const newBackupPath = join(newBackupDir, backupFileName)
977
978 try {
979 await link(oldBackupPath, newBackupPath)

Callers 2

REPLFunction · 0.85

Calls 10

fileHistoryEnabledFunction · 0.85
getSessionIdFunction · 0.85
logForDebuggingFunction · 0.85
mkdirFunction · 0.85
resolveBackupPathFunction · 0.85
linkFunction · 0.85
getErrnoCodeFunction · 0.85
logEventFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected