annotate .svn/pristine/2a/2a59baf70fb68ac9f90f4306e930088af4138a70.svn-base @ 1464:261b3d9a4903 redmine-2.4

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