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