Chris@1464: # Redmine - project management software Chris@1464: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1464: # Chris@1464: # This program is free software; you can redistribute it and/or Chris@1464: # modify it under the terms of the GNU General Public License Chris@1464: # as published by the Free Software Foundation; either version 2 Chris@1464: # of the License, or (at your option) any later version. Chris@1464: # Chris@1464: # This program is distributed in the hope that it will be useful, Chris@1464: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1464: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1464: # GNU General Public License for more details. Chris@1464: # Chris@1464: # You should have received a copy of the GNU General Public License Chris@1464: # along with this program; if not, write to the Free Software Chris@1464: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1464: Chris@1464: require 'redmine/scm/adapters/subversion_adapter' Chris@1464: Chris@1464: class Repository::Subversion < Repository Chris@1464: attr_protected :root_url Chris@1464: validates_presence_of :url Chris@1464: validates_format_of :url, :with => /\A(http|https|svn(\+[^\s:\/\\]+)?|file):\/\/.+/i Chris@1464: Chris@1464: def self.scm_adapter_class Chris@1464: Redmine::Scm::Adapters::SubversionAdapter Chris@1464: end Chris@1464: Chris@1464: def self.scm_name Chris@1464: 'Subversion' Chris@1464: end Chris@1464: Chris@1464: def supports_directory_revisions? Chris@1464: true Chris@1464: end Chris@1464: Chris@1464: def repo_log_encoding Chris@1464: 'UTF-8' Chris@1464: end Chris@1464: Chris@1464: def latest_changesets(path, rev, limit=10) Chris@1464: revisions = scm.revisions(path, rev, nil, :limit => limit) Chris@1464: if revisions Chris@1464: identifiers = revisions.collect(&:identifier).compact Chris@1464: changesets.where(:revision => identifiers).reorder("committed_on DESC").includes(:repository, :user).all Chris@1464: else Chris@1464: [] Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Returns a path relative to the url of the repository Chris@1464: def relative_path(path) Chris@1464: path.gsub(Regexp.new("^\/?#{Regexp.escape(relative_url)}"), '') Chris@1464: end Chris@1464: Chris@1464: def fetch_changesets Chris@1464: scm_info = scm.info Chris@1464: if scm_info Chris@1464: # latest revision found in database Chris@1464: db_revision = latest_changeset ? latest_changeset.revision.to_i : 0 Chris@1464: # latest revision in the repository Chris@1464: scm_revision = scm_info.lastrev.identifier.to_i Chris@1464: if db_revision < scm_revision Chris@1464: logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug? Chris@1464: identifier_from = db_revision + 1 Chris@1464: while (identifier_from <= scm_revision) Chris@1464: # loads changesets by batches of 200 Chris@1464: identifier_to = [identifier_from + 199, scm_revision].min Chris@1464: revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true) Chris@1464: revisions.reverse_each do |revision| Chris@1464: transaction do Chris@1464: changeset = Changeset.create(:repository => self, Chris@1464: :revision => revision.identifier, Chris@1464: :committer => revision.author, Chris@1464: :committed_on => revision.time, Chris@1464: :comments => revision.message) Chris@1464: Chris@1464: revision.paths.each do |change| Chris@1464: changeset.create_change(change) Chris@1464: end unless changeset.new_record? Chris@1464: end Chris@1464: end unless revisions.nil? Chris@1464: identifier_from = identifier_to + 1 Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: protected Chris@1464: Chris@1464: def load_entries_changesets(entries) Chris@1464: return unless entries Chris@1464: Chris@1464: entries_with_identifier = entries.select {|entry| entry.lastrev && entry.lastrev.identifier.present?} Chris@1464: identifiers = entries_with_identifier.map {|entry| entry.lastrev.identifier}.compact.uniq Chris@1464: Chris@1464: if identifiers.any? Chris@1464: changesets_by_identifier = changesets.where(:revision => identifiers).includes(:user, :repository).all.group_by(&:revision) Chris@1464: entries_with_identifier.each do |entry| Chris@1464: if m = changesets_by_identifier[entry.lastrev.identifier] Chris@1464: entry.changeset = m.first Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: private Chris@1464: Chris@1464: # Returns the relative url of the repository Chris@1464: # Eg: root_url = file:///var/svn/foo Chris@1464: # url = file:///var/svn/foo/bar Chris@1464: # => returns /bar Chris@1464: def relative_url Chris@1464: @relative_url ||= url.gsub(Regexp.new("^#{Regexp.escape(root_url || scm.root_url)}", Regexp::IGNORECASE), '') Chris@1464: end Chris@1464: end