Rudimentary support for renames/deletions of files

diffHighlighter.js, history.js: Distinguish created, renamed, changed and deleted files and display them accordingly
This commit is contained in:
Pieter de Bie
2009-01-20 17:53:05 +01:00
parent 9c0468a4dc
commit 5482dfe450
2 changed files with 64 additions and 24 deletions
+53 -20
View File
@@ -18,7 +18,8 @@ var highlightDiff = function(diff, element, callbacks) {
var file_index = 0;
var filename = "";
var startname = "";
var endname = "";
var line1 = "";
var line2 = "";
var diffContent = "";
@@ -33,8 +34,26 @@ var highlightDiff = function(diff, element, callbacks) {
var finishContent = function()
{
finalContent += '<div class="file" id="file_index_' + (file_index - 2) + '">' +
'<div class="fileHeader">' + filename + '</div>';
if (!file_index)
{
file_index++;
return;
}
if (callbacks["newfile"])
callbacks["newfile"](startname, endname, "file_index_" + (file_index - 1));
var title = startname;
if (endname == "/dev/null")
title = startname;
else if (startname == "/dev/null")
title = endname;
else if (startname != endname)
title = startname + " renamed to " + endname;
finalContent += '<div class="file" id="file_index_' + (file_index - 1) + '">' +
'<div class="fileHeader">' + title + '</div>';
if (!binary) {
finalContent += '<div class="diffContent">' +
'<div class="lineno">' + line1 + "</div>" +
@@ -50,37 +69,52 @@ var highlightDiff = function(diff, element, callbacks) {
}
finalContent += '</div>';
line1 = "";
line2 = "";
diffContent = "";
file_index++;
startname = "";
endname = "";
}
for (var lineno = 0; lineno < lines.length; lineno++) {
var l = lines[lineno];
var firstChar = l.charAt(0);
// Matches "diff --git ...."
if (firstChar == "d" && l.charAt(1) == "i") { // "diff", i.e. new file, we have to reset everything
header = true; // diff always starts with a header
if (firstChar == "d") { // New file, we have to reset everything
header = true;
if (file_index++) // Finish last file
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));
}
finishContent(); // Finish last file
binary = false;
continue;
}
if (header) {
if (firstChar == "+" || firstChar == "-")
if (firstChar == "-") {
if (match = l.match(/^--- (a\/)?(.*)$/))
startname = match[2];
continue;
}
if (firstChar == "+") {
if (match = l.match(/^\+\+\+ (b\/)?(.*)$/))
endname = match[2];
continue;
}
// If it is a complete rename, we don't know the name yet
// We can figure this out from the 'rename from.. rename to.. thing
if (firstChar == 'r')
{
if (match = l.match(/^rename (from|to) (.*)$/))
{
if (match[1] == "from")
startname = match[2];
else
endname = match[2];
}
continue;
}
if (firstChar == "B") // "Binary files"
{
binary = true;
@@ -126,7 +160,6 @@ var highlightDiff = function(diff, element, callbacks) {
}
}
file_index++;
finishContent();
// This takes about 7ms
@@ -135,4 +168,4 @@ var highlightDiff = function(diff, element, callbacks) {
// TODO: Replace this with a performance pref call
if (false)
Controller.log_("Total time:" + (new Date().getTime() - start));
}
}
+11 -4
View File
@@ -173,8 +173,15 @@ var loadCommit = function(commitObject, currentRef) {
}
var showDiff = function() {
var newfile = function(name, id) {
$("files").innerHTML += "<a href='#" + id + "'>" + name + "</a><br>";
var newfile = function(name1, name2, id) {
if (name1 == name2)
$("files").innerHTML += "<a href='#" + id + "'>" + name1 + "</a> <span class='changedfile'>changed</span><br>";
else if (name1 == "/dev/null")
$("files").innerHTML += "<a href='#" + id + "'>" + name2 + "</a> <span class='addedfile'>created</span><br>";
else if (name2 == "/dev/null")
$("files").innerHTML += "<a href='#" + id + "'>" + name1 + "</a> <span class='deletedfile'>deleted</span><br>";
else
$("files").innerHTML += "<a href='#" + id + "'>" + name1 + "</a> <span class='movedfile'>moved to</span> " + name2 + "<br>";
}
var binaryDiff = function(filename) {
@@ -184,7 +191,7 @@ var showDiff = function() {
return "Binary file differs";
}
highlightDiff(commit.diff, $("diff"), { "newfile" : newfile, "binaryFile" : binaryDiff });
highlightDiff(commit.diff, $("diff"), { "newfile" : newfile, "binaryFile" : binaryDiff});
}
var showImage = function(element, filename)
@@ -225,4 +232,4 @@ var loadExtendedCommit = function(commit)
hideNotification();
enableFeatures();
}
}