annotate app/models/.svn/text-base/repository.rb.svn-base @ 119:8661b858af72

* Update to Redmine trunk rev 4705
author Chris Cannam
date Thu, 13 Jan 2011 14:12:06 +0000
parents 513646585e45
children cd2282d2aa55 07fa8a8b56a8
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@0 19 belongs_to :project
Chris@0 20 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
Chris@0 21 has_many :changes, :through => :changesets
Chris@0 22
Chris@0 23 # Raw SQL to delete changesets and changes in the database
Chris@0 24 # has_many :changesets, :dependent => :destroy is too slow for big repositories
Chris@0 25 before_destroy :clear_changesets
Chris@0 26
Chris@0 27 # Checks if the SCM is enabled when creating a repository
Chris@0 28 validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
Chris@0 29
Chris@0 30 # Removes leading and trailing whitespace
Chris@0 31 def url=(arg)
Chris@0 32 write_attribute(:url, arg ? arg.to_s.strip : nil)
Chris@0 33 end
Chris@0 34
Chris@0 35 # Removes leading and trailing whitespace
Chris@0 36 def root_url=(arg)
Chris@0 37 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
Chris@0 38 end
Chris@0 39
Chris@0 40 def scm
Chris@0 41 @scm ||= self.scm_adapter.new url, root_url, login, password
Chris@0 42 update_attribute(:root_url, @scm.root_url) if root_url.blank?
Chris@0 43 @scm
Chris@0 44 end
Chris@0 45
Chris@0 46 def scm_name
Chris@0 47 self.class.scm_name
Chris@0 48 end
Chris@0 49
Chris@0 50 def supports_cat?
Chris@0 51 scm.supports_cat?
Chris@0 52 end
Chris@0 53
Chris@0 54 def supports_annotate?
Chris@0 55 scm.supports_annotate?
Chris@0 56 end
Chris@0 57
Chris@0 58 def entry(path=nil, identifier=nil)
Chris@0 59 scm.entry(path, identifier)
Chris@0 60 end
Chris@0 61
Chris@0 62 def entries(path=nil, identifier=nil)
Chris@0 63 scm.entries(path, identifier)
Chris@0 64 end
Chris@0 65
Chris@0 66 def branches
Chris@0 67 scm.branches
Chris@0 68 end
Chris@0 69
Chris@0 70 def tags
Chris@0 71 scm.tags
Chris@0 72 end
Chris@0 73
Chris@0 74 def default_branch
Chris@0 75 scm.default_branch
Chris@0 76 end
Chris@0 77
Chris@0 78 def properties(path, identifier=nil)
Chris@0 79 scm.properties(path, identifier)
Chris@0 80 end
Chris@0 81
Chris@0 82 def cat(path, identifier=nil)
Chris@0 83 scm.cat(path, identifier)
Chris@0 84 end
Chris@0 85
Chris@0 86 def diff(path, rev, rev_to)
Chris@0 87 scm.diff(path, rev, rev_to)
Chris@0 88 end
Chris@119 89
Chris@119 90 def diff_format_revisions(cs, cs_to, sep=':')
Chris@119 91 text = ""
Chris@119 92 text << cs_to.format_identifier + sep if cs_to
Chris@119 93 text << cs.format_identifier if cs
Chris@119 94 text
Chris@119 95 end
Chris@119 96
Chris@0 97 # Returns a path relative to the url of the repository
Chris@0 98 def relative_path(path)
Chris@0 99 path
Chris@0 100 end
Chris@0 101
Chris@0 102 # Finds and returns a revision with a number or the beginning of a hash
Chris@0 103 def find_changeset_by_name(name)
Chris@119 104 return nil if name.nil? || name.empty?
Chris@0 105 changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%']))
Chris@0 106 end
Chris@0 107
Chris@0 108 def latest_changeset
Chris@0 109 @latest_changeset ||= changesets.find(:first)
Chris@0 110 end
Chris@0 111
Chris@0 112 # Returns the latest changesets for +path+
Chris@0 113 # Default behaviour is to search in cached changesets
Chris@0 114 def latest_changesets(path, rev, limit=10)
Chris@0 115 if path.blank?
Chris@0 116 changesets.find(:all, :include => :user,
Chris@0 117 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@0 118 :limit => limit)
Chris@0 119 else
Chris@0 120 changes.find(:all, :include => {:changeset => :user},
Chris@0 121 :conditions => ["path = ?", path.with_leading_slash],
Chris@0 122 :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
Chris@0 123 :limit => limit).collect(&:changeset)
Chris@0 124 end
Chris@0 125 end
Chris@0 126
Chris@0 127 def scan_changesets_for_issue_ids
Chris@0 128 self.changesets.each(&:scan_comment_for_issue_ids)
Chris@0 129 end
Chris@0 130
Chris@0 131 # Returns an array of committers usernames and associated user_id
Chris@0 132 def committers
Chris@0 133 @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
Chris@0 134 end
Chris@0 135
Chris@0 136 # Maps committers username to a user ids
Chris@0 137 def committer_ids=(h)
Chris@0 138 if h.is_a?(Hash)
Chris@0 139 committers.each do |committer, user_id|
Chris@0 140 new_user_id = h[committer]
Chris@0 141 if new_user_id && (new_user_id.to_i != user_id.to_i)
Chris@0 142 new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
Chris@0 143 Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer])
Chris@0 144 end
Chris@0 145 end
Chris@0 146 @committers = nil
Chris@0 147 @found_committer_users = nil
Chris@0 148 true
Chris@0 149 else
Chris@0 150 false
Chris@0 151 end
Chris@0 152 end
Chris@0 153
Chris@0 154 # Returns the Redmine User corresponding to the given +committer+
Chris@0 155 # It will return nil if the committer is not yet mapped and if no User
Chris@0 156 # with the same username or email was found
Chris@0 157 def find_committer_user(committer)
Chris@0 158 unless committer.blank?
Chris@0 159 @found_committer_users ||= {}
Chris@0 160 return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
Chris@0 161
Chris@0 162 user = nil
Chris@0 163 c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
Chris@0 164 if c && c.user
Chris@0 165 user = c.user
Chris@0 166 elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
Chris@0 167 username, email = $1.strip, $3
Chris@0 168 u = User.find_by_login(username)
Chris@0 169 u ||= User.find_by_mail(email) unless email.blank?
Chris@0 170 user = u
Chris@0 171 end
Chris@0 172 @found_committer_users[committer] = user
Chris@0 173 user
Chris@0 174 end
Chris@0 175 end
Chris@0 176
Chris@0 177 # Fetches new changesets for all repositories of active projects
Chris@0 178 # Can be called periodically by an external script
Chris@0 179 # eg. ruby script/runner "Repository.fetch_changesets"
Chris@0 180 def self.fetch_changesets
Chris@0 181 Project.active.has_module(:repository).find(:all, :include => :repository).each do |project|
Chris@0 182 if project.repository
Chris@0 183 project.repository.fetch_changesets
Chris@0 184 end
Chris@0 185 end
Chris@0 186 end
Chris@0 187
Chris@0 188 # scan changeset comments to find related and fixed issues for all repositories
Chris@0 189 def self.scan_changesets_for_issue_ids
Chris@0 190 find(:all).each(&:scan_changesets_for_issue_ids)
Chris@0 191 end
Chris@0 192
Chris@0 193 def self.scm_name
Chris@0 194 'Abstract'
Chris@0 195 end
Chris@0 196
Chris@0 197 def self.available_scm
Chris@0 198 subclasses.collect {|klass| [klass.scm_name, klass.name]}
Chris@0 199 end
Chris@0 200
Chris@0 201 def self.factory(klass_name, *args)
Chris@0 202 klass = "Repository::#{klass_name}".constantize
Chris@0 203 klass.new(*args)
Chris@0 204 rescue
Chris@0 205 nil
Chris@0 206 end
Chris@0 207
Chris@0 208 private
Chris@0 209
Chris@0 210 def before_save
Chris@0 211 # Strips url and root_url
Chris@0 212 url.strip!
Chris@0 213 root_url.strip!
Chris@0 214 true
Chris@0 215 end
Chris@0 216
Chris@0 217 def clear_changesets
Chris@0 218 cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}"
Chris@0 219 connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@0 220 connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
Chris@0 221 connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
Chris@0 222 end
Chris@0 223 end