Fix filename parsing with special characters and prefixes
* Add support for diffs with prefixes through configuration * Fix parsing of filenames with special chars
This commit is contained in:
parent
c0f7c7abd8
commit
e44b2a2885
3 changed files with 74 additions and 10 deletions
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
|
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
|
||||||
|
|
||||||
DiffParser.prototype.generateDiffJson = function(diffInput) {
|
DiffParser.prototype.generateDiffJson = function(diffInput, config) {
|
||||||
var files = [];
|
var files = [];
|
||||||
var currentFile = null;
|
var currentFile = null;
|
||||||
var currentBlock = null;
|
var currentBlock = null;
|
||||||
|
|
@ -133,11 +133,11 @@
|
||||||
var deletedFileMode = /^deleted file mode (\d{6})/;
|
var deletedFileMode = /^deleted file mode (\d{6})/;
|
||||||
var newFileMode = /^new file mode (\d{6})/;
|
var newFileMode = /^new file mode (\d{6})/;
|
||||||
|
|
||||||
var copyFrom = /^copy from (.+)/;
|
var copyFrom = /^copy from "?(.+?)"?/;
|
||||||
var copyTo = /^copy to (.+)/;
|
var copyTo = /^copy to "?(.+?)"?/;
|
||||||
|
|
||||||
var renameFrom = /^rename from (.+)/;
|
var renameFrom = /^rename from "?(.+?)"?/;
|
||||||
var renameTo = /^rename to (.+)/;
|
var renameTo = /^rename to "?(.+?)"?/;
|
||||||
|
|
||||||
var similarityIndex = /^similarity index (\d+)%/;
|
var similarityIndex = /^similarity index (\d+)%/;
|
||||||
var dissimilarityIndex = /^dissimilarity index (\d+)%/;
|
var dissimilarityIndex = /^dissimilarity index (\d+)%/;
|
||||||
|
|
@ -160,10 +160,10 @@
|
||||||
var values = [];
|
var values = [];
|
||||||
if (utils.startsWith(line, 'diff')) {
|
if (utils.startsWith(line, 'diff')) {
|
||||||
startFile();
|
startFile();
|
||||||
} else if (currentFile && !currentFile.oldName && (values = /^--- [aiwco]\/(.+)$/.exec(line))) {
|
} else if (currentFile && !currentFile.oldName && (values = getSrcFilename(line, config))) {
|
||||||
currentFile.oldName = values[1];
|
currentFile.oldName = values[1];
|
||||||
currentFile.language = getExtension(currentFile.oldName, currentFile.language);
|
currentFile.language = getExtension(currentFile.oldName, currentFile.language);
|
||||||
} else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [biwco]?\/(.+)$/.exec(line))) {
|
} else if (currentFile && !currentFile.newName && (values = getDstFilename(line, config))) {
|
||||||
currentFile.newName = values[1];
|
currentFile.newName = values[1];
|
||||||
currentFile.language = getExtension(currentFile.newName, currentFile.language);
|
currentFile.language = getExtension(currentFile.newName, currentFile.language);
|
||||||
} else if (currentFile && utils.startsWith(line, '@@')) {
|
} else if (currentFile && utils.startsWith(line, '@@')) {
|
||||||
|
|
@ -226,6 +226,27 @@
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSrcFilename(line, cfg) {
|
||||||
|
var prefixes = ["a\\/", "i\\/", "w\\/", "c\\/", "o\\/"];
|
||||||
|
|
||||||
|
if (cfg.srcPrefix) prefixes.push(cfg.srcPrefix);
|
||||||
|
|
||||||
|
return _getFilename('---', line, prefixes);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDstFilename(line, cfg) {
|
||||||
|
var prefixes = ["b\\/", "i\\/", "w\\/", "c\\/", "o\\/"];
|
||||||
|
|
||||||
|
if (cfg.dstPrefix) prefixes.push(cfg.dstPrefix);
|
||||||
|
|
||||||
|
return _getFilename('\\+\\+\\+', line, prefixes);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getFilename(linePrefix, line, prefixes) {
|
||||||
|
var prefixesStr = prefixes.join("|");
|
||||||
|
return new RegExp('^' + linePrefix + ' "?(?:' + prefixesStr + ')(.+?)"?$').exec(line);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.DiffParser = new DiffParser();
|
module.exports.DiffParser = new DiffParser();
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,9 @@
|
||||||
/*
|
/*
|
||||||
* Generates json object from string diff input
|
* Generates json object from string diff input
|
||||||
*/
|
*/
|
||||||
Diff2Html.prototype.getJsonFromDiff = function(diffInput) {
|
Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) {
|
||||||
return diffParser.generateDiffJson(diffInput);
|
var configOrEmpty = config || {};
|
||||||
|
return diffParser.generateDiffJson(diffInput, configOrEmpty);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -38,7 +39,7 @@
|
||||||
|
|
||||||
var diffJson = diffInput;
|
var diffJson = diffInput;
|
||||||
if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
|
if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
|
||||||
diffJson = diffParser.generateDiffJson(diffInput);
|
diffJson = diffParser.generateDiffJson(diffInput, configOrEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileList = '';
|
var fileList = '';
|
||||||
|
|
|
||||||
|
|
@ -64,5 +64,47 @@ describe('DiffParser', function() {
|
||||||
assert.equal(1, file1.blocks.length);
|
assert.equal(1, file1.blocks.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it('should parse diff with special characters', function() {
|
||||||
|
var diff =
|
||||||
|
'diff --git "a/bla with \ttab.scala" "b/bla with \ttab.scala"\n' +
|
||||||
|
'index 4c679d7..e9bd385 100644\n' +
|
||||||
|
'--- "a/bla with \ttab.scala"\n' +
|
||||||
|
'+++ "b/bla with \ttab.scala"\n' +
|
||||||
|
'@@ -1 +1,2 @@\n' +
|
||||||
|
'-cenas\n' +
|
||||||
|
'+cenas com ananas\n' +
|
||||||
|
'+bananas';
|
||||||
|
|
||||||
|
var result = Diff2Html.getJsonFromDiff(diff);
|
||||||
|
var file1 = result[0];
|
||||||
|
assert.equal(1, result.length);
|
||||||
|
assert.equal(2, file1.addedLines);
|
||||||
|
assert.equal(1, file1.deletedLines);
|
||||||
|
assert.equal('bla with \ttab.scala', file1.oldName);
|
||||||
|
assert.equal('bla with \ttab.scala', file1.newName);
|
||||||
|
assert.equal(1, file1.blocks.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse diff with prefix', function() {
|
||||||
|
var diff =
|
||||||
|
'diff --git "\tbla with \ttab.scala" "\tbla with \ttab.scala"\n' +
|
||||||
|
'index 4c679d7..e9bd385 100644\n' +
|
||||||
|
'--- "\tbla with \ttab.scala"\n' +
|
||||||
|
'+++ "\tbla with \ttab.scala"\n' +
|
||||||
|
'@@ -1 +1,2 @@\n' +
|
||||||
|
'-cenas\n' +
|
||||||
|
'+cenas com ananas\n' +
|
||||||
|
'+bananas';
|
||||||
|
|
||||||
|
var result = Diff2Html.getJsonFromDiff(diff, {"srcPrefix": "\t", "dstPrefix": "\t"});
|
||||||
|
var file1 = result[0];
|
||||||
|
assert.equal(1, result.length);
|
||||||
|
assert.equal(2, file1.addedLines);
|
||||||
|
assert.equal(1, file1.deletedLines);
|
||||||
|
assert.equal('bla with \ttab.scala', file1.oldName);
|
||||||
|
assert.equal('bla with \ttab.scala', file1.newName);
|
||||||
|
assert.equal(1, file1.blocks.length);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue