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