diff --git a/README.md b/README.md index f4fa3ad..e84e7f8 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ diff2html generates pretty HTML diffs from git or unified diff output. * Easy code selection +* Support `svn diff --git` output + ## Online Example > Go to [diff2html](https://diff2html.xyz/) @@ -99,6 +101,8 @@ The HTML output accepts a Javascript object with configuration. Possible options - `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500` - `templates`: object with previously compiled templates to replace parts of the html - `rawTemplates`: object with raw not compiled templates to replace parts of the html + - `ignoreSvnPropertyChange`: If you use `svn diff --git`, and add or delete a binary file, you will get two file change records in output. Set `ignoreSvnPropertyChange = true` will help you ignore the second record. + > For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates) ** Diff2HtmlUI Helper Options ** diff --git a/src/diff-parser.js b/src/diff-parser.js index 4f3fdf6..4a012c5 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -198,8 +198,8 @@ /* Diff */ var oldMode = /^old mode (\d{6})/; var newMode = /^new mode (\d{6})/; - var deletedFileMode = /^deleted file mode (\d{6})/; - var newFileMode = /^new file mode (\d{6})/; + var deletedFileMode = /^deleted file mode (\d{5,6})/; + var newFileMode = /^new file mode (\d{5,6})/; var copyFrom = /^copy from "?(.+)"?/; var copyTo = /^copy to "?(.+)"?/; @@ -220,6 +220,41 @@ var combinedNewFile = /^new file mode (\d{6})/; var combinedDeletedFile = /^deleted file mode (\d{6}),(\d{6})/; + var svnNullFileMode = /^(.+)(\s+)(\(nonexistent\))$/; + var svnWithVersionFileMode = /^(.+)(\s+)(\(revision\s+\d+\))$/; + + /* if we use command `svn diff --git`. we will get header like that: + * + * ``` + * diff --git a/color_doc.docx b/color_doc.docx + * --- a/color_doc.docx (nonexistent) + * +++ b/color_doc.docx (revision 10) + * ``` + * + * `(nonexistent)` means that file do not exist. + * `(revision 10)` shows the current version of file. When the string exit, this file is in reposity. + */ + function checkSvnFileMode(fileName, isOld) { + var result = null; + if (isOld) { + if ((result = svnNullFileMode.exec(fileName))) { + currentFile.oldName = result[1]; + } else if ((result = svnWithVersionFileMode.exec(fileName))) { + currentFile.oldName = result[1]; + } else { + currentFile.oldName = fileName; + } + } else { + if ((result = svnNullFileMode.exec(fileName))) { + currentFile.newName = result[1]; + } else if ((result = svnWithVersionFileMode.exec(fileName))) { + currentFile.newName = result[1]; + } else { + currentFile.newName = fileName; + } + } + } + diffLines.forEach(function(line, lineIndex) { // Unmerged paths, and possibly other non-diffable files // https://github.com/scottgonzalez/pretty-diff/issues/11 @@ -279,8 +314,10 @@ * --- 2002-02-21 23:30:39.942229878 -0800 */ if (currentFile && !currentFile.oldName && - utils.startsWith(line, '--- ') && (values = getSrcFilename(line, config))) { - currentFile.oldName = values; + utils.startsWith(line, '--- ') && + (values = getSrcFilename(line, config)) + ) { + checkSvnFileMode(values, true); currentFile.language = getExtension(currentFile.oldName, currentFile.language); return; } @@ -291,7 +328,7 @@ */ if (currentFile && !currentFile.newName && utils.startsWith(line, '+++ ') && (values = getDstFilename(line, config))) { - currentFile.newName = values; + checkSvnFileMode(values, false); currentFile.language = getExtension(currentFile.newName, currentFile.language); return; } @@ -386,9 +423,57 @@ saveBlock(); saveFile(); + if (config.ignoreSvnPropertyChange) { + files = dropSvnPropertyChangeFiles(files); + } return files; }; + /** + * If you use `svn diff --git`, and add or delete a binary file, you will get two file change records in output, like that: + * + * ``` + * =================================================================== + * diff --git a/text.png b/text.png + * deleted file mode 10644 + * GIT binary patch + * ... + * =================================================================== + * diff --git a/text.png b/text.png + * --- a/text.png (revision 15) + * +++ b/text.png (nonexistent) + * + * Property changes on: text.png + * ... + * ``` + * First record for "delete/new" a file. + * Second record for svn property change. + * + * You proberly do not want to see the second record. So will provide an `ignoreSvnPropertyChange` option to do that for you. + */ + function dropSvnPropertyChangeFiles(files) { + const GIT_BINARY_HEADER = 'GIT binary patch'; + const PROPERTY_CHANGE_HEADER = 'Property changes on:'; + var ret = []; + for (var i = 0; i < files.length - 1; i++) { + var file = files[i]; + var nextFile = files[i + 1]; + + ret.push(file); + if (file.blocks.length > 0 && + utils.startsWith(file.blocks[0].header, GIT_BINARY_HEADER) && + nextFile.blocks.length > 0 && + utils.startsWith(nextFile.blocks[0].header, PROPERTY_CHANGE_HEADER) && + file.name === nextFile.name && + (file.isDeleted === true || file.isNew === true) + ) { + i += 1; + } + } + + return ret; + } + function getExtension(filename, language) { var nameSplit = filename.split('.'); if (nameSplit.length > 1) { @@ -443,3 +528,4 @@ module.exports.DiffParser = new DiffParser(); })(); + diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js index b910868..7f77d8b 100644 --- a/test/diff-parser-tests.js +++ b/test/diff-parser-tests.js @@ -5,8 +5,7 @@ var DiffParser = require('../src/diff-parser.js').DiffParser; describe('DiffParser', function() { describe('generateDiffJson', function() { it('should parse unix with \n diff', function() { - var diff = - 'diff --git a/sample b/sample\n' + + var diff = 'diff --git a/sample b/sample\n' + 'index 0000001..0ddf2ba\n' + '--- a/sample\n' + '+++ b/sample\n' + @@ -17,8 +16,7 @@ describe('DiffParser', function() { }); it('should parse windows with \r\n diff', function() { - var diff = - 'diff --git a/sample b/sample\r\n' + + var diff = 'diff --git a/sample b/sample\r\n' + 'index 0000001..0ddf2ba\r\n' + '--- a/sample\r\n' + '+++ b/sample\r\n' + @@ -29,8 +27,7 @@ describe('DiffParser', function() { }); it('should parse old os x with \r diff', function() { - var diff = - 'diff --git a/sample b/sample\r' + + var diff = 'diff --git a/sample b/sample\r' + 'index 0000001..0ddf2ba\r' + '--- a/sample\r' + '+++ b/sample\r' + @@ -41,8 +38,7 @@ describe('DiffParser', function() { }); it('should parse mixed eols diff', function() { - var diff = - 'diff --git a/sample b/sample\n' + + var diff = 'diff --git a/sample b/sample\n' + 'index 0000001..0ddf2ba\r\n' + '--- a/sample\r' + '+++ b/sample\r\n' + @@ -64,8 +60,7 @@ describe('DiffParser', function() { } it('should parse diff with special characters', function() { - var diff = - 'diff --git "a/bla with \ttab.scala" "b/bla with \ttab.scala"\n' + + 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' + @@ -85,8 +80,7 @@ describe('DiffParser', function() { }); it('should parse diff with prefix', function() { - var diff = - 'diff --git "\tbla with \ttab.scala" "\tbla with \ttab.scala"\n' + + 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' + @@ -95,7 +89,10 @@ describe('DiffParser', function() { '+cenas com ananas\n' + '+bananas'; - var result = DiffParser.generateDiffJson(diff, {'srcPrefix': '\t', 'dstPrefix': '\t'}); + var result = DiffParser.generateDiffJson(diff, { + 'srcPrefix': '\t', + 'dstPrefix': '\t' + }); var file1 = result[0]; assert.equal(1, result.length); assert.equal(2, file1.addedLines); @@ -106,8 +103,7 @@ describe('DiffParser', function() { }); it('should parse diff with deleted file', function() { - var diff = - 'diff --git a/src/var/strundefined.js b/src/var/strundefined.js\n' + + var diff = 'diff --git a/src/var/strundefined.js b/src/var/strundefined.js\n' + 'deleted file mode 100644\n' + 'index 04e16b0..0000000\n' + '--- a/src/var/strundefined.js\n' + @@ -133,8 +129,7 @@ describe('DiffParser', function() { }); it('should parse diff with new file', function() { - var diff = - 'diff --git a/test.js b/test.js\n' + + var diff = 'diff --git a/test.js b/test.js\n' + 'new file mode 100644\n' + 'index 0000000..e1e22ec\n' + '--- /dev/null\n' + @@ -163,8 +158,7 @@ describe('DiffParser', function() { }); it('should parse diff with nested diff', function() { - var diff = - 'diff --git a/src/offset.js b/src/offset.js\n' + + var diff = 'diff --git a/src/offset.js b/src/offset.js\n' + 'index cc6ffb4..fa51f18 100644\n' + '--- a/src/offset.js\n' + '+++ b/src/offset.js\n' + @@ -192,8 +186,7 @@ describe('DiffParser', function() { }); it('should parse diff with multiple blocks', function() { - var diff = - 'diff --git a/src/attributes/classes.js b/src/attributes/classes.js\n' + + var diff = 'diff --git a/src/attributes/classes.js b/src/attributes/classes.js\n' + 'index c617824..c8d1393 100644\n' + '--- a/src/attributes/classes.js\n' + '+++ b/src/attributes/classes.js\n' + @@ -236,8 +229,7 @@ describe('DiffParser', function() { }); it('should parse diff with multiple files', function() { - var diff = - 'diff --git a/src/core/init.js b/src/core/init.js\n' + + var diff = 'diff --git a/src/core/init.js b/src/core/init.js\n' + 'index e49196a..50f310c 100644\n' + '--- a/src/core/init.js\n' + '+++ b/src/core/init.js\n' + @@ -289,8 +281,7 @@ describe('DiffParser', function() { }); it('should parse combined diff', function() { - var diff = - 'diff --combined describe.c\n' + + var diff = 'diff --combined describe.c\n' + 'index fabadb8,cc95eb0..4866510\n' + '--- a/describe.c\n' + '+++ b/describe.c\n' + @@ -334,8 +325,7 @@ describe('DiffParser', function() { }); it('should parse diffs with copied files', function() { - var diff = - 'diff --git a/index.js b/more-index.js\n' + + var diff = 'diff --git a/index.js b/more-index.js\n' + 'dissimilarity index 5%\n' + 'copy from index.js\n' + 'copy to more-index.js\n'; @@ -354,8 +344,7 @@ describe('DiffParser', function() { }); it('should parse diffs with moved files', function() { - var diff = - 'diff --git a/more-index.js b/other-index.js\n' + + var diff = 'diff --git a/more-index.js b/other-index.js\n' + 'similarity index 86%\n' + 'rename from more-index.js\n' + 'rename to other-index.js\n'; @@ -374,8 +363,7 @@ describe('DiffParser', function() { }); it('should parse diffs correct line numbers', function() { - var diff = - 'diff --git a/sample b/sample\n' + + var diff = 'diff --git a/sample b/sample\n' + 'index 0000001..0ddf2ba\n' + '--- a/sample\n' + '+++ b/sample\n' + @@ -400,8 +388,7 @@ describe('DiffParser', function() { }); it('should parse unified non git diff and strip timestamps off the headers', function() { - var diff = - '--- a/sample.js 2016-10-25 11:37:14.000000000 +0200\n' + + var diff = '--- a/sample.js 2016-10-25 11:37:14.000000000 +0200\n' + '+++ b/sample.js 2016-10-25 11:37:14.000000000 +0200\n' + '@@ -1 +1,2 @@\n' + '-test\n' + @@ -424,8 +411,7 @@ describe('DiffParser', function() { }); it('should parse unified non git diff', function() { - var diff = - '--- a/sample.js\n' + + var diff = '--- a/sample.js\n' + '+++ b/sample.js\n' + '@@ -1 +1,2 @@\n' + '-test\n' + @@ -448,8 +434,7 @@ describe('DiffParser', function() { }); it('should parse unified diff with multiple hunks and files', function() { - var diff = - '--- sample.js\n' + + var diff = '--- sample.js\n' + '+++ sample.js\n' + '@@ -1 +1,2 @@\n' + '-test\n' + @@ -494,8 +479,7 @@ describe('DiffParser', function() { }); it('should parse diff with --- and +++ in the context lines', function() { - var diff = - '--- sample.js\n' + + var diff = '--- sample.js\n' + '+++ sample.js\n' + '@@ -1,8 +1,8 @@\n' + ' test\n' + @@ -525,8 +509,7 @@ describe('DiffParser', function() { }); it('should parse diff without proper hunk headers', function() { - var diff = - '--- sample.js\n' + + var diff = '--- sample.js\n' + '+++ sample.js\n' + '@@ @@\n' + ' test'; @@ -547,8 +530,7 @@ describe('DiffParser', function() { }); it('should parse binary file diff', function() { - var diff = - 'diff --git a/last-changes-config.png b/last-changes-config.png\n' + + var diff = 'diff --git a/last-changes-config.png b/last-changes-config.png\n' + 'index 322248b..56fc1f2 100644\n' + '--- a/last-changes-config.png\n' + '+++ b/last-changes-config.png\n' + @@ -567,8 +549,7 @@ describe('DiffParser', function() { }); it('should parse diff with --find-renames', function() { - var diff = - 'diff --git a/src/test-bar.js b/src/test-baz.js\n' + + var diff = 'diff --git a/src/test-bar.js b/src/test-baz.js\n' + 'similarity index 98%\n' + 'rename from src/test-bar.js\n' + 'rename to src/test-baz.js\n' + @@ -599,8 +580,7 @@ describe('DiffParser', function() { }); it('should parse diff with prefix', function() { - var diff = - 'diff --git "\tTest.scala" "\tScalaTest.scala"\n' + + var diff = 'diff --git "\tTest.scala" "\tScalaTest.scala"\n' + 'similarity index 88%\n' + 'rename from Test.scala\n' + 'rename to ScalaTest.scala\n' + @@ -642,7 +622,10 @@ describe('DiffParser', function() { ' }\n' + ' '; - var result = DiffParser.generateDiffJson(diff, {'srcPrefix': '\t', 'dstPrefix': '\t'}); + var result = DiffParser.generateDiffJson(diff, { + 'srcPrefix': '\t', + 'dstPrefix': '\t' + }); assert.equal(3, result.length); var file1 = result[0]; @@ -673,8 +656,7 @@ describe('DiffParser', function() { }); it('should parse binary with content', function() { - var diff = - 'diff --git a/favicon.png b/favicon.png\n' + + var diff = 'diff --git a/favicon.png b/favicon.png\n' + 'deleted file mode 100644\n' + 'index 2a9d516a5647205d7be510dd0dff93a3663eff6f..0000000000000000000000000000000000000000\n' + 'GIT binary patch\n' + @@ -728,5 +710,141 @@ describe('DiffParser', function() { assert.deepEqual(linesContent, [' function foo() {', '-var bar = "Whoops!";', '+var baz = "Whoops!";', ' }', ' ']); }); + + it('should ignore the property set record of `svn diff --git` with ignoreSvnPropertyChange = true', function() { + var diff = 'Index: color_doc.docx\n' + + '===================================================================\n' + + 'diff --git a/color_doc.docx b/color_doc.docx\n' + + 'new file mode 10644\n' + + 'GIT binary patch\n' + + 'literal 296291\n' + + 'zc%1CHV|b>|mmvJaPteH|b!;ac+w9n8$F^;E+_5|E*jC3@$F^-JoBsW0W@mS2->>`O\n' + + ' \n' + + 'literal 0\n' + + 'Hc$@|mmvJaPteH|b!;ac+w9n8$F^;E+_5|E*jC3@$F^-JoBsW0W@mS2->>`O\n' + + ' \n' + + 'literal 0\n' + + 'Hc$@