* Computes the number of lines changed in the diff.
( originalFile: string, backupFileName?: string, )
| 677 | * Computes the number of lines changed in the diff. |
| 678 | */ |
| 679 | async function computeDiffStatsForFile( |
| 680 | originalFile: string, |
| 681 | backupFileName?: string, |
| 682 | ): Promise<DiffStats> { |
| 683 | const filesChanged: string[] = [] |
| 684 | let insertions = 0 |
| 685 | let deletions = 0 |
| 686 | try { |
| 687 | const backupPath = backupFileName |
| 688 | ? resolveBackupPath(backupFileName) |
| 689 | : undefined |
| 690 | |
| 691 | const [originalContent, backupContent] = await Promise.all([ |
| 692 | readFileAsyncOrNull(originalFile), |
| 693 | backupPath ? readFileAsyncOrNull(backupPath) : null, |
| 694 | ]) |
| 695 | |
| 696 | if (originalContent === null && backupContent === null) { |
| 697 | return { |
| 698 | filesChanged, |
| 699 | insertions, |
| 700 | deletions, |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | filesChanged.push(originalFile) |
| 705 | |
| 706 | // Compute the diff |
| 707 | const changes = diffLines(originalContent ?? '', backupContent ?? '') |
| 708 | changes.forEach(c => { |
| 709 | if (c.added) { |
| 710 | insertions += c.count || 0 |
| 711 | } |
| 712 | if (c.removed) { |
| 713 | deletions += c.count || 0 |
| 714 | } |
| 715 | }) |
| 716 | } catch (error) { |
| 717 | logError(new Error(`FileHistory: Error generating diffStats: ${error}`)) |
| 718 | } |
| 719 | |
| 720 | return { |
| 721 | filesChanged, |
| 722 | insertions, |
| 723 | deletions, |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | function getBackupFileName(filePath: string, version: number): string { |
| 728 | const fileNameHash = createHash('sha256') |
no test coverage detected