|
-
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.calcLineCount = calcLineCount;
- exports.merge = merge;
-
-
- var
-
- _create = require("./create")
-
- ;
-
- var
-
- _parse = require("./parse")
-
- ;
-
- var
-
- _array = require("../util/array")
-
- ;
-
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
-
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
-
- function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
-
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
-
-
- function calcLineCount(hunk) {
-
- var _calcOldNewLineCount =
-
- calcOldNewLineCount(hunk.lines),
- oldLines = _calcOldNewLineCount.oldLines,
- newLines = _calcOldNewLineCount.newLines;
-
- if (oldLines !== undefined) {
- hunk.oldLines = oldLines;
- } else {
- delete hunk.oldLines;
- }
-
- if (newLines !== undefined) {
- hunk.newLines = newLines;
- } else {
- delete hunk.newLines;
- }
- }
-
- function merge(mine, theirs, base) {
- mine = loadPatch(mine, base);
- theirs = loadPatch(theirs, base);
- var ret = {};
-
-
-
- if (mine.index || theirs.index) {
- ret.index = mine.index || theirs.index;
- }
-
- if (mine.newFileName || theirs.newFileName) {
- if (!fileNameChanged(mine)) {
-
- ret.oldFileName = theirs.oldFileName || mine.oldFileName;
- ret.newFileName = theirs.newFileName || mine.newFileName;
- ret.oldHeader = theirs.oldHeader || mine.oldHeader;
- ret.newHeader = theirs.newHeader || mine.newHeader;
- } else if (!fileNameChanged(theirs)) {
-
- ret.oldFileName = mine.oldFileName;
- ret.newFileName = mine.newFileName;
- ret.oldHeader = mine.oldHeader;
- ret.newHeader = mine.newHeader;
- } else {
-
- ret.oldFileName = selectField(ret, mine.oldFileName, theirs.oldFileName);
- ret.newFileName = selectField(ret, mine.newFileName, theirs.newFileName);
- ret.oldHeader = selectField(ret, mine.oldHeader, theirs.oldHeader);
- ret.newHeader = selectField(ret, mine.newHeader, theirs.newHeader);
- }
- }
-
- ret.hunks = [];
- var mineIndex = 0,
- theirsIndex = 0,
- mineOffset = 0,
- theirsOffset = 0;
-
- while (mineIndex < mine.hunks.length || theirsIndex < theirs.hunks.length) {
- var mineCurrent = mine.hunks[mineIndex] || {
- oldStart: Infinity
- },
- theirsCurrent = theirs.hunks[theirsIndex] || {
- oldStart: Infinity
- };
-
- if (hunkBefore(mineCurrent, theirsCurrent)) {
-
- ret.hunks.push(cloneHunk(mineCurrent, mineOffset));
- mineIndex++;
- theirsOffset += mineCurrent.newLines - mineCurrent.oldLines;
- } else if (hunkBefore(theirsCurrent, mineCurrent)) {
-
- ret.hunks.push(cloneHunk(theirsCurrent, theirsOffset));
- theirsIndex++;
- mineOffset += theirsCurrent.newLines - theirsCurrent.oldLines;
- } else {
-
- var mergedHunk = {
- oldStart: Math.min(mineCurrent.oldStart, theirsCurrent.oldStart),
- oldLines: 0,
- newStart: Math.min(mineCurrent.newStart + mineOffset, theirsCurrent.oldStart + theirsOffset),
- newLines: 0,
- lines: []
- };
- mergeLines(mergedHunk, mineCurrent.oldStart, mineCurrent.lines, theirsCurrent.oldStart, theirsCurrent.lines);
- theirsIndex++;
- mineIndex++;
- ret.hunks.push(mergedHunk);
- }
- }
-
- return ret;
- }
-
- function loadPatch(param, base) {
- if (typeof param === 'string') {
- if (/^@@/m.test(param) || /^Index:/m.test(param)) {
- return (
-
- (0,
-
-
-
- _parse
-
- .
-
- parsePatch)
-
- (param)[0]
- );
- }
-
- if (!base) {
- throw new Error('Must provide a base reference or pass in a patch');
- }
-
- return (
-
- (0,
-
-
-
- _create
-
- .
-
- structuredPatch)
-
- (undefined, undefined, base, param)
- );
- }
-
- return param;
- }
-
- function fileNameChanged(patch) {
- return patch.newFileName && patch.newFileName !== patch.oldFileName;
- }
-
- function selectField(index, mine, theirs) {
- if (mine === theirs) {
- return mine;
- } else {
- index.conflict = true;
- return {
- mine: mine,
- theirs: theirs
- };
- }
- }
-
- function hunkBefore(test, check) {
- return test.oldStart < check.oldStart && test.oldStart + test.oldLines < check.oldStart;
- }
-
- function cloneHunk(hunk, offset) {
- return {
- oldStart: hunk.oldStart,
- oldLines: hunk.oldLines,
- newStart: hunk.newStart + offset,
- newLines: hunk.newLines,
- lines: hunk.lines
- };
- }
-
- function mergeLines(hunk, mineOffset, mineLines, theirOffset, theirLines) {
-
-
- var mine = {
- offset: mineOffset,
- lines: mineLines,
- index: 0
- },
- their = {
- offset: theirOffset,
- lines: theirLines,
- index: 0
- };
-
- insertLeading(hunk, mine, their);
- insertLeading(hunk, their, mine);
-
- while (mine.index < mine.lines.length && their.index < their.lines.length) {
- var mineCurrent = mine.lines[mine.index],
- theirCurrent = their.lines[their.index];
-
- if ((mineCurrent[0] === '-' || mineCurrent[0] === '+') && (theirCurrent[0] === '-' || theirCurrent[0] === '+')) {
-
- mutualChange(hunk, mine, their);
- } else if (mineCurrent[0] === '+' && theirCurrent[0] === ' ') {
-
- var _hunk$lines;
-
-
-
-
-
- (_hunk$lines =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines
-
- ,
-
- _toConsumableArray(
-
- collectChange(mine)));
- } else if (theirCurrent[0] === '+' && mineCurrent[0] === ' ') {
-
- var _hunk$lines2;
-
-
-
-
-
- (_hunk$lines2 =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines2
-
- ,
-
- _toConsumableArray(
-
- collectChange(their)));
- } else if (mineCurrent[0] === '-' && theirCurrent[0] === ' ') {
-
- removal(hunk, mine, their);
- } else if (theirCurrent[0] === '-' && mineCurrent[0] === ' ') {
-
- removal(hunk, their, mine, true);
- } else if (mineCurrent === theirCurrent) {
-
- hunk.lines.push(mineCurrent);
- mine.index++;
- their.index++;
- } else {
-
- conflict(hunk, collectChange(mine), collectChange(their));
- }
- }
-
-
- insertTrailing(hunk, mine);
- insertTrailing(hunk, their);
- calcLineCount(hunk);
- }
-
- function mutualChange(hunk, mine, their) {
- var myChanges = collectChange(mine),
- theirChanges = collectChange(their);
-
- if (allRemoves(myChanges) && allRemoves(theirChanges)) {
-
- if (
-
- (0,
-
-
-
- _array
-
- .
-
- arrayStartsWith)
-
- (myChanges, theirChanges) && skipRemoveSuperset(their, myChanges, myChanges.length - theirChanges.length)) {
-
- var _hunk$lines3;
-
-
-
-
- (_hunk$lines3 =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines3
-
- ,
-
- _toConsumableArray(
-
- myChanges));
-
- return;
- } else if (
-
- (0,
-
-
-
- _array
-
- .
-
- arrayStartsWith)
-
- (theirChanges, myChanges) && skipRemoveSuperset(mine, theirChanges, theirChanges.length - myChanges.length)) {
-
- var _hunk$lines4;
-
-
-
-
- (_hunk$lines4 =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines4
-
- ,
-
- _toConsumableArray(
-
- theirChanges));
-
- return;
- }
- } else if (
-
- (0,
-
-
-
- _array
-
- .
-
- arrayEqual)
-
- (myChanges, theirChanges)) {
-
- var _hunk$lines5;
-
-
-
-
- (_hunk$lines5 =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines5
-
- ,
-
- _toConsumableArray(
-
- myChanges));
-
- return;
- }
-
- conflict(hunk, myChanges, theirChanges);
- }
-
- function removal(hunk, mine, their, swap) {
- var myChanges = collectChange(mine),
- theirChanges = collectContext(their, myChanges);
-
- if (theirChanges.merged) {
-
- var _hunk$lines6;
-
-
-
-
- (_hunk$lines6 =
-
- hunk.lines).push.
-
- apply
-
- (
-
- _hunk$lines6
-
- ,
-
- _toConsumableArray(
-
- theirChanges.merged));
- } else {
- conflict(hunk, swap ? theirChanges : myChanges, swap ? myChanges : theirChanges);
- }
- }
-
- function conflict(hunk, mine, their) {
- hunk.conflict = true;
- hunk.lines.push({
- conflict: true,
- mine: mine,
- theirs: their
- });
- }
-
- function insertLeading(hunk, insert, their) {
- while (insert.offset < their.offset && insert.index < insert.lines.length) {
- var line = insert.lines[insert.index++];
- hunk.lines.push(line);
- insert.offset++;
- }
- }
-
- function insertTrailing(hunk, insert) {
- while (insert.index < insert.lines.length) {
- var line = insert.lines[insert.index++];
- hunk.lines.push(line);
- }
- }
-
- function collectChange(state) {
- var ret = [],
- operation = state.lines[state.index][0];
-
- while (state.index < state.lines.length) {
- var line = state.lines[state.index];
-
- if (operation === '-' && line[0] === '+') {
- operation = '+';
- }
-
- if (operation === line[0]) {
- ret.push(line);
- state.index++;
- } else {
- break;
- }
- }
-
- return ret;
- }
-
- function collectContext(state, matchChanges) {
- var changes = [],
- merged = [],
- matchIndex = 0,
- contextChanges = false,
- conflicted = false;
-
- while (matchIndex < matchChanges.length && state.index < state.lines.length) {
- var change = state.lines[state.index],
- match = matchChanges[matchIndex];
-
- if (match[0] === '+') {
- break;
- }
-
- contextChanges = contextChanges || change[0] !== ' ';
- merged.push(match);
- matchIndex++;
-
-
- if (change[0] === '+') {
- conflicted = true;
-
- while (change[0] === '+') {
- changes.push(change);
- change = state.lines[++state.index];
- }
- }
-
- if (match.substr(1) === change.substr(1)) {
- changes.push(change);
- state.index++;
- } else {
- conflicted = true;
- }
- }
-
- if ((matchChanges[matchIndex] || '')[0] === '+' && contextChanges) {
- conflicted = true;
- }
-
- if (conflicted) {
- return changes;
- }
-
- while (matchIndex < matchChanges.length) {
- merged.push(matchChanges[matchIndex++]);
- }
-
- return {
- merged: merged,
- changes: changes
- };
- }
-
- function allRemoves(changes) {
- return changes.reduce(function (prev, change) {
- return prev && change[0] === '-';
- }, true);
- }
-
- function skipRemoveSuperset(state, removeChanges, delta) {
- for (var i = 0; i < delta; i++) {
- var changeContent = removeChanges[removeChanges.length - delta + i].substr(1);
-
- if (state.lines[state.index + i] !== ' ' + changeContent) {
- return false;
- }
- }
-
- state.index += delta;
- return true;
- }
-
- function calcOldNewLineCount(lines) {
- var oldLines = 0;
- var newLines = 0;
- lines.forEach(function (line) {
- if (typeof line !== 'string') {
- var myCount = calcOldNewLineCount(line.mine);
- var theirCount = calcOldNewLineCount(line.theirs);
-
- if (oldLines !== undefined) {
- if (myCount.oldLines === theirCount.oldLines) {
- oldLines += myCount.oldLines;
- } else {
- oldLines = undefined;
- }
- }
-
- if (newLines !== undefined) {
- if (myCount.newLines === theirCount.newLines) {
- newLines += myCount.newLines;
- } else {
- newLines = undefined;
- }
- }
- } else {
- if (newLines !== undefined && (line[0] === '+' || line[0] === ' ')) {
- newLines++;
- }
-
- if (oldLines !== undefined && (line[0] === '-' || line[0] === ' ')) {
- oldLines++;
- }
- }
- });
- return {
- oldLines: oldLines,
- newLines: newLines
- };
- }
|