Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Repository < ActiveRecord::Base Chris@0: belongs_to :project Chris@0: has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" Chris@0: has_many :changes, :through => :changesets Chris@0: Chris@0: # Raw SQL to delete changesets and changes in the database Chris@0: # has_many :changesets, :dependent => :destroy is too slow for big repositories Chris@0: before_destroy :clear_changesets Chris@0: Chris@0: # Checks if the SCM is enabled when creating a repository Chris@0: validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) } Chris@0: Chris@0: # Removes leading and trailing whitespace Chris@0: def url=(arg) Chris@0: write_attribute(:url, arg ? arg.to_s.strip : nil) Chris@0: end Chris@0: Chris@0: # Removes leading and trailing whitespace Chris@0: def root_url=(arg) Chris@0: write_attribute(:root_url, arg ? arg.to_s.strip : nil) Chris@0: end Chris@0: Chris@0: def scm Chris@0: @scm ||= self.scm_adapter.new url, root_url, login, password Chris@0: update_attribute(:root_url, @scm.root_url) if root_url.blank? Chris@0: @scm Chris@0: end Chris@0: Chris@0: def scm_name Chris@0: self.class.scm_name Chris@0: end Chris@0: Chris@0: def supports_cat? Chris@0: scm.supports_cat? Chris@0: end Chris@0: Chris@0: def supports_annotate? Chris@0: scm.supports_annotate? Chris@0: end Chris@0: Chris@0: def entry(path=nil, identifier=nil) Chris@0: scm.entry(path, identifier) Chris@0: end Chris@0: Chris@0: def entries(path=nil, identifier=nil) Chris@0: scm.entries(path, identifier) Chris@0: end Chris@0: Chris@0: def branches Chris@0: scm.branches Chris@0: end Chris@0: Chris@0: def tags Chris@0: scm.tags Chris@0: end Chris@0: Chris@0: def default_branch Chris@0: scm.default_branch Chris@0: end Chris@0: Chris@0: def properties(path, identifier=nil) Chris@0: scm.properties(path, identifier) Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: scm.cat(path, identifier) Chris@0: end Chris@0: Chris@0: def diff(path, rev, rev_to) Chris@0: scm.diff(path, rev, rev_to) Chris@0: end Chris@0: Chris@0: # Returns a path relative to the url of the repository Chris@0: def relative_path(path) Chris@0: path Chris@0: end Chris@0: Chris@0: # Finds and returns a revision with a number or the beginning of a hash Chris@0: def find_changeset_by_name(name) Chris@117: return nil if name.nil? || name.empty? Chris@0: changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%'])) Chris@0: end Chris@0: Chris@0: def latest_changeset Chris@0: @latest_changeset ||= changesets.find(:first) Chris@0: end Chris@0: Chris@0: # Returns the latest changesets for +path+ Chris@0: # Default behaviour is to search in cached changesets Chris@0: def latest_changesets(path, rev, limit=10) Chris@0: if path.blank? Chris@0: changesets.find(:all, :include => :user, Chris@0: :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", Chris@0: :limit => limit) Chris@0: else Chris@0: changes.find(:all, :include => {:changeset => :user}, Chris@0: :conditions => ["path = ?", path.with_leading_slash], Chris@0: :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", Chris@0: :limit => limit).collect(&:changeset) Chris@0: end Chris@0: end Chris@0: Chris@0: def scan_changesets_for_issue_ids Chris@0: self.changesets.each(&:scan_comment_for_issue_ids) Chris@0: end Chris@0: Chris@0: # Returns an array of committers usernames and associated user_id Chris@0: def committers Chris@0: @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") Chris@0: end Chris@0: Chris@0: # Maps committers username to a user ids Chris@0: def committer_ids=(h) Chris@0: if h.is_a?(Hash) Chris@0: committers.each do |committer, user_id| Chris@0: new_user_id = h[committer] Chris@0: if new_user_id && (new_user_id.to_i != user_id.to_i) Chris@0: new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil) Chris@0: Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer]) Chris@0: end Chris@0: end Chris@0: @committers = nil Chris@0: @found_committer_users = nil Chris@0: true Chris@0: else Chris@0: false Chris@0: end Chris@0: end Chris@0: Chris@0: # Returns the Redmine User corresponding to the given +committer+ Chris@0: # It will return nil if the committer is not yet mapped and if no User Chris@0: # with the same username or email was found Chris@0: def find_committer_user(committer) Chris@0: unless committer.blank? Chris@0: @found_committer_users ||= {} Chris@0: return @found_committer_users[committer] if @found_committer_users.has_key?(committer) Chris@0: Chris@0: user = nil Chris@0: c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user) Chris@0: if c && c.user Chris@0: user = c.user Chris@0: elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ Chris@0: username, email = $1.strip, $3 Chris@0: u = User.find_by_login(username) Chris@0: u ||= User.find_by_mail(email) unless email.blank? Chris@0: user = u Chris@0: end Chris@0: @found_committer_users[committer] = user Chris@0: user Chris@0: end Chris@0: end Chris@0: Chris@0: # Fetches new changesets for all repositories of active projects Chris@0: # Can be called periodically by an external script Chris@0: # eg. ruby script/runner "Repository.fetch_changesets" Chris@0: def self.fetch_changesets Chris@0: Project.active.has_module(:repository).find(:all, :include => :repository).each do |project| Chris@0: if project.repository Chris@0: project.repository.fetch_changesets Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # scan changeset comments to find related and fixed issues for all repositories Chris@0: def self.scan_changesets_for_issue_ids Chris@0: find(:all).each(&:scan_changesets_for_issue_ids) Chris@0: end Chris@0: Chris@0: def self.scm_name Chris@0: 'Abstract' Chris@0: end Chris@0: Chris@0: def self.available_scm Chris@0: subclasses.collect {|klass| [klass.scm_name, klass.name]} Chris@0: end Chris@0: Chris@0: def self.factory(klass_name, *args) Chris@0: klass = "Repository::#{klass_name}".constantize Chris@0: klass.new(*args) Chris@0: rescue Chris@0: nil Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: def before_save Chris@0: # Strips url and root_url Chris@0: url.strip! Chris@0: root_url.strip! Chris@0: true Chris@0: end Chris@0: Chris@0: def clear_changesets Chris@0: cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}" Chris@0: connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") Chris@0: connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") Chris@0: connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") Chris@0: end Chris@0: end