##// END OF EJS Templates
Ruby 1.9 compatibility of unified_diff.rb....
Toshi MARUYAMA -
r4777:7a9136f93ac5
parent child
Show More
@@ -1,190 +1,198
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 = diff.split("\n") if diff.is_a?(String)
24 diff_type = options[:type] || 'inline'
24 diff_type = options[:type] || 'inline'
25
26 lines = 0
25 lines = 0
27 @truncated = false
26 @truncated = false
28 diff_table = DiffTable.new(diff_type)
27 diff_table = DiffTable.new(diff_type)
29 diff.each do |line|
28 diff.each do |line|
29 line_encoding = nil
30 if line.respond_to?(:force_encoding)
31 line_encoding = line.encoding
32 # TODO: UTF-16 and Japanese CP932 which is imcompatible with ASCII
33 # In Japan, diffrence between file path encoding
34 # and file contents encoding is popular.
35 line.force_encoding('ASCII-8BIT')
36 end
30 unless diff_table.add_line line
37 unless diff_table.add_line line
38 line.force_encoding(line_encoding) if line_encoding
31 self << diff_table if diff_table.length > 1
39 self << diff_table if diff_table.length > 1
32 diff_table = DiffTable.new(diff_type)
40 diff_table = DiffTable.new(diff_type)
33 end
41 end
34 lines += 1
42 lines += 1
35 if options[:max_lines] && lines > options[:max_lines]
43 if options[:max_lines] && lines > options[:max_lines]
36 @truncated = true
44 @truncated = true
37 break
45 break
38 end
46 end
39 end
47 end
40 self << diff_table unless diff_table.empty?
48 self << diff_table unless diff_table.empty?
41 self
49 self
42 end
50 end
43
51
44 def truncated?; @truncated; end
52 def truncated?; @truncated; end
45 end
53 end
46
54
47 # Class that represents a file diff
55 # Class that represents a file diff
48 class DiffTable < Hash
56 class DiffTable < Hash
49 attr_reader :file_name, :line_num_l, :line_num_r
57 attr_reader :file_name, :line_num_l, :line_num_r
50
58
51 # Initialize with a Diff file and the type of Diff View
59 # Initialize with a Diff file and the type of Diff View
52 # The type view must be inline or sbs (side_by_side)
60 # The type view must be inline or sbs (side_by_side)
53 def initialize(type="inline")
61 def initialize(type="inline")
54 @parsing = false
62 @parsing = false
55 @nb_line = 1
63 @nb_line = 1
56 @start = false
64 @start = false
57 @before = 'same'
65 @before = 'same'
58 @second = true
66 @second = true
59 @type = type
67 @type = type
60 end
68 end
61
69
62 # Function for add a line of this Diff
70 # Function for add a line of this Diff
63 # Returns false when the diff ends
71 # Returns false when the diff ends
64 def add_line(line)
72 def add_line(line)
65 unless @parsing
73 unless @parsing
66 if line =~ /^(---|\+\+\+) (.*)$/
74 if line =~ /^(---|\+\+\+) (.*)$/
67 @file_name = $2
75 @file_name = $2
68 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
76 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
69 @line_num_l = $2.to_i
77 @line_num_l = $2.to_i
70 @line_num_r = $5.to_i
78 @line_num_r = $5.to_i
71 @parsing = true
79 @parsing = true
72 end
80 end
73 else
81 else
74 if line =~ /^[^\+\-\s@\\]/
82 if line =~ /^[^\+\-\s@\\]/
75 @parsing = false
83 @parsing = false
76 return false
84 return false
77 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
85 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
78 @line_num_l = $2.to_i
86 @line_num_l = $2.to_i
79 @line_num_r = $5.to_i
87 @line_num_r = $5.to_i
80 else
88 else
81 @nb_line += 1 if parse_line(line, @type)
89 @nb_line += 1 if parse_line(line, @type)
82 end
90 end
83 end
91 end
84 return true
92 return true
85 end
93 end
86
94
87 def inspect
95 def inspect
88 puts '### DIFF TABLE ###'
96 puts '### DIFF TABLE ###'
89 puts "file : #{file_name}"
97 puts "file : #{file_name}"
90 self.each do |d|
98 self.each do |d|
91 d.inspect
99 d.inspect
92 end
100 end
93 end
101 end
94
102
95 private
103 private
96 # Test if is a Side By Side type
104 # Test if is a Side By Side type
97 def sbs?(type, func)
105 def sbs?(type, func)
98 if @start and type == "sbs"
106 if @start and type == "sbs"
99 if @before == func and @second
107 if @before == func and @second
100 tmp_nb_line = @nb_line
108 tmp_nb_line = @nb_line
101 self[tmp_nb_line] = Diff.new
109 self[tmp_nb_line] = Diff.new
102 else
110 else
103 @second = false
111 @second = false
104 tmp_nb_line = @start
112 tmp_nb_line = @start
105 @start += 1
113 @start += 1
106 @nb_line -= 1
114 @nb_line -= 1
107 end
115 end
108 else
116 else
109 tmp_nb_line = @nb_line
117 tmp_nb_line = @nb_line
110 @start = @nb_line
118 @start = @nb_line
111 self[tmp_nb_line] = Diff.new
119 self[tmp_nb_line] = Diff.new
112 @second = true
120 @second = true
113 end
121 end
114 unless self[tmp_nb_line]
122 unless self[tmp_nb_line]
115 @nb_line += 1
123 @nb_line += 1
116 self[tmp_nb_line] = Diff.new
124 self[tmp_nb_line] = Diff.new
117 else
125 else
118 self[tmp_nb_line]
126 self[tmp_nb_line]
119 end
127 end
120 end
128 end
121
129
122 # Escape the HTML for the diff
130 # Escape the HTML for the diff
123 def escapeHTML(line)
131 def escapeHTML(line)
124 CGI.escapeHTML(line)
132 CGI.escapeHTML(line)
125 end
133 end
126
134
127 def parse_line(line, type="inline")
135 def parse_line(line, type="inline")
128 if line[0, 1] == "+"
136 if line[0, 1] == "+"
129 diff = sbs? type, 'add'
137 diff = sbs? type, 'add'
130 @before = 'add'
138 @before = 'add'
131 diff.line_right = escapeHTML line[1..-1]
139 diff.line_right = escapeHTML line[1..-1]
132 diff.nb_line_right = @line_num_r
140 diff.nb_line_right = @line_num_r
133 diff.type_diff_right = 'diff_in'
141 diff.type_diff_right = 'diff_in'
134 @line_num_r += 1
142 @line_num_r += 1
135 true
143 true
136 elsif line[0, 1] == "-"
144 elsif line[0, 1] == "-"
137 diff = sbs? type, 'remove'
145 diff = sbs? type, 'remove'
138 @before = 'remove'
146 @before = 'remove'
139 diff.line_left = escapeHTML line[1..-1]
147 diff.line_left = escapeHTML line[1..-1]
140 diff.nb_line_left = @line_num_l
148 diff.nb_line_left = @line_num_l
141 diff.type_diff_left = 'diff_out'
149 diff.type_diff_left = 'diff_out'
142 @line_num_l += 1
150 @line_num_l += 1
143 true
151 true
144 elsif line[0, 1] =~ /\s/
152 elsif line[0, 1] =~ /\s/
145 @before = 'same'
153 @before = 'same'
146 @start = false
154 @start = false
147 diff = Diff.new
155 diff = Diff.new
148 diff.line_right = escapeHTML line[1..-1]
156 diff.line_right = escapeHTML line[1..-1]
149 diff.nb_line_right = @line_num_r
157 diff.nb_line_right = @line_num_r
150 diff.line_left = escapeHTML line[1..-1]
158 diff.line_left = escapeHTML line[1..-1]
151 diff.nb_line_left = @line_num_l
159 diff.nb_line_left = @line_num_l
152 self[@nb_line] = diff
160 self[@nb_line] = diff
153 @line_num_l += 1
161 @line_num_l += 1
154 @line_num_r += 1
162 @line_num_r += 1
155 true
163 true
156 elsif line[0, 1] = "\\"
164 elsif line[0, 1] = "\\"
157 true
165 true
158 else
166 else
159 false
167 false
160 end
168 end
161 end
169 end
162 end
170 end
163
171
164 # A line of diff
172 # A line of diff
165 class Diff
173 class Diff
166 attr_accessor :nb_line_left
174 attr_accessor :nb_line_left
167 attr_accessor :line_left
175 attr_accessor :line_left
168 attr_accessor :nb_line_right
176 attr_accessor :nb_line_right
169 attr_accessor :line_right
177 attr_accessor :line_right
170 attr_accessor :type_diff_right
178 attr_accessor :type_diff_right
171 attr_accessor :type_diff_left
179 attr_accessor :type_diff_left
172
180
173 def initialize()
181 def initialize()
174 self.nb_line_left = ''
182 self.nb_line_left = ''
175 self.nb_line_right = ''
183 self.nb_line_right = ''
176 self.line_left = ''
184 self.line_left = ''
177 self.line_right = ''
185 self.line_right = ''
178 self.type_diff_right = ''
186 self.type_diff_right = ''
179 self.type_diff_left = ''
187 self.type_diff_left = ''
180 end
188 end
181
189
182 def inspect
190 def inspect
183 puts '### Start Line Diff ###'
191 puts '### Start Line Diff ###'
184 puts self.nb_line_left
192 puts self.nb_line_left
185 puts self.line_left
193 puts self.line_left
186 puts self.nb_line_right
194 puts self.nb_line_right
187 puts self.line_right
195 puts self.line_right
188 end
196 end
189 end
197 end
190 end
198 end
General Comments 0
You need to be logged in to leave comments. Login now