comparison lib/redmine/.svn/text-base/unified_diff.rb.svn-base @ 0:513646585e45

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