patch #9627 Add Side by Side in Diff view (Cyril Mougel)

git-svn-id: http://redmine.rubyforge.org/svn/trunk@390 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2007-03-29 18:14:59 +00:00
parent d8ce97f766
commit 2b70760594
10 changed files with 277 additions and 64 deletions

View File

@ -74,7 +74,8 @@ class RepositoriesController < ApplicationController
def diff def diff
@rev_to = params[:rev_to] || (@rev-1) @rev_to = params[:rev_to] || (@rev-1)
@diff = @repository.scm.diff(params[:path], @rev, @rev_to) type = params[:type] || 'inline'
@diff = @repository.scm.diff(params[:path], @rev, @rev_to, type)
show_error and return unless @diff show_error and return unless @diff
end end
@ -204,4 +205,4 @@ class Date
def weeks_ago(date = Date.today) def weeks_ago(date = Date.today)
(date.year - self.year)*52 + (date.cweek - self.cweek) (date.year - self.year)*52 + (date.cweek - self.cweek)
end end
end end

View File

@ -16,6 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'rexml/document' require 'rexml/document'
require 'cgi'
module SvnRepos module SvnRepos
@ -142,7 +143,7 @@ module SvnRepos
raise CommandFailed raise CommandFailed
end end
def diff(path, identifier_from, identifier_to=nil) def diff(path, identifier_from, identifier_to=nil, type="inline")
path ||= '' path ||= ''
if identifier_to and identifier_to.to_i > 0 if identifier_to and identifier_to.to_i > 0
identifier_to = identifier_to.to_i identifier_to = identifier_to.to_i
@ -161,7 +162,8 @@ module SvnRepos
end end
end end
return nil if $? && $?.exitstatus != 0 return nil if $? && $?.exitstatus != 0
diff DiffTableList.new diff, type
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
raise CommandFailed raise CommandFailed
end end
@ -263,5 +265,167 @@ module SvnRepos
self.message = attributes[:message] || "" self.message = attributes[:message] || ""
self.paths = attributes[:paths] self.paths = attributes[:paths]
end end
end
# A line of Diff
class Diff
attr_accessor :nb_line_left
attr_accessor :line_left
attr_accessor :nb_line_right
attr_accessor :line_right
attr_accessor :type_diff_right
attr_accessor :type_diff_left
def initialize ()
self.nb_line_left = ''
self.nb_line_right = ''
self.line_left = ''
self.line_right = ''
self.type_diff_right = ''
self.type_diff_left = ''
end
def inspect
puts '### Start Line Diff ###'
puts self.nb_line_left
puts self.line_left
puts self.nb_line_right
puts self.line_right
end
end
class DiffTableList < Array
def initialize (diff, type="inline")
diff_table = DiffTable.new type
diff.each do |line|
if line =~ /^Index: (.*)$/
self << diff_table if diff_table.length > 1
diff_table = DiffTable.new type
end
a = diff_table.add_line line
end
self << diff_table
end
end
# Class for create a Diff
class DiffTable < Hash
attr_reader :file_name, :line_num_l, :line_num_r
# Initialize with a Diff file and the type of Diff View
# The type view must be inline or sbs (side_by_side)
def initialize (type="inline")
@parsing = false
@nb_line = 1
@start = false
@before = 'same'
@second = true
@type = type
end
# Function for add a line of this Diff
def add_line(line)
unless @parsing
if line =~ /^Index: (.*)$/
@file_name = $1
return false
elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
@line_num_l = $2.to_i
@line_num_r = $5.to_i
@parsing = true
end
else
if line =~ /^_+$/
self.delete(self.keys.sort.last)
@parsing = false
return false
elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
@line_num_l = $2.to_i
@line_num_r = $5.to_i
else
@nb_line += 1 if parse_line(line, @type)
end
end
return true
end
def inspect
puts '### DIFF TABLE ###'
puts "file : #{file_name}"
self.each do |d|
d.inspect
end
end
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
def escapeHTML(line)
CGI.escapeHTML(line).gsub(/\s/, '&nbsp;')
end
def parse_line (line, type="inline")
if line[0, 1] == "+"
diff = sbs? type, 'add'
@before = 'add'
diff.line_left = escapeHTML line[1..-1]
diff.nb_line_left = @line_num_l
diff.type_diff_left = 'diff_in'
@line_num_l += 1
true
elsif line[0, 1] == "-"
diff = sbs? type, 'remove'
@before = 'remove'
diff.line_right = escapeHTML line[1..-1]
diff.nb_line_right = @line_num_r
diff.type_diff_right = 'diff_out'
@line_num_r += 1
true
elsif line[0, 1] =~ /\s/
@before = 'same'
@start = false
diff = Diff.new
diff.line_right = escapeHTML line[1..-1]
diff.nb_line_right = @line_num_r
diff.line_left = escapeHTML line[1..-1]
diff.nb_line_left = @line_num_l
self[@nb_line] = diff
@line_num_l += 1
@line_num_r += 1
true
else
false
end
end
end end
end end

