annotate app/models/repository.rb @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children 5e80956cc792 433d4f72a19b
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@441 22
Chris@0 23 belongs_to :project
Chris@0 24 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
Chris@0 25 has_many :changes, :through => :changesets
Chris@441 26
Chris@441 27 serialize :extra_info
Chris@441 28
Chris@0 29 # Raw SQL to delete changesets and changes in the database
Chris@0 30 # has_many :changesets, :dependent => :destroy is too slow for big repositories
Chris@0 31 before_destroy :clear_changesets
Chris@441 32
Chris@245 33 validates_length_of :password, :maximum => 255, :allow_nil => true
Chris@0 34 # Checks if the SCM is enabled when creating a repository
Chris@909 35 validate :repo_create_validation, :on => :create
Chris@909 36
Chris@909 37 def repo_create_validation
Chris@909 38 unless Setting.enabled_scm.include?(self.class.name.demodulize)
Chris@909 39 errors.add(:type, :invalid)
Chris@909 40 end
Chris@909 41 end
Chris@245 42
Chris@441 43 def self.human_attribute_name(attribute_key_name)
Chris@441 44 attr_name = attribute_key_name
Chris@441 45 if attr_name == "log_encoding"
Chris@441 46 attr_name = "commit_logs_encoding"
Chris@441 47 end
Chris@441 48 super(attr_name)
Chris@441 49 end
Chris@441 50
Chris@0 51 # Removes leading and trailing whitespace
Chris@0 52 def url=(arg)
Chris@0 53 write_attribute(:url, arg ? arg.to_s.strip : nil)
Chris@0 54 end
Chris@245 55
Chris@0 56 # Removes leading and trailing whitespace
Chris@0 57 def root_url=(arg)
Chris@0 58 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
Chris@0 59 end
Chris@441 60
Chris@245 61 def password
Chris@245 62 read_ciphered_attribute(:password)
Chris@245 63 end
Chris@441 64
Chris@245 65 def password=(arg)
Chris@245 66 write_ciphered_attribute(:password, arg)
Chris@245 67 end
Chris@245 68
Chris@245 69 def scm_adapter
Chris@245 70 self.class.scm_adapter_class
Chris@245 71 end
Chris@0 72
Chris@0 73 def scm
Chris@245 74 @scm ||= self.scm_adapter.new(url, root_url,
Chris@245 75 login, password, path_encoding)
Chris@0 76 update_attribute(:root_url, @scm.root_url) if root_url.blank?
Chris@0 77 @scm
Chris@0 78 end
Chris@245 79
Chris@0 80 def scm_name
Chris@0 81 self.class.scm_name
Chris@0 82 end
Chris@245 83
Chris@441 84 def merge_extra_info(arg)
Chris@441 85 h = extra_info || {}
Chris@441 86 return h if arg.nil?
Chris@441 87 h.merge!(arg)
Chris@441 88 write_attribute(:extra_info, h)
Chris@441 89 end
Chris@441 90
Chris@441 91 def report_last_commit
Chris@441 92 true
Chris@441 93 end
Chris@441 94
Chris@0 95 def supports_cat?
Chris@0 96 scm.supports_cat?
Chris@0 97 end
Chris@0 98
Chris@0 99 def supports_annotate?
Chris@0 100 scm.supports_annotate?
Chris@0 101 end
Chris@441 102
Chris@441 103 def supports_all_revisions?
Chris@441 104 true
Chris@441 105 end
Chris@441 106
Chris@441 107 def supports_directory_revisions?
Chris@441 108 false
Chris@441 109 end
Chris@441 110
Chris@909 111 def supports_revision_graph?
Chris@909 112 false
Chris@909 113 end
Chris@909 114
Chris@0 115 def entry(path=nil, identifier=nil)
Chris@0 116 scm.entry(path, identifier)
Chris@0 117 end
Chris@441 118
Chris@0 119 def entries(path=nil, identifier=nil)
Chris@0 120 scm.entries(path, identifier)
Chris@0 121 end
Chris@0 122
Chris@0 123 def branches
Chris@0 124 scm.branches
Chris@0 125 end
Chris@0 126
Chris@0 127 def tags
Chris@0 128 scm.tags
Chris@0 129 end
Chris@0 130
Chris@0 131 def default_branch
Chris@507 132 nil
Chris@0 133 end
Chris@441 134
Chris@0 135 def properties(path, identifier=nil)
Chris@0 136 scm.properties(path, identifier)
Chris@0 137 end
Chris@441 138
Chris@0 139 def cat(path, identifier=nil)
Chris@0 140 scm.cat(path, identifier)
Chris@0 141 end
Chris@441 142
Chris@0 143 def diff(path, rev, rev_to)
Chris@0 144 scm.diff(path, rev, rev_to)
Chris@0 145 end
Chris@119 146
Chris@119 147 def diff_format_revisions(cs, cs_to, sep=':')
Chris@119 148 text = ""
Chris@119 149 text << cs_to.format_identifier + sep if cs_to
Chris@119 150 text << cs.format_identifier if cs
Chris@119 151 text
Chris@119 152 end
Chris@119 153
Chris@0 154 # Returns a path relative to the url of the repository
Chris@0 155 def relative_path(path)
Chris@0 156 path
Chris@0 157 end
Chris@128 158
Chris@0 159 # Finds and returns a revision with a number or the beginning of a hash
Chris@0 160 def find_changeset_by_name(name)
Chris@128 161 return nil if name.blank?
Chris@441 162 changesets.find(:first, :conditions => (name.match(/^\d*$/) ?
Chris@441 163 ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%']))
Chris@0 164 end
Chris@128 165
Chris@0 166 def latest_changeset
Chris@0 167 @latest_changeset ||= changesets.find(:first)
Chris@0 168 end
Chris@0 169
Chris@0 170 # Returns the latest changesets for +path+
Chris@0 171 # Default behaviour is to search in cached changesets
Chris@0 172 def latest_changesets(path, rev, limit=10)
Chris@0 173 if path.blank?
Chris@441 174 changesets.find(
Chris@441 175 :all,
Chris@441 176 :include => :user,
Chris@441 177 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@441 178 :limit => limit)
Chris@0 179 else
Chris@441 180 changes.find(
Chris@441 181 :all,
Chris@441 182 :include => {:changeset => :user},
Chris@441 183 :conditions => ["path = ?", path.with_leading_slash],
Chris@441 184 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@441 185 :limit => limit
Chris@441 186 ).collect(&:changeset)
Chris@0 187 end
Chris@0 188 end
Chris@441 189
Chris@0 190 def scan_changesets_for_issue_ids
Chris@0 191 self.changesets.each(&:scan_comment_for_issue_ids)
Chris@0 192 end
Chris@0 193
Chris@0 194 # Returns an array of committers usernames and associated user_id
Chris@0 195 def committers
Chris@441 196 @committers ||= Changeset.connection.select_rows(
Chris@441 197 "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
Chris@0 198 end
Chris@441 199
Chris@0 200 # Maps committers username to a user ids
Chris@0 201 def committer_ids=(h)
Chris@0 202 if h.is_a?(Hash)
Chris@0 203 committers.each do |committer, user_id|
Chris@0 204 new_user_id = h[committer]
Chris@0 205 if new_user_id && (new_user_id.to_i != user_id.to_i)
Chris@0 206 new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
Chris@441 207 Changeset.update_all(
Chris@441 208 "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }",
Chris@441 209 ["repository_id = ? AND committer = ?", id, committer])
Chris@0 210 end
Chris@0 211 end
Chris@441 212 @committers = nil
Chris@0 213 @found_committer_users = nil
Chris@0 214 true
Chris@0 215 else
Chris@0 216 false
Chris@0 217 end
Chris@0 218 end
Chris@441 219
Chris@0 220 # Returns the Redmine User corresponding to the given +committer+
Chris@0 221 # It will return nil if the committer is not yet mapped and if no User
Chris@0 222 # with the same username or email was found
Chris@0 223 def find_committer_user(committer)
Chris@0 224 unless committer.blank?
Chris@0 225 @found_committer_users ||= {}
Chris@0 226 return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
Chris@441 227
Chris@0 228 user = nil
Chris@0 229 c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
Chris@0 230 if c && c.user
Chris@0 231 user = c.user
Chris@0 232 elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
Chris@0 233 username, email = $1.strip, $3
Chris@0 234 u = User.find_by_login(username)
Chris@0 235 u ||= User.find_by_mail(email) unless email.blank?
Chris@0 236 user = u
Chris@0 237 end
Chris@0 238 @found_committer_users[committer] = user
Chris@0 239 user
Chris@0 240 end
Chris@0 241 end
Chris@245 242
Chris@245 243 def repo_log_encoding
Chris@245 244 encoding = log_encoding.to_s.strip
Chris@245 245 encoding.blank? ? 'UTF-8' : encoding
Chris@245 246 end
Chris@245 247
Chris@0 248 # Fetches new changesets for all repositories of active projects
Chris@0 249 # Can be called periodically by an external script
Chris@0 250 # eg. ruby script/runner "Repository.fetch_changesets"
Chris@0 251 def self.fetch_changesets
Chris@0 252 Project.active.has_module(:repository).find(:all, :include => :repository).each do |project|
Chris@0 253 if project.repository
Chris@245 254 begin
Chris@245 255 project.repository.fetch_changesets
Chris@245 256 rescue Redmine::Scm::Adapters::CommandFailed => e
Chris@245 257 logger.error "scm: error during fetching changesets: #{e.message}"
Chris@245 258 end
Chris@0 259 end
Chris@0 260 end
Chris@0 261 end
Chris@245 262
Chris@0 263 # scan changeset comments to find related and fixed issues for all repositories
Chris@0 264 def self.scan_changesets_for_issue_ids
Chris@0 265 find(:all).each(&:scan_changesets_for_issue_ids)
Chris@0 266 end
Chris@0 267
Chris@0 268 def self.scm_name
Chris@0 269 'Abstract'
Chris@0 270 end
Chris@441 271
Chris@0 272 def self.available_scm
Chris@0 273 subclasses.collect {|klass| [klass.scm_name, klass.name]}
Chris@0 274 end
Chris@245 275
Chris@0 276 def self.factory(klass_name, *args)
Chris@0 277 klass = "Repository::#{klass_name}".constantize
Chris@0 278 klass.new(*args)
Chris@0 279 rescue
Chris@0 280 nil
Chris@0 281 end
Chris@245 282
Chris@245 283 def self.scm_adapter_class
Chris@245 284 nil
Chris@245 285 end
Chris@245 286
Chris@245 287 def self.scm_command
Chris@245 288 ret = ""
Chris@245 289 begin
Chris@245 290 ret = self.scm_adapter_class.client_command if self.scm_adapter_class
Chris@441 291 rescue Exception => e
Chris@245 292 logger.error "scm: error during get command: #{e.message}"
Chris@245 293 end
Chris@245 294 ret
Chris@245 295 end
Chris@245 296
Chris@245 297 def self.scm_version_string
Chris@245 298 ret = ""
Chris@245 299 begin
Chris@245 300 ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
Chris@441 301 rescue Exception => e
Chris@245 302 logger.error "scm: error during get version string: #{e.message}"
Chris@245 303 end
Chris@245 304 ret
Chris@245 305 end
Chris@245 306
Chris@245 307 def self.scm_available
Chris@245 308 ret = false
Chris@245 309 begin
Chris@441 310 ret = self.scm_adapter_class.client_available if self.scm_adapter_class
Chris@441 311 rescue Exception => e
Chris@245 312 logger.error "scm: error during get scm available: #{e.message}"
Chris@245 313 end
Chris@245 314 ret
Chris@245 315 end
Chris@245 316
Chris@0 317 private
Chris@245 318
Chris@0 319 def clear_changesets
Chris@0 320 cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}"
Chris@0 321 connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@0 322 connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@0 323 connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
Chris@0 324 end
Chris@0 325 end