##// END OF EJS Templates
String#each removed in ruby1.9....
Jean-Philippe Lang -
r2800:4450e6f24b30
parent child
Show More
@@ -1,190 +1,191
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 # Class used to parse unified diffs
19 # Class used to parse unified diffs
20 class UnifiedDiff < Array
20 class UnifiedDiff < Array
21 def initialize(diff, options={})
21 def initialize(diff, options={})
22 options.assert_valid_keys(:type, :max_lines)
22 options.assert_valid_keys(:type, :max_lines)
23 diff = diff.split("\n") if diff.is_a?(String)
23 diff_type = options[:type] || 'inline'
24 diff_type = options[:type] || 'inline'
24
25
25 lines = 0
26 lines = 0
26 @truncated = false
27 @truncated = false
27 diff_table = DiffTable.new(diff_type)
28 diff_table = DiffTable.new(diff_type)
28 diff.each do |line|
29 diff.each do |line|
29 if line =~ /^(---|\+\+\+) (.*)$/
30 if line =~ /^(---|\+\+\+) (.*)$/
30 self << diff_table if diff_table.length > 1
31 self << diff_table if diff_table.length > 1
31 diff_table = DiffTable.new(diff_type)
32 diff_table = DiffTable.new(diff_type)
32 end
33 end
33 diff_table.add_line line
34 diff_table.add_line line
34 lines += 1
35 lines += 1
35 if options[:max_lines] && lines > options[:max_lines]
36 if options[:max_lines] && lines > options[:max_lines]
36 @truncated = true
37 @truncated = true
37 break
38 break
38 end
39 end
39 end
40 end
40 self << diff_table unless diff_table.empty?
41 self << diff_table unless diff_table.empty?
41 self
42 self
42 end
43 end
43
44
44 def truncated?; @truncated; end
45 def truncated?; @truncated; end
45 end
46 end
46
47
47 # Class that represents a file diff
48 # Class that represents a file diff
48 class DiffTable < Hash
49 class DiffTable < Hash
49 attr_reader :file_name, :line_num_l, :line_num_r
50 attr_reader :file_name, :line_num_l, :line_num_r
50
51
51 # Initialize with a Diff file and the type of Diff View
52 # Initialize with a Diff file and the type of Diff View
52 # The type view must be inline or sbs (side_by_side)
53 # The type view must be inline or sbs (side_by_side)
53 def initialize(type="inline")
54 def initialize(type="inline")
54 @parsing = false
55 @parsing = false
55 @nb_line = 1
56 @nb_line = 1
56 @start = false
57 @start = false
57 @before = 'same'
58 @before = 'same'
58 @second = true
59 @second = true
59 @type = type
60 @type = type
60 end
61 end
61
62
62 # Function for add a line of this Diff
63 # Function for add a line of this Diff
63 def add_line(line)
64 def add_line(line)
64 unless @parsing
65 unless @parsing
65 if line =~ /^(---|\+\+\+) (.*)$/
66 if line =~ /^(---|\+\+\+) (.*)$/
66 @file_name = $2
67 @file_name = $2
67 return false
68 return false
68 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
69 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
69 @line_num_l = $2.to_i
70 @line_num_l = $2.to_i
70 @line_num_r = $5.to_i
71 @line_num_r = $5.to_i
71 @parsing = true
72 @parsing = true
72 end
73 end
73 else
74 else
74 if line =~ /^[^\+\-\s@\\]/
75 if line =~ /^[^\+\-\s@\\]/
75 @parsing = false
76 @parsing = false
76 return false
77 return false
77 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
78 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
78 @line_num_l = $2.to_i
79 @line_num_l = $2.to_i
79 @line_num_r = $5.to_i
80 @line_num_r = $5.to_i
80 else
81 else
81 @nb_line += 1 if parse_line(line, @type)
82 @nb_line += 1 if parse_line(line, @type)
82 end
83 end
83 end
84 end
84 return true
85 return true
85 end
86 end
86
87
87 def inspect
88 def inspect
88 puts '### DIFF TABLE ###'
89 puts '### DIFF TABLE ###'
89 puts "file : #{file_name}"
90 puts "file : #{file_name}"
90 self.each do |d|
91 self.each do |d|
91 d.inspect
92 d.inspect
92 end
93 end
93 end
94 end
94
95
95 private
96 private
96 # Test if is a Side By Side type
97 # Test if is a Side By Side type
97 def sbs?(type, func)
98 def sbs?(type, func)
98 if @start and type == "sbs"
99 if @start and type == "sbs"
99 if @before == func and @second
100 if @before == func and @second
100 tmp_nb_line = @nb_line
101 tmp_nb_line = @nb_line
101 self[tmp_nb_line] = Diff.new
102 self[tmp_nb_line] = Diff.new
102 else
103 else
103 @second = false
104 @second = false
104 tmp_nb_line = @start
105 tmp_nb_line = @start
105 @start += 1
106 @start += 1
106 @nb_line -= 1
107 @nb_line -= 1
107 end
108 end
108 else
109 else
109 tmp_nb_line = @nb_line
110 tmp_nb_line = @nb_line
110 @start = @nb_line
111 @start = @nb_line
111 self[tmp_nb_line] = Diff.new
112 self[tmp_nb_line] = Diff.new
112 @second = true
113 @second = true
113 end
114 end
114 unless self[tmp_nb_line]
115 unless self[tmp_nb_line]
115 @nb_line += 1
116 @nb_line += 1
116 self[tmp_nb_line] = Diff.new
117 self[tmp_nb_line] = Diff.new
117 else
118 else
118 self[tmp_nb_line]
119 self[tmp_nb_line]
119 end
120 end
120 end
121 end
121
122
122 # Escape the HTML for the diff
123 # Escape the HTML for the diff
123 def escapeHTML(line)
124 def escapeHTML(line)
124 CGI.escapeHTML(line)
125 CGI.escapeHTML(line)
125 end
126 end
126
127
127 def parse_line(line, type="inline")
128 def parse_line(line, type="inline")
128 if line[0, 1] == "+"
129 if line[0, 1] == "+"
129 diff = sbs? type, 'add'
130 diff = sbs? type, 'add'
130 @before = 'add'
131 @before = 'add'
131 diff.line_right = escapeHTML line[1..-1]
132 diff.line_right = escapeHTML line[1..-1]
132 diff.nb_line_right = @line_num_r
133 diff.nb_line_right = @line_num_r
133 diff.type_diff_right = 'diff_in'
134 diff.type_diff_right = 'diff_in'
134 @line_num_r += 1
135 @line_num_r += 1
135 true
136 true
136 elsif line[0, 1] == "-"
137 elsif line[0, 1] == "-"
137 diff = sbs? type, 'remove'
138 diff = sbs? type, 'remove'
138 @before = 'remove'
139 @before = 'remove'
139 diff.line_left = escapeHTML line[1..-1]
140 diff.line_left = escapeHTML line[1..-1]
140 diff.nb_line_left = @line_num_l
141 diff.nb_line_left = @line_num_l
141 diff.type_diff_left = 'diff_out'
142 diff.type_diff_left = 'diff_out'
142 @line_num_l += 1
143 @line_num_l += 1
143 true
144 true
144 elsif line[0, 1] =~ /\s/
145 elsif line[0, 1] =~ /\s/
145 @before = 'same'
146 @before = 'same'
146 @start = false
147 @start = false
147 diff = Diff.new
148 diff = Diff.new
148 diff.line_right = escapeHTML line[1..-1]
149 diff.line_right = escapeHTML line[1..-1]
149 diff.nb_line_right = @line_num_r
150 diff.nb_line_right = @line_num_r
150 diff.line_left = escapeHTML line[1..-1]
151 diff.line_left = escapeHTML line[1..-1]
151 diff.nb_line_left = @line_num_l
152 diff.nb_line_left = @line_num_l
152 self[@nb_line] = diff
153 self[@nb_line] = diff
153 @line_num_l += 1
154 @line_num_l += 1
154 @line_num_r += 1
155 @line_num_r += 1
155 true
156 true
156 elsif line[0, 1] = "\\"
157 elsif line[0, 1] = "\\"
157 true
158 true
158 else
159 else
159 false
160 false
160 end
161 end
161 end
162 end
162 end
163 end
163
164
164 # A line of diff
165 # A line of diff
165 class Diff
166 class Diff
166 attr_accessor :nb_line_left
167 attr_accessor :nb_line_left
167 attr_accessor :line_left
168 attr_accessor :line_left
168 attr_accessor :nb_line_right
169 attr_accessor :nb_line_right
169 attr_accessor :line_right
170 attr_accessor :line_right
170 attr_accessor :type_diff_right
171 attr_accessor :type_diff_right
171 attr_accessor :type_diff_left
172 attr_accessor :type_diff_left
172
173
173 def initialize()
174 def initialize()
174 self.nb_line_left = ''
175 self.nb_line_left = ''
175 self.nb_line_right = ''
176 self.nb_line_right = ''
176 self.line_left = ''
177 self.line_left = ''
177 self.line_right = ''
178 self.line_right = ''
178 self.type_diff_right = ''
179 self.type_diff_right = ''
179 self.type_diff_left = ''
180 self.type_diff_left = ''
180 end
181 end
181
182
182 def inspect
183 def inspect
183 puts '### Start Line Diff ###'
184 puts '### Start Line Diff ###'
184 puts self.nb_line_left
185 puts self.nb_line_left
185 puts self.line_left
186 puts self.line_left
186 puts self.nb_line_right
187 puts self.nb_line_right
187 puts self.line_right
188 puts self.line_right
188 end
189 end
189 end
190 end
190 end
191 end
General Comments 0
You need to be logged in to leave comments. Login now