MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / ApplyPatch

Function ApplyPatch

internal/diff/diff.go:24–40  ·  view source on GitHub ↗

ApplyPatch applies a patch to baseText to produce the target text. Returns an error if the patch cannot be applied cleanly.

(baseText, patchText string)

Source from the content-addressed store, hash-verified

22// ApplyPatch applies a patch to baseText to produce the target text.
23// Returns an error if the patch cannot be applied cleanly.
24func 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

Callers 6

ListVersionsResolvedMethod · 0.92
TestCreateAndApplyPatchFunction · 0.85
TestReversePatchFunction · 0.85
TestEmptyPatchFunction · 0.85

Calls

no outgoing calls

Tested by 4

TestCreateAndApplyPatchFunction · 0.68
TestReversePatchFunction · 0.68
TestEmptyPatchFunction · 0.68