##// END OF EJS Templates
Merged r8876 from trunk....
Jean-Philippe Lang -
r8990:90c6a8579bb4
parent child
Show More
@@ -1,258 +1,259
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 # Class used to parse unified diffs
20 20 class UnifiedDiff < Array
21 21 attr_reader :diff_type
22 22
23 23 def initialize(diff, options={})
24 24 options.assert_valid_keys(:type, :max_lines)
25 25 diff = diff.split("\n") if diff.is_a?(String)
26 26 @diff_type = options[:type] || 'inline'
27 27 lines = 0
28 28 @truncated = false
29 29 diff_table = DiffTable.new(@diff_type)
30 30 diff.each do |line|
31 31 line_encoding = nil
32 32 if line.respond_to?(:force_encoding)
33 33 line_encoding = line.encoding
34 34 # TODO: UTF-16 and Japanese CP932 which is imcompatible with ASCII
35 35 # In Japan, diffrence between file path encoding
36 36 # and file contents encoding is popular.
37 37 line.force_encoding('ASCII-8BIT')
38 38 end
39 39 unless diff_table.add_line line
40 40 line.force_encoding(line_encoding) if line_encoding
41 41 self << diff_table if diff_table.length > 0
42 42 diff_table = DiffTable.new(diff_type)
43 43 end
44 44 lines += 1
45 45 if options[:max_lines] && lines > options[:max_lines]
46 46 @truncated = true
47 47 break
48 48 end
49 49 end
50 50 self << diff_table unless diff_table.empty?
51 51 self
52 52 end
53 53
54 54 def truncated?; @truncated; end
55 55 end
56 56
57 57 # Class that represents a file diff
58 58 class DiffTable < Array
59 59 attr_reader :file_name
60 60
61 61 # Initialize with a Diff file and the type of Diff View
62 62 # The type view must be inline or sbs (side_by_side)
63 63 def initialize(type="inline")
64 64 @parsing = false
65 65 @added = 0
66 66 @removed = 0
67 67 @type = type
68 68 end
69 69
70 70 # Function for add a line of this Diff
71 71 # Returns false when the diff ends
72 72 def add_line(line)
73 73 unless @parsing
74 74 if line =~ /^(---|\+\+\+) (.*)$/
75 75 @file_name = $2
76 76 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
77 77 @line_num_l = $2.to_i
78 78 @line_num_r = $5.to_i
79 79 @parsing = true
80 80 end
81 81 else
82 82 if line =~ /^[^\+\-\s@\\]/
83 83 @parsing = false
84 84 return false
85 85 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
86 86 @line_num_l = $2.to_i
87 87 @line_num_r = $5.to_i
88 88 else
89 89 parse_line(line, @type)
90 90 end
91 91 end
92 92 return true
93 93 end
94 94
95 95 def each_line
96 96 prev_line_left, prev_line_right = nil, nil
97 97 each do |line|
98 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 99 yield spacing, line
100 100 prev_line_left = line.nb_line_left.to_i if line.nb_line_left.to_i > 0
101 101 prev_line_right = line.nb_line_right.to_i if line.nb_line_right.to_i > 0
102 102 end
103 103 end
104 104
105 105 def inspect
106 106 puts '### DIFF TABLE ###'
107 107 puts "file : #{file_name}"
108 108 self.each do |d|
109 109 d.inspect
110 110 end
111 111 end
112 112
113 113 private
114 114
115 # Escape the HTML for the diff
116 def escapeHTML(line)
117 CGI.escapeHTML(line)
118 end
119
120 115 def diff_for_added_line
121 116 if @type == 'sbs' && @removed > 0 && @added < @removed
122 117 self[-(@removed - @added)]
123 118 else
124 119 diff = Diff.new
125 120 self << diff
126 121 diff
127 122 end
128 123 end
129 124
130 125 def parse_line(line, type="inline")
131 126 if line[0, 1] == "+"
132 127 diff = diff_for_added_line
133 diff.line_right = escapeHTML line[1..-1]
128 diff.line_right = line[1..-1]
134 129 diff.nb_line_right = @line_num_r
135 130 diff.type_diff_right = 'diff_in'
136 131 @line_num_r += 1
137 132 @added += 1
138 133 true
139 134 elsif line[0, 1] == "-"
140 135 diff = Diff.new
141 diff.line_left = escapeHTML line[1..-1]
136 diff.line_left = line[1..-1]
142 137 diff.nb_line_left = @line_num_l
143 138 diff.type_diff_left = 'diff_out'
144 139 self << diff
145 140 @line_num_l += 1
146 141 @removed += 1
147 142 true
148 143 else
149 144 write_offsets
150 145 if line[0, 1] =~ /\s/
151 146 diff = Diff.new
152 diff.line_right = escapeHTML line[1..-1]
147 diff.line_right = line[1..-1]
153 148 diff.nb_line_right = @line_num_r
154 diff.line_left = escapeHTML line[1..-1]
149 diff.line_left = line[1..-1]
155 150 diff.nb_line_left = @line_num_l
156 151 self << diff
157 152 @line_num_l += 1
158 153 @line_num_r += 1
159 154 true
160 155 elsif line[0, 1] = "\\"
161 156 true
162 157 else
163 158 false
164 159 end
165 160 end
166 161 end
167 162
168 163 def write_offsets
169 164 if @added > 0 && @added == @removed
170 165 @added.times do |i|
171 166 line = self[-(1 + i)]
172 167 removed = (@type == 'sbs') ? line : self[-(1 + @added + i)]
173 168 offsets = offsets(removed.line_left, line.line_right)
174 169 removed.offsets = line.offsets = offsets
175 170 end
176 171 end
177 172 @added = 0
178 173 @removed = 0
179 174 end
180 175
181 176 def offsets(line_left, line_right)
182 177 if line_left.present? && line_right.present? && line_left != line_right
183 178 max = [line_left.size, line_right.size].min
184 179 starting = 0
185 180 while starting < max && line_left[starting] == line_right[starting]
186 181 starting += 1
187 182 end
188 183 ending = -1
189 184 while ending >= -(max - starting) && line_left[ending] == line_right[ending]
190 185 ending -= 1
191 186 end
192 187 unless starting == 0 && ending == -1
193 188 [starting, ending]
194 189 end
195 190 end
196 191 end
197 192 end
198 193
199 194 # A line of diff
200 195 class Diff
201 196 attr_accessor :nb_line_left
202 197 attr_accessor :line_left
203 198 attr_accessor :nb_line_right
204 199 attr_accessor :line_right
205 200 attr_accessor :type_diff_right
206 201 attr_accessor :type_diff_left
207 202 attr_accessor :offsets
208 203
209 204 def initialize()
210 205 self.nb_line_left = ''
211 206 self.nb_line_right = ''
212 207 self.line_left = ''
213 208 self.line_right = ''
214 209 self.type_diff_right = ''
215 210 self.type_diff_left = ''
216 211 end
217 212
218 213 def type_diff
219 214 type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
220 215 end
221 216
222 217 def line
223 218 type_diff_right == 'diff_in' ? line_right : line_left
224 219 end
225 220
226 221 def html_line_left
227 if offsets
228 line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
229 else
230 line_left
231 end
222 line_to_html(line_left, offsets)
232 223 end
233 224
234 225 def html_line_right
235 if offsets
236 line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
237 else
238 line_right
239 end
226 line_to_html(line_right, offsets)
240 227 end
241 228
242 229 def html_line
243 if offsets
244 line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
245 else
246 line
247 end
230 line_to_html(line, offsets)
248 231 end
249 232
250 233 def inspect
251 234 puts '### Start Line Diff ###'
252 235 puts self.nb_line_left
253 236 puts self.line_left
254 237 puts self.nb_line_right
255 238 puts self.line_right
256 239 end
240
241 private
242
243 def line_to_html(line, offsets)
244 if offsets
245 s = ''
246 unless offsets.first == 0
247 s << CGI.escapeHTML(line[0..offsets.first-1])
248 end
249 s << '<span>' + CGI.escapeHTML(line[offsets.first..offsets.last]) + '</span>'
250 unless offsets.last == -1
251 s << CGI.escapeHTML(line[offsets.last+1..-1])
252 end
253 s
254 else
255 CGI.escapeHTML(line)
256 end
257 end
257 258 end
258 259 end
@@ -1,156 +1,179
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../../test_helper', __FILE__)
19 19
20 20 class Redmine::UnifiedDiffTest < ActiveSupport::TestCase
21 21
22 22 def setup
23 23 end
24 24
25 25 def test_subversion_diff
26 26 diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'))
27 27 # number of files
28 28 assert_equal 4, diff.size
29 29 assert diff.detect {|file| file.file_name =~ %r{^config/settings.yml}}
30 30 end
31 31
32 32 def test_truncate_diff
33 33 diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'), :max_lines => 20)
34 34 assert_equal 2, diff.size
35 35 end
36 36
37 37 def test_inline_partials
38 38 diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
39 39 assert_equal 1, diff.size
40 40 diff = diff.first
41 41 assert_equal 43, diff.size
42 42
43 43 assert_equal [51, -1], diff[0].offsets
44 44 assert_equal [51, -1], diff[1].offsets
45 45 assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line
46 46 assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line
47 47
48 48 assert_nil diff[2].offsets
49 49 assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line
50 50
51 51 assert_equal [0, -14], diff[3].offsets
52 52 assert_equal [0, -14], diff[4].offsets
53 53 assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line
54 54 assert_equal '<span>xxx</span> auctor justo', diff[4].html_line
55 55
56 56 assert_equal [13, -19], diff[6].offsets
57 57 assert_equal [13, -19], diff[7].offsets
58 58
59 59 assert_equal [24, -8], diff[9].offsets
60 60 assert_equal [24, -8], diff[10].offsets
61 61
62 62 assert_equal [37, -1], diff[12].offsets
63 63 assert_equal [37, -1], diff[13].offsets
64 64
65 65 assert_equal [0, -38], diff[15].offsets
66 66 assert_equal [0, -38], diff[16].offsets
67 67 end
68 68
69 69 def test_side_by_side_partials
70 70 diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
71 71 assert_equal 1, diff.size
72 72 diff = diff.first
73 73 assert_equal 32, diff.size
74 74
75 75 assert_equal [51, -1], diff[0].offsets
76 76 assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left
77 77 assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right
78 78
79 79 assert_nil diff[1].offsets
80 80 assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left
81 81 assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right
82 82
83 83 assert_equal [0, -14], diff[2].offsets
84 84 assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left
85 85 assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right
86 86
87 87 assert_equal [13, -19], diff[4].offsets
88 88 assert_equal [24, -8], diff[6].offsets
89 89 assert_equal [37, -1], diff[8].offsets
90 90 assert_equal [0, -38], diff[10].offsets
91 91
92 92 end
93 93
94 def test_partials_with_html_entities
95 raw = <<-DIFF
96 --- test.orig.txt Wed Feb 15 16:10:39 2012
97 +++ test.new.txt Wed Feb 15 16:11:25 2012
98 @@ -1,5 +1,5 @@
99 Semicolons were mysteriously appearing in code diffs in the repository
100
101 -void DoSomething(std::auto_ptr<MyClass> myObj)
102 +void DoSomething(const MyClass& myObj)
103
104 DIFF
105
106 diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
107 assert_equal 1, diff.size
108 assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line_left
109 assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[2].html_line_right
110
111 diff = Redmine::UnifiedDiff.new(raw, :type => 'inline')
112 assert_equal 1, diff.size
113 assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line
114 assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[3].html_line
115 end
116
94 117 def test_line_starting_with_dashes
95 118 diff = Redmine::UnifiedDiff.new(<<-DIFF
96 119 --- old.txt Wed Nov 11 14:24:58 2009
97 120 +++ new.txt Wed Nov 11 14:25:02 2009
98 121 @@ -1,8 +1,4 @@
99 122 -Lines that starts with dashes:
100 123 -
101 124 -------------------------
102 125 --- file.c
103 126 -------------------------
104 127 +A line that starts with dashes:
105 128
106 129 and removed.
107 130
108 131 @@ -23,4 +19,4 @@
109 132
110 133
111 134
112 135 -Another chunk of change
113 136 +Another chunk of changes
114 137
115 138 DIFF
116 139 )
117 140 assert_equal 1, diff.size
118 141 end
119 142
120 143 def test_one_line_new_files
121 144 diff = Redmine::UnifiedDiff.new(<<-DIFF
122 145 diff -r 000000000000 -r ea98b14f75f0 README1
123 146 --- /dev/null
124 147 +++ b/README1
125 148 @@ -0,0 +1,1 @@
126 149 +test1
127 150 diff -r 000000000000 -r ea98b14f75f0 README2
128 151 --- /dev/null
129 152 +++ b/README2
130 153 @@ -0,0 +1,1 @@
131 154 +test2
132 155 diff -r 000000000000 -r ea98b14f75f0 README3
133 156 --- /dev/null
134 157 +++ b/README3
135 158 @@ -0,0 +1,3 @@
136 159 +test4
137 160 +test5
138 161 +test6
139 162 diff -r 000000000000 -r ea98b14f75f0 README4
140 163 --- /dev/null
141 164 +++ b/README4
142 165 @@ -0,0 +1,3 @@
143 166 +test4
144 167 +test5
145 168 +test6
146 169 DIFF
147 170 )
148 171 assert_equal 4, diff.size
149 172 end
150 173
151 174 private
152 175
153 176 def read_diff_fixture(filename)
154 177 File.new(File.join(File.dirname(__FILE__), '/../../../fixtures/diffs', filename)).read
155 178 end
156 179 end
General Comments 0
You need to be logged in to leave comments. Login now