2017-05-28 00:38:50 +02:00
|
|
|
/*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(/\r\n|[\n\v\f\r\x85]/),
|
|
|
|
delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [],
|
|
|
|
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 headerPattern = /^(---|\+\+\+)\s+([\S ]*)(?:\t(.*?)\s*)?$/;
|
|
|
|
var fileHeader = headerPattern.exec(diffstr[i]);
|
|
|
|
if (fileHeader) {
|
|
|
|
var keyPrefix = fileHeader[1] === '---' ? 'old' : 'new';
|
2017-08-14 05:01:11 +02:00
|
|
|
var fileName = fileHeader[2].replace(/\\\\/g, '\\');
|
|
|
|
if (/^".*"$/.test(fileName)) {
|
|
|
|
fileName = fileName.substr(1, fileName.length - 2);
|
|
|
|
}
|
|
|
|
index[keyPrefix + 'FileName'] = fileName;
|
2017-05-28 00:38:50 +02:00
|
|
|
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: [],
|
|
|
|
linedelimiters: []
|
|
|
|
};
|
|
|
|
|
|
|
|
var addCount = 0,
|
|
|
|
removeCount = 0;
|
|
|
|
for (; i < diffstr.length; i++) {
|
|
|
|
// Lines starting with '---' could be mistaken for the "remove line" operation
|
|
|
|
// But they could be the header for the next file. Therefore prune such cases out.
|
|
|
|
if (diffstr[i].indexOf('--- ') === 0 && i + 2 < diffstr.length && diffstr[i + 1].indexOf('+++ ') === 0 && diffstr[i + 2].indexOf('@@') === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var operation = diffstr[i][0];
|
|
|
|
|
|
|
|
if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') {
|
|
|
|
hunk.lines.push(diffstr[i]);
|
|
|
|
hunk.linedelimiters.push(delimiters[i] || '\n');
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2Vz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