annotate app/models/.svn/text-base/repository.rb.svn-base @ 245:051f544170fe

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