(params: {
oldContent: string
diff: string
})
| 499 | } |
| 500 | |
| 501 | function tryApplyPatchWithFallbacks(params: { |
| 502 | oldContent: string |
| 503 | diff: string |
| 504 | }): { |
| 505 | patched: string | null |
| 506 | attemptedStrategies: string[] |
| 507 | lastError?: string |
| 508 | } { |
| 509 | const attempts = buildPatchAttempts(params.oldContent, params.diff) |
| 510 | const attemptedStrategies: string[] = [] |
| 511 | let lastError: string | undefined |
| 512 | |
| 513 | const seen = new Set<string>() |
| 514 | |
| 515 | for (const attempt of attempts) { |
| 516 | const key = JSON.stringify({ |
| 517 | source: attempt.source, |
| 518 | diff: attempt.diff, |
| 519 | }) |
| 520 | |
| 521 | if (seen.has(key)) { |
| 522 | continue |
| 523 | } |
| 524 | |
| 525 | seen.add(key) |
| 526 | attemptedStrategies.push(attempt.name) |
| 527 | |
| 528 | try { |
| 529 | const { result: patched } = applyDiff(attempt.source, attempt.diff, 'default') |
| 530 | |
| 531 | if (patchHasIntendedChanges(attempt.diff) && patched === attempt.source) { |
| 532 | lastError = 'Patch produced no content changes' |
| 533 | continue |
| 534 | } |
| 535 | |
| 536 | return { |
| 537 | patched, |
| 538 | attemptedStrategies, |
| 539 | } |
| 540 | } catch (error) { |
| 541 | lastError = error instanceof Error ? error.message : String(error) |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return { |
| 546 | patched: null, |
| 547 | attemptedStrategies, |
| 548 | ...(lastError ? { lastError } : {}), |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | function formatPatchFailureMessage(params: { |
| 553 | path: string |
no test coverage detected