From e44b2a2885c29adc8d5182041a7e5b43c5fa2eec Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Tue, 23 Feb 2016 18:21:34 +0000 Subject: [PATCH] Fix filename parsing with special characters and prefixes * Add support for diffs with prefixes through configuration * Fix parsing of filenames with special chars --- src/diff-parser.js | 35 +++++++++++++++++++++++++------- src/diff2html.js | 7 ++++--- test/diff-parser-tests.js | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/diff-parser.js b/src/diff-parser.js index 9bdd39a..52c6984 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -23,7 +23,7 @@ DiffParser.prototype.LINE_TYPE = LINE_TYPE; - DiffParser.prototype.generateDiffJson = function(diffInput) { + DiffParser.prototype.generateDiffJson = function(diffInput, config) { var files = []; var currentFile = null; var currentBlock = null; @@ -133,11 +133,11 @@ var deletedFileMode = /^deleted file mode (\d{6})/; var newFileMode = /^new file mode (\d{6})/; - var copyFrom = /^copy from (.+)/; - var copyTo = /^copy to (.+)/; + var copyFrom = /^copy from "?(.+?)"?/; + var copyTo = /^copy to "?(.+?)"?/; - var renameFrom = /^rename from (.+)/; - var renameTo = /^rename to (.+)/; + var renameFrom = /^rename from "?(.+?)"?/; + var renameTo = /^rename to "?(.+?)"?/; var similarityIndex = /^similarity index (\d+)%/; var dissimilarityIndex = /^dissimilarity index (\d+)%/; @@ -160,10 +160,10 @@ var values = []; if (utils.startsWith(line, 'diff')) { startFile(); - } else if (currentFile && !currentFile.oldName && (values = /^--- [aiwco]\/(.+)$/.exec(line))) { + } else if (currentFile && !currentFile.oldName && (values = getSrcFilename(line, config))) { currentFile.oldName = values[1]; 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.language = getExtension(currentFile.newName, currentFile.language); } else if (currentFile && utils.startsWith(line, '@@')) { @@ -226,6 +226,27 @@ 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(); })(); diff --git a/src/diff2html.js b/src/diff2html.js index 49a535c..0fe622f 100644 --- a/src/diff2html.js +++ b/src/diff2html.js @@ -26,8 +26,9 @@ /* * Generates json object from string diff input */ - Diff2Html.prototype.getJsonFromDiff = function(diffInput) { - return diffParser.generateDiffJson(diffInput); + Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) { + var configOrEmpty = config || {}; + return diffParser.generateDiffJson(diffInput, configOrEmpty); }; /* @@ -38,7 +39,7 @@ var diffJson = diffInput; if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') { - diffJson = diffParser.generateDiffJson(diffInput); + diffJson = diffParser.generateDiffJson(diffInput, configOrEmpty); } var fileList = ''; diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js index 0f842ee..08e4881 100644 --- a/test/diff-parser-tests.js +++ b/test/diff-parser-tests.js @@ -64,5 +64,47 @@ describe('DiffParser', function() { 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); + }); + }); });