|
| 1 | +/* |
| 2 | + * |
| 3 | + * Diff Parser (diff-parser.js) |
| 4 | + * Author: rtfpessoa |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +var utils = new Utils(); |
| 9 | + |
| 10 | +var LINE_TYPE = { |
| 11 | + INSERTS: "d2h-ins", |
| 12 | + DELETES: "d2h-del", |
| 13 | + CONTEXT: "d2h-cntx", |
| 14 | + INFO: "d2h-info" |
| 15 | +}; |
| 16 | + |
| 17 | +function DiffParser() { |
| 18 | +} |
| 19 | + |
| 20 | +DiffParser.prototype.LINE_TYPE = LINE_TYPE; |
| 21 | + |
| 22 | +DiffParser.prototype.generateDiffJson = function (diffInput) { |
| 23 | + var files = [], |
| 24 | + currentFile = null, |
| 25 | + currentBlock = null, |
| 26 | + oldLine = null, |
| 27 | + newLine = null; |
| 28 | + |
| 29 | + var saveBlock = function () { |
| 30 | + /* add previous block(if exists) before start a new file */ |
| 31 | + if (currentBlock) { |
| 32 | + currentFile.blocks.push(currentBlock); |
| 33 | + currentBlock = null; |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + var saveFile = function () { |
| 38 | + /* |
| 39 | + * add previous file(if exists) before start a new one |
| 40 | + * if it has name (to avoid binary files errors) |
| 41 | + */ |
| 42 | + if (currentFile && currentFile.newName) { |
| 43 | + files.push(currentFile); |
| 44 | + currentFile = null; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + var startFile = function () { |
| 49 | + saveBlock(); |
| 50 | + saveFile(); |
| 51 | + |
| 52 | + /* create file structure */ |
| 53 | + currentFile = {}; |
| 54 | + currentFile.blocks = []; |
| 55 | + currentFile.deletedLines = 0; |
| 56 | + currentFile.addedLines = 0; |
| 57 | + }; |
| 58 | + |
| 59 | + var startBlock = function (line) { |
| 60 | + saveBlock(); |
| 61 | + |
| 62 | + var values; |
| 63 | + |
| 64 | + if (values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)) { |
| 65 | + currentFile.isTripleDiff = false; |
| 66 | + } else if (values = /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)) { |
| 67 | + currentFile.isTripleDiff = true; |
| 68 | + } else { |
| 69 | + values = [0, 0]; |
| 70 | + currentFile.isTripleDiff = false; |
| 71 | + } |
| 72 | + |
| 73 | + oldLine = values[1]; |
| 74 | + newLine = values[2]; |
| 75 | + |
| 76 | + /* create block metadata */ |
| 77 | + currentBlock = {}; |
| 78 | + currentBlock.lines = []; |
| 79 | + currentBlock.oldStartLine = oldLine; |
| 80 | + currentBlock.newStartLine = newLine; |
| 81 | + currentBlock.header = line; |
| 82 | + }; |
| 83 | + |
| 84 | + var createLine = function (line) { |
| 85 | + var currentLine = {}; |
| 86 | + currentLine.content = line; |
| 87 | + |
| 88 | + /* fill the line data */ |
| 89 | + if (utils.startsWith(line, "+") || utils.startsWith(line, " +")) { |
| 90 | + currentFile.addedLines++; |
| 91 | + |
| 92 | + currentLine.type = LINE_TYPE.INSERTS; |
| 93 | + currentLine.oldNumber = null; |
| 94 | + currentLine.newNumber = newLine++; |
| 95 | + |
| 96 | + currentBlock.lines.push(currentLine); |
| 97 | + |
| 98 | + } else if (utils.startsWith(line, "-") || utils.startsWith(line, " -")) { |
| 99 | + currentFile.deletedLines++; |
| 100 | + |
| 101 | + currentLine.type = LINE_TYPE.DELETES; |
| 102 | + currentLine.oldNumber = oldLine++; |
| 103 | + currentLine.newNumber = null; |
| 104 | + |
| 105 | + currentBlock.lines.push(currentLine); |
| 106 | + |
| 107 | + } else { |
| 108 | + currentLine.type = LINE_TYPE.CONTEXT; |
| 109 | + currentLine.oldNumber = oldLine++; |
| 110 | + currentLine.newNumber = newLine++; |
| 111 | + |
| 112 | + currentBlock.lines.push(currentLine); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + var diffLines = diffInput.split("\n"); |
| 117 | + diffLines.forEach(function (line) { |
| 118 | + // Unmerged paths, and possibly other non-diffable files |
| 119 | + // https://github.com/scottgonzalez/pretty-diff/issues/11 |
| 120 | + // Also, remove some useless lines |
| 121 | + if (!line || utils.startsWith(line, "*") || |
| 122 | + utils.startsWith(line, "new") || utils.startsWith(line, "index")) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + var values = []; |
| 127 | + if (utils.startsWith(line, "diff")) { |
| 128 | + startFile(); |
| 129 | + } else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) { |
| 130 | + currentFile.oldName = values[1]; |
| 131 | + } else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [b]?\/(\S+).*$/.exec(line))) { |
| 132 | + currentFile.newName = values[1]; |
| 133 | + |
| 134 | + var fileSplit = currentFile.newName.split("."); |
| 135 | + currentFile.language = fileSplit[fileSplit.length - 1]; |
| 136 | + } else if (currentFile && utils.startsWith(line, "@@")) { |
| 137 | + startBlock(line); |
| 138 | + } else if (currentBlock) { |
| 139 | + createLine(line); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + saveBlock(); |
| 144 | + saveFile(); |
| 145 | + |
| 146 | + return files; |
| 147 | +}; |
0 commit comments