Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'redmine/scm/adapters/mercurial_adapter' Chris@909: Chris@909: class Repository::Mercurial < Repository Chris@909: # sort changesets by revision number Chris@909: has_many :changesets, Chris@909: :order => "#{Changeset.table_name}.id DESC", Chris@909: :foreign_key => 'repository_id' Chris@909: Chris@909: attr_protected :root_url Chris@909: validates_presence_of :url Chris@909: Chris@909: # number of changesets to fetch at once Chris@909: FETCH_AT_ONCE = 100 Chris@909: Chris@909: def self.human_attribute_name(attribute_key_name) Chris@909: attr_name = attribute_key_name Chris@909: if attr_name == "url" Chris@909: attr_name = "path_to_repository" Chris@909: end Chris@909: super(attr_name) Chris@909: end Chris@909: Chris@909: def self.scm_adapter_class Chris@909: Redmine::Scm::Adapters::MercurialAdapter Chris@909: end Chris@909: Chris@909: def self.scm_name Chris@909: 'Mercurial' Chris@909: end Chris@909: Chris@909: def supports_directory_revisions? Chris@909: true Chris@909: end Chris@909: Chris@909: def supports_revision_graph? Chris@909: true Chris@909: end Chris@909: Chris@909: def repo_log_encoding Chris@909: 'UTF-8' Chris@909: end Chris@909: Chris@909: # Returns the readable identifier for the given mercurial changeset Chris@909: def self.format_changeset_identifier(changeset) Chris@909: "#{changeset.revision}:#{changeset.scmid}" Chris@909: end Chris@909: Chris@909: # Returns the identifier for the given Mercurial changeset Chris@909: def self.changeset_identifier(changeset) Chris@909: changeset.scmid Chris@909: end Chris@909: Chris@909: def diff_format_revisions(cs, cs_to, sep=':') Chris@909: super(cs, cs_to, ' ') Chris@909: end Chris@909: Chris@909: # Finds and returns a revision with a number or the beginning of a hash Chris@909: def find_changeset_by_name(name) Chris@909: return nil if name.nil? || name.empty? Chris@909: if /[^\d]/ =~ name or name.to_s.size > 8 Chris@909: e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s]) Chris@909: else Chris@909: e = changesets.find(:first, :conditions => ['revision = ?', name.to_s]) Chris@909: end Chris@909: return e if e Chris@909: changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch Chris@909: end Chris@909: Chris@909: # Returns the latest changesets for +path+; sorted by revision number Chris@909: # Chris@909: # Because :order => 'id DESC' is defined at 'has_many', Chris@909: # there is no need to set 'order'. Chris@909: # But, MySQL test fails. Chris@909: # Sqlite3 and PostgreSQL pass. Chris@909: # Is this MySQL bug? Chris@909: def latest_changesets(path, rev, limit=10) Chris@909: changesets.find(:all, Chris@909: :include => :user, Chris@909: :conditions => latest_changesets_cond(path, rev, limit), Chris@909: :limit => limit, Chris@909: :order => "#{Changeset.table_name}.id DESC") Chris@909: end Chris@909: Chris@909: def latest_changesets_cond(path, rev, limit) Chris@909: cond, args = [], [] Chris@909: if scm.branchmap.member? rev Chris@909: # Mercurial named branch is *stable* in each revision. Chris@909: # So, named branch can be stored in database. Chris@909: # Mercurial provides *bookmark* which is equivalent with git branch. Chris@909: # But, bookmark is not implemented. Chris@909: cond << "#{Changeset.table_name}.scmid IN (?)" Chris@909: # Revisions in root directory and sub directory are not equal. Chris@909: # So, in order to get correct limit, we need to get all revisions. Chris@909: # But, it is very heavy. Chris@909: # Mercurial does not treat direcotry. Chris@909: # So, "hg log DIR" is very heavy. Chris@909: branch_limit = path.blank? ? limit : ( limit * 5 ) Chris@909: args << scm.nodes_in_branch(rev, :limit => branch_limit) Chris@909: elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil Chris@909: cond << "#{Changeset.table_name}.id <= ?" Chris@909: args << last.id Chris@909: end Chris@909: unless path.blank? Chris@909: cond << "EXISTS (SELECT * FROM #{Change.table_name} Chris@909: WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id Chris@909: AND (#{Change.table_name}.path = ? Chris@909: OR #{Change.table_name}.path LIKE ? ESCAPE ?))" Chris@909: args << path.with_leading_slash Chris@909: args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\' Chris@909: end Chris@909: [cond.join(' AND '), *args] unless cond.empty? Chris@909: end Chris@909: private :latest_changesets_cond Chris@909: Chris@909: def fetch_changesets Chris@909: return if scm.info.nil? Chris@909: scm_rev = scm.info.lastrev.revision.to_i Chris@909: db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 Chris@909: return unless db_rev < scm_rev # already up-to-date Chris@909: Chris@909: logger.debug "Fetching changesets for repository #{url}" if logger Chris@909: (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| Chris@909: transaction do Chris@909: scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| Chris@909: cs = Changeset.create(:repository => self, Chris@909: :revision => re.revision, Chris@909: :scmid => re.scmid, Chris@909: :committer => re.author, Chris@909: :committed_on => re.time, Chris@909: :comments => re.message) Chris@909: re.paths.each { |e| cs.create_change(e) } Chris@909: parents = {} Chris@909: parents[cs] = re.parents unless re.parents.nil? Chris@909: parents.each do |ch, chparents| Chris@909: ch.parents = chparents.collect{|rp| find_changeset_by_name(rp)}.compact Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end