View File

@ -1,66 +1,88 @@
<h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2> <h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2>
<% parsing = false <!-- Choose view type -->
line_num_l = 0 <% form_tag({ :controller => 'repositories', :action => 'diff'}, :method => 'get') do %>
line_num_r = 0 %> <% params.each do |k, p| %>
<% @diff.each do |line| %> <% if k != "type" %>
<% <%= hidden_field_tag(k,p) %>
if line =~ /^Index: (.*)$/ <% end %>
if parsing %> <% end %>
</tbody></table> <p><label><%= l(:label_view_diff) %></label>
<% <%= select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], params[:type]), :onchange => "if (this.value != '') {this.form.submit()}" %>
end <%= submit_tag l(:button_apply) %></p>
parsing = false %> <% end %>
<table class="list"><thead> <% @diff.each do |table_file| %>
<tr><th colspan="3" class="list-filename"><%= l(:label_attachment) %>: <%= $1 %></th></tr> <% if params[:type] == 'sbs' %>
<tr><th>@<%= @rev %></th><th>@<%= @rev_to %></th><th></th></tr> <table class="list">
</thead><tbody> <thead>
<% <tr>
next <th colspan="4" class="list-filename">
elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/ <%= l(:label_attachment) %>: <%= table_file.file_name %>
line_num_l = $2.to_i </th>
line_num_r = $5.to_i </tr>
parsing = true <tr>
next <th colspan="2"><%= l(:label_revision) %> <%= @rev %></th>
elsif line =~ /^_+$/ <th colspan="2"><%= l(:label_revision) %> <%= @rev_to %></th>
# We have reached the 'Properties' section. </tr>
parsing = false </thead>
next <tbody>
end <% table_file.keys.sort.each do |key| %>
next unless parsing <tr>
%> <th class="line-num">
<%= table_file[key].nb_line_left %>
<tr> </th>
<td class="line-code <%= table_file[key].type_diff_left %>">
<% case line[0, 1] <%= table_file[key].line_left %>
when " " %> </td>
<th class="line-num"><%= line_num_l %></th> <th class="line-num">
<th class="line-num"><%= line_num_r %></th> <%= table_file[key].nb_line_right %>
<td class="line-code"> </th>
<% line_num_l = line_num_l + 1 <td class="line-code <%= table_file[key].type_diff_right %>">
line_num_r = line_num_r + 1 <%= table_file[key].line_right %>
</td>
when "-" %> </tr>
<th class="line-num"></th> <% end %>
<th class="line-num"><%= line_num_r %></th> </tbody>
<td class="line-code" style="background: #fdd;"> </table>
<% line_num_r = line_num_r + 1
<% else %>
when "+" %> <table class="list">
<th class="line-num"><%= line_num_l %></th> <thead>
<th class="line-num"></th> <tr>
<td class="line-code" style="background: #dfd;"> <th colspan="3" class="list-filename">
<% line_num_l = line_num_l + 1 <%= l(:label_attachment) %>: <%= table_file.file_name %>
</th>
else </tr>
next <tr>
end %> <th>@<%= @rev %></th>
<th>@<%= @rev_to %></th>
<%= h(line[1..-1]).gsub(/\s/, "&nbsp;") %></td></tr> <th></th>
</tr>
</thead>
<tbody>
<% table_file.keys.sort.each do |key, line| %>
<tr>
<th class="line-num">
<%= table_file[key].nb_line_left %>
</th>
<th class="line-num">
<%= table_file[key].nb_line_right %>
</th>
<% if table_file[key].line_left.empty? %>
<td class="line-code <%= table_file[key].type_diff_right %>">
<%= table_file[key].line_right %>
</td>
<% else %>
<td class="line-code <%= table_file[key].type_diff_left %>">
<%= table_file[key].line_left %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %> <% end %>
</tbody>
</table>
<% content_for :header_tags do %> <% content_for :header_tags do %>
<%= stylesheet_link_tag "scm" %> <%= stylesheet_link_tag "scm" %>

