annotate app/models/repository.rb @ 1628:9c5f8e24dadc live tip

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