135 lines
12 KiB
JavaScript
135 lines
12 KiB
JavaScript
![]() |
/*istanbul ignore start*/'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports. /*istanbul ignore end*/parsePatch = parsePatch;
|
||
|
function parsePatch(uniDiff) {
|
||
|
/*istanbul ignore start*/var /*istanbul ignore end*/options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||
|
|
||
|
var diffstr = uniDiff.split('\n'),
|
||
|
list = [],
|
||
|
i = 0;
|
||
|
|
||
|
function parseIndex() {
|
||
|
var index = {};
|
||
|
list.push(index);
|
||
|
|
||
|
// Parse diff metadata
|
||
|
while (i < diffstr.length) {
|
||
|
var line = diffstr[i];
|
||
|
|
||
|
// File header found, end parsing diff metadata
|
||
|
if (/^(\-\-\-|\+\+\+|@@)\s/.test(line)) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// Diff index
|
||
|
var header = /^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/.exec(line);
|
||
|
if (header) {
|
||
|
index.index = header[1];
|
||
|
}
|
||
|
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
// Parse file headers if they are defined. Unified diff requires them, but
|
||
|
// there's no technical issues to have an isolated hunk without file header
|
||
|
parseFileHeader(index);
|
||
|
parseFileHeader(index);
|
||
|
|
||
|
// Parse hunks
|
||
|
index.hunks = [];
|
||
|
|
||
|
while (i < diffstr.length) {
|
||
|
var _line = diffstr[i];
|
||
|
|
||
|
if (/^(Index:|diff|\-\-\-|\+\+\+)\s/.test(_line)) {
|
||
|
break;
|
||
|
} else if (/^@@/.test(_line)) {
|
||
|
index.hunks.push(parseHunk());
|
||
|
} else if (_line && options.strict) {
|
||
|
// Ignore unexpected content unless in strict mode
|
||
|
throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(_line));
|
||
|
} else {
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Parses the --- and +++ headers, if none are found, no lines
|
||
|
// are consumed.
|
||
|
function parseFileHeader(index) {
|
||
|
var fileHeader = /^(\-\-\-|\+\+\+)\s+(\S*)\s?(.*?)\s*$/.exec(diffstr[i]);
|
||
|
if (fileHeader) {
|
||
|
var keyPrefix = fileHeader[1] === '---' ? 'old' : 'new';
|
||
|
index[keyPrefix + 'FileName'] = fileHeader[2];
|
||
|
index[keyPrefix + 'Header'] = fileHeader[3];
|
||
|
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Parses a hunk
|
||
|
// This assumes that we are at the start of a hunk.
|
||
|
function parseHunk() {
|
||
|
var chunkHeaderIndex = i,
|
||
|
chunkHeaderLine = diffstr[i++],
|
||
|
chunkHeader = chunkHeaderLine.split(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
|
||
|
|
||
|
var hunk = {
|
||
|
oldStart: +chunkHeader[1],
|
||
|
oldLines: +chunkHeader[2] || 1,
|
||
|
newStart: +chunkHeader[3],
|
||
|
newLines: +chunkHeader[4] || 1,
|
||
|
lines: []
|
||
|
};
|
||
|
|
||
|
var addCount = 0,
|
||
|
removeCount = 0;
|
||
|
for (; i < diffstr.length; i++) {
|
||
|
var operation = diffstr[i][0];
|
||
|
|
||
|
if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') {
|
||
|
hunk.lines.push(diffstr[i]);
|
||
|
|
||
|
if (operation === '+') {
|
||
|
addCount++;
|
||
|
} else if (operation === '-') {
|
||
|
removeCount++;
|
||
|
} else if (operation === ' ') {
|
||
|
addCount++;
|
||
|
removeCount++;
|
||
|
}
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handle the empty block count case
|
||
|
if (!addCount && hunk.newLines === 1) {
|
||
|
hunk.newLines = 0;
|
||
|
}
|
||
|
if (!removeCount && hunk.oldLines === 1) {
|
||
|
hunk.oldLines = 0;
|
||
|
}
|
||
|
|
||
|
// Perform optional sanity checking
|
||
|
if (options.strict) {
|
||
|
if (addCount !== hunk.newLines) {
|
||
|
throw new Error('Added line count did not match for hunk at line ' + (chunkHeaderIndex + 1));
|
||
|
}
|
||
|
if (removeCount !== hunk.oldLines) {
|
||
|
throw new Error('Removed line count did not match for hunk at line ' + (chunkHeaderIndex + 1));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return hunk;
|
||
|
}
|
||
|
|
||
|
while (i < diffstr.length) {
|
||
|
parseIndex();
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9wYXJzZS5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Z0NBQWdCO0FBQVQsU0FBUyxVQUFULENBQW9CLE9BQXBCLEVBQTJDO3NEQUFkLGdFQUFVLGtCQUFJOztBQUNoRCxNQUFJLFVBQVUsUUFBUSxLQUFSLENBQWMsSUFBZCxDQUFWO01BQ0EsT0FBTyxFQUFQO01BQ0EsSUFBSSxDQUFKLENBSDRDOztBQUtoRCxXQUFTLFVBQVQsR0FBc0I7QUFDcEIsUUFBSSxRQUFRLEVBQVIsQ0FEZ0I7QUFFcEIsU0FBSyxJQUFMLENBQVUsS0FBVjs7O0FBRm9CLFdBS2IsSUFBSSxRQUFRLE1BQVIsRUFBZ0I7QUFDekIsVUFBSSxPQUFPLFFBQVEsQ0FBUixDQUFQOzs7QUFEcUIsVUFJckIsd0JBQXdCLElBQXhCLENBQTZCLElBQTdCLENBQUosRUFBd0M7QUFDdEMsY0FEc0M7T0FBeEM7OztBQUp5QixVQVNyQixTQUFTLDJDQUE2QyxJQUE3QyxDQUFrRCxJQUFsRCxDQUFULENBVHFCO0FBVXpCLFVBQUksTUFBSixFQUFZO0FBQ1YsY0FBTSxLQUFOLEdBQWMsT0FBTyxDQUFQLENBQWQsQ0FEVTtPQUFaOztBQUlBLFVBZHlCO0tBQTNCOzs7O0FBTG9CLG1CQXdCcEI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