View File

@ -348,6 +348,9 @@ label_change_plural: Änderungen
label_statistics: Statistiken label_statistics: Statistiken
label_commits_per_month: Übertragungen pro Monat label_commits_per_month: Übertragungen pro Monat
label_commits_per_author: Übertragungen pro Autor label_commits_per_author: Übertragungen pro Autor
label_view_diff: View differences
label_diff_inline: inline
label_diff_side_by_side: side by side
button_login: Einloggen button_login: Einloggen
button_submit: OK button_submit: OK

View File

@ -348,6 +348,9 @@ label_change_plural: Changes
label_statistics: Statistics label_statistics: Statistics
label_commits_per_month: Commits per month label_commits_per_month: Commits per month
label_commits_per_author: Commits per author label_commits_per_author: Commits per author
label_view_diff: View differences
label_diff_inline: inline
label_diff_side_by_side: side by side
button_login: Login button_login: Login
button_submit: Submit button_submit: Submit

View File

@ -348,6 +348,9 @@ label_change_plural: Changes
label_statistics: Statistics label_statistics: Statistics
label_commits_per_month: Commits per month label_commits_per_month: Commits per month
label_commits_per_author: Commits per author label_commits_per_author: Commits per author
label_view_diff: View differences
label_diff_inline: inline
label_diff_side_by_side: side by side
button_login: Conexión button_login: Conexión
button_submit: Someter button_submit: Someter

View File

@ -348,6 +348,9 @@ label_change_plural: Changements
label_statistics: Statistiques label_statistics: Statistiques
label_commits_per_month: Commits par mois label_commits_per_month: Commits par mois
label_commits_per_author: Commits par auteur label_commits_per_author: Commits par auteur
label_view_diff: Voir les différences
label_diff_inline: en ligne
label_diff_side_by_side: côte à côte
button_login: Connexion button_login: Connexion
button_submit: Soumettre button_submit: Soumettre

View File

@ -348,6 +348,9 @@ label_change_plural: Changes
label_statistics: Statistics label_statistics: Statistics
label_commits_per_month: Commits per month label_commits_per_month: Commits per month
label_commits_per_author: Commits per author label_commits_per_author: Commits per author
label_view_diff: View differences
label_diff_inline: inline
label_diff_side_by_side: side by side
button_login: Login button_login: Login
button_submit: Invia button_submit: Invia

View File

@ -349,6 +349,9 @@ label_change_plural: Changes
label_statistics: Statistics label_statistics: Statistics
label_commits_per_month: Commits per month label_commits_per_month: Commits per month
label_commits_per_author: Commits per author label_commits_per_author: Commits per author
label_view_diff: View differences
label_diff_inline: inline
label_diff_side_by_side: side by side
button_login: ログイン button_login: ログイン
button_submit: 変更 button_submit: 変更

View File

@ -26,3 +26,11 @@ table.list thead th.list-filename {
font-weight: bolder; font-weight: bolder;
text-align: left; text-align: left;
} }
.diff_out{
background: #fdd;
}
.diff_in{
background: #dfd;
}