Mercurial > hg > soundsoftware-site
comparison .svn/pristine/d1/d1beea22853d2b080b44752ca5d9ee2a41a0ab7d.svn-base @ 1494:e248c7af89ec redmine-2.4
Update to Redmine SVN revision 12979 on 2.4-stable branch
author | Chris Cannam |
---|---|
date | Mon, 17 Mar 2014 08:54:02 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1464:261b3d9a4903 | 1494:e248c7af89ec |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 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 class ScmFetchError < Exception; end | |
19 | |
20 class Repository < ActiveRecord::Base | |
21 include Redmine::Ciphering | |
22 include Redmine::SafeAttributes | |
23 | |
24 # Maximum length for repository identifiers | |
25 IDENTIFIER_MAX_LENGTH = 255 | |
26 | |
27 belongs_to :project | |
28 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" | |
29 has_many :filechanges, :class_name => 'Change', :through => :changesets | |
30 | |
31 serialize :extra_info | |
32 | |
33 before_save :check_default | |
34 | |
35 # Raw SQL to delete changesets and changes in the database | |
36 # has_many :changesets, :dependent => :destroy is too slow for big repositories | |
37 before_destroy :clear_changesets | |
38 | |
39 validates_length_of :password, :maximum => 255, :allow_nil => true | |
40 validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH, :allow_blank => true | |
41 validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? } | |
42 validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true | |
43 validates_exclusion_of :identifier, :in => %w(show entry raw changes annotate diff show stats graph) | |
44 # donwcase letters, digits, dashes, underscores but not digits only | |
45 validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :allow_blank => true | |
46 # Checks if the SCM is enabled when creating a repository | |
47 validate :repo_create_validation, :on => :create | |
48 | |
49 safe_attributes 'identifier', | |
50 'login', | |
51 'password', | |
52 'path_encoding', | |
53 'log_encoding', | |
54 'is_default' | |
55 | |
56 safe_attributes 'url', | |
57 :if => lambda {|repository, user| repository.new_record?} | |
58 | |
59 def repo_create_validation | |
60 unless Setting.enabled_scm.include?(self.class.name.demodulize) | |
61 errors.add(:type, :invalid) | |
62 end | |
63 end | |
64 | |
65 def self.human_attribute_name(attribute_key_name, *args) | |
66 attr_name = attribute_key_name.to_s | |
67 if attr_name == "log_encoding" | |
68 attr_name = "commit_logs_encoding" | |
69 end | |
70 super(attr_name, *args) | |
71 end | |
72 | |
73 # Removes leading and trailing whitespace | |
74 def url=(arg) | |
75 write_attribute(:url, arg ? arg.to_s.strip : nil) | |
76 end | |
77 | |
78 # Removes leading and trailing whitespace | |
79 def root_url=(arg) | |
80 write_attribute(:root_url, arg ? arg.to_s.strip : nil) | |
81 end | |
82 | |
83 def password | |
84 read_ciphered_attribute(:password) | |
85 end | |
86 | |
87 def password=(arg) | |
88 write_ciphered_attribute(:password, arg) | |
89 end | |
90 | |
91 def scm_adapter | |
92 self.class.scm_adapter_class | |
93 end | |
94 | |
95 def scm | |
96 unless @scm | |
97 @scm = self.scm_adapter.new(url, root_url, | |
98 login, password, path_encoding) | |
99 if root_url.blank? && @scm.root_url.present? | |
100 update_attribute(:root_url, @scm.root_url) | |
101 end | |
102 end | |
103 @scm | |
104 end | |
105 | |
106 def scm_name | |
107 self.class.scm_name | |
108 end | |
109 | |
110 def name | |
111 if identifier.present? | |
112 identifier | |
113 elsif is_default? | |
114 l(:field_repository_is_default) | |
115 else | |
116 scm_name | |
117 end | |
118 end | |
119 | |
120 def identifier=(identifier) | |
121 super unless identifier_frozen? | |
122 end | |
123 | |
124 def identifier_frozen? | |
125 errors[:identifier].blank? && !(new_record? || identifier.blank?) | |
126 end | |
127 | |
128 def identifier_param | |
129 if is_default? | |
130 nil | |
131 elsif identifier.present? | |
132 identifier | |
133 else | |
134 id.to_s | |
135 end | |
136 end | |
137 | |
138 def <=>(repository) | |
139 if is_default? | |
140 -1 | |
141 elsif repository.is_default? | |
142 1 | |
143 else | |
144 identifier.to_s <=> repository.identifier.to_s | |
145 end | |
146 end | |
147 | |
148 def self.find_by_identifier_param(param) | |
149 if param.to_s =~ /^\d+$/ | |
150 find_by_id(param) | |
151 else | |
152 find_by_identifier(param) | |
153 end | |
154 end | |
155 | |
156 # TODO: should return an empty hash instead of nil to avoid many ||{} | |
157 def extra_info | |
158 h = read_attribute(:extra_info) | |
159 h.is_a?(Hash) ? h : nil | |
160 end | |
161 | |
162 def merge_extra_info(arg) | |
163 h = extra_info || {} | |
164 return h if arg.nil? | |
165 h.merge!(arg) | |
166 write_attribute(:extra_info, h) | |
167 end | |
168 | |
169 def report_last_commit | |
170 true | |
171 end | |
172 | |
173 def supports_cat? | |
174 scm.supports_cat? | |
175 end | |
176 | |
177 def supports_annotate? | |
178 scm.supports_annotate? | |
179 end | |
180 | |
181 def supports_all_revisions? | |
182 true | |
183 end | |
184 | |
185 def supports_directory_revisions? | |
186 false | |
187 end | |
188 | |
189 def supports_revision_graph? | |
190 false | |
191 end | |
192 | |
193 def entry(path=nil, identifier=nil) | |
194 scm.entry(path, identifier) | |
195 end | |
196 | |
197 def entries(path=nil, identifier=nil) | |
198 entries = scm.entries(path, identifier) | |
199 load_entries_changesets(entries) | |
200 entries | |
201 end | |
202 | |
203 def branches | |
204 scm.branches | |
205 end | |
206 | |
207 def tags | |
208 scm.tags | |
209 end | |
210 | |
211 def default_branch | |
212 nil | |
213 end | |
214 | |
215 def properties(path, identifier=nil) | |
216 scm.properties(path, identifier) | |
217 end | |
218 | |
219 def cat(path, identifier=nil) | |
220 scm.cat(path, identifier) | |
221 end | |
222 | |
223 def diff(path, rev, rev_to) | |
224 scm.diff(path, rev, rev_to) | |
225 end | |
226 | |
227 def diff_format_revisions(cs, cs_to, sep=':') | |
228 text = "" | |
229 text << cs_to.format_identifier + sep if cs_to | |
230 text << cs.format_identifier if cs | |
231 text | |
232 end | |
233 | |
234 # Returns a path relative to the url of the repository | |
235 def relative_path(path) | |
236 path | |
237 end | |
238 | |
239 # Finds and returns a revision with a number or the beginning of a hash | |
240 def find_changeset_by_name(name) | |
241 return nil if name.blank? | |
242 s = name.to_s | |
243 if s.match(/^\d*$/) | |
244 changesets.where("revision = ?", s).first | |
245 else | |
246 changesets.where("revision LIKE ?", s + '%').first | |
247 end | |
248 end | |
249 | |
250 def latest_changeset | |
251 @latest_changeset ||= changesets.first | |
252 end | |
253 | |
254 # Returns the latest changesets for +path+ | |
255 # Default behaviour is to search in cached changesets | |
256 def latest_changesets(path, rev, limit=10) | |
257 if path.blank? | |
258 changesets. | |
259 reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). | |
260 limit(limit). | |
261 preload(:user). | |
262 all | |
263 else | |
264 filechanges. | |
265 where("path = ?", path.with_leading_slash). | |
266 reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). | |
267 limit(limit). | |
268 preload(:changeset => :user). | |
269 collect(&:changeset) | |
270 end | |
271 end | |
272 | |
273 def scan_changesets_for_issue_ids | |
274 self.changesets.each(&:scan_comment_for_issue_ids) | |
275 end | |
276 | |
277 # Returns an array of committers usernames and associated user_id | |
278 def committers | |
279 @committers ||= Changeset.connection.select_rows( | |
280 "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") | |
281 end | |
282 | |
283 # Maps committers username to a user ids | |
284 def committer_ids=(h) | |
285 if h.is_a?(Hash) | |
286 committers.each do |committer, user_id| | |
287 new_user_id = h[committer] | |
288 if new_user_id && (new_user_id.to_i != user_id.to_i) | |
289 new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil) | |
290 Changeset.update_all( | |
291 "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", | |
292 ["repository_id = ? AND committer = ?", id, committer]) | |
293 end | |
294 end | |
295 @committers = nil | |
296 @found_committer_users = nil | |
297 true | |
298 else | |
299 false | |
300 end | |
301 end | |
302 | |
303 # Returns the Redmine User corresponding to the given +committer+ | |
304 # It will return nil if the committer is not yet mapped and if no User | |
305 # with the same username or email was found | |
306 def find_committer_user(committer) | |
307 unless committer.blank? | |
308 @found_committer_users ||= {} | |
309 return @found_committer_users[committer] if @found_committer_users.has_key?(committer) | |
310 | |
311 user = nil | |
312 c = changesets.where(:committer => committer).includes(:user).first | |
313 if c && c.user | |
314 user = c.user | |
315 elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ | |
316 username, email = $1.strip, $3 | |
317 u = User.find_by_login(username) | |
318 u ||= User.find_by_mail(email) unless email.blank? | |
319 user = u | |
320 end | |
321 @found_committer_users[committer] = user | |
322 user | |
323 end | |
324 end | |
325 | |
326 def repo_log_encoding | |
327 encoding = log_encoding.to_s.strip | |
328 encoding.blank? ? 'UTF-8' : encoding | |
329 end | |
330 | |
331 # Fetches new changesets for all repositories of active projects | |
332 # Can be called periodically by an external script | |
333 # eg. ruby script/runner "Repository.fetch_changesets" | |
334 def self.fetch_changesets | |
335 Project.active.has_module(:repository).all.each do |project| | |
336 project.repositories.each do |repository| | |
337 begin | |
338 repository.fetch_changesets | |
339 rescue Redmine::Scm::Adapters::CommandFailed => e | |
340 logger.error "scm: error during fetching changesets: #{e.message}" | |
341 end | |
342 end | |
343 end | |
344 end | |
345 | |
346 # scan changeset comments to find related and fixed issues for all repositories | |
347 def self.scan_changesets_for_issue_ids | |
348 all.each(&:scan_changesets_for_issue_ids) | |
349 end | |
350 | |
351 def self.scm_name | |
352 'Abstract' | |
353 end | |
354 | |
355 def self.available_scm | |
356 subclasses.collect {|klass| [klass.scm_name, klass.name]} | |
357 end | |
358 | |
359 def self.factory(klass_name, *args) | |
360 klass = "Repository::#{klass_name}".constantize | |
361 klass.new(*args) | |
362 rescue | |
363 nil | |
364 end | |
365 | |
366 def self.scm_adapter_class | |
367 nil | |
368 end | |
369 | |
370 def self.scm_command | |
371 ret = "" | |
372 begin | |
373 ret = self.scm_adapter_class.client_command if self.scm_adapter_class | |
374 rescue Exception => e | |
375 logger.error "scm: error during get command: #{e.message}" | |
376 end | |
377 ret | |
378 end | |
379 | |
380 def self.scm_version_string | |
381 ret = "" | |
382 begin | |
383 ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class | |
384 rescue Exception => e | |
385 logger.error "scm: error during get version string: #{e.message}" | |
386 end | |
387 ret | |
388 end | |
389 | |
390 def self.scm_available | |
391 ret = false | |
392 begin | |
393 ret = self.scm_adapter_class.client_available if self.scm_adapter_class | |
394 rescue Exception => e | |
395 logger.error "scm: error during get scm available: #{e.message}" | |
396 end | |
397 ret | |
398 end | |
399 | |
400 def set_as_default? | |
401 new_record? && project && Repository.where(:project_id => project.id).empty? | |
402 end | |
403 | |
404 protected | |
405 | |
406 def check_default | |
407 if !is_default? && set_as_default? | |
408 self.is_default = true | |
409 end | |
410 if is_default? && is_default_changed? | |
411 Repository.update_all(["is_default = ?", false], ["project_id = ?", project_id]) | |
412 end | |
413 end | |
414 | |
415 def load_entries_changesets(entries) | |
416 if entries | |
417 entries.each do |entry| | |
418 if entry.lastrev && entry.lastrev.identifier | |
419 entry.changeset = find_changeset_by_name(entry.lastrev.identifier) | |
420 end | |
421 end | |
422 end | |
423 end | |
424 | |
425 private | |
426 | |
427 # Deletes repository data | |
428 def clear_changesets | |
429 cs = Changeset.table_name | |
430 ch = Change.table_name | |
431 ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}" | |
432 cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}" | |
433 | |
434 connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | |
435 connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | |
436 connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | |
437 connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") | |
438 clear_extra_info_of_changesets | |
439 end | |
440 | |
441 def clear_extra_info_of_changesets | |
442 end | |
443 end |