add local cache repository for speed purpose (subversion) or for browsing a external repository
for SCM that can handle only local copy (git). git-svn-id: http://redmine.rubyforge.org/svn/branches/nbc@1899 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
# redMine - project management software
|
||||
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
||||
#
|
||||
@@ -19,14 +20,14 @@ class Repository < ActiveRecord::Base
|
||||
belongs_to :project
|
||||
has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
|
||||
has_many :changes, :through => :changesets
|
||||
|
||||
|
||||
# Raw SQL to delete changesets and changes in the database
|
||||
# has_many :changesets, :dependent => :destroy is too slow for big repositories
|
||||
before_destroy :clear_changesets
|
||||
|
||||
|
||||
# Checks if the SCM is enabled when creating a repository
|
||||
validate_on_create { |r| r.errors.add(:type, :activerecord_error_invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
|
||||
|
||||
|
||||
# Removes leading and trailing whitespace
|
||||
def url=(arg)
|
||||
write_attribute(:url, arg ? arg.to_s.strip : nil)
|
||||
@@ -38,7 +39,8 @@ class Repository < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def scm
|
||||
@scm ||= self.scm_adapter.new url, root_url, login, password
|
||||
init_cache if cache_path.blank? and respond_to?(:init_cache)
|
||||
@scm ||= self.scm_adapter.new(url, root_url, login, password, cache_path)
|
||||
update_attribute(:root_url, @scm.root_url) if root_url.blank?
|
||||
@scm
|
||||
end
|
||||
@@ -122,9 +124,28 @@ class Repository < ActiveRecord::Base
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def remove_cache
|
||||
scm.remove_cache if cache
|
||||
end
|
||||
|
||||
def create_or_sync_cache
|
||||
begin
|
||||
scm.create_cache
|
||||
rescue => e
|
||||
# clean if problem in creation
|
||||
scm.remove_cache
|
||||
end
|
||||
scm.synchronize
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
def repositories_cache_directory
|
||||
dir = Setting.repositories_cache_directory.gsub(/^([^#{File::SEPARATOR}].*)/, RAILS_ROOT + '/\1')
|
||||
return dir if File.directory?(dir)
|
||||
end
|
||||
|
||||
def before_save
|
||||
# Strips url and root_url
|
||||
url.strip!
|
||||
|
||||
@@ -21,6 +21,17 @@ class Repository::Git < Repository
|
||||
attr_protected :root_url
|
||||
validates_presence_of :url
|
||||
|
||||
before_destroy :remove_cache
|
||||
|
||||
def init_cache
|
||||
return unless dir = repositories_cache_directory
|
||||
# we need to use a cache only if repository isn't local and dir exists
|
||||
if url[/^(rsync|https?|git|ssh):\/\//]
|
||||
update_attribute(:cache_path, dir + project.identifier)
|
||||
update_attribute(:cache, true)
|
||||
end
|
||||
end
|
||||
|
||||
def scm_adapter
|
||||
Redmine::Scm::Adapters::GitAdapter
|
||||
end
|
||||
@@ -36,6 +47,8 @@ class Repository::Git < Repository
|
||||
end
|
||||
|
||||
def fetch_changesets
|
||||
create_or_sync_cache if cache
|
||||
|
||||
scm_info = scm.info
|
||||
if scm_info
|
||||
# latest revision found in database
|
||||
|
||||
@@ -22,6 +22,16 @@ class Repository::Subversion < Repository
|
||||
validates_presence_of :url
|
||||
validates_format_of :url, :with => /^(http|https|svn|svn\+ssh|file):\/\/.+/i
|
||||
|
||||
before_destroy :remove_cache
|
||||
|
||||
def init_cache
|
||||
return unless dir = repositories_cache_directory
|
||||
# we need to use a cache only if repository isn't local and dir exists
|
||||
if cache and url[/^(svn|https?|svn\+ssh):\/\//]
|
||||
update_attribute(:cache_path, dir + project.identifier)
|
||||
end
|
||||
end
|
||||
|
||||
def scm_adapter
|
||||
Redmine::Scm::Adapters::SubversionAdapter
|
||||
end
|
||||
@@ -41,6 +51,8 @@ class Repository::Subversion < Repository
|
||||
end
|
||||
|
||||
def fetch_changesets
|
||||
create_or_sync_cache if cache
|
||||
|
||||
scm_info = scm.info
|
||||
if scm_info
|
||||
# latest revision found in database
|
||||
|
||||
@@ -161,4 +161,14 @@ private
|
||||
setting = find_by_name(name)
|
||||
setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def validate
|
||||
if self.name.to_s == "repositories_cache_directory" and not File.directory?(self.value.to_s)
|
||||
logger.error("Le repertoire #{self.value.to_s} n'existe pas")
|
||||
errors.add("Le repertoire #{self.value.to_s} n'existe pas")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user