mirror of
https://github.com/kennethreitz-archive/gitx.git
synced 2026-06-05 23:40:18 +00:00
DiffHighlighter: Add a binary file callback
This allows a caller to return custom code when a file is marked as binary, such as the option to display the file
This commit is contained in:
+48
-25
@@ -21,46 +21,76 @@ var highlightDiff = function(diff, element, callbacks) {
|
||||
var diffContent = "";
|
||||
var finalContent = "";
|
||||
var lines = content.split('\n');
|
||||
var binary = false;
|
||||
|
||||
var hunk_start_line_1 = -1;
|
||||
var hunk_start_line_2 = -1;
|
||||
|
||||
var header = false;
|
||||
|
||||
var finishContent = function()
|
||||
{
|
||||
finalContent += '<div class="file" id="file_index_' + (file_index - 2) + '">' +
|
||||
'<div class="fileHeader">' + filename + '</div>';
|
||||
if (!binary) {
|
||||
finalContent += '<div class="diffContent">' +
|
||||
'<div class="lineno">' + line1 + "</div>" +
|
||||
'<div class="lineno">' + line2 + "</div>" +
|
||||
'<div class="lines">' + diffContent + "</div>" +
|
||||
'</div>';
|
||||
}
|
||||
else {
|
||||
if (callbacks["binaryFile"])
|
||||
finalContent += callbacks["binaryFile"](filename);
|
||||
else
|
||||
finalContent += "<div>Binary file differs</div>";
|
||||
}
|
||||
|
||||
finalContent += '</div>';
|
||||
line1 = "";
|
||||
line2 = "";
|
||||
diffContent = "";
|
||||
}
|
||||
for (var lineno = 0; lineno < lines.length; lineno++) {
|
||||
var l = lines[lineno];
|
||||
|
||||
var firstChar = l.charAt(0);
|
||||
|
||||
if (header) {
|
||||
if (firstChar == "+" || firstChar == "-")
|
||||
continue;
|
||||
} else if (firstChar == "d") { // New file, we have to reset everything
|
||||
// Matches "diff --git ...."
|
||||
|
||||
if (firstChar == "d") { // New file, we have to reset everything
|
||||
header = true;
|
||||
|
||||
if (file_index++) // Finish last file
|
||||
{
|
||||
finalContent += '<div class="file" id="file_index_' + (file_index - 2) + '">' +
|
||||
'<div class="fileHeader">' + filename + '</div>' +
|
||||
'<div class="diffContent">' +
|
||||
'<div class="lineno">' + line1 + "</div>" +
|
||||
'<div class="lineno">' + line2 + "</div>" +
|
||||
'<div class="lines">' + diffContent + "</div>" +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
line1 = "";
|
||||
line2 = "";
|
||||
diffContent = "";
|
||||
}
|
||||
finishContent();
|
||||
|
||||
binary = false;
|
||||
|
||||
if(match = l.match(/diff --git a\/(\S*)/)) {
|
||||
filename = match[1];
|
||||
if (callbacks["newfile"])
|
||||
callbacks["newfile"](filename, "file_index_" + (file_index - 1));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (header) {
|
||||
if (firstChar == "+" || firstChar == "-")
|
||||
continue;
|
||||
if (firstChar == "B") // "Binary files"
|
||||
{
|
||||
binary = true;
|
||||
Controller.log_("Binary file");
|
||||
}
|
||||
|
||||
// Finish the header
|
||||
if (firstChar == "@")
|
||||
header = false;
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
if (firstChar == "+") {
|
||||
// Highlight trailing whitespace
|
||||
if (m = l.match(/\s+$/))
|
||||
@@ -93,14 +123,7 @@ var highlightDiff = function(diff, element, callbacks) {
|
||||
}
|
||||
}
|
||||
|
||||
finalContent += '<div class="file" id="file_index_' + (file_index - 1) + '">' +
|
||||
'<div class="fileHeader">' + filename + '</div>' +
|
||||
'<div class="diffContent">' +
|
||||
'<div class="lineno">' + line1 + "</div>" +
|
||||
'<div class="lineno">' + line2 + "</div>" +
|
||||
'<div class="lines">' + diffContent + "</div>" +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
finishContent();
|
||||
|
||||
// This takes about 7ms
|
||||
element.innerHTML = finalContent;
|
||||
|
||||
@@ -33,13 +33,6 @@ var Commit = function(obj) {
|
||||
|
||||
this.author_date = new Date(parseInt(match[3]) * 1000);
|
||||
|
||||
this.files = this.raw.match(/diff --git a\/\S*/g);
|
||||
|
||||
for (var i=0; i < this.files.length; i++) {
|
||||
var match = this.files[i].match(/diff --git a\/(.*)/);
|
||||
this.files[i] = match[1];
|
||||
}
|
||||
|
||||
match = this.header.match(/\ncommitter (.*) <(.*@.*)> ([0-9].*)/);
|
||||
this.committer_name = match[1];
|
||||
this.committer_email = match[2];
|
||||
|
||||
Reference in New Issue
Block a user