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