MCPcopy Create free account
hub / github.com/EmNudge/watlings / patch

Function patch

scripts/utils/patch.mjs:46–64  ·  view source on GitHub ↗
(patchString, targetString)

Source from the content-addressed store, hash-verified

44 * @param {string} targetString
45 */
46export function patch(patchString, targetString) {
47 // Fix Windows inserting carriage returns
48 if (process.platform === 'win32') {
49 targetString = targetString.replace(/\r/g, '');
50 patchString = patchString.replace(/\r/g, '');
51 }
52
53 const targetArr = targetString.split('\n');
54
55 for (const { delLines, addLines, deleteCount, start } of parsePatch(patchString)) {
56 if (delLines && !targetString.includes(delLines.slice(1).replace(/< /g, ''))) {
57 throw new Error('Patch contains deletion that does not exist in target');
58 }
59
60 targetArr.splice(start - 1, deleteCount, ...addLines);
61 }
62
63 return targetArr.join('\n');
64}

Callers 1

patchFileFunction · 0.90

Calls 1

parsePatchFunction · 0.85

Tested by

no test coverage detected