parse.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Parses a patch into structured data, in the same structure returned by `structuredPatch`.
  3. *
  4. * @return a JSON object representation of the a patch, suitable for use with the `applyPatch` method.
  5. */
  6. export function parsePatch(uniDiff) {
  7. const diffstr = uniDiff.split(/\n/), list = [];
  8. let i = 0;
  9. function parseIndex() {
  10. const index = {};
  11. list.push(index);
  12. // Parse diff metadata
  13. while (i < diffstr.length) {
  14. const line = diffstr[i];
  15. // File header found, end parsing diff metadata
  16. if ((/^(---|\+\+\+|@@)\s/).test(line)) {
  17. break;
  18. }
  19. // Diff index
  20. const header = (/^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/).exec(line);
  21. if (header) {
  22. index.index = header[1];
  23. }
  24. i++;
  25. }
  26. // Parse file headers if they are defined. Unified diff requires them, but
  27. // there's no technical issues to have an isolated hunk without file header
  28. parseFileHeader(index);
  29. parseFileHeader(index);
  30. // Parse hunks
  31. index.hunks = [];
  32. while (i < diffstr.length) {
  33. const line = diffstr[i];
  34. if ((/^(Index:\s|diff\s|---\s|\+\+\+\s|===================================================================)/).test(line)) {
  35. break;
  36. }
  37. else if ((/^@@/).test(line)) {
  38. index.hunks.push(parseHunk());
  39. }
  40. else if (line) {
  41. throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(line));
  42. }
  43. else {
  44. i++;
  45. }
  46. }
  47. }
  48. // Parses the --- and +++ headers, if none are found, no lines
  49. // are consumed.
  50. function parseFileHeader(index) {
  51. const fileHeader = (/^(---|\+\+\+)\s+(.*)\r?$/).exec(diffstr[i]);
  52. if (fileHeader) {
  53. const data = fileHeader[2].split('\t', 2), header = (data[1] || '').trim();
  54. let fileName = data[0].replace(/\\\\/g, '\\');
  55. if ((/^".*"$/).test(fileName)) {
  56. fileName = fileName.substr(1, fileName.length - 2);
  57. }
  58. if (fileHeader[1] === '---') {
  59. index.oldFileName = fileName;
  60. index.oldHeader = header;
  61. }
  62. else {
  63. index.newFileName = fileName;
  64. index.newHeader = header;
  65. }
  66. i++;
  67. }
  68. }
  69. // Parses a hunk
  70. // This assumes that we are at the start of a hunk.
  71. function parseHunk() {
  72. var _a;
  73. const chunkHeaderIndex = i, chunkHeaderLine = diffstr[i++], chunkHeader = chunkHeaderLine.split(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
  74. const hunk = {
  75. oldStart: +chunkHeader[1],
  76. oldLines: typeof chunkHeader[2] === 'undefined' ? 1 : +chunkHeader[2],
  77. newStart: +chunkHeader[3],
  78. newLines: typeof chunkHeader[4] === 'undefined' ? 1 : +chunkHeader[4],
  79. lines: []
  80. };
  81. // Unified Diff Format quirk: If the chunk size is 0,
  82. // the first number is one lower than one would expect.
  83. // https://www.artima.com/weblogs/viewpost.jsp?thread=164293
  84. if (hunk.oldLines === 0) {
  85. hunk.oldStart += 1;
  86. }
  87. if (hunk.newLines === 0) {
  88. hunk.newStart += 1;
  89. }
  90. let addCount = 0, removeCount = 0;
  91. for (; i < diffstr.length && (removeCount < hunk.oldLines || addCount < hunk.newLines || ((_a = diffstr[i]) === null || _a === void 0 ? void 0 : _a.startsWith('\\'))); i++) {
  92. const operation = (diffstr[i].length == 0 && i != (diffstr.length - 1)) ? ' ' : diffstr[i][0];
  93. if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') {
  94. hunk.lines.push(diffstr[i]);
  95. if (operation === '+') {
  96. addCount++;
  97. }
  98. else if (operation === '-') {
  99. removeCount++;
  100. }
  101. else if (operation === ' ') {
  102. addCount++;
  103. removeCount++;
  104. }
  105. }
  106. else {
  107. throw new Error(`Hunk at line ${chunkHeaderIndex + 1} contained invalid line ${diffstr[i]}`);
  108. }
  109. }
  110. // Handle the empty block count case
  111. if (!addCount && hunk.newLines === 1) {
  112. hunk.newLines = 0;
  113. }
  114. if (!removeCount && hunk.oldLines === 1) {
  115. hunk.oldLines = 0;
  116. }
  117. // Perform sanity checking
  118. if (addCount !== hunk.newLines) {
  119. throw new Error('Added line count did not match for hunk at line ' + (chunkHeaderIndex + 1));
  120. }
  121. if (removeCount !== hunk.oldLines) {
  122. throw new Error('Removed line count did not match for hunk at line ' + (chunkHeaderIndex + 1));
  123. }
  124. return hunk;
  125. }
  126. while (i < diffstr.length) {
  127. parseIndex();
  128. }
  129. return list;
  130. }