Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 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@441: # 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@441: # 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@1136: require_dependency 'redmine/scm/adapters/subversion_adapter' Chris@0: Chris@0: class Repository::Subversion < Repository Chris@0: attr_protected :root_url Chris@0: validates_presence_of :url Chris@1464: validates_format_of :url, :with => %r{\A(http|https|svn(\+[^\s:\/\\]+)?|file):\/\/.+}i Chris@0: Chris@245: def self.scm_adapter_class Chris@0: Redmine::Scm::Adapters::SubversionAdapter Chris@0: end Chris@245: Chris@0: def self.scm_name Chris@0: 'Subversion' Chris@0: end Chris@0: Chris@441: def supports_directory_revisions? Chris@441: true Chris@441: end Chris@441: Chris@245: def repo_log_encoding Chris@245: 'UTF-8' Chris@245: end Chris@245: Chris@0: def latest_changesets(path, rev, limit=10) Chris@0: revisions = scm.revisions(path, rev, nil, :limit => limit) Chris@1115: if revisions Chris@1115: identifiers = revisions.collect(&:identifier).compact Chris@1115: changesets.where(:revision => identifiers).reorder("committed_on DESC").includes(:repository, :user).all Chris@1115: else Chris@1115: [] Chris@1115: end Chris@0: end Chris@441: Chris@0: # Returns a path relative to the url of the repository Chris@0: def relative_path(path) Chris@0: path.gsub(Regexp.new("^\/?#{Regexp.escape(relative_url)}"), '') Chris@0: end Chris@441: Chris@0: def fetch_changesets Chris@0: scm_info = scm.info Chris@0: if scm_info Chris@0: # latest revision found in database Chris@0: db_revision = latest_changeset ? latest_changeset.revision.to_i : 0 Chris@0: # latest revision in the repository Chris@0: scm_revision = scm_info.lastrev.identifier.to_i Chris@0: if db_revision < scm_revision Chris@0: logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug? Chris@0: identifier_from = db_revision + 1 Chris@0: while (identifier_from <= scm_revision) Chris@0: # loads changesets by batches of 200 Chris@0: identifier_to = [identifier_from + 199, scm_revision].min Chris@0: revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true) Chris@0: revisions.reverse_each do |revision| Chris@0: transaction do Chris@441: changeset = Changeset.create(:repository => self, Chris@441: :revision => revision.identifier, Chris@441: :committer => revision.author, Chris@0: :committed_on => revision.time, Chris@441: :comments => revision.message) Chris@441: Chris@0: revision.paths.each do |change| Chris@0: changeset.create_change(change) Chris@0: end unless changeset.new_record? Chris@0: end Chris@0: end unless revisions.nil? Chris@0: identifier_from = identifier_to + 1 Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@441: Chris@1115: protected Chris@1115: Chris@1115: def load_entries_changesets(entries) Chris@1115: return unless entries Chris@1517: entries_with_identifier = Chris@1517: entries.select {|entry| entry.lastrev && entry.lastrev.identifier.present?} Chris@1115: identifiers = entries_with_identifier.map {|entry| entry.lastrev.identifier}.compact.uniq Chris@1115: if identifiers.any? Chris@1517: changesets_by_identifier = Chris@1517: changesets.where(:revision => identifiers). Chris@1517: includes(:user, :repository).group_by(&:revision) Chris@1115: entries_with_identifier.each do |entry| Chris@1115: if m = changesets_by_identifier[entry.lastrev.identifier] Chris@1115: entry.changeset = m.first Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@0: private Chris@441: Chris@0: # Returns the relative url of the repository Chris@0: # Eg: root_url = file:///var/svn/foo Chris@0: # url = file:///var/svn/foo/bar Chris@0: # => returns /bar Chris@0: def relative_url Chris@0: @relative_url ||= url.gsub(Regexp.new("^#{Regexp.escape(root_url || scm.root_url)}", Regexp::IGNORECASE), '') Chris@0: end Chris@0: end