( patch: StructuredPatchHunk[], newFileContent?: string, )
| 47 | * @param newFileContent Optional content string for new files |
| 48 | */ |
| 49 | export function countLinesChanged( |
| 50 | patch: StructuredPatchHunk[], |
| 51 | newFileContent?: string, |
| 52 | ): void { |
| 53 | let numAdditions = 0 |
| 54 | let numRemovals = 0 |
| 55 | |
| 56 | if (patch.length === 0 && newFileContent) { |
| 57 | // For new files, count all lines as additions |
| 58 | numAdditions = newFileContent.split(/\r?\n/).length |
| 59 | } else { |
| 60 | numAdditions = patch.reduce( |
| 61 | (acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('+')), |
| 62 | 0, |
| 63 | ) |
| 64 | numRemovals = patch.reduce( |
| 65 | (acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('-')), |
| 66 | 0, |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | addToTotalLinesChanged(numAdditions, numRemovals) |
| 71 | |
| 72 | getLocCounter()?.add(numAdditions, { type: 'added' }) |
| 73 | getLocCounter()?.add(numRemovals, { type: 'removed' }) |
| 74 | |
| 75 | logEvent('tengu_file_changed', { |
| 76 | lines_added: numAdditions, |
| 77 | lines_removed: numRemovals, |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | export function getPatchFromContents({ |
| 82 | filePath, |
no test coverage detected