Skip to content

Commit 2a18c91

Browse files
committed
Add another way to recognize binary file names
1 parent d3b053c commit 2a18c91

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/diff-parser.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@
195195
var dissimilarityIndex = /^dissimilarity index (\d+)%/;
196196
var index = /^index ([0-9a-z]+)\.\.([0-9a-z]+)\s*(\d{6})?/;
197197

198+
var binaryFiles = /^Binary files (.*) and (.*) differ/;
199+
198200
/* Combined Diff */
199201
var combinedIndex = /^index ([0-9a-z]+),([0-9a-z]+)\.\.([0-9a-z]+)/;
200202
var combinedMode = /^mode (\d{6}),(\d{6})\.\.(\d{6})/;
@@ -324,6 +326,10 @@
324326
currentFile.newName = values[1];
325327
}
326328
currentFile.isRename = true;
329+
} else if ((values = binaryFiles.exec(line))) {
330+
currentFile.isBinary = true;
331+
currentFile.oldName = _getFilename(null, values[1], [config.srcPrefix]);
332+
currentFile.newName = _getFilename(null, values[2], [config.dstPrefix]);
327333
} else if ((values = similarityIndex.exec(line))) {
328334
currentFile.unchangedPercentage = values[1];
329335
} else if ((values = dissimilarityIndex.exec(line))) {
@@ -383,7 +389,12 @@
383389
}
384390

385391
function _getFilename(linePrefix, line, prefixes) {
386-
var FilenameRegExp = new RegExp('^' + linePrefix + ' "?(.+?)"?$');
392+
var FilenameRegExp;
393+
if (linePrefix) {
394+
FilenameRegExp = new RegExp('^' + linePrefix + ' "?(.+?)"?$');
395+
} else {
396+
FilenameRegExp = new RegExp('^"?(.+?)"?$');
397+
}
387398

388399
var filename;
389400
var values = FilenameRegExp.exec(line);

src/ui/js/diff2html-ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
var cfg = config || {};
3434
cfg.inputFormat = 'json';
3535
var $target = this._getTarget(targetId);
36-
$target.html(Diff2Html.getPrettyHtml(diffJson, cfg.inputFormat));
36+
$target.html(Diff2Html.getPrettyHtml(diffJson, cfg));
3737

3838
synchronisedScroll($target, cfg);
3939
};

test/diff-parser-tests.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,5 +573,53 @@ describe('DiffParser', function() {
573573
assert.deepEqual(linesContent,
574574
[' function foo() {', '-var bar = "Whoops!";', '+var baz = "Whoops!";', ' }', ' ']);
575575
});
576+
577+
it('should parse diff with prefix', function() {
578+
var diff =
579+
'diff --git "\tTest.scala" "\tScalaTest.scala"\n' +
580+
'similarity index 88%\n' +
581+
'rename from Test.scala\n' +
582+
'rename to ScalaTest.scala\n' +
583+
'index 7d1f9bf..8b13271 100644\n' +
584+
'--- "\tTest.scala"\n' +
585+
'+++ "\tScalaTest.scala"\n' +
586+
'@@ -1,6 +1,8 @@\n' +
587+
' class Test {\n' +
588+
' \n' +
589+
' def method1 = ???\n' +
590+
'+\n' +
591+
'+ def method2 = ???\n' +
592+
' \n' +
593+
' def myMethod = ???\n' +
594+
' \n' +
595+
'@@ -10,7 +12,6 @@ class Test {\n' +
596+
' \n' +
597+
' def + = ???\n' +
598+
' \n' +
599+
'- def |> = ???\n' +
600+
' \n' +
601+
' }\n' +
602+
' \n' +
603+
'diff --git "\ttardis.png" "\ttardis.png"\n' +
604+
'new file mode 100644\n' +
605+
'index 0000000..d503a29\n' +
606+
'Binary files /dev/null and "\ttardis.png" differ\n';
607+
608+
var result = DiffParser.generateDiffJson(diff, {'srcPrefix': '\t', 'dstPrefix': '\t'});
609+
assert.equal(2, result.length);
610+
611+
var file1 = result[0];
612+
assert.equal(2, file1.addedLines);
613+
assert.equal(1, file1.deletedLines);
614+
assert.equal('Test.scala', file1.oldName);
615+
assert.equal('ScalaTest.scala', file1.newName);
616+
assert.equal(2, file1.blocks.length);
617+
assert.equal(8, file1.blocks[0].lines.length);
618+
assert.equal(7, file1.blocks[1].lines.length);
619+
620+
var file2 = result[1];
621+
assert.equal('/dev/null', file2.oldName);
622+
assert.equal('tardis.png', file2.newName);
623+
});
576624
});
577625
});

0 commit comments

Comments
 (0)