(files: FileMap, modifiedFiles: Map<string, string>)
| 15 | type FileModifications = Record<string, ModifiedFile>; |
| 16 | |
| 17 | export function computeFileModifications(files: FileMap, modifiedFiles: Map<string, string>) { |
| 18 | const modifications: FileModifications = {}; |
| 19 | |
| 20 | let hasModifiedFiles = false; |
| 21 | |
| 22 | for (const [filePath, originalContent] of modifiedFiles) { |
| 23 | const file = files[filePath]; |
| 24 | |
| 25 | if (file?.type !== 'file') { |
| 26 | continue; |
| 27 | } |
| 28 | |
| 29 | const unifiedDiff = diffFiles(filePath, originalContent, file.content); |
| 30 | |
| 31 | if (!unifiedDiff) { |
| 32 | // files are identical |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | hasModifiedFiles = true; |
| 37 | |
| 38 | if (unifiedDiff.length > file.content.length) { |
| 39 | // if there are lots of changes we simply grab the current file content since it's smaller than the diff |
| 40 | modifications[filePath] = { type: 'file', content: file.content }; |
| 41 | } else { |
| 42 | // otherwise we use the diff since it's smaller |
| 43 | modifications[filePath] = { type: 'diff', content: unifiedDiff }; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (!hasModifiedFiles) { |
| 48 | return undefined; |
| 49 | } |
| 50 | |
| 51 | return modifications; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Computes a diff in the unified format. The only difference is that the header is omitted |
no test coverage detected