comparison lib/redmine/unified_diff.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents cbce1fd3b1b7
children 5f33065ddc4b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
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 attr_reader :diff_type 21 attr_reader :diff_type
22 22
23 def initialize(diff, options={}) 23 def initialize(diff, options={})
24 options.assert_valid_keys(:type, :max_lines) 24 options.assert_valid_keys(:type, :max_lines)
25 diff = diff.split("\n") if diff.is_a?(String) 25 diff = diff.split("\n") if diff.is_a?(String)
26 @diff_type = options[:type] || 'inline' 26 @diff_type = options[:type] || 'inline'
27 lines = 0 27 lines = 0
53 53
54 def truncated?; @truncated; end 54 def truncated?; @truncated; end
55 end 55 end
56 56
57 # Class that represents a file diff 57 # Class that represents a file diff
58 class DiffTable < Array 58 class DiffTable < Array
59 attr_reader :file_name 59 attr_reader :file_name
60 60
61 # Initialize with a Diff file and the type of Diff View 61 # Initialize with a Diff file and the type of Diff View
62 # The type view must be inline or sbs (side_by_side) 62 # The type view must be inline or sbs (side_by_side)
63 def initialize(type="inline") 63 def initialize(type="inline")
84 return false 84 return false
85 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/ 85 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
86 @line_num_l = $2.to_i 86 @line_num_l = $2.to_i
87 @line_num_r = $5.to_i 87 @line_num_r = $5.to_i
88 else 88 else
89 parse_line(line, @type) 89 parse_line(line, @type)
90 end 90 end
91 end 91 end
92 return true 92 return true
93 end 93 end
94 94
95 def each_line 95 def each_line
96 prev_line_left, prev_line_right = nil, nil 96 prev_line_left, prev_line_right = nil, nil
97 each do |line| 97 each do |line|
98 spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1) 98 spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1)
99 yield spacing, line 99 yield spacing, line
114 114
115 # Escape the HTML for the diff 115 # Escape the HTML for the diff
116 def escapeHTML(line) 116 def escapeHTML(line)
117 CGI.escapeHTML(line) 117 CGI.escapeHTML(line)
118 end 118 end
119 119
120 def diff_for_added_line 120 def diff_for_added_line
121 if @type == 'sbs' && @removed > 0 && @added < @removed 121 if @type == 'sbs' && @removed > 0 && @added < @removed
122 self[-(@removed - @added)] 122 self[-(@removed - @added)]
123 else 123 else
124 diff = Diff.new 124 diff = Diff.new
162 else 162 else
163 false 163 false
164 end 164 end
165 end 165 end
166 end 166 end
167 167
168 def write_offsets 168 def write_offsets
169 if @added > 0 && @added == @removed 169 if @added > 0 && @added == @removed
170 @added.times do |i| 170 @added.times do |i|
171 line = self[-(1 + i)] 171 line = self[-(1 + i)]
172 removed = (@type == 'sbs') ? line : self[-(1 + @added + i)] 172 removed = (@type == 'sbs') ? line : self[-(1 + @added + i)]
175 end 175 end
176 end 176 end
177 @added = 0 177 @added = 0
178 @removed = 0 178 @removed = 0
179 end 179 end
180 180
181 def offsets(line_left, line_right) 181 def offsets(line_left, line_right)
182 if line_left.present? && line_right.present? && line_left != line_right 182 if line_left.present? && line_right.present? && line_left != line_right
183 max = [line_left.size, line_right.size].min 183 max = [line_left.size, line_right.size].min
184 starting = 0 184 starting = 0
185 while starting < max && line_left[starting] == line_right[starting] 185 while starting < max && line_left[starting] == line_right[starting]
195 end 195 end
196 end 196 end
197 end 197 end
198 198
199 # A line of diff 199 # A line of diff
200 class Diff 200 class Diff
201 attr_accessor :nb_line_left 201 attr_accessor :nb_line_left
202 attr_accessor :line_left 202 attr_accessor :line_left
203 attr_accessor :nb_line_right 203 attr_accessor :nb_line_right
204 attr_accessor :line_right 204 attr_accessor :line_right
205 attr_accessor :type_diff_right 205 attr_accessor :type_diff_right
206 attr_accessor :type_diff_left 206 attr_accessor :type_diff_left
207 attr_accessor :offsets 207 attr_accessor :offsets
208 208
209 def initialize() 209 def initialize()
210 self.nb_line_left = '' 210 self.nb_line_left = ''
211 self.nb_line_right = '' 211 self.nb_line_right = ''
212 self.line_left = '' 212 self.line_left = ''
213 self.line_right = '' 213 self.line_right = ''
214 self.type_diff_right = '' 214 self.type_diff_right = ''
215 self.type_diff_left = '' 215 self.type_diff_left = ''
216 end 216 end
217 217
218 def type_diff 218 def type_diff
219 type_diff_right == 'diff_in' ? type_diff_right : type_diff_left 219 type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
220 end 220 end
221 221
222 def line 222 def line
223 type_diff_right == 'diff_in' ? line_right : line_left 223 type_diff_right == 'diff_in' ? line_right : line_left
224 end 224 end
225 225
226 def html_line_left 226 def html_line_left
227 if offsets 227 if offsets
228 line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>') 228 line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
229 else 229 else
230 line_left 230 line_left
231 end 231 end
232 end 232 end
233 233
234 def html_line_right 234 def html_line_right
235 if offsets 235 if offsets
236 line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>') 236 line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
237 else 237 else
238 line_right 238 line_right
239 end 239 end
240 end 240 end
241 241
242 def html_line 242 def html_line
243 if offsets 243 if offsets
244 line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>') 244 line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
245 else 245 else
246 line 246 line