| 1396 | } |
| 1397 | |
| 1398 | function collectContext(state, matchChanges) { |
| 1399 | var changes = [], |
| 1400 | merged = [], |
| 1401 | matchIndex = 0, |
| 1402 | contextChanges = false, |
| 1403 | conflicted = false; |
| 1404 | |
| 1405 | while (matchIndex < matchChanges.length && state.index < state.lines.length) { |
| 1406 | var change = state.lines[state.index], |
| 1407 | match = matchChanges[matchIndex]; // Once we've hit our add, then we are done |
| 1408 | |
| 1409 | if (match[0] === '+') { |
| 1410 | break; |
| 1411 | } |
| 1412 | |
| 1413 | contextChanges = contextChanges || change[0] !== ' '; |
| 1414 | merged.push(match); |
| 1415 | matchIndex++; // Consume any additions in the other block as a conflict to attempt |
| 1416 | // to pull in the remaining context after this |
| 1417 | |
| 1418 | if (change[0] === '+') { |
| 1419 | conflicted = true; |
| 1420 | |
| 1421 | while (change[0] === '+') { |
| 1422 | changes.push(change); |
| 1423 | change = state.lines[++state.index]; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if (match.substr(1) === change.substr(1)) { |
| 1428 | changes.push(change); |
| 1429 | state.index++; |
| 1430 | } else { |
| 1431 | conflicted = true; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | if ((matchChanges[matchIndex] || '')[0] === '+' && contextChanges) { |
| 1436 | conflicted = true; |
| 1437 | } |
| 1438 | |
| 1439 | if (conflicted) { |
| 1440 | return changes; |
| 1441 | } |
| 1442 | |
| 1443 | while (matchIndex < matchChanges.length) { |
| 1444 | merged.push(matchChanges[matchIndex++]); |
| 1445 | } |
| 1446 | |
| 1447 | return { |
| 1448 | merged: merged, |
| 1449 | changes: changes |
| 1450 | }; |
| 1451 | } |
| 1452 | |
| 1453 | function allRemoves(changes) { |
| 1454 | return changes.reduce(function (prev, change) { |