( state: FileHistoryState, messageId: UUID, )
| 412 | * if reverting to that snapshot. |
| 413 | */ |
| 414 | export async function fileHistoryGetDiffStats( |
| 415 | state: FileHistoryState, |
| 416 | messageId: UUID, |
| 417 | ): Promise<DiffStats> { |
| 418 | if (!fileHistoryEnabled()) { |
| 419 | return undefined |
| 420 | } |
| 421 | |
| 422 | const targetSnapshot = state.snapshots.findLast( |
| 423 | snapshot => snapshot.messageId === messageId, |
| 424 | ) |
| 425 | |
| 426 | if (!targetSnapshot) { |
| 427 | return undefined |
| 428 | } |
| 429 | |
| 430 | const results = await Promise.all( |
| 431 | Array.from(state.trackedFiles, async trackingPath => { |
| 432 | try { |
| 433 | const filePath = maybeExpandFilePath(trackingPath) |
| 434 | const targetBackup = targetSnapshot.trackedFileBackups[trackingPath] |
| 435 | |
| 436 | const backupFileName: BackupFileName | undefined = targetBackup |
| 437 | ? targetBackup.backupFileName |
| 438 | : getBackupFileNameFirstVersion(trackingPath, state) |
| 439 | |
| 440 | if (backupFileName === undefined) { |
| 441 | // Error resolving the backup, so don't touch the file |
| 442 | logError( |
| 443 | new Error('FileHistory: Error finding the backup file to apply'), |
| 444 | ) |
| 445 | logEvent('tengu_file_history_rewind_restore_file_failed', { |
| 446 | dryRun: true, |
| 447 | }) |
| 448 | return null |
| 449 | } |
| 450 | |
| 451 | const stats = await computeDiffStatsForFile( |
| 452 | filePath, |
| 453 | backupFileName === null ? undefined : backupFileName, |
| 454 | ) |
| 455 | if (stats?.insertions || stats?.deletions) { |
| 456 | return { filePath, stats } |
| 457 | } |
| 458 | if (backupFileName === null && (await pathExists(filePath))) { |
| 459 | // Zero-byte file created after snapshot: counts as changed even |
| 460 | // though diffLines reports 0/0. |
| 461 | return { filePath, stats } |
| 462 | } |
| 463 | return null |
| 464 | } catch (error) { |
| 465 | logError(error) |
| 466 | logEvent('tengu_file_history_rewind_restore_file_failed', { |
| 467 | dryRun: true, |
| 468 | }) |
| 469 | return null |
| 470 | } |
| 471 | }), |
no test coverage detected