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: require 'redmine/scm/adapters/mercurial_adapter' Chris@0: Chris@0: class Repository::Mercurial < Repository Chris@119: # sort changesets by revision number Chris@119: has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id' Chris@119: Chris@0: attr_protected :root_url Chris@0: validates_presence_of :url Chris@0: Chris@245: FETCH_AT_ONCE = 100 # number of changesets to fetch at once Chris@245: Chris@245: ATTRIBUTE_KEY_NAMES = { Chris@245: "url" => "Root directory", Chris@245: } Chris@245: def self.human_attribute_name(attribute_key_name) Chris@245: ATTRIBUTE_KEY_NAMES[attribute_key_name] || super Chris@245: end Chris@245: Chris@245: def self.scm_adapter_class Chris@0: Redmine::Scm::Adapters::MercurialAdapter Chris@0: end Chris@119: Chris@0: def self.scm_name Chris@0: 'Mercurial' Chris@0: end Chris@119: Chris@245: def repo_log_encoding Chris@245: 'UTF-8' Chris@245: end Chris@245: Chris@119: # Returns the readable identifier for the given mercurial changeset Chris@119: def self.format_changeset_identifier(changeset) Chris@119: "#{changeset.revision}:#{changeset.scmid}" Chris@119: end Chris@119: Chris@119: # Returns the identifier for the given Mercurial changeset Chris@119: def self.changeset_identifier(changeset) Chris@119: changeset.scmid Chris@119: end Chris@119: Chris@245: def branches Chris@245: nil Chris@245: end Chris@245: Chris@245: def tags Chris@245: nil Chris@245: end Chris@245: Chris@119: def diff_format_revisions(cs, cs_to, sep=':') Chris@119: super(cs, cs_to, ' ') Chris@119: end Chris@119: Chris@119: # Finds and returns a revision with a number or the beginning of a hash Chris@119: def find_changeset_by_name(name) Chris@119: return nil if name.nil? || name.empty? Chris@119: if /[^\d]/ =~ name or name.to_s.size > 8 Chris@119: e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s]) Chris@119: else Chris@119: e = changesets.find(:first, :conditions => ['revision = ?', name.to_s]) Chris@119: end Chris@119: return e if e Chris@119: changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch Chris@119: end Chris@119: Chris@119: # Returns the latest changesets for +path+; sorted by revision number Chris@245: # Default behavior is to search in cached changesets Chris@119: def latest_changesets(path, rev, limit=10) Chris@119: if path.blank? Chris@119: changesets.find(:all, :include => :user, :limit => limit) Chris@119: else Chris@245: changesets.find(:all, :select => "DISTINCT #{Changeset.table_name}.*", Chris@245: :joins => :changes, Chris@245: :conditions => ["#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ? ESCAPE ?", Chris@245: path.with_leading_slash, Chris@245: "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%", '\\'], Chris@245: :include => :user, :limit => limit) Chris@119: end Chris@119: end Chris@119: Chris@0: def fetch_changesets Chris@245: scm_rev = scm.info.lastrev.revision.to_i Chris@245: db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 Chris@245: return unless db_rev < scm_rev # already up-to-date Chris@245: Chris@245: logger.debug "Fetching changesets for repository #{url}" if logger Chris@245: (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| Chris@245: transaction do Chris@245: scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| Chris@245: cs = Changeset.create(:repository => self, Chris@245: :revision => re.revision, Chris@245: :scmid => re.scmid, Chris@245: :committer => re.author, Chris@245: :committed_on => re.time, Chris@245: :comments => re.message) Chris@245: re.paths.each { |e| cs.create_change(e) } Chris@0: end Chris@0: end Chris@0: end Chris@245: self Chris@0: end Chris@0: end