Highlight changes inside diff lines (#7139).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5094 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-03-11 20:23:29 +00:00
parent 099ba68836
commit 21c79827ff
5 changed files with 241 additions and 86 deletions

View File

@ -1,66 +1,56 @@
<% diff = Redmine::UnifiedDiff.new(diff, :type => diff_type, :max_lines => Setting.diff_max_lines_displayed.to_i) -%> <% diff = Redmine::UnifiedDiff.new(diff, :type => diff_type, :max_lines => Setting.diff_max_lines_displayed.to_i) -%>
<% diff.each do |table_file| -%> <% diff.each do |table_file| -%>
<div class="autoscroll"> <div class="autoscroll">
<% if diff_type == 'sbs' -%> <% if diff.diff_type == 'sbs' -%>
<table class="filecontent"> <table class="filecontent">
<thead> <thead>
<tr><th colspan="4" class="filename"><%=to_utf8 table_file.file_name %></th></tr> <tr><th colspan="4" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
</thead> </thead>
<tbody> <tbody>
<% prev_line_left, prev_line_right = nil, nil -%> <% table_file.each_line do |spacing, line| -%>
<% table_file.keys.sort.each do |key| -%> <% if spacing -%>
<% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
<tr class="spacing"> <tr class="spacing">
<th class="line-num">...</th><td></td><th class="line-num">...</th><td></td> <th class="line-num">...</th><td></td><th class="line-num">...</th><td></td>
</tr>
<% end -%> <% end -%>
<tr> <tr>
<th class="line-num"><%= table_file[key].nb_line_left %></th> <th class="line-num"><%= line.nb_line_left %></th>
<td class="line-code <%= table_file[key].type_diff_left %>"> <td class="line-code <%= line.type_diff_left %>">
<pre><%=to_utf8 table_file[key].line_left %></pre> <pre><%=to_utf8 line.html_line_left %></pre>
</td> </td>
<th class="line-num"><%= table_file[key].nb_line_right %></th> <th class="line-num"><%= line.nb_line_right %></th>
<td class="line-code <%= table_file[key].type_diff_right %>"> <td class="line-code <%= line.type_diff_right %>">
<pre><%=to_utf8 table_file[key].line_right %></pre> <pre><%=to_utf8 line.html_line_right %></pre>
</td> </td>
</tr> </tr>
<% prev_line_left, prev_line_right = table_file[key].nb_line_left.to_i, table_file[key].nb_line_right.to_i -%>
<% end -%> <% end -%>
</tbody> </tbody>
</table> </table>
<% else -%> <% else -%>
<table class="filecontent syntaxhl"> <table class="filecontent">
<thead> <thead>
<tr><th colspan="3" class="filename"><%=to_utf8 table_file.file_name %></th></tr> <tr><th colspan="3" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
</thead> </thead>
<tbody> <tbody>
<% prev_line_left, prev_line_right = nil, nil -%> <% table_file.each_line do |spacing, line| %>
<% table_file.keys.sort.each do |key, line| %> <% if spacing -%>
<% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
<tr class="spacing"> <tr class="spacing">
<th class="line-num">...</th><th class="line-num">...</th><td></td> <th class="line-num">...</th><th class="line-num">...</th><td></td>
</tr> </tr>
<% end -%> <% end -%>
<tr> <tr>
<th class="line-num"><%= table_file[key].nb_line_left %></th> <th class="line-num"><%= line.nb_line_left %></th>
<th class="line-num"><%= table_file[key].nb_line_right %></th> <th class="line-num"><%= line.nb_line_right %></th>
<% if table_file[key].line_left.empty? -%> <td class="line-code <%= line.type_diff %>">
<td class="line-code <%= table_file[key].type_diff_right %>"> <pre><%=to_utf8 line.html_line %></pre>
<pre><%=to_utf8 table_file[key].line_right %></pre>
</td> </td>
<% else -%>
<td class="line-code <%= table_file[key].type_diff_left %>">
<pre><%=to_utf8 table_file[key].line_left %></pre>
</td>
<% end -%>
</tr> </tr>
<% prev_line_left = table_file[key].nb_line_left.to_i if table_file[key].nb_line_left.to_i > 0 -%>
<% prev_line_right = table_file[key].nb_line_right.to_i if table_file[key].nb_line_right.to_i > 0 -%>
<% end -%> <% end -%>
</tbody> </tbody>
</table> </table>
<% end -%> <% end -%>
</div> </div>
<% end -%> <% end -%>

View File

@ -1,5 +1,5 @@
# redMine - project management software # Redmine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang # Copyright (C) 2006-2011 Jean-Philippe Lang
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -18,13 +18,15 @@
module Redmine module Redmine
# Class used to parse unified diffs # Class used to parse unified diffs
class UnifiedDiff < Array class UnifiedDiff < Array
attr_reader :diff_type
def initialize(diff, options={}) def initialize(diff, options={})
options.assert_valid_keys(:type, :max_lines) options.assert_valid_keys(:type, :max_lines)
diff = diff.split("\n") if diff.is_a?(String) diff = diff.split("\n") if diff.is_a?(String)
diff_type = options[:type] || 'inline' @diff_type = options[:type] || 'inline'
lines = 0 lines = 0
@truncated = false @truncated = false
diff_table = DiffTable.new(diff_type) diff_table = DiffTable.new(@diff_type)
diff.each do |line| diff.each do |line|
line_encoding = nil line_encoding = nil
if line.respond_to?(:force_encoding) if line.respond_to?(:force_encoding)
@ -53,17 +55,15 @@ module Redmine
end end
# Class that represents a file diff # Class that represents a file diff
class DiffTable < Hash class DiffTable < Array
attr_reader :file_name, :line_num_l, :line_num_r attr_reader :file_name
# Initialize with a Diff file and the type of Diff View # Initialize with a Diff file and the type of Diff View
# The type view must be inline or sbs (side_by_side) # The type view must be inline or sbs (side_by_side)
def initialize(type="inline") def initialize(type="inline")
@parsing = false @parsing = false
@nb_line = 1 @added = 0
@start = false @removed = 0
@before = 'same'
@second = true
@type = type @type = type
end end
@ -86,12 +86,22 @@ module Redmine
@line_num_l = $2.to_i @line_num_l = $2.to_i
@line_num_r = $5.to_i @line_num_r = $5.to_i
else else
@nb_line += 1 if parse_line(line, @type) parse_line(line, @type)
end end
end end
return true return true
end end
def each_line
prev_line_left, prev_line_right = nil, nil
each do |line|
spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1)
yield spacing, line
prev_line_left = line.nb_line_left.to_i if line.nb_line_left.to_i > 0
prev_line_right = line.nb_line_right.to_i if line.nb_line_right.to_i > 0
end
end
def inspect def inspect
puts '### DIFF TABLE ###' puts '### DIFF TABLE ###'
puts "file : #{file_name}" puts "file : #{file_name}"
@ -101,63 +111,49 @@ module Redmine
end end
private private
# Test if is a Side By Side type
def sbs?(type, func)
if @start and type == "sbs"
if @before == func and @second
tmp_nb_line = @nb_line
self[tmp_nb_line] = Diff.new
else
@second = false
tmp_nb_line = @start
@start += 1
@nb_line -= 1
end
else
tmp_nb_line = @nb_line
@start = @nb_line
self[tmp_nb_line] = Diff.new
@second = true
end
unless self[tmp_nb_line]
@nb_line += 1
self[tmp_nb_line] = Diff.new
else
self[tmp_nb_line]
end
end
# Escape the HTML for the diff # Escape the HTML for the diff
def escapeHTML(line) def escapeHTML(line)
CGI.escapeHTML(line) CGI.escapeHTML(line)
end end
def diff_for_added_line
if @type == 'sbs' && @removed > 0 && @added < @removed
self[-(@removed - @added)]
else
diff = Diff.new
self << diff
diff
end
end
def parse_line(line, type="inline") def parse_line(line, type="inline")
if line[0, 1] == "+" if line[0, 1] == "+"
diff = sbs? type, 'add' diff = diff_for_added_line
@before = 'add'
diff.line_right = escapeHTML line[1..-1] diff.line_right = escapeHTML line[1..-1]
diff.nb_line_right = @line_num_r diff.nb_line_right = @line_num_r
diff.type_diff_right = 'diff_in' diff.type_diff_right = 'diff_in'
@line_num_r += 1 @line_num_r += 1
@added += 1
true true
elsif line[0, 1] == "-" elsif line[0, 1] == "-"
diff = sbs? type, 'remove' diff = Diff.new
@before = 'remove'
diff.line_left = escapeHTML line[1..-1] diff.line_left = escapeHTML line[1..-1]
diff.nb_line_left = @line_num_l diff.nb_line_left = @line_num_l
diff.type_diff_left = 'diff_out' diff.type_diff_left = 'diff_out'
self << diff
@line_num_l += 1 @line_num_l += 1
@removed += 1
true true
elsif line[0, 1] =~ /\s/ else
@before = 'same' write_offsets
@start = false if line[0, 1] =~ /\s/
diff = Diff.new diff = Diff.new
diff.line_right = escapeHTML line[1..-1] diff.line_right = escapeHTML line[1..-1]
diff.nb_line_right = @line_num_r diff.nb_line_right = @line_num_r
diff.line_left = escapeHTML line[1..-1] diff.line_left = escapeHTML line[1..-1]
diff.nb_line_left = @line_num_l diff.nb_line_left = @line_num_l
self[@nb_line] = diff self << diff
@line_num_l += 1 @line_num_l += 1
@line_num_r += 1 @line_num_r += 1
true true
@ -169,6 +165,37 @@ module Redmine
end end
end end
def write_offsets
if @added > 0 && @added == @removed
@added.times do |i|
line = self[-(1 + i)]
removed = (@type == 'sbs') ? line : self[-(1 + @added + i)]
offsets = offsets(removed.line_left, line.line_right)
removed.offsets = line.offsets = offsets
end
end
@added = 0
@removed = 0
end
def offsets(line_left, line_right)
if line_left.present? && line_right.present? && line_left != line_right
max = [line_left.size, line_right.size].min
starting = 0
while starting < max && line_left[starting] == line_right[starting]
starting += 1
end
ending = -1
while ending >= -(max - starting) && line_left[ending] == line_right[ending]
ending -= 1
end
unless starting == 0 && ending == -1
[starting, ending]
end
end
end
end
# A line of diff # A line of diff
class Diff class Diff
attr_accessor :nb_line_left attr_accessor :nb_line_left
@ -177,6 +204,7 @@ module Redmine
attr_accessor :line_right attr_accessor :line_right
attr_accessor :type_diff_right attr_accessor :type_diff_right
attr_accessor :type_diff_left attr_accessor :type_diff_left
attr_accessor :offsets
def initialize() def initialize()
self.nb_line_left = '' self.nb_line_left = ''
@ -187,6 +215,38 @@ module Redmine
self.type_diff_left = '' self.type_diff_left = ''
end end
def type_diff
type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
end
def line
type_diff_right == 'diff_in' ? line_right : line_left
end
def html_line_left
if offsets
line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line_left
end
end
def html_line_right
if offsets
line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line_right
end
end
def html_line
if offsets
line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
else
line
end
end
def inspect def inspect
puts '### Start Line Diff ###' puts '### Start Line Diff ###'
puts self.nb_line_left puts self.nb_line_left

