diff options
| author | agarwalravikant <[email protected]> | 2025-08-08 10:08:07 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-08 04:38:07 +0000 |
| commit | 5ab184fcaf40d4e7dec9ba6a0526cac39b602ee2 (patch) | |
| tree | 9534a46370f1197d4c582c652bbc91e201c0d638 /packages/core/src/tools/diffOptions.ts | |
| parent | 86eaa03f8a0c52bbcab2cb8b6554918e30918f66 (diff) | |
Fix for git issue 5657 to add lines of code added/removed telemetry (#5823)
Co-authored-by: Ravikant Agarwal <[email protected]>
Diffstat (limited to 'packages/core/src/tools/diffOptions.ts')
| -rw-r--r-- | packages/core/src/tools/diffOptions.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/packages/core/src/tools/diffOptions.ts b/packages/core/src/tools/diffOptions.ts index 598b46f1..50574226 100644 --- a/packages/core/src/tools/diffOptions.ts +++ b/packages/core/src/tools/diffOptions.ts @@ -5,8 +5,61 @@ */ import * as Diff from 'diff'; +import { DiffStat } from './tools.js'; export const DEFAULT_DIFF_OPTIONS: Diff.PatchOptions = { context: 3, ignoreWhitespace: true, }; + +export function getDiffStat( + fileName: string, + oldStr: string, + aiStr: string, + userStr: string, +): DiffStat { + const countLines = (patch: Diff.ParsedDiff) => { + let added = 0; + let removed = 0; + patch.hunks.forEach((hunk: Diff.Hunk) => { + hunk.lines.forEach((line: string) => { + if (line.startsWith('+')) { + added++; + } else if (line.startsWith('-')) { + removed++; + } + }); + }); + return { added, removed }; + }; + + const patch = Diff.structuredPatch( + fileName, + fileName, + oldStr, + aiStr, + 'Current', + 'Proposed', + DEFAULT_DIFF_OPTIONS, + ); + const { added: aiAddedLines, removed: aiRemovedLines } = countLines(patch); + + const userPatch = Diff.structuredPatch( + fileName, + fileName, + aiStr, + userStr, + 'Proposed', + 'User', + DEFAULT_DIFF_OPTIONS, + ); + const { added: userAddedLines, removed: userRemovedLines } = + countLines(userPatch); + + return { + ai_added_lines: aiAddedLines, + ai_removed_lines: aiRemovedLines, + user_added_lines: userAddedLines, + user_removed_lines: userRemovedLines, + }; +} |
