diff --git a/src/diff-parser.js b/src/diff-parser.js index 7a81895..e027558 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -195,6 +195,8 @@ var dissimilarityIndex = /^dissimilarity index (\d+)%/; var index = /^index ([0-9a-z]+)\.\.([0-9a-z]+)\s*(\d{6})?/; + var binaryFiles = /^Binary files (.*) and (.*) differ/; + /* Combined Diff */ var combinedIndex = /^index ([0-9a-z]+),([0-9a-z]+)\.\.([0-9a-z]+)/; var combinedMode = /^mode (\d{6}),(\d{6})\.\.(\d{6})/; @@ -324,6 +326,10 @@ currentFile.newName = values[1]; } currentFile.isRename = true; + } else if ((values = binaryFiles.exec(line))) { + currentFile.isBinary = true; + currentFile.oldName = _getFilename(null, values[1], [config.srcPrefix]); + currentFile.newName = _getFilename(null, values[2], [config.dstPrefix]); } else if ((values = similarityIndex.exec(line))) { currentFile.unchangedPercentage = values[1]; } else if ((values = dissimilarityIndex.exec(line))) { @@ -383,7 +389,12 @@ } function _getFilename(linePrefix, line, prefixes) { - var FilenameRegExp = new RegExp('^' + linePrefix + ' "?(.+?)"?$'); + var FilenameRegExp; + if (linePrefix) { + FilenameRegExp = new RegExp('^' + linePrefix + ' "?(.+?)"?$'); + } else { + FilenameRegExp = new RegExp('^"?(.+?)"?$'); + } var filename; var values = FilenameRegExp.exec(line); diff --git a/src/ui/js/diff2html-ui.js b/src/ui/js/diff2html-ui.js index 3fc6691..36c65a8 100644 --- a/src/ui/js/diff2html-ui.js +++ b/src/ui/js/diff2html-ui.js @@ -33,7 +33,7 @@ var cfg = config || {}; cfg.inputFormat = 'json'; var $target = this._getTarget(targetId); - $target.html(Diff2Html.getPrettyHtml(diffJson, cfg.inputFormat)); + $target.html(Diff2Html.getPrettyHtml(diffJson, cfg)); synchronisedScroll($target, cfg); }; diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js index 5dcb078..f4dba32 100644 --- a/test/diff-parser-tests.js +++ b/test/diff-parser-tests.js @@ -573,5 +573,53 @@ describe('DiffParser', function() { assert.deepEqual(linesContent, [' function foo() {', '-var bar = "Whoops!";', '+var baz = "Whoops!";', ' }', ' ']); }); + + it('should parse diff with prefix', function() { + var diff = + 'diff --git "\tTest.scala" "\tScalaTest.scala"\n' + + 'similarity index 88%\n' + + 'rename from Test.scala\n' + + 'rename to ScalaTest.scala\n' + + 'index 7d1f9bf..8b13271 100644\n' + + '--- "\tTest.scala"\n' + + '+++ "\tScalaTest.scala"\n' + + '@@ -1,6 +1,8 @@\n' + + ' class Test {\n' + + ' \n' + + ' def method1 = ???\n' + + '+\n' + + '+ def method2 = ???\n' + + ' \n' + + ' def myMethod = ???\n' + + ' \n' + + '@@ -10,7 +12,6 @@ class Test {\n' + + ' \n' + + ' def + = ???\n' + + ' \n' + + '- def |> = ???\n' + + ' \n' + + ' }\n' + + ' \n' + + 'diff --git "\ttardis.png" "\ttardis.png"\n' + + 'new file mode 100644\n' + + 'index 0000000..d503a29\n' + + 'Binary files /dev/null and "\ttardis.png" differ\n'; + + var result = DiffParser.generateDiffJson(diff, {'srcPrefix': '\t', 'dstPrefix': '\t'}); + assert.equal(2, result.length); + + var file1 = result[0]; + assert.equal(2, file1.addedLines); + assert.equal(1, file1.deletedLines); + assert.equal('Test.scala', file1.oldName); + assert.equal('ScalaTest.scala', file1.newName); + assert.equal(2, file1.blocks.length); + assert.equal(8, file1.blocks[0].lines.length); + assert.equal(7, file1.blocks[1].lines.length); + + var file2 = result[1]; + assert.equal('/dev/null', file2.oldName); + assert.equal('tardis.png', file2.newName); + }); }); });