ignore binary files && fix filenames && fix line count

This commit is contained in:
Rodrigo Fernandes 2015-01-24 22:22:20 +00:00
parent 1f53a81a3b
commit ad9181531a
3 changed files with 77 additions and 128 deletions

View file

@ -3,7 +3,7 @@
* Diff to HTML (diff2html.js) * Diff to HTML (diff2html.js)
* Author: rtfpessoa * Author: rtfpessoa
* Date: Friday 29 August 2014 * Date: Friday 29 August 2014
* Last Update: Saturday 27 September 2014 * Last Update: Saturday 24 January 2015
* *
* Diff command: * Diff command:
* git diff * git diff
@ -77,14 +77,17 @@
}; };
var saveFile = function () { var saveFile = function () {
/* add previous file(if exists) before start a new one */ /*
if (currentFile) { * add previous file(if exists) before start a new one
* if it has name (to avoid binary files errors)
*/
if (currentFile && currentFile.newName) {
files.push(currentFile); files.push(currentFile);
currentFile = null; currentFile = null;
} }
}; };
var startFile = function (line) { var startFile = function () {
saveBlock(); saveBlock();
saveFile(); saveFile();
@ -93,24 +96,17 @@
currentFile.blocks = []; currentFile.blocks = [];
currentFile.deletedLines = 0; currentFile.deletedLines = 0;
currentFile.addedLines = 0; currentFile.addedLines = 0;
/* save file paths, before and after the diff */
var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);
currentFile.oldName = values[1];
currentFile.newName = values[2];
}; };
var startBlock = function (line) { var startBlock = function (line) {
saveBlock(); saveBlock();
var values; var values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line) ||
if (values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line)) { /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line) ||
oldLine = values[2]; [0, 0, 0];
newLine = values[4];
} else { oldLine = values[1];
oldLine = 0; newLine = values[2];
newLine = 0;
}
/* create block metadata */ /* create block metadata */
currentBlock = {}; currentBlock = {};
@ -125,7 +121,7 @@
currentLine.content = line; currentLine.content = line;
/* fill the line data */ /* fill the line data */
if (startsWith(line, "+")) { if (startsWith(line, "+") || startsWith(line, " +")) {
currentFile.addedLines++; currentFile.addedLines++;
currentLine.type = LINE_TYPE.INSERTS; currentLine.type = LINE_TYPE.INSERTS;
@ -134,7 +130,7 @@
currentBlock.lines.push(currentLine); currentBlock.lines.push(currentLine);
} else if (startsWith(line, "-")) { } else if (startsWith(line, "-") || startsWith(line, " -")) {
currentFile.deletedLines++; currentFile.deletedLines++;
currentLine.type = LINE_TYPE.DELETES; currentLine.type = LINE_TYPE.DELETES;
@ -158,13 +154,17 @@
// https://github.com/scottgonzalez/pretty-diff/issues/11 // https://github.com/scottgonzalez/pretty-diff/issues/11
// Also, remove some useless lines // Also, remove some useless lines
if (!line || startsWith(line, "*") || if (!line || startsWith(line, "*") ||
startsWith(line, "new") || startsWith(line, "index") || startsWith(line, "new") || startsWith(line, "index")) {
startsWith(line, "---") || startsWith(line, "+++")) {
return; return;
} }
var values = [];
if (startsWith(line, "diff")) { if (startsWith(line, "diff")) {
startFile(line); startFile();
} else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) {
currentFile.oldName = values[1];
} else if (currentFile && !currentFile.newName && (values = /^\+\+\+ b\/(\S+).*$/.exec(line))) {
currentFile.newName = values[1];
} else if (currentFile && startsWith(line, "@@")) { } else if (currentFile && startsWith(line, "@@")) {
startBlock(line); startBlock(line);
} else if (currentBlock) { } else if (currentBlock) {
@ -363,7 +363,7 @@
}; };
var generateSingleLineHtml = function (type, number, content) { var generateSingleLineHtml = function (type, number, content) {
return"<tr>\n" + return "<tr>\n" +
" <td class=\"d2h-code-side-linenumber " + type + "\">" + number + "</td>\n" + " <td class=\"d2h-code-side-linenumber " + type + "\">" + number + "</td>\n" +
" <td class=\"" + type + "\">" + " <td class=\"" + type + "\">" +
" <div class=\"d2h-code-side-line " + type + "\">" + content + "</div>" + " <div class=\"d2h-code-side-line " + type + "\">" + content + "</div>" +
@ -376,7 +376,15 @@
*/ */
var getDiffName = function (oldFilename, newFilename) { var getDiffName = function (oldFilename, newFilename) {
return oldFilename === newFilename ? newFilename : oldFilename + " -> " + newFilename; if (oldFilename && newFilename && oldFilename !== newFilename) {
return oldFilename + " -> " + newFilename;
} else if (newFilename) {
return newFilename;
} else if (oldFilename) {
return oldFilename;
} else {
return "Unknown filename";
}
}; };
var removeIns = function (line) { var removeIns = function (line) {

2
diff2html.min.js vendored

File diff suppressed because one or more lines are too long

125
jsdiff.js
View file

@ -2,6 +2,7 @@
* Javascript Diff Algorithm * Javascript Diff Algorithm
* By John Resig (http://ejohn.org/) * By John Resig (http://ejohn.org/)
* Modified by Chu Alan "sprite" * Modified by Chu Alan "sprite"
* Modified by Rodrigo Fernandes "rtfpessoa"
* *
* Released under the MIT license. * Released under the MIT license.
* *
@ -9,21 +10,11 @@
* http://ejohn.org/projects/javascript-diff-algorithm/ * http://ejohn.org/projects/javascript-diff-algorithm/
*/ */
function escape(s) { function diffString(o, n) {
var n = s;
n = n.replace(/&/g, "&amp;");
n = n.replace(/</g, "&lt;");
n = n.replace(/>/g, "&gt;");
n = n.replace(/"/g, "&quot;");
return n;
}
function diffString( o, n ) {
o = o.replace(/\s+$/, ''); o = o.replace(/\s+$/, '');
n = n.replace(/\s+$/, ''); n = n.replace(/\s+$/, '');
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/) ); var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
var str = ""; var str = "";
var oSpace = o.match(/\s+/g); var oSpace = o.match(/\s+/g);
@ -41,23 +32,23 @@ function diffString( o, n ) {
if (out.n.length == 0) { if (out.n.length == 0) {
for (var i = 0; i < out.o.length; i++) { for (var i = 0; i < out.o.length; i++) {
str += '<del>' + escape(out.o[i]) + oSpace[i] + "</del>"; str += '<del>' + out.o[i] + oSpace[i] + "</del>";
} }
} else { } else {
if (out.n[0].text == null) { if (out.n[0].text == null) {
for (n = 0; n < out.o.length && out.o[n].text == null; n++) { for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
str += '<del>' + escape(out.o[n]) + oSpace[n] + "</del>"; str += '<del>' + out.o[n] + oSpace[n] + "</del>";
} }
} }
for ( var i = 0; i < out.n.length; i++ ) { for (var i = 0; i < out.n.length; i++) {
if (out.n[i].text == null) { if (out.n[i].text == null) {
str += '<ins>' + escape(out.n[i]) + nSpace[i] + "</ins>"; str += '<ins>' + out.n[i] + nSpace[i] + "</ins>";
} else { } else {
var pre = ""; var pre = "";
for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++ ) { for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
pre += '<del>' + escape(out.o[n]) + oSpace[n] + "</del>"; pre += '<del>' + out.o[n] + oSpace[n] + "</del>";
} }
str += " " + out.n[i].text + nSpace[i] + pre; str += " " + out.n[i].text + nSpace[i] + pre;
} }
@ -67,94 +58,44 @@ function diffString( o, n ) {
return str; return str;
} }
function randomColor() { function diff(o, n) {
return "rgb(" + (Math.random() * 100) + "%, " +
(Math.random() * 100) + "%, " +
(Math.random() * 100) + "%)";
}
function diffString2( o, n ) {
o = o.replace(/\s+$/, '');
n = n.replace(/\s+$/, '');
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/) );
var oSpace = o.match(/\s+/g);
if (oSpace == null) {
oSpace = ["\n"];
} else {
oSpace.push("\n");
}
var nSpace = n.match(/\s+/g);
if (nSpace == null) {
nSpace = ["\n"];
} else {
nSpace.push("\n");
}
var os = "";
var colors = new Array();
for (var i = 0; i < out.o.length; i++) {
colors[i] = randomColor();
if (out.o[i].text != null) {
os += '<span style="background-color: ' +colors[i]+ '">' +
escape(out.o[i].text) + oSpace[i] + "</span>";
} else {
os += "<del>" + escape(out.o[i]) + oSpace[i] + "</del>";
}
}
var ns = "";
for (var i = 0; i < out.n.length; i++) {
if (out.n[i].text != null) {
ns += '<span style="background-color: ' +colors[out.n[i].row]+ '">' +
escape(out.n[i].text) + nSpace[i] + "</span>";
} else {
ns += "<ins>" + escape(out.n[i]) + nSpace[i] + "</ins>";
}
}
return { o : os , n : ns };
}
function diff( o, n ) {
var ns = new Object(); var ns = new Object();
var os = new Object(); var os = new Object();
for ( var i = 0; i < n.length; i++ ) { for (var i = 0; i < n.length; i++) {
if ( ns[ n[i] ] == null ) if (ns[n[i]] == null)
ns[ n[i] ] = { rows: new Array(), o: null }; ns[n[i]] = {rows: new Array(), o: null};
ns[ n[i] ].rows.push( i ); ns[n[i]].rows.push(i);
} }
for ( var i = 0; i < o.length; i++ ) { for (var i = 0; i < o.length; i++) {
if ( os[ o[i] ] == null ) if (os[o[i]] == null)
os[ o[i] ] = { rows: new Array(), n: null }; os[o[i]] = {rows: new Array(), n: null};
os[ o[i] ].rows.push( i ); os[o[i]].rows.push(i);
} }
for ( var i in ns ) { for (var i in ns) {
if ( ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1 ) { if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
n[ ns[i].rows[0] ] = { text: n[ ns[i].rows[0] ], row: os[i].rows[0] }; n[ns[i].rows[0]] = {text: n[ns[i].rows[0]], row: os[i].rows[0]};
o[ os[i].rows[0] ] = { text: o[ os[i].rows[0] ], row: ns[i].rows[0] }; o[os[i].rows[0]] = {text: o[os[i].rows[0]], row: ns[i].rows[0]};
} }
} }
for ( var i = 0; i < n.length - 1; i++ ) { for (var i = 0; i < n.length - 1; i++) {
if ( n[i].text != null && n[i+1].text == null && n[i].row + 1 < o.length && o[ n[i].row + 1 ].text == null && if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null &&
n[i+1] == o[ n[i].row + 1 ] ) { n[i + 1] == o[n[i].row + 1]) {
n[i+1] = { text: n[i+1], row: n[i].row + 1 }; n[i + 1] = {text: n[i + 1], row: n[i].row + 1};
o[n[i].row+1] = { text: o[n[i].row+1], row: i + 1 }; o[n[i].row + 1] = {text: o[n[i].row + 1], row: i + 1};
} }
} }
for ( var i = n.length - 1; i > 0; i-- ) { for (var i = n.length - 1; i > 0; i--) {
if ( n[i].text != null && n[i-1].text == null && n[i].row > 0 && o[ n[i].row - 1 ].text == null && if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null &&
n[i-1] == o[ n[i].row - 1 ] ) { n[i - 1] == o[n[i].row - 1]) {
n[i-1] = { text: n[i-1], row: n[i].row - 1 }; n[i - 1] = {text: n[i - 1], row: n[i].row - 1};
o[n[i].row-1] = { text: o[n[i].row-1], row: i - 1 }; o[n[i].row - 1] = {text: o[n[i].row - 1], row: i - 1};
} }
} }
return { o: o, n: n }; return {o: o, n: n};
} }