123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- "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
- };
- }
|