From 359346a2d3ab279ebe19fd7b30df2cc53ce21d35 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 1 Dec 2008 15:38:40 +0100 Subject: [PATCH] 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 --- html/lib/diffHighlighter.js | 73 +++++++++++++++++++++++------------ html/views/history/history.js | 7 ---- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/html/lib/diffHighlighter.js b/html/lib/diffHighlighter.js index a9b30cd..73dc787 100644 --- a/html/lib/diffHighlighter.js +++ b/html/lib/diffHighlighter.js @@ -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 += '
' + + '
' + filename + '
'; + if (!binary) { + finalContent += '
' + + '
' + line1 + "
" + + '
' + line2 + "
" + + '
' + diffContent + "
" + + '
'; + } + else { + if (callbacks["binaryFile"]) + finalContent += callbacks["binaryFile"](filename); + else + finalContent += "
Binary file differs
"; + } + + finalContent += '
'; + 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 += '
' + - '
' + filename + '
' + - '
' + - '
' + line1 + "
" + - '
' + line2 + "
" + - '
' + diffContent + "
" + - '
' + - '
'; - 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 += '
' + - '
' + filename + '
' + - '
' + - '
' + line1 + "
" + - '
' + line2 + "
" + - '
' + diffContent + "
" + - '
' + - '
'; + finishContent(); // This takes about 7ms element.innerHTML = finalContent; diff --git a/html/views/history/history.js b/html/views/history/history.js index cd489a4..140fa9a 100644 --- a/html/views/history/history.js +++ b/html/views/history/history.js @@ -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];