annotate .svn/pristine/01/01e977490126a0e666397a535bfda934cac273ac.svn-base @ 1296:038ba2d95de8 redmine-2.2

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