annotate app/models/.svn/text-base/repository.rb.svn-base @ 507:0c939c159af4 redmine-1.2

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