@@ -1,344 +1,344 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | class QueryColumn |
|
19 | 19 | attr_accessor :name, :sortable |
|
20 | 20 | include GLoc |
|
21 | 21 | |
|
22 | 22 | def initialize(name, options={}) |
|
23 | 23 | self.name = name |
|
24 | 24 | self.sortable = options[:sortable] |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def caption |
|
28 | 28 | set_language_if_valid(User.current.language) |
|
29 | 29 | l("field_#{name}") |
|
30 | 30 | end |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | class QueryCustomFieldColumn < QueryColumn |
|
34 | 34 | |
|
35 | 35 | def initialize(custom_field) |
|
36 | 36 | self.name = "cf_#{custom_field.id}".to_sym |
|
37 | 37 | self.sortable = false |
|
38 | 38 | @cf = custom_field |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def caption |
|
42 | 42 | @cf.name |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def custom_field |
|
46 | 46 | @cf |
|
47 | 47 | end |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | class Query < ActiveRecord::Base |
|
51 | 51 | belongs_to :project |
|
52 | 52 | belongs_to :user |
|
53 | 53 | serialize :filters |
|
54 | 54 | serialize :column_names |
|
55 | 55 | |
|
56 | 56 | attr_protected :project, :user |
|
57 | 57 | attr_accessor :executed_by |
|
58 | 58 | |
|
59 | 59 | validates_presence_of :name, :on => :save |
|
60 | 60 | validates_length_of :name, :maximum => 255 |
|
61 | 61 | |
|
62 | 62 | @@operators = { "=" => :label_equals, |
|
63 | 63 | "!" => :label_not_equals, |
|
64 | 64 | "o" => :label_open_issues, |
|
65 | 65 | "c" => :label_closed_issues, |
|
66 | 66 | "!*" => :label_none, |
|
67 | 67 | "*" => :label_all, |
|
68 | 68 | ">=" => '>=', |
|
69 | 69 | "<=" => '<=', |
|
70 | 70 | "<t+" => :label_in_less_than, |
|
71 | 71 | ">t+" => :label_in_more_than, |
|
72 | 72 | "t+" => :label_in, |
|
73 | 73 | "t" => :label_today, |
|
74 | 74 | "w" => :label_this_week, |
|
75 | 75 | ">t-" => :label_less_than_ago, |
|
76 | 76 | "<t-" => :label_more_than_ago, |
|
77 | 77 | "t-" => :label_ago, |
|
78 | 78 | "~" => :label_contains, |
|
79 | 79 | "!~" => :label_not_contains } |
|
80 | 80 | |
|
81 | 81 | cattr_reader :operators |
|
82 | 82 | |
|
83 | 83 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
84 | 84 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
85 | 85 | :list_optional => [ "=", "!", "!*", "*" ], |
|
86 | 86 | :list_one_or_more => [ "*", "=" ], |
|
87 | 87 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], |
|
88 | 88 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], |
|
89 | 89 | :string => [ "=", "~", "!", "!~" ], |
|
90 | 90 | :text => [ "~", "!~" ], |
|
91 | 91 | :integer => [ "=", ">=", "<=" ] } |
|
92 | 92 | |
|
93 | 93 | cattr_reader :operators_by_filter_type |
|
94 | 94 | |
|
95 | 95 | @@available_columns = [ |
|
96 | 96 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"), |
|
97 | 97 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"), |
|
98 | 98 | QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position"), |
|
99 | 99 | QueryColumn.new(:subject), |
|
100 | 100 | QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"), |
|
101 | 101 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on"), |
|
102 | 102 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"), |
|
103 | 103 | QueryColumn.new(:fixed_version), |
|
104 | 104 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
105 | 105 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
106 | 106 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
107 | 107 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"), |
|
108 | 108 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on"), |
|
109 | 109 | ] |
|
110 | 110 | cattr_reader :available_columns |
|
111 | 111 | |
|
112 | 112 | def initialize(attributes = nil) |
|
113 | 113 | super attributes |
|
114 | 114 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
115 | 115 | @executed_by = User.current.logged? ? User.current : nil |
|
116 | 116 | set_language_if_valid(executed_by.language) if executed_by |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def validate |
|
120 | 120 | filters.each_key do |field| |
|
121 | 121 | errors.add label_for(field), :activerecord_error_blank unless |
|
122 | 122 | # filter requires one or more values |
|
123 | 123 | (values_for(field) and !values_for(field).first.empty?) or |
|
124 | 124 | # filter doesn't require any value |
|
125 | 125 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
126 | 126 | end if filters |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def editable_by?(user) |
|
130 | 130 | return false unless user |
|
131 | 131 | return true if !is_public && self.user_id == user.id |
|
132 | 132 | is_public && user.allowed_to?(:manage_public_queries, project) |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def available_filters |
|
136 | 136 | return @available_filters if @available_filters |
|
137 | 137 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
138 | 138 | "tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
139 | 139 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } }, |
|
140 | 140 | "subject" => { :type => :text, :order => 8 }, |
|
141 | 141 | "created_on" => { :type => :date_past, :order => 9 }, |
|
142 | 142 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
143 | 143 | "start_date" => { :type => :date, :order => 11 }, |
|
144 | 144 | "due_date" => { :type => :date, :order => 12 }, |
|
145 | 145 | "done_ratio" => { :type => :integer, :order => 13 }} |
|
146 | 146 | |
|
147 | 147 | user_values = [] |
|
148 | 148 | user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by |
|
149 | 149 | if project |
|
150 | 150 | user_values += project.users.collect{|s| [s.name, s.id.to_s] } |
|
151 | 151 | elsif executed_by |
|
152 | 152 | # members of the user's projects |
|
153 | 153 | user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } |
|
154 | 154 | end |
|
155 | 155 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? |
|
156 | 156 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? |
|
157 | 157 | |
|
158 | 158 | if project |
|
159 | 159 | # project specific filters |
|
160 | 160 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } |
|
161 | 161 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } |
|
162 | 162 | unless @project.active_children.empty? |
|
163 | 163 | @available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } |
|
164 | 164 | end |
|
165 | 165 | @project.all_custom_fields.select(&:is_filter?).each do |field| |
|
166 | 166 | case field.field_format |
|
167 | when "string", "int" | |
|
168 | options = { :type => :string, :order => 20 } | |
|
169 | 167 | when "text" |
|
170 | 168 | options = { :type => :text, :order => 20 } |
|
171 | 169 | when "list" |
|
172 | 170 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
173 | 171 | when "date" |
|
174 | 172 | options = { :type => :date, :order => 20 } |
|
175 | 173 | when "bool" |
|
176 | 174 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
175 | else | |
|
176 | options = { :type => :string, :order => 20 } | |
|
177 | 177 | end |
|
178 | 178 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
179 | 179 | end |
|
180 | 180 | # remove category filter if no category defined |
|
181 | 181 | @available_filters.delete "category_id" if @available_filters["category_id"][:values].empty? |
|
182 | 182 | end |
|
183 | 183 | @available_filters |
|
184 | 184 | end |
|
185 | 185 | |
|
186 | 186 | def add_filter(field, operator, values) |
|
187 | 187 | # values must be an array |
|
188 | 188 | return unless values and values.is_a? Array # and !values.first.empty? |
|
189 | 189 | # check if field is defined as an available filter |
|
190 | 190 | if available_filters.has_key? field |
|
191 | 191 | filter_options = available_filters[field] |
|
192 | 192 | # check if operator is allowed for that filter |
|
193 | 193 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
194 | 194 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
195 | 195 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
196 | 196 | #end |
|
197 | 197 | filters[field] = {:operator => operator, :values => values } |
|
198 | 198 | end |
|
199 | 199 | end |
|
200 | 200 | |
|
201 | 201 | def add_short_filter(field, expression) |
|
202 | 202 | return unless expression |
|
203 | 203 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first |
|
204 | 204 | add_filter field, (parms[0] || "="), [parms[1] || ""] |
|
205 | 205 | end |
|
206 | 206 | |
|
207 | 207 | def has_filter?(field) |
|
208 | 208 | filters and filters[field] |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def operator_for(field) |
|
212 | 212 | has_filter?(field) ? filters[field][:operator] : nil |
|
213 | 213 | end |
|
214 | 214 | |
|
215 | 215 | def values_for(field) |
|
216 | 216 | has_filter?(field) ? filters[field][:values] : nil |
|
217 | 217 | end |
|
218 | 218 | |
|
219 | 219 | def label_for(field) |
|
220 | 220 | label = @available_filters[field][:name] if @available_filters.has_key?(field) |
|
221 | 221 | label ||= field.gsub(/\_id$/, "") |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | def available_columns |
|
225 | 225 | return @available_columns if @available_columns |
|
226 | 226 | @available_columns = Query.available_columns |
|
227 | 227 | @available_columns += (project ? |
|
228 | 228 | project.custom_fields : |
|
229 | 229 | IssueCustomField.find(:all, :conditions => {:is_for_all => true}) |
|
230 | 230 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | def columns |
|
234 | 234 | if has_default_columns? |
|
235 | 235 | available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) } |
|
236 | 236 | else |
|
237 | 237 | # preserve the column_names order |
|
238 | 238 | column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact |
|
239 | 239 | end |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | def column_names=(names) |
|
243 | 243 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names |
|
244 | 244 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names |
|
245 | 245 | write_attribute(:column_names, names) |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | def has_column?(column) |
|
249 | 249 | column_names && column_names.include?(column.name) |
|
250 | 250 | end |
|
251 | 251 | |
|
252 | 252 | def has_default_columns? |
|
253 | 253 | column_names.nil? || column_names.empty? |
|
254 | 254 | end |
|
255 | 255 | |
|
256 | 256 | def statement |
|
257 | 257 | # project/subprojects clause |
|
258 | 258 | clause = '' |
|
259 | 259 | if project && has_filter?("subproject_id") |
|
260 | 260 | subproject_ids = [] |
|
261 | 261 | if operator_for("subproject_id") == "=" |
|
262 | 262 | subproject_ids = values_for("subproject_id").each(&:to_i) |
|
263 | 263 | else |
|
264 | 264 | subproject_ids = project.active_children.collect{|p| p.id} |
|
265 | 265 | end |
|
266 | 266 | clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project |
|
267 | 267 | elsif project |
|
268 | 268 | clause << "#{Issue.table_name}.project_id=%d" % project.id |
|
269 | 269 | else |
|
270 | 270 | clause << Project.visible_by(executed_by) |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | # filters clauses |
|
274 | 274 | filters_clauses = [] |
|
275 | 275 | filters.each_key do |field| |
|
276 | 276 | next if field == "subproject_id" |
|
277 | 277 | v = values_for(field).clone |
|
278 | 278 | next unless v and !v.empty? |
|
279 | 279 | |
|
280 | 280 | sql = '' |
|
281 | 281 | if field =~ /^cf_(\d+)$/ |
|
282 | 282 | # custom field |
|
283 | 283 | db_table = CustomValue.table_name |
|
284 | 284 | db_field = 'value' |
|
285 | 285 | sql << "#{Issue.table_name}.id IN (SELECT #{db_table}.customized_id FROM #{db_table} where #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} AND " |
|
286 | 286 | else |
|
287 | 287 | # regular field |
|
288 | 288 | db_table = Issue.table_name |
|
289 | 289 | db_field = field |
|
290 | 290 | sql << '(' |
|
291 | 291 | end |
|
292 | 292 | |
|
293 | 293 | # "me" value subsitution |
|
294 | 294 | if %w(assigned_to_id author_id).include?(field) |
|
295 | 295 | v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me") |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | case operator_for field |
|
299 | 299 | when "=" |
|
300 | 300 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
301 | 301 | when "!" |
|
302 | 302 | sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
303 | 303 | when "!*" |
|
304 | 304 | sql = sql + "#{db_table}.#{db_field} IS NULL" |
|
305 | 305 | when "*" |
|
306 | 306 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" |
|
307 | 307 | when ">=" |
|
308 | 308 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" |
|
309 | 309 | when "<=" |
|
310 | 310 | sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}" |
|
311 | 311 | when "o" |
|
312 | 312 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" |
|
313 | 313 | when "c" |
|
314 | 314 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" |
|
315 | 315 | when ">t-" |
|
316 | 316 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] |
|
317 | 317 | when "<t-" |
|
318 | 318 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) |
|
319 | 319 | when "t-" |
|
320 | 320 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] |
|
321 | 321 | when ">t+" |
|
322 | 322 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) |
|
323 | 323 | when "<t+" |
|
324 | 324 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
325 | 325 | when "t+" |
|
326 | 326 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
327 | 327 | when "t" |
|
328 | 328 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] |
|
329 | 329 | when "w" |
|
330 | 330 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Time.now.at_beginning_of_week), connection.quoted_date(Time.now.next_week.yesterday)] |
|
331 | 331 | when "~" |
|
332 | 332 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" |
|
333 | 333 | when "!~" |
|
334 | 334 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" |
|
335 | 335 | end |
|
336 | 336 | sql << ')' |
|
337 | 337 | filters_clauses << sql |
|
338 | 338 | end if filters and valid? |
|
339 | 339 | |
|
340 | 340 | clause << ' AND ' unless clause.empty? |
|
341 | 341 | clause << filters_clauses.join(' AND ') unless filters_clauses.empty? |
|
342 | 342 | clause |
|
343 | 343 | end |
|
344 | 344 | end |
@@ -1,31 +1,30 | |||
|
1 | 1 | <h2><%= l(:label_home) %></h2> |
|
2 | 2 | |
|
3 | 3 | <div class="splitcontentleft"> |
|
4 | 4 | <%= textilizable Setting.welcome_text %> |
|
5 | 5 | <% if @news.any? %> |
|
6 | 6 | <div class="box"> |
|
7 | 7 | <h3><%=l(:label_news_latest)%></h3> |
|
8 | 8 | <%= render :partial => 'news/news', :collection => @news %> |
|
9 | 9 | <%= link_to l(:label_issue_view_all), :controller => 'news' %> |
|
10 | 10 | </div> |
|
11 | 11 | <% end %> |
|
12 | 12 | </div> |
|
13 | 13 | |
|
14 | 14 | <div class="splitcontentright"> |
|
15 | 15 | <div class="box"> |
|
16 | 16 | <h3 class="icon22 icon22-projects"><%=l(:label_project_latest)%></h3> |
|
17 | 17 | <ul> |
|
18 | 18 | <% for project in @projects %> |
|
19 | 19 | <li> |
|
20 | 20 | <%= link_to project.name, :controller => 'projects', :action => 'show', :id => project %> (<%= format_time(project.created_on) %>) |
|
21 | 21 | <%= textilizable project.description, :project => project %> |
|
22 | 22 | </li> |
|
23 | 23 | <% end %> |
|
24 | 24 | </ul> |
|
25 | 25 | </div> |
|
26 | 26 | </div> |
|
27 | 27 | |
|
28 | 28 | <% content_for :header_tags do %> |
|
29 |
<%= auto_discovery_link_tag(: |
|
|
30 | <%= auto_discovery_link_tag(:atom, {:controller => 'feeds', :action => 'news', :key => @key, :format => 'atom'}, {:title => l(:label_news_latest)}) %> | |
|
29 | <%= auto_discovery_link_tag(:atom, {:controller => 'news', :action => 'index', :key => User.current.rss_key, :format => 'atom'}, {:title => l(:label_news_latest)}) %> | |
|
31 | 30 | <% end %> |
@@ -1,163 +1,163 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 'redmine/scm/adapters/abstract_adapter' |
|
19 | 19 | |
|
20 | 20 | module Redmine |
|
21 | 21 | module Scm |
|
22 | 22 | module Adapters |
|
23 | 23 | class MercurialAdapter < AbstractAdapter |
|
24 | 24 | |
|
25 | 25 | # Mercurial executable name |
|
26 | 26 | HG_BIN = "hg" |
|
27 | 27 | |
|
28 | 28 | def info |
|
29 | 29 | cmd = "#{HG_BIN} -R #{target('')} root" |
|
30 | 30 | root_url = nil |
|
31 | 31 | shellout(cmd) do |io| |
|
32 | 32 | root_url = io.gets |
|
33 | 33 | end |
|
34 | 34 | return nil if $? && $?.exitstatus != 0 |
|
35 | 35 | info = Info.new({:root_url => root_url.chomp, |
|
36 | 36 | :lastrev => revisions(nil,nil,nil,{:limit => 1}).last |
|
37 | 37 | }) |
|
38 | 38 | info |
|
39 | 39 | rescue Errno::ENOENT => e |
|
40 | 40 | return nil |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def entries(path=nil, identifier=nil) |
|
44 | 44 | path ||= '' |
|
45 | 45 | entries = Entries.new |
|
46 | 46 | cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate -X */*/*" |
|
47 | 47 | cmd << " -r #{identifier.to_i}" if identifier |
|
48 | 48 | cmd << " * */*" |
|
49 | 49 | shellout(cmd) do |io| |
|
50 | 50 | io.each_line do |line| |
|
51 | 51 | e = line.chomp.split('\\') |
|
52 | 52 | entries << Entry.new({:name => e.first, |
|
53 | 53 | :path => (path.empty? ? e.first : "#{path}/#{e.first}"), |
|
54 | 54 | :kind => (e.size > 1 ? 'dir' : 'file'), |
|
55 | 55 | :lastrev => Revision.new |
|
56 | 56 | }) unless entries.detect{|entry| entry.name == e.first} |
|
57 | 57 | end |
|
58 | 58 | end |
|
59 | 59 | return nil if $? && $?.exitstatus != 0 |
|
60 | 60 | entries.sort_by_name |
|
61 | 61 | rescue Errno::ENOENT => e |
|
62 | 62 | raise CommandFailed |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def entry(path=nil, identifier=nil) |
|
66 | 66 | path ||= '' |
|
67 | 67 | search_path = path.split('/')[0..-2].join('/') |
|
68 | 68 | entry_name = path.split('/').last |
|
69 | 69 | e = entries(search_path, identifier) |
|
70 | 70 | e ? e.detect{|entry| entry.name == entry_name} : nil |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
74 | 74 | revisions = Revisions.new |
|
75 | 75 | cmd = "#{HG_BIN} -v -R #{target('')} log" |
|
76 | 76 | cmd << " -r #{identifier_from.to_i}:" if identifier_from |
|
77 | 77 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] |
|
78 | 78 | shellout(cmd) do |io| |
|
79 | 79 | changeset = {} |
|
80 | 80 | parsing_descr = false |
|
81 | 81 | line_feeds = 0 |
|
82 | 82 | |
|
83 | 83 | io.each_line do |line| |
|
84 | 84 | if line =~ /^(\w+):\s*(.*)$/ |
|
85 | 85 | key = $1 |
|
86 | 86 | value = $2 |
|
87 | 87 | if parsing_descr && line_feeds > 1 |
|
88 | 88 | parsing_descr = false |
|
89 | 89 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
90 | 90 | :scmid => changeset[:changeset].split(':').last, |
|
91 | 91 | :author => changeset[:user], |
|
92 | 92 | :time => Time.parse(changeset[:date]), |
|
93 | 93 | :message => changeset[:description], |
|
94 | :paths => changeset[:files].split.collect{|path| {:action => 'X', :path => "/#{path}"}} | |
|
94 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} | |
|
95 | 95 | }) |
|
96 | 96 | changeset = {} |
|
97 | 97 | end |
|
98 | 98 | if !parsing_descr |
|
99 | 99 | changeset.store key.to_sym, value |
|
100 | 100 | if $1 == "description" |
|
101 | 101 | parsing_descr = true |
|
102 | 102 | line_feeds = 0 |
|
103 | 103 | next |
|
104 | 104 | end |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | if parsing_descr |
|
108 | 108 | changeset[:description] << line |
|
109 | 109 | line_feeds += 1 if line.chomp.empty? |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
113 | 113 | :scmid => changeset[:changeset].split(':').last, |
|
114 | 114 | :author => changeset[:user], |
|
115 | 115 | :time => Time.parse(changeset[:date]), |
|
116 | 116 | :message => changeset[:description], |
|
117 | 117 | :paths => changeset[:files].split.collect{|path| {:action => 'X', :path => "/#{path}"}} |
|
118 | 118 | }) |
|
119 | 119 | end |
|
120 | 120 | return nil if $? && $?.exitstatus != 0 |
|
121 | 121 | revisions |
|
122 | 122 | rescue Errno::ENOENT => e |
|
123 | 123 | raise CommandFailed |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def diff(path, identifier_from, identifier_to=nil, type="inline") |
|
127 | 127 | path ||= '' |
|
128 | 128 | if identifier_to |
|
129 | 129 | identifier_to = identifier_to.to_i |
|
130 | 130 | else |
|
131 | 131 | identifier_to = identifier_from.to_i - 1 |
|
132 | 132 | end |
|
133 | 133 | cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" |
|
134 | 134 | cmd << " -I #{target(path)}" unless path.empty? |
|
135 | 135 | diff = [] |
|
136 | 136 | shellout(cmd) do |io| |
|
137 | 137 | io.each_line do |line| |
|
138 | 138 | diff << line |
|
139 | 139 | end |
|
140 | 140 | end |
|
141 | 141 | return nil if $? && $?.exitstatus != 0 |
|
142 | 142 | DiffTableList.new diff, type |
|
143 | 143 | |
|
144 | 144 | rescue Errno::ENOENT => e |
|
145 | 145 | raise CommandFailed |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def cat(path, identifier=nil) |
|
149 | 149 | cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}" |
|
150 | 150 | cat = nil |
|
151 | 151 | shellout(cmd) do |io| |
|
152 | 152 | io.binmode |
|
153 | 153 | cat = io.read |
|
154 | 154 | end |
|
155 | 155 | return nil if $? && $?.exitstatus != 0 |
|
156 | 156 | cat |
|
157 | 157 | rescue Errno::ENOENT => e |
|
158 | 158 | raise CommandFailed |
|
159 | 159 | end |
|
160 | 160 | end |
|
161 | 161 | end |
|
162 | 162 | end |
|
163 | 163 | end |
General Comments 0
You need to be logged in to leave comments.
Login now