MCPcopy Index your code
hub / github.com/bigcode-project/jupytercoder / applyPatch

Function applyPatch

src/diff.js:756–889  ·  view source on GitHub ↗
(source, uniDiff)

Source from the content-addressed store, hash-verified

754 }
755
756 function applyPatch(source, uniDiff) {
757 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
758
759 if (typeof uniDiff === 'string') {
760 uniDiff = parsePatch(uniDiff);
761 }
762
763 if (Array.isArray(uniDiff)) {
764 if (uniDiff.length > 1) {
765 throw new Error('applyPatch only works with a single input.');
766 }
767
768 uniDiff = uniDiff[0];
769 } // Apply the diff to the input
770
771
772 var lines = source.split(/\r\n|[\n\v\f\r\x85]/),
773 delimiters = source.match(/\r\n|[\n\v\f\r\x85]/g) || [],
774 hunks = uniDiff.hunks,
775 compareLine = options.compareLine || function (lineNumber, line, operation, patchContent) {
776 return line === patchContent;
777 },
778 errorCount = 0,
779 fuzzFactor = options.fuzzFactor || 0,
780 minLine = 0,
781 offset = 0,
782 removeEOFNL,
783 addEOFNL;
784 /**
785 * Checks if the hunk exactly fits on the provided location
786 */
787
788
789 function hunkFits(hunk, toPos) {
790 for (var j = 0; j < hunk.lines.length; j++) {
791 var line = hunk.lines[j],
792 operation = line.length > 0 ? line[0] : ' ',
793 content = line.length > 0 ? line.substr(1) : line;
794
795 if (operation === ' ' || operation === '-') {
796 // Context sanity check
797 if (!compareLine(toPos + 1, lines[toPos], operation, content)) {
798 errorCount++;
799
800 if (errorCount > fuzzFactor) {
801 return false;
802 }
803 }
804
805 toPos++;
806 }
807 }
808
809 return true;
810 } // Search best fit offsets for each hunk based on the previous ones
811
812
813 for (var i = 0; i < hunks.length; i++) {

Callers 1

processIndexFunction · 0.85

Calls 3

parsePatchFunction · 0.85
distanceIteratorFunction · 0.85
hunkFitsFunction · 0.85

Tested by

no test coverage detected