annotate .svn/pristine/c3/c3cde01a42095e130d429cd695c3b938d274bb0b.svn-base @ 1295:622f24f53b42 redmine-2.3

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