(uniDiff)
| 555 | } |
| 556 | |
| 557 | function parsePatch(uniDiff) { |
| 558 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 559 | var diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/), |
| 560 | delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [], |
| 561 | list = [], |
| 562 | i = 0; |
| 563 | |
| 564 | function parseIndex() { |
| 565 | var index = {}; |
| 566 | list.push(index); // Parse diff metadata |
| 567 | |
| 568 | while (i < diffstr.length) { |
| 569 | var line = diffstr[i]; // File header found, end parsing diff metadata |
| 570 | |
| 571 | if (/^(\-\-\-|\+\+\+|@@)\s/.test(line)) { |
| 572 | break; |
| 573 | } // Diff index |
| 574 | |
| 575 | |
| 576 | var header = /^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/.exec(line); |
| 577 | |
| 578 | if (header) { |
| 579 | index.index = header[1]; |
| 580 | } |
| 581 | |
| 582 | i++; |
| 583 | } // Parse file headers if they are defined. Unified diff requires them, but |
| 584 | // there's no technical issues to have an isolated hunk without file header |
| 585 | |
| 586 | |
| 587 | parseFileHeader(index); |
| 588 | parseFileHeader(index); // Parse hunks |
| 589 | |
| 590 | index.hunks = []; |
| 591 | |
| 592 | while (i < diffstr.length) { |
| 593 | var _line = diffstr[i]; |
| 594 | |
| 595 | if (/^(Index:|diff|\-\-\-|\+\+\+)\s/.test(_line)) { |
| 596 | break; |
| 597 | } else if (/^@@/.test(_line)) { |
| 598 | index.hunks.push(parseHunk()); |
| 599 | } else if (_line && options.strict) { |
| 600 | // Ignore unexpected content unless in strict mode |
| 601 | throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(_line)); |
| 602 | } else { |
| 603 | i++; |
| 604 | } |
| 605 | } |
| 606 | } // Parses the --- and +++ headers, if none are found, no lines |
| 607 | // are consumed. |
| 608 | |
| 609 | |
| 610 | function parseFileHeader(index) { |
| 611 | var fileHeader = /^(---|\+\+\+)\s+(.*)$/.exec(diffstr[i]); |
| 612 | |
| 613 | if (fileHeader) { |
| 614 | var keyPrefix = fileHeader[1] === '---' ? 'old' : 'new'; |
no test coverage detected