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