Fix parsing of line numbers
This commit is contained in:
parent
0261b30a26
commit
3fd5eb86bb
2 changed files with 52 additions and 6 deletions
|
|
@ -28,6 +28,7 @@
|
|||
var currentFile = null;
|
||||
var currentBlock = null;
|
||||
var oldLine = null;
|
||||
var oldLine2 = null; // Used for combined diff
|
||||
var newLine = null;
|
||||
|
||||
var saveBlock = function() {
|
||||
|
|
@ -67,22 +68,41 @@
|
|||
|
||||
var values;
|
||||
|
||||
if (values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)) {
|
||||
currentFile.isCombined = false;
|
||||
} else if (values = /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)) {
|
||||
currentFile.isCombined = true;
|
||||
} else {
|
||||
values = [0, 0];
|
||||
currentFile.isCombined = false;
|
||||
}
|
||||
/**
|
||||
* From Range:
|
||||
* -<start line>[,<number of lines>]
|
||||
*
|
||||
* To Range:
|
||||
* +<start line>[,<number of lines>]
|
||||
*
|
||||
* @@ from-file-range to-file-range @@
|
||||
*
|
||||
* @@@ from-file-range from-file-range to-file-range @@@
|
||||
*
|
||||
* number of lines is optional, if omited consider 0
|
||||
*/
|
||||
|
||||
if (values = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@.*/.exec(line)) {
|
||||
currentFile.isCombined = false;
|
||||
oldLine = values[1];
|
||||
newLine = values[2];
|
||||
} else if (values = /^@@@ -(\d+)(?:,\d+)? -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@@.*/.exec(line)) {
|
||||
currentFile.isCombined = true;
|
||||
oldLine = values[1];
|
||||
oldLine2 = values[2];
|
||||
newLine = values[3];
|
||||
} else {
|
||||
console.error("Failed to parse lines, starting in 0!");
|
||||
oldLine = 0;
|
||||
newLine = 0;
|
||||
currentFile.isCombined = false;
|
||||
}
|
||||
|
||||
/* Create block metadata */
|
||||
currentBlock = {};
|
||||
currentBlock.lines = [];
|
||||
currentBlock.oldStartLine = oldLine;
|
||||
currentBlock.oldStartLine2 = oldLine2;
|
||||
currentBlock.newStartLine = newLine;
|
||||
currentBlock.header = line;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -374,5 +374,31 @@ describe('DiffParser', function() {
|
|||
assert.equal(86, file1.unchangedPercentage);
|
||||
});
|
||||
|
||||
it('should parse diffs correct line numbers', function() {
|
||||
var diff =
|
||||
'diff --git a/sample b/sample\n' +
|
||||
'index 0000001..0ddf2ba\n' +
|
||||
'--- a/sample\n' +
|
||||
'+++ b/sample\n' +
|
||||
'@@ -1 +1,2 @@\n' +
|
||||
'-test\n' +
|
||||
'+test1r\n';
|
||||
|
||||
var result = Diff2Html.getJsonFromDiff(diff);
|
||||
assert.equal(1, result.length);
|
||||
|
||||
var file1 = result[0];
|
||||
assert.equal(1, file1.addedLines);
|
||||
assert.equal(1, file1.deletedLines);
|
||||
assert.equal('sample', file1.oldName);
|
||||
assert.equal('sample', file1.newName);
|
||||
assert.equal(1, file1.blocks.length);
|
||||
assert.equal(2, file1.blocks[0].lines.length);
|
||||
assert.equal(1, file1.blocks[0].lines[0].oldNumber);
|
||||
assert.equal(null, file1.blocks[0].lines[0].newNumber);
|
||||
assert.equal(null, file1.blocks[0].lines[1].oldNumber);
|
||||
assert.equal(1, file1.blocks[0].lines[1].newNumber);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue