( fileAContents: string, fileBContents: string, )
| 360 | * TODO: Unify this with the other snippet logic. |
| 361 | */ |
| 362 | export function getSnippetForTwoFileDiff( |
| 363 | fileAContents: string, |
| 364 | fileBContents: string, |
| 365 | ): string { |
| 366 | const patch = structuredPatch( |
| 367 | 'file.txt', |
| 368 | 'file.txt', |
| 369 | fileAContents, |
| 370 | fileBContents, |
| 371 | undefined, |
| 372 | undefined, |
| 373 | { |
| 374 | context: 8, |
| 375 | timeout: DIFF_TIMEOUT_MS, |
| 376 | }, |
| 377 | ) |
| 378 | |
| 379 | if (!patch) { |
| 380 | return '' |
| 381 | } |
| 382 | |
| 383 | const full = patch.hunks |
| 384 | .map(_ => ({ |
| 385 | startLine: _.oldStart, |
| 386 | content: _.lines |
| 387 | // Filter out deleted lines AND diff metadata lines |
| 388 | .filter(_ => !_.startsWith('-') && !_.startsWith('\\')) |
| 389 | .map(_ => _.slice(1)) |
| 390 | .join('\n'), |
| 391 | })) |
| 392 | .map(addLineNumbers) |
| 393 | .join('\n...\n') |
| 394 | |
| 395 | if (full.length <= DIFF_SNIPPET_MAX_BYTES) { |
| 396 | return full |
| 397 | } |
| 398 | |
| 399 | // Truncate at the last line boundary that fits within the cap. |
| 400 | // Marker format matches BashTool/utils.ts. |
| 401 | const cutoff = full.lastIndexOf('\n', DIFF_SNIPPET_MAX_BYTES) |
| 402 | const kept = |
| 403 | cutoff > 0 ? full.slice(0, cutoff) : full.slice(0, DIFF_SNIPPET_MAX_BYTES) |
| 404 | const remaining = countCharInString(full, '\n', kept.length) + 1 |
| 405 | return `${kept}\n\n... [${remaining} lines truncated] ...` |
| 406 | } |
| 407 | |
| 408 | const CONTEXT_LINES = 4 |
| 409 |
no test coverage detected