ignore binary files && fix filenames && fix line count
This commit is contained in:
parent
1f53a81a3b
commit
ad9181531a
3 changed files with 77 additions and 128 deletions
54
diff2html.js
54
diff2html.js
|
|
@ -3,7 +3,7 @@
|
|||
* Diff to HTML (diff2html.js)
|
||||
* Author: rtfpessoa
|
||||
* Date: Friday 29 August 2014
|
||||
* Last Update: Saturday 27 September 2014
|
||||
* Last Update: Saturday 24 January 2015
|
||||
*
|
||||
* Diff command:
|
||||
* git diff
|
||||
|
|
@ -77,14 +77,17 @@
|
|||
};
|
||||
|
||||
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);
|
||||
currentFile = null;
|
||||
}
|
||||
};
|
||||
|
||||
var startFile = function (line) {
|
||||
var startFile = function () {
|
||||
saveBlock();
|
||||
saveFile();
|
||||
|
||||
|
|
@ -93,24 +96,17 @@
|
|||
currentFile.blocks = [];
|
||||
currentFile.deletedLines = 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) {
|
||||
saveBlock();
|
||||
|
||||
var values;
|
||||
if (values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line)) {
|
||||
oldLine = values[2];
|
||||
newLine = values[4];
|
||||
} else {
|
||||
oldLine = 0;
|
||||
newLine = 0;
|
||||
}
|
||||
var values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line) ||
|
||||
/^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line) ||
|
||||
[0, 0, 0];
|
||||
|
||||
oldLine = values[1];
|
||||
newLine = values[2];
|
||||
|
||||
/* create block metadata */
|
||||
currentBlock = {};
|
||||
|
|
@ -125,7 +121,7 @@
|
|||
currentLine.content = line;
|
||||
|
||||
/* fill the line data */
|
||||
if (startsWith(line, "+")) {
|
||||
if (startsWith(line, "+") || startsWith(line, " +")) {
|
||||
currentFile.addedLines++;
|
||||
|
||||
currentLine.type = LINE_TYPE.INSERTS;
|
||||
|
|
@ -134,7 +130,7 @@
|
|||
|
||||
currentBlock.lines.push(currentLine);
|
||||
|
||||
} else if (startsWith(line, "-")) {
|
||||
} else if (startsWith(line, "-") || startsWith(line, " -")) {
|
||||
currentFile.deletedLines++;
|
||||
|
||||
currentLine.type = LINE_TYPE.DELETES;
|
||||
|
|
@ -158,13 +154,17 @@
|
|||
// https://github.com/scottgonzalez/pretty-diff/issues/11
|
||||
// Also, remove some useless lines
|
||||
if (!line || startsWith(line, "*") ||
|
||||
startsWith(line, "new") || startsWith(line, "index") ||
|
||||
startsWith(line, "---") || startsWith(line, "+++")) {
|
||||
startsWith(line, "new") || startsWith(line, "index")) {
|
||||
return;
|
||||
}
|
||||
|
||||
var values = [];
|
||||
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, "@@")) {
|
||||
startBlock(line);
|
||||
} else if (currentBlock) {
|
||||
|
|
@ -376,7 +376,15 @@
|
|||
*/
|
||||
|
||||
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) {
|
||||
|
|
|
|||
2
diff2html.min.js
vendored
2
diff2html.min.js
vendored
File diff suppressed because one or more lines are too long
69
jsdiff.js
69
jsdiff.js
|
|
@ -2,6 +2,7 @@
|
|||
* Javascript Diff Algorithm
|
||||
* By John Resig (http://ejohn.org/)
|
||||
* Modified by Chu Alan "sprite"
|
||||
* Modified by Rodrigo Fernandes "rtfpessoa"
|
||||
*
|
||||
* Released under the MIT license.
|
||||
*
|
||||
|
|
@ -9,16 +10,6 @@
|
|||
* http://ejohn.org/projects/javascript-diff-algorithm/
|
||||
*/
|
||||
|
||||
function escape(s) {
|
||||
var n = s;
|
||||
n = n.replace(/&/g, "&");
|
||||
n = n.replace(/</g, "<");
|
||||
n = n.replace(/>/g, ">");
|
||||
n = n.replace(/"/g, """);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
function diffString(o, n) {
|
||||
o = o.replace(/\s+$/, '');
|
||||
n = n.replace(/\s+$/, '');
|
||||
|
|
@ -41,23 +32,23 @@ function diffString( o, n ) {
|
|||
|
||||
if (out.n.length == 0) {
|
||||
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 {
|
||||
if (out.n[0].text == null) {
|
||||
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++) {
|
||||
if (out.n[i].text == null) {
|
||||
str += '<ins>' + escape(out.n[i]) + nSpace[i] + "</ins>";
|
||||
str += '<ins>' + out.n[i] + nSpace[i] + "</ins>";
|
||||
} else {
|
||||
var pre = "";
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -67,56 +58,6 @@ function diffString( o, n ) {
|
|||
return str;
|
||||
}
|
||||
|
||||
function randomColor() {
|
||||
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 os = new Object();
|
||||
|
|
|
|||
Loading…
Reference in a new issue