scm: code clean up repository model.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5653 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-05-05 07:30:22 +00:00
parent 9c3a9fdd87
commit fbdbdf96fe

View File

@ -1,31 +1,31 @@
# redMine - project management software # Redmine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang # Copyright (C) 2006-2011 Jean-Philippe Lang
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class Repository < ActiveRecord::Base class Repository < ActiveRecord::Base
include Redmine::Ciphering include Redmine::Ciphering
belongs_to :project belongs_to :project
has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
has_many :changes, :through => :changesets has_many :changes, :through => :changesets
# Raw SQL to delete changesets and changes in the database # Raw SQL to delete changesets and changes in the database
# has_many :changesets, :dependent => :destroy is too slow for big repositories # has_many :changesets, :dependent => :destroy is too slow for big repositories
before_destroy :clear_changesets before_destroy :clear_changesets
validates_length_of :password, :maximum => 255, :allow_nil => true validates_length_of :password, :maximum => 255, :allow_nil => true
# Checks if the SCM is enabled when creating a repository # Checks if the SCM is enabled when creating a repository
validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) } validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
@ -47,11 +47,11 @@ class Repository < ActiveRecord::Base
def root_url=(arg) def root_url=(arg)
write_attribute(:root_url, arg ? arg.to_s.strip : nil) write_attribute(:root_url, arg ? arg.to_s.strip : nil)
end end
def password def password
read_ciphered_attribute(:password) read_ciphered_attribute(:password)
end end
def password=(arg) def password=(arg)
write_ciphered_attribute(:password, arg) write_ciphered_attribute(:password, arg)
end end
@ -82,15 +82,15 @@ class Repository < ActiveRecord::Base
def supports_all_revisions? def supports_all_revisions?
true true
end end
def supports_directory_revisions? def supports_directory_revisions?
false false
end end
def entry(path=nil, identifier=nil) def entry(path=nil, identifier=nil)
scm.entry(path, identifier) scm.entry(path, identifier)
end end
def entries(path=nil, identifier=nil) def entries(path=nil, identifier=nil)
scm.entries(path, identifier) scm.entries(path, identifier)
end end
@ -146,14 +146,19 @@ class Repository < ActiveRecord::Base
# Default behaviour is to search in cached changesets # Default behaviour is to search in cached changesets
def latest_changesets(path, rev, limit=10) def latest_changesets(path, rev, limit=10)
if path.blank? if path.blank?
changesets.find(:all, :include => :user, changesets.find(
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", :all,
:limit => limit) :include => :user,
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
:limit => limit)
else else
changes.find(:all, :include => {:changeset => :user}, changes.find(
:conditions => ["path = ?", path.with_leading_slash], :all,
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", :include => {:changeset => :user},
:limit => limit).collect(&:changeset) :conditions => ["path = ?", path.with_leading_slash],
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
:limit => limit
).collect(&:changeset)
end end
end end
@ -238,7 +243,7 @@ class Repository < ActiveRecord::Base
def self.scm_name def self.scm_name
'Abstract' 'Abstract'
end end
def self.available_scm def self.available_scm
subclasses.collect {|klass| [klass.scm_name, klass.name]} subclasses.collect {|klass| [klass.scm_name, klass.name]}
end end
@ -277,7 +282,7 @@ class Repository < ActiveRecord::Base
def self.scm_available def self.scm_available
ret = false ret = false
begin begin
ret = self.scm_adapter_class.client_available if self.scm_adapter_class ret = self.scm_adapter_class.client_available if self.scm_adapter_class
rescue Redmine::Scm::Adapters::CommandFailed => e rescue Redmine::Scm::Adapters::CommandFailed => e
logger.error "scm: error during get scm available: #{e.message}" logger.error "scm: error during get scm available: #{e.message}"
end end