ApplyPatch applies a patch to baseText to produce the target text. Returns an error if the patch cannot be applied cleanly.
(baseText, patchText string)
| 22 | // ApplyPatch applies a patch to baseText to produce the target text. |
| 23 | // Returns an error if the patch cannot be applied cleanly. |
| 24 | func ApplyPatch(baseText, patchText string) (string, error) { |
| 25 | if patchText == "" { |
| 26 | return baseText, nil |
| 27 | } |
| 28 | patches, err := dmp.PatchFromText(patchText) |
| 29 | if err != nil { |
| 30 | return "", fmt.Errorf("parse patch: %w", err) |
| 31 | } |
| 32 | result, applied := dmp.PatchApply(patches, baseText) |
| 33 | // Check that all hunks applied |
| 34 | for i, ok := range applied { |
| 35 | if !ok { |
| 36 | return "", fmt.Errorf("patch hunk %d failed to apply", i) |
| 37 | } |
| 38 | } |
| 39 | return result, nil |
| 40 | } |
| 41 | |
| 42 | // IsDiffSmaller returns true if storing a diff would be meaningfully smaller |
| 43 | // than storing the full content. Returns false for near-complete rewrites |
no outgoing calls