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