Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count oldLines and newLines when there are conflicts #188

Merged
merged 5 commits into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions src/patch/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@ import {parsePatch} from './parse';
import {arrayEqual, arrayStartsWith} from '../util/array';

export function calcLineCount(hunk) {
let conflicted = false;
const {oldLines, newLines} = calcOldNewLineCount(hunk.lines);

hunk.oldLines = 0;
hunk.newLines = 0;

hunk.lines.forEach(function(line) {
if (typeof line !== 'string') {
conflicted = true;
return;
}

if (line[0] === '+' || line[0] === ' ') {
hunk.newLines++;
}
if (line[0] === '-' || line[0] === ' ') {
hunk.oldLines++;
}
});

if (conflicted) {
if (oldLines !== undefined) {
hunk.oldLines = oldLines;
} else {
delete hunk.oldLines;
}

if (newLines !== undefined) {
hunk.newLines = newLines;
} else {
delete hunk.newLines;
}
}
Expand Down Expand Up @@ -347,3 +337,40 @@ function skipRemoveSuperset(state, removeChanges, delta) {
state.index += delta;
return true;
}

function calcOldNewLineCount(lines) {
let oldLines = 0;
let newLines = 0;

lines.forEach(function(line) {
if (typeof line !== 'string') {
let myCount = calcOldNewLineCount(line.mine);
let 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, newLines};
}
155 changes: 154 additions & 1 deletion test/patch/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 6,
newStart: 1,
newLines: 3,
lines: [
' line2',
' line3',
Expand Down Expand Up @@ -407,6 +409,7 @@ describe('patch/merge', function() {
conflict: true,
oldStart: 1,
newStart: 1,
newLines: 4,
lines: [
' line2',
' line3',
Expand Down Expand Up @@ -454,6 +457,7 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 3,
newStart: 1,
lines: [
' line2',
Expand Down Expand Up @@ -499,6 +503,7 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 3,
newStart: 1,
lines: [
' line2',
Expand Down Expand Up @@ -538,6 +543,7 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 2,
newStart: 1,
lines: [
' line2',
Expand Down Expand Up @@ -829,6 +835,7 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 4,
newStart: 1,
lines: [
'-line2',
Expand Down Expand Up @@ -936,6 +943,7 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 6,
newStart: 1,
lines: [
'-line2',
Expand Down Expand Up @@ -1003,7 +1011,150 @@ describe('patch/merge', function() {
swapConflicts(expected);
expect(merge(theirs, mine)).to.eql(expected);
});
it('should conflict context mismatch', function() {

it('should handle multiple conflicts in one hunk', function() {
const mine =
'@@ -1,10 +1,10 @@\n'
+ ' line1\n'
+ '-line2\n'
+ '+line2-1\n'
+ ' line3\n'
+ ' line4\n'
+ ' line5\n'
+ '-line6\n'
+ '+line6-1\n'
+ ' line7\n';
const theirs =
'@@ -1,10 +1,10 @@\n'
+ ' line1\n'
+ '-line2\n'
+ '+line2-2\n'
+ ' line3\n'
+ ' line4\n'
+ ' line5\n'
+ '-line6\n'
+ '+line6-2\n'
+ ' line7\n';
const expected = {
hunks: [
{
conflict: true,
oldStart: 1,
oldLines: 7,
newStart: 1,
newLines: 7,
lines: [
' line1',
{
conflict: true,
mine: [
'-line2',
'+line2-1'
],
theirs: [
'-line2',
'+line2-2'
]
},
' line3',
' line4',
' line5',
{
conflict: true,
mine: [
'-line6',
'+line6-1'
],
theirs: [
'-line6',
'+line6-2'
]
},
' line7'
]
}
]
};

expect(merge(mine, theirs)).to.eql(expected);

swapConflicts(expected);
expect(merge(theirs, mine)).to.eql(expected);
});

it('should remove oldLines if base differs', function() {
const mine =
'@@ -1,10 +1,10 @@\n'
+ ' line1\n'
+ '-line2\n'
+ '-line2-0\n'
+ '+line2-1\n'
+ ' line3\n'
+ ' line4\n'
+ ' line5\n'
+ '-line6\n'
+ '+line6-1\n'
+ ' line7\n';
const theirs =
'@@ -1,10 +1,10 @@\n'
+ ' line1\n'
+ '-line2\n'
+ '+line2-2\n'
+ '+line2-3\n'
+ ' line3\n'
+ ' line4\n'
+ ' line5\n'
+ '-line6\n'
+ '+line6-2\n'
+ ' line7\n';
const expected = {
hunks: [
{
conflict: true,
oldStart: 1,
newStart: 1,
lines: [
' line1',
{
conflict: true,
mine: [
'-line2',
'-line2-0',
'+line2-1'
],
theirs: [
'-line2',
'+line2-2',
'+line2-3'
]
},
' line3',
' line4',
' line5',
{
conflict: true,
mine: [
'-line6',
'+line6-1'
],
theirs: [
'-line6',
'+line6-2'
]
},
' line7'
]
}
]
};

expect(merge(mine, theirs)).to.eql(expected);

swapConflicts(expected);
expect(merge(theirs, mine)).to.eql(expected);
});

it('should handle multiple conflict sections', function() {
const mine =
'@@ -1,3 +1,4 @@\n'
+ ' line2\n'
Expand All @@ -1017,7 +1168,9 @@ describe('patch/merge', function() {
{
conflict: true,
oldStart: 1,
oldLines: 2,
newStart: 1,
newLines: 2,
lines: [
{
conflict: true,
Expand Down