View File

@ -672,7 +672,9 @@ div.autocomplete ul li span.informal {
/***** Diff *****/ /***** Diff *****/
.diff_out { background: #fcc; } .diff_out { background: #fcc; }
.diff_out span { background: #faa; }
.diff_in { background: #cfc; } .diff_in { background: #cfc; }
.diff_in span { background: #afa; }
.text-diff { .text-diff {
padding: 1em; padding: 1em;

46
test/fixtures/diffs/partials.diff vendored Normal file
View File

@ -0,0 +1,46 @@
--- partials.txt Wed Jan 19 12:06:17 2011
+++ partials.1.txt Wed Jan 19 12:06:10 2011
@@ -1,31 +1,31 @@
-Lorem ipsum dolor sit amet, consectetur adipiscing elit
+Lorem ipsum dolor sit amet, consectetur adipiscing xx
Praesent et sagittis dui. Vivamus ac diam diam
-Ut sed auctor justo
+xxx auctor justo
Suspendisse venenatis sollicitudin magna quis suscipit
-Sed blandit gravida odio ac ultrices
+Sed blandit gxxxxa odio ac ultrices
Morbi rhoncus est ut est aliquam tempus
-Morbi id nisi vel felis tincidunt tempus
+Morbi id nisi vel felis xx tempus
Mauris auctor sagittis ante eu luctus
-Fusce commodo felis sed ligula congue molestie
+Fusce commodo felis sed ligula congue
Lorem ipsum dolor sit amet, consectetur adipiscing elit
-Praesent et sagittis dui. Vivamus ac diam diam
+et sagittis dui. Vivamus ac diam diam
Ut sed auctor justo
Suspendisse venenatis sollicitudin magna quis suscipit
Sed blandit gravida odio ac ultrices
-Lorem ipsum dolor sit amet, consectetur adipiscing elit
-Praesent et sagittis dui. Vivamus ac diam diam
+Lorem ipsum dolor sit amet, xxxx adipiscing elit
Ut sed auctor justo
Suspendisse venenatis sollicitudin magna quis suscipit
Sed blandit gravida odio ac ultrices
-Morbi rhoncus est ut est aliquam tempus
+Morbi rhoncus est ut est xxxx tempus
+New line
Morbi id nisi vel felis tincidunt tempus
Mauris auctor sagittis ante eu luctus
Fusce commodo felis sed ligula congue molestie
-Lorem ipsum dolor sit amet, consectetur adipiscing elit
-Praesent et sagittis dui. Vivamus ac diam diam
-Ut sed auctor justo
+Lorem ipsum dolor sit amet, xxxxtetur adipiscing elit
+Praesent et xxxxx. Vivamus ac diam diam
+Ut sed auctor
Suspendisse venenatis sollicitudin magna quis suscipit
Sed blandit gravida odio ac ultrices
Morbi rhoncus est ut est aliquam tempus

View File

@ -1,5 +1,5 @@
# Redmine - project management software # Redmine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang # Copyright (C) 2006-2011 Jean-Philippe Lang
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -34,6 +34,63 @@ class Redmine::UnifiedDiffTest < ActiveSupport::TestCase
assert_equal 2, diff.size assert_equal 2, diff.size
end end
def test_inline_partials
diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
assert_equal 1, diff.size
diff = diff.first
assert_equal 43, diff.size
assert_equal [51, -1], diff[0].offsets
assert_equal [51, -1], diff[1].offsets
assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line
assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line
assert_nil diff[2].offsets
assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line
assert_equal [0, -14], diff[3].offsets
assert_equal [0, -14], diff[4].offsets
assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line
assert_equal '<span>xxx</span> auctor justo', diff[4].html_line
assert_equal [13, -19], diff[6].offsets
assert_equal [13, -19], diff[7].offsets
assert_equal [24, -8], diff[9].offsets
assert_equal [24, -8], diff[10].offsets
assert_equal [37, -1], diff[12].offsets
assert_equal [37, -1], diff[13].offsets
assert_equal [0, -38], diff[15].offsets
assert_equal [0, -38], diff[16].offsets
end
def test_side_by_side_partials
diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
assert_equal 1, diff.size
diff = diff.first
assert_equal 32, diff.size
assert_equal [51, -1], diff[0].offsets
assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left
assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right
assert_nil diff[1].offsets
assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left
assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right
assert_equal [0, -14], diff[2].offsets
assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left
assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right
assert_equal [13, -19], diff[4].offsets
assert_equal [24, -8], diff[6].offsets
assert_equal [37, -1], diff[8].offsets
assert_equal [0, -38], diff[10].offsets
end
def test_line_starting_with_dashes def test_line_starting_with_dashes
diff = Redmine::UnifiedDiff.new(<<-DIFF diff = Redmine::UnifiedDiff.new(<<-DIFF
--- old.txt Wed Nov 11 14:24:58 2009 --- old.txt Wed Nov 11 14:24:58 2009